Skip to content
Merged
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
33 changes: 33 additions & 0 deletions TextFilter.Test/TextFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,39 @@
Assert.IsTrue(TextFilter.IsMatch("Docs/README.md", "**/readme.md", TextFilterType.Glob, TextFilterMatchOptions.ByWholeString, TextFilterCaseSensitivity.CaseInsensitive));
}

[TestMethod]
[DataRow("**/")]
[DataRow("**/**/")]
public void GlobTokenOfOnlyGlobStarSlashMatchesItemsEndingInSeparator(string filter)
{
List<string> result = [.. TextFilter.Filter(["readme.md", "src/a.cs", "docs/", "a b"], filter)];

Assert.AreEqual(1, result.Count);

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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.HasCount' instead of 'Assert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGfQKqsFKH6QC6b0Ip3&open=AaGfQKqsFKH6QC6b0Ip3&pullRequest=182
Assert.AreEqual("docs/", result[0]);
}

[TestMethod]
[DataRow("-**/")]
[DataRow("-**/**/")]
public void GlobExcludedTokenOfOnlyGlobStarSlashHidesItemsEndingInSeparator(string filter)
{
List<string> result = [.. TextFilter.Filter(["readme.md", "src/a.cs", "docs/", "a b"], filter)];

Assert.AreEqual(3, result.Count);

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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.HasCount' instead of 'Assert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGfQKqsFKH6QC6b0Ip4&open=AaGfQKqsFKH6QC6b0Ip4&pullRequest=182
Assert.AreEqual("readme.md", result[0]);
Assert.AreEqual("src/a.cs", result[1]);
Assert.AreEqual("a b", result[2]);
}

[TestMethod]
public void GlobRequiredTokenOfOnlyGlobStarSlashMatchesOnlyItemsEndingInSeparator()
{
List<string> result = [.. TextFilter.Filter(["readme.md", "src/a.cs", "docs/"], "+**/")];

Assert.AreEqual(1, result.Count);

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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.HasCount' instead of 'Assert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGfQKqsFKH6QC6b0Ip5&open=AaGfQKqsFKH6QC6b0Ip5&pullRequest=182
Assert.AreEqual("docs/", result[0]);
}

[TestMethod]
public void GlobLiteralSlashInPatternStillMatchesSlashInText()
{
Expand Down
13 changes: 10 additions & 3 deletions TextFilter/TextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
private static readonly TimeSpan RegexMatchTimeout = TimeSpan.FromSeconds(1);

[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "SYSLIB1045:Convert to 'GeneratedRegexAttribute'.", Justification = "Not available in older frameworks")]
private static Regex RegexMatchAnything() => new(".*", RegexOptions.Compiled);

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

// Stands in for a pattern that has already run out of time once. The timeout bounds a single
// IsMatch call, so without this the same pathological pattern would pay it again for every word
Expand Down Expand Up @@ -300,7 +300,7 @@

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

Check warning on line 303 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.

Check warning on line 303 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.

Check warning on line 303 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.

Check warning on line 303 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.

Check warning on line 303 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 @@ -643,9 +643,16 @@
try
{
string? expandedToken = ExpandGlobClasses(filterToken);
globs = expandedToken is null ? null : [.. ExpandGlobStarSegments(MaskPathSeparators(expandedToken)).Select(pattern => caseSensitivity is TextFilterCaseSensitivity.CaseInsensitive
? Glob.Parse(pattern, CaseInsensitiveGlobOptions)
: Glob.Parse(pattern))];
// A token made only of "**/" segments expands to the empty string as well as to "*/".
// Glob.Parse throws on an empty pattern, which would mark the whole token unparseable and
// discard the valid expansions with it. An empty pattern could only match empty text, which
// never reaches the matcher, so drop it; a token with nothing left is still unparseable.
Glob[]? parsed = expandedToken is null ? null : [.. ExpandGlobStarSegments(MaskPathSeparators(expandedToken))
.Where(pattern => pattern.Length != 0)
.Select(pattern => caseSensitivity is TextFilterCaseSensitivity.CaseInsensitive
? Glob.Parse(pattern, CaseInsensitiveGlobOptions)
: Glob.Parse(pattern))];
globs = parsed is { Length: > 0 } ? parsed : null;
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
Expand Down
Loading