Skip to content

Commit

Permalink
#704 Improved handling of file filters (path separators)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpalme committed Nov 9, 2024
1 parent 20438c8 commit 9cc5ad8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ For further details take a look at LICENSE.txt.

CHANGELOG

5.3.12.0
5.4.0.0

* Fix: #699 Rounding crap score to full number
* Fix: #700 Fixed handling of .netconfig files (sourceDirectories, reportTypes, plugins, assemblyFilters, etc.)
* Fix: #704 Improved handling of file filters

5.3.11.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public void OnlyIncludesWithWildcards_NotMatchingElement_ElementIsNotAccepted()
{
IFilter filter = new DefaultFilter(new[] { "+Test*" });

Assert.False(filter.IsElementIncludedInReport("PrefixTest"), "Element is expected to be included.");
Assert.False(filter.IsElementIncludedInReport("prefixtest"), "Element is expected to be included.");
Assert.False(filter.IsElementIncludedInReport("PrefixTest123"), "Element is expected to be included.");
Assert.False(filter.IsElementIncludedInReport("prefixtest123"), "Element is expected to be included.");
Assert.False(filter.IsElementIncludedInReport("PrefixTest"), "Element is expected to be excluded.");
Assert.False(filter.IsElementIncludedInReport("prefixtest"), "Element is expected to be excluded.");
Assert.False(filter.IsElementIncludedInReport("PrefixTest123"), "Element is expected to be excluded.");
Assert.False(filter.IsElementIncludedInReport("prefixtest123"), "Element is expected to be excluded.");
Assert.True(filter.HasCustomFilters);
}

Expand Down Expand Up @@ -100,10 +100,50 @@ public void IncludesAndExcludesWithWildcards_NotMatchingElement_ElementIsNotAcce
{
IFilter filter = new DefaultFilter(new[] { "+Test*", "-Tes*" });

Assert.False(filter.IsElementIncludedInReport("Test"), "Element is expected to be included.");
Assert.False(filter.IsElementIncludedInReport("test"), "Element is expected to be included.");
Assert.False(filter.IsElementIncludedInReport("PrefixTest123"), "Element is expected to be included.");
Assert.False(filter.IsElementIncludedInReport("prefixtest123"), "Element is expected to be included.");
Assert.False(filter.IsElementIncludedInReport("Test"), "Element is expected to be excluded.");
Assert.False(filter.IsElementIncludedInReport("test"), "Element is expected to be excluded.");
Assert.False(filter.IsElementIncludedInReport("PrefixTest123"), "Element is expected to be excluded.");
Assert.False(filter.IsElementIncludedInReport("prefixtest123"), "Element is expected to be excluded.");
Assert.True(filter.HasCustomFilters);
}

[Fact]
public void LinuxPath_NoOsIndependantPathSeparator()
{
IFilter filter = new DefaultFilter(new[] { "+abc/def" });

Assert.True(filter.IsElementIncludedInReport("abc/def"), "Element is expected to be included.");
Assert.False(filter.IsElementIncludedInReport("abc\\def"), "Element is expected to be excluded.");
Assert.True(filter.HasCustomFilters);
}

[Fact]
public void LinuxPath_OsIndependantPathSeparator()
{
IFilter filter = new DefaultFilter(new[] { "+abc/def" }, true);

Assert.True(filter.IsElementIncludedInReport("abc/def"), "Element is expected to be included.");
Assert.True(filter.IsElementIncludedInReport("abc\\def"), "Element is expected to be included.");
Assert.True(filter.HasCustomFilters);
}

[Fact]
public void WindowsPath_NoOsIndependantPathSeparator()
{
IFilter filter = new DefaultFilter(new[] { "+abc\\def" });

Assert.False(filter.IsElementIncludedInReport("abc/def"), "Element is expected to be excluded.");
Assert.True(filter.IsElementIncludedInReport("abc\\def"), "Element is expected to be included.");
Assert.True(filter.HasCustomFilters);
}

[Fact]
public void WindowsPath_OsIndependantPathSeparator()
{
IFilter filter = new DefaultFilter(new[] { "+abc/def" }, true);

Assert.True(filter.IsElementIncludedInReport("abc/def"), "Element is expected to be included.");
Assert.True(filter.IsElementIncludedInReport("abc\\def"), "Element is expected to be included.");
Assert.True(filter.HasCustomFilters);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ReportGenerator.Core/Parser/CoverageReportParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public CoverageReportParser(IReportContext reportContext)
this.sourceDirectories = reportContext.ReportConfiguration.SourceDirectories ?? throw new ArgumentNullException(nameof(reportContext.ReportConfiguration.SourceDirectories));
this.assemblyFilter = new DefaultFilter(reportContext.ReportConfiguration.AssemblyFilters);
this.classFilter = new DefaultFilter(reportContext.ReportConfiguration.ClassFilters);
this.fileFilter = new DefaultFilter(reportContext.ReportConfiguration.FileFilters);
this.fileFilter = new DefaultFilter(reportContext.ReportConfiguration.FileFilters, true);
}

/// <summary>
Expand Down
27 changes: 23 additions & 4 deletions src/ReportGenerator.Core/Parser/Filtering/DefaultFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public class DefaultFilter : IFilter
/// </summary>
/// <param name="filters">The filters.</param>
public DefaultFilter(IEnumerable<string> filters)
: this(filters, false)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DefaultFilter"/> class.
/// </summary>
/// <param name="filters">The filters.</param>
/// <param name="osIndependantPathSeparator">Indicates whether filter thould be treated as paths and the operating system format should be ignored.</param>
public DefaultFilter(IEnumerable<string> filters, bool osIndependantPathSeparator)
{
if (filters == null)
{
Expand All @@ -36,12 +46,12 @@ public DefaultFilter(IEnumerable<string> filters)

this.excludeFilters = filters
.Where(f => f.StartsWith("-", StringComparison.OrdinalIgnoreCase))
.Select(f => CreateFilterRegex(f))
.Select(f => CreateFilterRegex(f, osIndependantPathSeparator))
.ToArray();

this.includeFilters = filters
.Where(f => f.StartsWith("+", StringComparison.OrdinalIgnoreCase))
.Select(f => CreateFilterRegex(f))
.Select(f => CreateFilterRegex(f, osIndependantPathSeparator))
.ToArray();

this.HasCustomFilters = this.excludeFilters.Length > 0 || this.includeFilters.Length > 0;
Expand All @@ -50,7 +60,7 @@ public DefaultFilter(IEnumerable<string> filters)
{
this.includeFilters = new[]
{
CreateFilterRegex("+*")
CreateFilterRegex("+*", false)
};
}
}
Expand Down Expand Up @@ -87,14 +97,23 @@ public bool IsElementIncludedInReport(string name)
/// Special characters are escaped. Wildcards '*' are converted to '.*'.
/// </summary>
/// <param name="filter">The filter.</param>
/// <param name="osIndependantPathSeparator">Indicates whether filter thould be treated as paths and the operating system format should be ignored.</param>
/// <returns>The regular expression.</returns>
private static Regex CreateFilterRegex(string filter)
private static Regex CreateFilterRegex(string filter, bool osIndependantPathSeparator)
{
filter = filter.Substring(1);
filter = filter.Replace("*", "$$$*");
filter = Regex.Escape(filter);
filter = filter.Replace(@"\$\$\$\*", ".*");

if (osIndependantPathSeparator)
{
filter = filter
.Replace("/", "$$$")
.Replace("\\", "$$$")
.Replace("$$$", @"[/\\]");
}

return new Regex($"^{filter}$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
}
Expand Down

0 comments on commit 9cc5ad8

Please sign in to comment.