Skip to content

Commit

Permalink
Make sure that *.* always means all files in FileMatcher (#6235)
Browse files Browse the repository at this point in the history
### Context

#6151 introduced a regression where `FileMatcher` takes the pattern `*.*` too literally and returns only files that have a dot in the name. `*.*` should be special cased to mean all files in the directory, with or without an extension.

### Changes Made

Fixed the regression by explicitly testing for `*.*` and added test coverage.

### Testing

Existing and modified unit tests, repro project from dotnet/sdk#16185 (comment).

### Notes

Testing for both `*` and `*.*` is already happening elsewhere in the class. MSBuild calls `Directory.EnumerateFileSystemEntries` which under the covers uses `MatchType.Win32` and causes this behavior of unifying `*.*` with `*` on all platforms.
  • Loading branch information
ladipro committed Mar 10, 2021
1 parent 0def9d1 commit 7bc761a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Shared/FileMatcher.cs
Expand Up @@ -131,7 +131,7 @@ internal FileMatcher(IFileSystem fileSystem, GetFileSystemEntries getFileSystemE
"*",
directory,
false));
IEnumerable<string> filteredEntriesForPath = (pattern != null && pattern != "*")
IEnumerable<string> filteredEntriesForPath = (pattern != null && pattern != "*" && pattern != "*.*")
? allEntriesForPath.Where(o => IsMatch(Path.GetFileName(o), pattern))
: allEntriesForPath;
return stripProjectDirectory
Expand Down
21 changes: 17 additions & 4 deletions src/Shared/UnitTests/FileMatcher_Tests.cs
Expand Up @@ -5,6 +5,7 @@
using Shouldly;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -77,9 +78,9 @@ public void GetFilesComplexGlobbingMatching(GetFilesComplexGlobbingMatchingInfo
File.WriteAllBytes(fullPath, new byte[1]);
}

void Verify(string include, string[] excludes, bool shouldHaveNoMatches = false, string customMessage = null)
void VerifyImpl(FileMatcher fileMatcher, string include, string[] excludes, bool shouldHaveNoMatches = false, string customMessage = null)
{
string[] matchedFiles = FileMatcher.Default.GetFiles(testFolder.Path, include, excludes?.ToList());
string[] matchedFiles = fileMatcher.GetFiles(testFolder.Path, include, excludes?.ToList());

if (shouldHaveNoMatches)
{
Expand All @@ -99,6 +100,18 @@ void Verify(string include, string[] excludes, bool shouldHaveNoMatches = false,
}
}

var fileMatcherWithCache = new FileMatcher(FileSystems.Default, new ConcurrentDictionary<string, IReadOnlyList<string>>());

void Verify(string include, string[] excludes, bool shouldHaveNoMatches = false, string customMessage = null)
{
// Verify using the default non-caching FileMatcher.
VerifyImpl(FileMatcher.Default, include, excludes, shouldHaveNoMatches, customMessage);

// Verify using a caching FileMatcher and do it twice to exercise the cache.
VerifyImpl(fileMatcherWithCache, include, excludes, shouldHaveNoMatches, customMessage);
VerifyImpl(fileMatcherWithCache, include, excludes, shouldHaveNoMatches, customMessage);
}

// Normal matching
Verify(info.Include, info.Excludes);

Expand Down Expand Up @@ -153,7 +166,7 @@ public class GetFilesComplexGlobbingMatchingInfo
@"subdirectory\subdirectory.cs",
@"build\baz\foo.cs",
@"readme.txt",
@"licence.md"
@"licence"
};

/// <summary>
Expand Down Expand Up @@ -355,7 +368,7 @@ public static IEnumerable<object[]> GetTestData()
ExpectedMatches = new[]
{
@"readme.txt",
@"licence.md"
@"licence"
}
}
};
Expand Down

0 comments on commit 7bc761a

Please sign in to comment.