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
38 changes: 38 additions & 0 deletions TextFilter.Test/TextFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,44 @@
Assert.AreEqual("src/x/a.cs", result[1]);
}

[TestMethod]
[DataRow("src/", "src/**")]
[DataRow("src/a.cs", "src/**")]
[DataRow("src/lib/b.cs", "src/**")]
[DataRow("a/b/", "a/**/b/**")]
[DataRow("/", "/**")]
[DataRow("-b/", "*/**")]
[DataRow("-/", "?/**")]
[DataRow("b-/", "b-/**")]
[DataRow(@"src\", @"src\**")]
public void TrailingGlobStarSegmentMatchesTheEmptyRemainder(string text, string filter)
{
Assert.IsTrue(TextFilter.IsMatch(text, filter, TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
Assert.IsTrue(TextFilter.IsMatch(text, filter));
}

[TestMethod]
[DataRow("src", "src/**")]
[DataRow("docs/a.cs", "src/**")]
[DataRow("foo", "foo**")]
public void TrailingGlobStarSegmentStillNeedsItsDirectory(string text, string filter)
{
Assert.IsFalse(TextFilter.IsMatch(text, filter, TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
}

[TestMethod]
public void TrailingGlobStarSegmentFiltersLikeASingleStar()
{
string[] items = ["src/", "src/a.cs", "src/lib/b.cs", "build/", "build/out.dll", "readme.md"];

List<string> included = ["src/", "src/a.cs", "src/lib/b.cs"];
List<string> excluded = ["src/", "src/a.cs", "src/lib/b.cs", "readme.md"];

CollectionAssert.AreEqual(included, TextFilter.Filter(items, "src/**").ToList());

Check warning on line 1437 in TextFilter.Test/TextFilterTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGwPTWeBxEszC6s885k&open=AaGwPTWeBxEszC6s885k&pullRequest=192
CollectionAssert.AreEqual(TextFilter.Filter(items, "src/*").ToList(), TextFilter.Filter(items, "src/**").ToList());

Check warning on line 1438 in TextFilter.Test/TextFilterTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGwPTWeBxEszC6s885l&open=AaGwPTWeBxEszC6s885l&pullRequest=192
CollectionAssert.AreEqual(excluded, TextFilter.Filter(items, "-build/**").ToList());

Check warning on line 1439 in TextFilter.Test/TextFilterTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGwPTWeBxEszC6s885m&open=AaGwPTWeBxEszC6s885m&pullRequest=192
}

[TestMethod]
public void GlobStarSlashMatchesCaseInsensitively()
{
Expand Down
18 changes: 17 additions & 1 deletion TextFilter/TextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@

return ExcludedTokenPrefixes.Contains(prefix)
? TextFilterTokenType.Excluded
: RequiredTokenPrefixes.Contains(prefix)

Check warning on line 306 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.
? TextFilterTokenType.Required
: TextFilterTokenType.Optional;
})
Expand Down Expand Up @@ -334,7 +334,7 @@
/// matches nor rejects anything; as a required token it matches everything; and as an excluded
/// token it excludes nothing.
/// </remarks>
public static bool DoesMatchGlob(string text, string filter, TextFilterMatchOptions textFilterMatchOptions, TextFilterCaseSensitivity caseSensitivity = TextFilterCaseSensitivity.CaseSensitive)

Check warning on line 337 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 35 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check warning on line 337 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 35 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.
{
Ensure.NotNull(text);
Ensure.NotNull(filter);
Expand Down Expand Up @@ -408,7 +408,7 @@
/// <param name="textTokens">The set of text tokens to match against.</param>
/// <param name="caseSensitivity">Whether the match distinguishes uppercase from lowercase.</param>
/// <returns><c>true</c> if any token matches the glob filter token; otherwise, <c>false</c>.</returns>
public static bool AnyTokenMatchesGlobFilter(string filterToken, HashSet<string> textTokens, TextFilterCaseSensitivity caseSensitivity = TextFilterCaseSensitivity.CaseSensitive)

Check warning on line 411 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 45 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check warning on line 411 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 45 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check warning on line 411 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 45 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.
{
Ensure.NotNull(filterToken);
Ensure.NotNull(textTokens);
Expand All @@ -425,7 +425,7 @@
/// <param name="textTokens">The set of text tokens to match against.</param>
/// <param name="caseSensitivity">Whether the match distinguishes uppercase from lowercase.</param>
/// <returns><c>true</c> if all tokens match the glob filter token; otherwise, <c>false</c>.</returns>
public static bool AllTokensMatchGlobFilter(string filterToken, HashSet<string> textTokens, TextFilterCaseSensitivity caseSensitivity = TextFilterCaseSensitivity.CaseSensitive)

Check warning on line 428 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 50 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check warning on line 428 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 50 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check warning on line 428 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 50 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.
{
Ensure.NotNull(filterToken);
Ensure.NotNull(textTokens);
Expand Down Expand Up @@ -505,7 +505,23 @@
}

string tail = maskedToken[start..];
return [.. expansions.Select(prefix => prefix + tail).Distinct(StringComparer.Ordinal)];
return [.. expansions.Select(prefix => ExpandTrailingGlobStar(prefix + tail)).Distinct(StringComparer.Ordinal)];
}

// A trailing whole-segment "**", as in "src/**", should match the empty remainder too, so that
// "dir/**" matches at least what "dir/*" does, "dir/" included. With the separator masked,
// DotNet.Glob reads it as "**" joined to a literal, which needs at least one more character. Since
// * already crosses the masked separator, a plain "*" means the same and also matches nothing.
private static string ExpandTrailingGlobStar(string expansion)
{
if (!expansion.EndsWith("**", StringComparison.Ordinal))
{
return expansion;
}

int globStarStart = expansion.Length - 2;
bool isWholeSegment = globStarStart == 0 || expansion[globStarStart - 1] == MaskedPathSeparator;
return isWholeSegment ? expansion[..globStarStart] + "*" : expansion;
}

// The spans of the closed bracket classes in a token, read with the same boundary rules as
Expand Down Expand Up @@ -816,7 +832,7 @@
/// <see cref="TextFilterMatchOptions.ByWholeString"/> for those.
/// </para>
/// </remarks>
public static bool DoesMatchRegex(string text, string filter, TextFilterMatchOptions textFilterMatchOptions, TextFilterCaseSensitivity caseSensitivity = TextFilterCaseSensitivity.CaseSensitive)

Check warning on line 835 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 40 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.
{
Ensure.NotNull(text);
Ensure.NotNull(filter);
Expand Down
Loading