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

MockFileSystem.Directory.GetDirectories returns absolute paths instead of relative paths

Open tuan-tu-tran opened this issue 3 years ago • 0 comments

Describe the bug Calling MockFileSystem.Directory.GetDirectories(@"some\path") returns absolute paths instead of relative paths

To Reproduce In this code

var mockFs = new MockFileSystem();
mockFs.Directory.CreateDirectory(@"foo\bar");
var output = mockFs.Directory.GetDirectories("foo");

output is an array with a single string: C:\foo\bar, which is incorrect.

Expected behavior In the same example, output should be an array with a single string foo\bar

Additional context Here's a more comprehensive code snippet that shows:

  • how the System.IO.Directory class behaves
  • that FileSystem.Directory behaves correctly
  • that MockFileSystem.Directory behaves incorrectly
[TestMethod]
public void Directory_GetDirectories_Should_Return_Relative_Paths()
{
    //This shows how the system classes behave : it returns one relative directory
    Directory.CreateDirectory(@"foo\bar");
    var dirs1 = Directory.GetDirectories("foo");
    CollectionAssert.AreEqual(new[] { @"foo\bar" }, dirs1); //passes

    //This shows that the real FileSystem behaves correctly, same as system classes
    var fs = new FileSystem();
    var dirs2 = fs.Directory.GetDirectories("foo");
    CollectionAssert.AreEqual(dirs1, dirs2); //passes

    //This shows that the MockFileSystem behaves incorrectly : it returns one absolute directory path
    var mockFs = new MockFileSystem();
    mockFs.Directory.CreateDirectory(@"foo\bar");
    var dirs3 = mockFs.Directory.GetDirectories("foo");
    Console.WriteLine(dirs3[0]); // C:\foo\bar
    CollectionAssert.AreEqual(dirs1, dirs3, "dirs3 should equal dirs1"); //fails
}

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