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

fix: prefix bug in MockDirectory.EnumerateDirectories (#815) #1046

Merged
merged 8 commits into from
Dec 5, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -627,39 +627,41 @@ public override void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
/// <inheritdoc />
public override IEnumerable<string> EnumerateDirectories(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

return EnumerateDirectories(path, "*");
}

/// <inheritdoc />
public override IEnumerable<string> EnumerateDirectories(string path, string searchPattern)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

return EnumerateDirectories(path, searchPattern, SearchOption.TopDirectoryOnly);
}

/// <inheritdoc />
public override IEnumerable<string> EnumerateDirectories(string path, string searchPattern, SearchOption searchOption)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
var originalPath = path;
path = path.TrimSlashes();
path = mockFileDataAccessor.Path.GetFullPath(path);
return GetFilesInternal(mockFileDataAccessor.AllDirectories, path, searchPattern, searchOption)
.Where(p => !mockFileDataAccessor.StringOperations.Equals(p, path));
.Where(p => !mockFileDataAccessor.StringOperations.Equals(p, path))
.Select(p => FixPrefix(p, originalPath));
}


private string FixPrefix(string path, string originalPath)
{
var normalizedOriginalPath = mockFileDataAccessor.Path.GetFullPath(originalPath);
var pathWithoutOriginalPath = path.Substring(normalizedOriginalPath.Length)
.TrimStart(mockFileDataAccessor.Path.DirectorySeparatorChar);
return mockFileDataAccessor.Path.Combine(originalPath, pathWithoutOriginalPath);
}

