Skip to content

Commit

Permalink
Clarify the behavior of FilePatternMatch Path and Stem (dotnet#48093)
Browse files Browse the repository at this point in the history
* Add theories that illustrate globbing Path/Stem behavior

* Update Path/Stem docs for FilePatternMatch

* Apply suggestions from code review

Co-authored-by: Carlos Sanchez <[email protected]>

Co-authored-by: Carlos Sanchez <[email protected]>
  • Loading branch information
jeffhandley and carlossanlop authored Feb 18, 2021
1 parent fd9886d commit e7afcda
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,28 @@ namespace Microsoft.Extensions.FileSystemGlobbing
public struct FilePatternMatch : IEquatable<FilePatternMatch>
{
/// <summary>
/// The path to the file matched
/// The path to the file matched, relative to the beginning of the matching search pattern.
/// </summary>
/// <remarks>
/// If the matcher searched for "**/*.cs" using "src/Project" as the directory base and the pattern matcher found
/// "src/Project/Interfaces/IFile.cs", then Stem = "Interfaces/IFile.cs" and Path = "src/Project/Interfaces/IFile.cs".
/// If the matcher searched for "src/Project/**/*.cs" and the pattern matcher found "src/Project/Interfaces/IFile.cs",
/// then <see cref="Stem" /> = "Interfaces/IFile.cs" and <see cref="Path" /> = "src/Project/Interfaces/IFile.cs".
/// </remarks>
public string Path { get; }

/// <summary>
/// The subpath to the matched file under the base directory searched
/// The subpath to the file matched, relative to the first wildcard in the matching search pattern.
/// </summary>
/// <remarks>
/// If the matcher searched for "**/*.cs" using "src/Project" as the directory base and the pattern matcher found
/// "src/Project/Interfaces/IFile.cs",
/// then Stem = "Interfaces/IFile.cs" and Path = "src/Project/Interfaces/IFile.cs".
/// If the matcher searched for "src/Project/**/*.cs" and the pattern matcher found "src/Project/Interfaces/IFile.cs",
/// then <see cref="Stem" /> = "Interfaces/IFile.cs" and <see cref="Path" /> = "src/Project/Interfaces/IFile.cs".
/// </remarks>
public string Stem { get; }

/// <summary>
/// Initializes new instance of <see cref="FilePatternMatch" />
/// </summary>
/// <param name="path">The path to the matched file</param>
/// <param name="stem">The stem</param>
/// <param name="path">The path to the file matched, relative to the beginning of the matching search pattern.</param>
/// <param name="stem">The subpath to the file matched, relative to the first wildcard in the matching search pattern.</param>
public FilePatternMatch(string path, string stem)
{
Path = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,203 @@ public void MultipleSubDirsAfterFirstWildcardMatch_HasCorrectStem_WithInMemory()
AssertExtensions.CollectionEqual(expected, actual, StringComparer.OrdinalIgnoreCase);
}

[Theory] // rootDir, includePattern, expectedPath
[InlineData(@"root", @"*.0", @"test.0")]
[InlineData(@"root", @"**/*.0", @"test.0")]
public void PathIncludesAllSegmentsFromPattern_RootDirectory(string root, string includePattern, string expectedPath)
{
var fileToFind = @"root/test.0";

var matcher = new Matcher();
matcher.AddInclude(includePattern);

var results = matcher.Match(root, new[] { fileToFind });
var actualPath = results.Files.Select(file => file.Path).SingleOrDefault();

Assert.Equal(expectedPath, actualPath);

// Also test all scenarios with the `./` current directory prefix
matcher = new Matcher();
matcher.AddInclude("./" + includePattern);

results = matcher.Match(root, new[] { fileToFind });
actualPath = results.Files.Select(file => file.Path).SingleOrDefault();

Assert.Equal(expectedPath, actualPath);
}

[Theory] // rootDir, includePattern, expectedPath
[InlineData(@"root/dir1", @"*.1", @"test.1")]
[InlineData(@"root/dir1", @"**/*.1", @"test.1")]
[InlineData(@"root", @"dir1/*.1", @"dir1/test.1")]
[InlineData(@"root", @"dir1/**/*.1", @"dir1/test.1")]
[InlineData(@"root", @"**/dir1/*.1", @"dir1/test.1")]
[InlineData(@"root", @"**/dir1/**/*.1", @"dir1/test.1")]
[InlineData(@"root", @"**/*.1", @"dir1/test.1")]
public void PathIncludesAllSegmentsFromPattern_OneDirectoryDeep(string root, string includePattern, string expectedPath)
{
var fileToFind = @"root/dir1/test.1";

var matcher = new Matcher();
matcher.AddInclude(includePattern);

var results = matcher.Match(root, new[] { fileToFind });
var actualPath = results.Files.Select(file => file.Path).SingleOrDefault();

Assert.Equal(expectedPath, actualPath);

// Also test all scenarios with the `./` current directory prefix
matcher = new Matcher();
matcher.AddInclude("./" + includePattern);

results = matcher.Match(root, new[] { fileToFind });
actualPath = results.Files.Select(file => file.Path).SingleOrDefault();

Assert.Equal(expectedPath, actualPath);
}

[Theory] // rootDir, includePattern, expectedPath
[InlineData(@"root/dir1/dir2", @"*.2", @"test.2")]
[InlineData(@"root/dir1/dir2", @"**/*.2", @"test.2")]
[InlineData(@"root/dir1", @"dir2/*.2", @"dir2/test.2")]
[InlineData(@"root/dir1", @"dir2/**/*.2", @"dir2/test.2")]
[InlineData(@"root/dir1", @"**/dir2/*.2", @"dir2/test.2")]
[InlineData(@"root/dir1", @"**/dir2/**/*.2", @"dir2/test.2")]
[InlineData(@"root/dir1", @"**/*.2", @"dir2/test.2")]
[InlineData(@"root", @"dir1/dir2/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"dir1/dir2/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir1/dir2/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir1/**/dir2/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir1/**/dir2/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"dir1/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir1/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir2/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir2/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/*.2", @"dir1/dir2/test.2")]
public void PathIncludesAllSegmentsFromPattern_TwoDirectoriesDeep(string root, string includePattern, string expectedPath)
{
var fileToFind = @"root/dir1/dir2/test.2";

var matcher = new Matcher();
matcher.AddInclude(includePattern);

var results = matcher.Match(root, new[] { fileToFind });
var actualPath = results.Files.Select(file => file.Path).SingleOrDefault();

Assert.Equal(expectedPath, actualPath);

// Also test all scenarios with the `./` current directory prefix
matcher = new Matcher();
matcher.AddInclude("./" + includePattern);

results = matcher.Match(root, new[] { fileToFind });
actualPath = results.Files.Select(file => file.Path).SingleOrDefault();

Assert.Equal(expectedPath, actualPath);
}

[Theory] // rootDir, includePattern, expectedStem
[InlineData(@"root", @"*.0", @"test.0")]
[InlineData(@"root", @"**/*.0", @"test.0")]
public void StemIncludesAllSegmentsFromPatternStartingAtWildcard_RootDirectory(string root, string includePattern, string expectedStem)
{
var fileToFind = @"root/test.0";

var matcher = new Matcher();
matcher.AddInclude(includePattern);

var results = matcher.Match(root, new[] { fileToFind });
var actualStem = results.Files.Select(file => file.Stem).SingleOrDefault();

Assert.Equal(expectedStem, actualStem);

// Also test all scenarios with the `./` current directory prefix
matcher = new Matcher();
matcher.AddInclude("./" + includePattern);

results = matcher.Match(root, new[] { fileToFind });
actualStem = results.Files.Select(file => file.Stem).SingleOrDefault();

Assert.Equal(expectedStem, actualStem);
}

[Theory] // rootDir, includePattern, expectedStem
[InlineData(@"root/dir1", @"*.1", @"test.1")]
[InlineData(@"root/dir1", @"**/*.1", @"test.1")]
[InlineData(@"root", @"dir1/*.1", @"test.1")]
[InlineData(@"root", @"dir1/**/*.1", @"test.1")]
[InlineData(@"root", @"**/dir1/*.1", @"dir1/test.1")]
[InlineData(@"root", @"**/dir1/**/*.1", @"dir1/test.1")]
[InlineData(@"root", @"**/*.1", @"dir1/test.1")]
public void StemIncludesAllSegmentsFromPatternStartingAtWildcard_OneDirectoryDeep(string root, string includePattern, string expectedStem)
{
var fileToFind = @"root/dir1/test.1";

var matcher = new Matcher();
matcher.AddInclude(includePattern);

var results = matcher.Match(root, new[] { fileToFind });
var actualStem = results.Files.Select(file => file.Stem).SingleOrDefault();

Assert.Equal(expectedStem, actualStem);

// Also test all scenarios with the `./` current directory prefix
matcher = new Matcher();
matcher.AddInclude("./" + includePattern);

results = matcher.Match(root, new[] { fileToFind });
actualStem = results.Files.Select(file => file.Stem).SingleOrDefault();

Assert.Equal(expectedStem, actualStem);
}

[Theory] // rootDir, includePattern, expectedStem
[InlineData(@"root/dir1/dir2", @"*.2", @"test.2")]
[InlineData(@"root/dir1/dir2", @"**/*.2", @"test.2")]
[InlineData(@"root/dir1", @"dir2/*.2", @"test.2")]
[InlineData(@"root/dir1", @"dir2/**/*.2", @"test.2")]
[InlineData(@"root/dir1", @"**/dir2/*.2", @"dir2/test.2")]
[InlineData(@"root/dir1", @"**/dir2/**/*.2", @"dir2/test.2")]
[InlineData(@"root/dir1", @"**/*.2", @"dir2/test.2")]
[InlineData(@"root", @"dir1/dir2/*.2", @"test.2")]
[InlineData(@"root", @"dir1/dir2/**/*.2", @"test.2")]
[InlineData(@"root", @"**/dir1/dir2/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir1/**/dir2/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir1/**/dir2/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"dir1/**/*.2", @"dir2/test.2")]
[InlineData(@"root", @"**/dir1/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir2/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/dir2/**/*.2", @"dir1/dir2/test.2")]
[InlineData(@"root", @"**/*.2", @"dir1/dir2/test.2")]
public void StemIncludesAllSegmentsFromPatternStartingAtWildcard_TwoDirectoriesDeep(string root, string includePattern, string expectedStem)
{
var fileToFind = @"root/dir1/dir2/test.2";

var matcher = new Matcher();
matcher.AddInclude(includePattern);

var results = matcher.Match(root, new[] { fileToFind });
var actualStem = results.Files.Select(file => file.Stem).SingleOrDefault();

Assert.Equal(expectedStem, actualStem);

// Also test all scenarios with the `./` current directory prefix
matcher = new Matcher();
matcher.AddInclude("./" + includePattern);

results = matcher.Match(root, new[] { fileToFind });
actualStem = results.Files.Select(file => file.Stem).SingleOrDefault();

Assert.Equal(expectedStem, actualStem);
}

private List<string> GetFileList()
{
return new List<string>
{
"root/test.0",
"root/dir1/test.1",
"root/dir1/dir2/test.2",
"src/project/source1.cs",
"src/project/sub/source2.cs",
"src/project/sub/source3.cs",
Expand Down

0 comments on commit e7afcda

Please sign in to comment.