Merge pull request #15325 from RasmusWL/c#-filter-order

C#: Respect order of `LGTM_INDEX_FILTERS` in buildless extraction
This commit is contained in:
Rasmus Wriedt Larsen
2024-01-16 09:28:44 +01:00
committed by GitHub
2 changed files with 8 additions and 4 deletions

View File

@@ -74,16 +74,18 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
includeByDefault)
});
// Move included pathfilters to the front of the list:
pathFilters.Sort((pf1, pf2) => -1 * pf1.Include.CompareTo(pf2.Include));
return unfilteredResult.Where(f =>
{
var include = f.FileInclusion.Include;
foreach (var pathFilter in pathFilters)
// LGTM_INDEX_FILTERS is a prioritized list, where later filters take
// priority over earlier ones.
for (int i = pathFilters.Count - 1; i >= 0; i--)
{
var pathFilter = pathFilters[i];
if (pathFilter.Regex.IsMatch(f.FileInclusion.Path))
{
include = pathFilter.Include;
break;
}
}

View File

@@ -165,6 +165,7 @@ namespace Semmle.Extraction.Tests
{
(var testSubject, var progressMonitor, var files) = TestSetup();
// NOTE: the ordering DOES matter, later filters takes priority, so the exclude will end up not mattering at all.
Environment.SetEnvironmentVariable("LGTM_INDEX_FILTERS", """
exclude:c/x/z
include:c/x
@@ -174,7 +175,8 @@ namespace Semmle.Extraction.Tests
var expected = GetExpected(
[
"/a/b/c/x/y/i.cs"
"/a/b/c/x/y/i.cs",
"/a/b/c/x/z/i.cs"
]);
AssertFileInfoEquivalence(expected, filtered);