#if FEATURE_ENUMERATION_OPTIONS
/// <inheritdoc />
public override IEnumerable<string> EnumerateDirectories(string path, string searchPattern, EnumerationOptions enumerationOptions)
{
var searchOption = enumerationOptions.RecurseSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
path = path.TrimSlashes();
path = mockFileDataAccessor.Path.GetFullPath(path);
return GetFilesInternal(mockFileDataAccessor.AllDirectories, path, searchPattern, searchOption)
.Where(p => !mockFileDataAccessor.StringOperations.Equals(p, path));
return EnumerateDirectories(path, searchPattern, searchOption);
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@
fileSystem.AddFile(XFS.Path(@"C:\Folder\.foo\bar"), new MockFileData(string.Empty));

// Act
var actualResult = fileSystem.Directory.GetDirectories(XFS.Path(@"c:\Folder\"), "*.foo");
var actualResult = fileSystem.Directory.GetDirectories(XFS.Path(@"C:\Folder\"), "*.foo");

// Assert
Assert.That(actualResult, Is.EquivalentTo(new[] { XFS.Path(@"C:\Folder\.foo"), XFS.Path(@"C:\Folder\foo.foo") }));
Expand Down Expand Up @@ -1321,7 +1321,7 @@

// Assert
CollectionAssert.AreEqual(
new[] { XFS.Path(currentDirectory + @"\" + relativeDirPath + @"\child") },
new[] { XFS.Path(relativeDirPath + @"\child") },
actualResult
);
}
Expand Down Expand Up @@ -1353,7 +1353,7 @@
fileSystem.AddFile(XFS.Path(@"C:\Folder\.foo\bar"), new MockFileData(string.Empty));

// Act
var actualResult = fileSystem.Directory.GetDirectories(XFS.Path(@"c:\Folder\"), "*.foo", SearchOption.AllDirectories);
var actualResult = fileSystem.Directory.GetDirectories(XFS.Path(@"C:\Folder\"), "*.foo", SearchOption.AllDirectories);

// Assert
Assert.That(actualResult, Is.EquivalentTo(new[] { XFS.Path(@"C:\Folder\.foo"), XFS.Path(@"C:\Folder\foo.foo"), XFS.Path(@"C:\Folder\.foo\.foo") }));
Expand Down Expand Up @@ -1415,7 +1415,7 @@
fileSystem.AddFile(XFS.Path(@"C:\Folder\.foo\bar"), new MockFileData(string.Empty));

// Act
var actualResult = fileSystem.Directory.EnumerateDirectories(XFS.Path(@"c:\Folder\"), "*.foo");
var actualResult = fileSystem.Directory.EnumerateDirectories(XFS.Path(@"C:\Folder\"), "*.foo");

// Assert
Assert.That(actualResult, Is.EquivalentTo(new[] { XFS.Path(@"C:\Folder\.foo"), XFS.Path(@"C:\Folder\foo.foo") }));
Expand All @@ -1439,7 +1439,7 @@
};

// Act
var actualResult = fileSystem.Directory.EnumerateDirectories(XFS.Path(@"c:\Folder\"), "*.foo", enumerationOptions);
var actualResult = fileSystem.Directory.EnumerateDirectories(XFS.Path(@"C:\Folder\"), "*.foo", enumerationOptions);

// Assert
Assert.That(actualResult, Is.EquivalentTo(new[] { XFS.Path(@"C:\Folder\.foo"), XFS.Path(@"C:\Folder\foo.foo") }));
Expand All @@ -1457,7 +1457,7 @@
fileSystem.AddFile(XFS.Path(@"C:\Folder\.foo\bar"), new MockFileData(string.Empty));

// Act
var actualResult = fileSystem.Directory.EnumerateDirectories(XFS.Path(@"c:\Folder\"), "*.foo", SearchOption.AllDirectories);
var actualResult = fileSystem.Directory.EnumerateDirectories(XFS.Path(@"C:\Folder\"), "*.foo", SearchOption.AllDirectories);

// Assert
Assert.That(actualResult, Is.EquivalentTo(new[] { XFS.Path(@"C:\Folder\.foo"), XFS.Path(@"C:\Folder\foo.foo"), XFS.Path(@"C:\Folder\.foo\.foo") }));
Expand Down Expand Up @@ -1486,6 +1486,21 @@
// Assert
Assert.Throws<DirectoryNotFoundException>(action);
}


[TestCase("Folder", @"Folder\SubFolder")]
[TestCase(@"Folder\", @"Folder\SubFolder")]
[TestCase(@"Folder\..\.\Folder", @"Folder\..\.\Folder\SubFolder")]
public void MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath(
string queryPath, string expectedPath)
{
var fileSystem = new MockFileSystem();
fileSystem.Directory.CreateDirectory("Folder/SubFolder");

var actualResult = fileSystem.Directory.EnumerateDirectories(queryPath);

Check failure on line 1500 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder\\"

System.IO.DirectoryNotFoundException : Could not find a part of the path '/Folder\'.

Check failure on line 1500 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder\\..\\.\\Folder"

System.IO.DirectoryNotFoundException : Could not find a part of the path '/Folder\..\.\Folder'.

Check failure on line 1500 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder\\"

System.IO.DirectoryNotFoundException : Could not find a part of the path '/Folder\'.

Check failure on line 1500 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder\\..\\.\\Folder"

System.IO.DirectoryNotFoundException : Could not find a part of the path '/Folder\..\.\Folder'.

Check failure on line 1500 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder\\"

System.IO.DirectoryNotFoundException : Could not find a part of the path '/Folder\'.

Check failure on line 1500 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder\\..\\.\\Folder"

System.IO.DirectoryNotFoundException : Could not find a part of the path '/Folder\..\.\Folder'.

Check failure on line 1500 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder\\"

System.IO.DirectoryNotFoundException : Could not find a part of the path '/Folder\'.

Check failure on line 1500 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder\\..\\.\\Folder"

System.IO.DirectoryNotFoundException : Could not find a part of the path '/Folder\..\.\Folder'.

Check failure on line 1500 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder\\"

System.IO.DirectoryNotFoundException : Could not find a part of the path '/Folder\'.

Check failure on line 1500 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder\\..\\.\\Folder"

System.IO.DirectoryNotFoundException : Could not find a part of the path '/Folder\..\.\Folder'.

Check failure on line 1500 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder\\"

System.IO.DirectoryNotFoundException : Could not find a part of the path '/Folder\'.

Check failure on line 1500 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder\\..\\.\\Folder"

System.IO.DirectoryNotFoundException : Could not find a part of the path '/Folder\..\.\Folder'.

CollectionAssert.AreEqual(new[] { expectedPath }, actualResult);

Check failure on line 1502 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder"

Expected is <System.String[1]>, actual is <System.Linq.Enumerable+WhereSelectArrayIterator`2[System.String,System.String]> Values differ at index [0] String lengths are both 16. Strings differ at index 6. Expected: "Folder\\SubFolder" But was: "Folder/SubFolder" -----------------^

Check failure on line 1502 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder"

Expected is <System.String[1]>, actual is <System.Linq.Enumerable+WhereSelectArrayIterator`2[System.String,System.String]> Values differ at index [0] String lengths are both 16. Strings differ at index 6. Expected: "Folder\\SubFolder" But was: "Folder/SubFolder" -----------------^

Check failure on line 1502 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder"

Expected is <System.String[1]>, actual is <System.Linq.Enumerable+WhereSelectArrayIterator`2[System.String,System.String]> Values differ at index [0] String lengths are both 16. Strings differ at index 6. Expected: "Folder\\SubFolder" But was: "Folder/SubFolder" -----------------^

Check failure on line 1502 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder"

Expected is <System.String[1]>, actual is <System.Linq.Enumerable+WhereSelectArrayIterator`2[System.String,System.String]> Values differ at index [0] String lengths are both 16. Strings differ at index 6. Expected: "Folder\\SubFolder" But was: "Folder/SubFolder" -----------------^

Check failure on line 1502 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder"

Expected is <System.String[1]>, actual is <System.Linq.Enumerable+WhereSelectArrayIterator`2[System.String,System.String]> Values differ at index [0] String lengths are both 16. Strings differ at index 6. Expected: "Folder\\SubFolder" But was: "Folder/SubFolder" -----------------^

Check failure on line 1502 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder"

Expected is <System.String[1]>, actual is <System.Linq.Enumerable+WhereSelectArrayIterator`2[System.String,System.String]> Values differ at index [0] String lengths are both 16. Strings differ at index 6. Expected: "Folder\\SubFolder" But was: "Folder/SubFolder" -----------------^

Check failure on line 1502 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder"

Expected is <System.String[1]>, actual is <System.Linq.Enumerable+WhereSelectArrayIterator`2[System.String,System.String]> Values differ at index [0] String lengths are both 16. Strings differ at index 6. Expected: "Folder\\SubFolder" But was: "Folder/SubFolder" -----------------^

Check failure on line 1502 in tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

MockDirectory_EnumerateDirectories_ShouldReturnPathsPrefixedWithQueryPath("Folder"

Expected is <System.String[1]>, actual is <System.Linq.Enumerable+WhereSelectArrayIterator`2[System.String,System.String]> Values differ at index [0] String lengths are both 16. Strings differ at index 6. Expected: "Folder\\SubFolder" But was: "Folder/SubFolder" -----------------^
}

public static IEnumerable<object[]> GetPathsForMoving()
{
Expand Down
Loading