System.IO.Abstractions icon indicating copy to clipboard operation
System.IO.Abstractions copied to clipboard

MockFileSystem does not support Root directories

Open derecskey opened this issue 4 years ago • 2 comments

Describe the bug Including a Root drive/directory in a MockFileSystem causes ArgumentNullException at line 82 of MockFileSystem.cs

To Reproduce To recreate, execute the following line of code:

var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
    { @"c:\demo", new MockDirectoryData() },
    { @"z:\", new MockDirectoryData() },
    { @"Z:\foo", new MockDirectoryData() }
});

Expected behavior

"Z:" should be a valid directory.

Additional context

var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
    { @"c:\demo", new MockDirectoryData() },
    //{ @"z:\", new MockDirectoryData() },
    { @"Z:\foo", new MockDirectoryData() }
});  //This produces no exception

var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
    { @"c:\demo", new MockDirectoryData() },
    //{ @"z:\", new MockDirectoryData() },
    { @"Z:\foo", new MockDirectoryData() }
});  //This produces an exception.

derecskey avatar Feb 17 '21 17:02 derecskey

Thanks for reporting!

fgreinacher avatar Feb 18 '21 22:02 fgreinacher

Hi, in the Additional context section, the 2 examples are the same even though the comments say the 1st one does not throw an exception whereas the 2nd does.

I think in the 2nd example, the 2nd directory mock should not be commented, just like in the To reproduce section

For reference, the exception thrown is

System.ArgumentNullException: Value cannot be null. (Parameter 'key')

And here's the stack trace:

Dictionary`2.FindValue(TKey key)
Dictionary`2.TryGetValue(TKey key, TValue& value)
MockFileSystem.DirectoryExistsWithoutFixingPath(String path)
MockFileSystem.AddFile(String path, MockFileData mockFile)
MockFileSystem.ctor(IDictionary`2 files, String currentDirectory)

tuan-tu-tran avatar Feb 23 '22 21:02 tuan-tu-tran