Skip to content
Merged
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
17 changes: 14 additions & 3 deletions TUnit.Engine/Services/MetadataFilterMatcher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.RegularExpressions;
#if NET8_0_OR_GREATER
using System.Buffers;
#endif
Expand Down Expand Up @@ -117,7 +118,7 @@ public bool CouldMatch(string testClassName, string testMethodName)
/// Implementation of metadata filter matching logic extracted from TestBuilder.
/// Evaluates if test metadata could match an execution filter without building tests.
/// </summary>
internal sealed class MetadataFilterMatcher : IMetadataFilterMatcher
internal sealed partial class MetadataFilterMatcher : IMetadataFilterMatcher
{
#pragma warning disable TPEXP
private static readonly ConstructorInfo _treeNodeFilterConstructor =
Expand All @@ -129,6 +130,16 @@ internal sealed class MetadataFilterMatcher : IMetadataFilterMatcher

private static readonly ConcurrentDictionary<string, string> _strippedFilterCache = new();

// Matches property-bag filters like [key=value]; used to strip them before path parsing.
#if NET8_0_OR_GREATER
[GeneratedRegex(@"\[[^\]]*\]")]
private static partial Regex PropertyFilterRegex();
#else
private static readonly Regex _propertyFilterRegex = new(@"\[[^\]]*\]", RegexOptions.Compiled);

private static Regex PropertyFilterRegex() => _propertyFilterRegex;
#endif

/// <summary>
/// Extract hints from a filter that can be used to pre-filter test sources by type.
/// </summary>
Expand All @@ -149,7 +160,7 @@ public static FilterHints ExtractFilterHints(ITestExecutionFilter? filter)
// Strip property filters like [key=value]
if (filterString.Contains('['))
{
filterString = System.Text.RegularExpressions.Regex.Replace(filterString, @"\[([^\]]*)\]", "");
filterString = PropertyFilterRegex().Replace(filterString, "");
}

// Parse path: /{assembly}/{namespace}/{className}/{methodName}
Expand Down Expand Up @@ -407,7 +418,7 @@ private static bool CouldMatchTreeNodeFilter(TreeNodeFilter filter, TestMetadata
if (filterString.Contains('['))
{
var strippedFilterString = _strippedFilterCache.GetOrAdd(filterString,
static fs => System.Text.RegularExpressions.Regex.Replace(fs, @"\[([^\]]*)\]", ""));
static fs => PropertyFilterRegex().Replace(fs, ""));
pathOnlyFilter = CreateTreeNodeFilterViaReflection(strippedFilterString);
}
else
Expand Down
Loading