Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/Framework.UnitTests/FileMatcher_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,50 @@ public void DoNotFollowRecursiveSymlinks()
}
}
}

[RequiresSymbolicLinksFact]
public void ShouldNotSkipSymlinkDirectoryWhenProjectDirectoryIsPrefixedWithSymlinkTargetName()
{
TransientTestFolder rootFolder = _env.CreateFolder();

string targetFolderName = "target";
string fileAName = "A.cs";
string targetSubFolderName = "foo";
string fileBName = "B.cs";
TransientTestFolder targetFolder = _env.CreateFolder(Path.Combine(rootFolder.Path, targetFolderName));
_env.CreateFile(targetFolder, fileName: fileAName);
TransientTestFolder targetSubFolder = _env.CreateFolder(Path.Combine(targetFolder.Path, targetSubFolderName));
_env.CreateFile(targetSubFolder, fileName: fileBName);

TransientTestFolder projectFolder = _env.CreateFolder(Path.Combine(rootFolder.Path, $"{targetFolderName}Test"));
string symlinkName = "mySymlink";
string symlinkPath = Path.Combine(projectFolder.Path, symlinkName);
string include = Path.Combine(symlinkName, "**", "*.cs");

string[] expectedFiles =
[
Path.Combine(symlinkName, fileAName),
Path.Combine(symlinkName, targetSubFolderName, fileBName)
];

try
{
Directory.CreateSymbolicLink(symlinkPath, targetFolder.Path);
string[] fileMatches = FileMatcher.Default.GetFiles(projectFolder.Path, include).FileList;
fileMatches.Length.ShouldBe(expectedFiles.Length);
foreach (var item in fileMatches)
{
expectedFiles.ShouldContain(item);
}
}
finally
{
if (Directory.Exists(symlinkPath))
{
Directory.Delete(symlinkPath);
}
}
}
#endif

[Theory]
Expand Down
6 changes: 3 additions & 3 deletions src/Framework/Utilities/FileMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ private void GetFilesRecursive(
try
{
FileSystemInfo linkTarget = Directory.ResolveLinkTarget(recursionState.BaseDirectory, returnFinalTarget: true);
if (linkTarget is not null && recursionState.BaseDirectory.Contains(linkTarget.FullName))
if (linkTarget is not null && IsSubdirectoryOf(recursionState.BaseDirectory, linkTarget.FullName, FileUtilities.PathComparison))
{
return;
}
Comment thread
GangWang01 marked this conversation as resolved.
Expand Down Expand Up @@ -2647,15 +2647,15 @@ private bool InnerExceptionsAreAllIoRelated(AggregateException ex)
return ex.Flatten().InnerExceptions.All(ExceptionHandling.IsIoRelatedException);
}

private static bool IsSubdirectoryOf(string possibleChild, string possibleParent)
private static bool IsSubdirectoryOf(string possibleChild, string possibleParent, StringComparison pathComparison = StringComparison.OrdinalIgnoreCase)
{
if (possibleParent == string.Empty)
{
// Something is always possibly a child of nothing
return true;
}

bool prefixMatch = possibleChild.StartsWith(possibleParent, StringComparison.OrdinalIgnoreCase);
bool prefixMatch = possibleChild.StartsWith(possibleParent, pathComparison);
if (!prefixMatch)
{
return false;
Expand Down