Skip to content

AllowedFileShare not obeyed by MockFileStream #455

@roketworks

Description

@roketworks

Version 3.1.1

MockFileStream does not seem to be obeying the AllowedFileShare property. See the example below, I would expect to see an exception throw when trying to create the FileStream.

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions.TestingHelpers;
using System.Text;

namespace FileStreamTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IDictionary<string, MockFileData> files  = new Dictionary<string, MockFileData>
            {
                { "C:\\test\\file.txt", new MockFileData("contents") { AllowedFileShare = FileShare.None } }
            };
            
            var fs = new MockFileSystem(files);
            
            // Does not throw exception
            var stream = fs.FileStream.Create("C:\\test\\file.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            using (stream)
            {
                using (var reader = new StreamReader(stream, Encoding.Default))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        Console.Write(line);
                    }
                }
            }
           
            Console.Write(Environment.NewLine);
           
            // Throws System.IO.IOException as expected
            var text = fs.File.ReadAllText("C:\\test\\file.txt");
            Console.WriteLine(text);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: in workIssues that are currently worked ontype: bugIssues that describe misbehaving functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions