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,39 @@ 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);
return originalPath + path.Substring(normalizedOriginalPath.Length);
Phoenox marked this conversation as resolved.
Show resolved Hide resolved
}

#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 @@ public void MockDirectory_GetDirectories_WithTopDirectories_ShouldOnlyReturnTopD
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 @@ public void MockDirectory_GetDirectories_RelativeDirectory_WithChildren_ShouldRe

// Assert
CollectionAssert.AreEqual(
new[] { XFS.Path(currentDirectory + @"\" + relativeDirPath + @"\child") },
new[] { XFS.Path(relativeDirPath + @"\child") },
actualResult
);
}
Expand Down Expand Up @@ -1353,7 +1353,7 @@ public void MockDirectory_GetDirectories_WithAllDirectories_ShouldReturnsAllMatc
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 @@ public void MockDirectory_EnumerateDirectories_WithTopDirectories_ShouldOnlyRetu
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 @@ public void MockDirectory_EnumerateDirectories_WithEnumerationOptionsTopDirector
};

// 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 @@ public void MockDirectory_EnumerateDirectories_WithAllDirectories_ShouldReturnsA
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 @@ public void MockDirectory_EnumerateDirectories_ShouldThrowWhenPathIsNotMocked()
// 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);

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

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