Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tuan-tu-tran opened this issue Feb 23, 2022 · 1 comment · Fixed by #1046
Labels
flag: good-first-issue Issues that are good for first time contributors state: in work Issues that are currently worked on type: bug Issues that describe misbehaving functionality

Comments

@tuan-tu-tran
Copy link
Collaborator

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 tuan-tu-tran added state: needs discussion Issues that need further discussion type: bug Issues that describe misbehaving functionality labels Feb 23, 2022
@fgreinacher fgreinacher added the flag: good-first-issue Issues that are good for first time contributors label Feb 28, 2022
@fgreinacher fgreinacher added state: in work Issues that are currently worked on and removed state: needs discussion Issues that need further discussion labels Mar 21, 2022
@PapisKang
Copy link

Hi Im new over here. I would like to help on this one. It seems good for beginners

Phoenox added a commit to Phoenox/System.IO.Abstractions that referenced this issue Oct 18, 2023
`System.IO.Directory.EnumerateDirectories(path)` guarantees that returned paths are always prefixed with the value provided in the `path` parameter. However, `MockDirectory.EnumerateDirectories` always returned absolute paths, leading to an inconsistent behavior (e.g. when using relative paths).

We now check returned paths from `MockDirectory.EnumerateDirectories` and provide the proper prefix path.
Phoenox added a commit to Phoenox/System.IO.Abstractions that referenced this issue Oct 18, 2023
`System.IO.Directory.EnumerateDirectories(path)` guarantees that returned paths are always prefixed with the value provided in the `path` parameter. However, `MockDirectory.EnumerateDirectories` always returned absolute paths, leading to an inconsistent behavior (e.g. when using relative paths).

We now check returned paths from `MockDirectory.EnumerateDirectories` and provide the proper prefix path.
@mergify mergify bot closed this as completed in #1046 Dec 5, 2023
mergify bot pushed a commit that referenced this issue Dec 5, 2023
Fixes #815 

`System.IO.Directory.EnumerateDirectories(path)` guarantees that returned paths are always prefixed with the value provided in the `path` parameter. However, `MockDirectory.EnumerateDirectories` always returned absolute paths, leading to an inconsistent behavior (e.g. when using relative paths).

We now check returned paths from `MockDirectory.EnumerateDirectories` and provide the proper prefix path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag: good-first-issue Issues that are good for first time contributors state: in work Issues that are currently worked on type: bug Issues that describe misbehaving functionality
Projects
None yet
3 participants