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.EnumerateFiles returns incorrect results, based on input pattern. #596

Open
pianomanjh opened this issue May 29, 2020 · 12 comments
Labels
area: testinghelpers Issues that address the testing helpers state: needs discussion Issues that need further discussion state: stale type: bug Issues that describe misbehaving functionality

Comments

@pianomanjh
Copy link
Contributor

When calling Directory.EnumerateFiles on MockFileSystem, incorrect matching occurs, that differs from the real FileSystem.

Mock FileSystem

var results = Directory.EnumerateFiles(@"c:\jobfolder\result.rreq.001.afd", "*.rreq.*.*.*");
// results has no entries

Real FileSystem

var results = Directory.EnumerateFiles(@"c:\jobfolder\result.rreq.001.afd", "*.rreq.*.*.*");
// results has the entry `c:\jobfolder\result.rreq.001.afd`
@pianomanjh pianomanjh added state: needs discussion Issues that need further discussion type: bug Issues that describe misbehaving functionality labels May 29, 2020
@fgreinacher
Copy link
Contributor

Thanks for reporting! Am I right assuming result.rreq.001.afd is a file (rather than a directory)?

@fgreinacher fgreinacher added area: testinghelpers Issues that address the testing helpers state: ready to pick Issues that are ready for being worked on and removed state: needs discussion Issues that need further discussion labels Jun 12, 2020
@pianomanjh
Copy link
Contributor Author

pianomanjh commented Jun 15, 2020

my apologies, the sample is not right. Here is something more representative.

mockFileSystem.AddFile(@"c:\test\result.rreq.001.afd", MockFileData.NullObject);
var results = mockFileSystem.Directory.EnumerateFiles(@"c:\test", "*.rreq.*.*.*");
// results has no entries, but should have `c:\test\result.rreq.001.afd`

@fgreinacher
Copy link
Contributor

fgreinacher commented Jun 16, 2020

my apologies, the sample is not right. Here is something more representative.

mockFileSystem.AddFile(@"c:\test\result.rreq.001.afd", MockFileData.NullObject);
var results = mockFileSystem.Directory.EnumerateFiles(@"c:\test", "*.rreq.*.*.*");
// results has no entries, but should have `c:\test\result.rreq.001.afd`

This sample does also not look correct to me, the pattern has one asterisk too much:

result.rreq.001.afd
*     .rreq.*  .*  .*

The following test is passing:

// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
    { XFS.Path(@"c:\test\result.rreq.001.afd"), MockFileData.NullObject }
});

// Act
var result = fileSystem.Directory.EnumerateFiles(XFS.Path(@"c:\test"), "*.rreq.*.*");

// Assert
Assert.That(result, Is.EquivalentTo(new[]
{
    XFS.Path(@"c:\test\result.rreq.001.afd")
}));

@fgreinacher
Copy link
Contributor

fgreinacher commented Jun 16, 2020

Now I get the problem. It seems like the real implementation ignores the .* elements at the end. This test case also succeeds 😩

// Arrange
var fileSystem = new FileSystem();
// var fileSystem = new MockFileSystem();
// fileSystem.Directory.CreateDirectory(XFS.Path(@"c:\tmp\"));

fileSystem.File.CreateText(XFS.Path(@"c:\tmp\result.rreq.001.afd")).Close();

// Act
var result = fileSystem.Directory.EnumerateFiles(XFS.Path(@"c:\tmp"), "*.rreq.*.*.*.*.*.*.*.*.*");

// Assert
Assert.That(result, Is.EquivalentTo(new[]
{
    XFS.Path(@"c:\tmp\result.rreq.001.afd")
}));

@fgreinacher
Copy link
Contributor

fgreinacher commented Jun 18, 2020

@pianomanjh Please check whether the issue goes away once you use the "correct" pattern:

mockFileSystem.AddFile(@"c:\test\result.rreq.001.afd", MockFileData.NullObject);
mockFileSystem.Directory.EnumerateFiles(@"c:\test", "*.rreq.*.*"); // one `.*` pair less

@fgreinacher fgreinacher added state: in work Issues that are currently worked on and removed state: ready to pick Issues that are ready for being worked on labels Jun 18, 2020
@pianomanjh
Copy link
Contributor Author

If I use a different pattern, the one you describe, the issue no longer reproduces, yes.
However, using a different pattern doesn't fix the bug ;) (we need that pattern in production)

@fgreinacher
Copy link
Contributor

fgreinacher commented Jun 20, 2020

Why do you need that exact pattern? It seems wrong to me 🤔 .NET behavior seems to be much more complex than we anticipated when implementing this logic 🐙

https://github.com/dotnet/runtime/blob/cd02b0612b3c758037eac9f85faa4ec91b1fbbda/src/libraries/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemName.cs has some of the logic - we might want to reuse (parts of) it.

@fgreinacher fgreinacher added state: needs discussion Issues that need further discussion and removed state: in work Issues that are currently worked on labels Jun 20, 2020
@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@themcoo
Copy link

themcoo commented Feb 8, 2021

There are more differences in behaviour of Directory.EnumerateFiles between mock and real file system:

  • the string path parameter is case sensitive in the mock, while in real fs it isn't
  • real file system enumerates all files for the searchPattern value of string.Empty, while mock's enumeration yields no results

@fgreinacher
Copy link
Contributor

There are more differences in behaviour of Directory.EnumerateFiles between mock and real file system:

  • the string path parameter is case sensitive in the mock, while in real fs it isn't
  • real file system enumerates all files for the searchPattern value of string.Empty, while mock's enumeration yields no results

Thanks for the additional input here @themcoo. Would you mind creating new issues for the problems so that we can tackle them separately?

@friketrike
Copy link

Another pattern that works in the real filesystem and not on the MockFileSystem is "someDirectory\\*.txt", this includes ".\\*.txt"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: testinghelpers Issues that address the testing helpers state: needs discussion Issues that need further discussion state: stale type: bug Issues that describe misbehaving functionality
Projects
None yet
Development

No branches or pull requests

4 participants