Skip to content

Commit 9d09fde

Browse files
committed
complete filter expression evaluation
1 parent 1dd3b70 commit 9d09fde

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/coverlet.core/Helpers/InstrumentationHelper.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,44 +83,49 @@ public static void DeleteHitsFile(string path)
8383

8484
public static bool IsModuleExcluded(string module, string[] filters)
8585
{
86-
if (filters == null || !filters.Any())
86+
if (filters == null)
8787
return false;
8888

89-
module = Path.GetFileNameWithoutExtension(module);
9089
bool isMatch = false;
90+
module = Path.GetFileNameWithoutExtension(module);
9191

9292
foreach (var filter in filters)
9393
{
9494
if (!IsValidFilterExpression(filter))
9595
continue;
9696

97-
string pattern = filter.Substring(1, filter.IndexOf(']') - 1);
98-
pattern = WildcardToRegex(pattern);
97+
string modulePattern = filter.Substring(1, filter.IndexOf(']') - 1);
98+
string typePattern = filter.Substring(filter.IndexOf(']') + 1);
9999

100-
var regex = new Regex(pattern);
101-
isMatch = regex.IsMatch(module);
100+
modulePattern = WildcardToRegex(modulePattern);
101+
102+
var regex = new Regex(modulePattern);
103+
isMatch = regex.IsMatch(module) && typePattern == "*";
102104
}
103105

104106
return isMatch;
105107
}
106108

107-
public static bool IsTypeExcluded(string type, string[] filters)
109+
public static bool IsTypeExcluded(string module, string type, string[] filters)
108110
{
109-
if (filters == null || !filters.Any())
111+
if (filters == null)
110112
return false;
111113

112114
bool isMatch = false;
115+
module = Path.GetFileNameWithoutExtension(module);
113116

114117
foreach (var filter in filters)
115118
{
116119
if (!IsValidFilterExpression(filter))
117120
continue;
118121

119-
string pattern = filter.Substring(filter.IndexOf(']') + 1);
120-
pattern = WildcardToRegex(pattern);
122+
string typePattern = filter.Substring(filter.IndexOf(']') + 1);
123+
string modulePattern = filter.Substring(1, filter.IndexOf(']') - 1);
121124

122-
var regex = new Regex(pattern);
123-
isMatch = regex.IsMatch(type);
125+
typePattern = WildcardToRegex(typePattern);
126+
modulePattern = WildcardToRegex(modulePattern);
127+
128+
isMatch = new Regex(typePattern).IsMatch(type) && new Regex(modulePattern).IsMatch(module);
124129
}
125130

126131
return isMatch;
@@ -206,7 +211,10 @@ private static bool IsValidFilterExpression(string filter)
206211
if (filter.IndexOf(']') - filter.IndexOf('[') == 1)
207212
return false;
208213

209-
if (new Regex(@"[^\w*]").IsMatch(filter.Replace("[", "").Replace("]", "")))
214+
if (filter.EndsWith("]"))
215+
return false;
216+
217+
if (new Regex(@"[^\w*]").IsMatch(filter.Replace(".", "").Replace("[", "").Replace("]", "")))
210218
return false;
211219

212220
return true;

src/coverlet.core/Instrumentation/Instrumenter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private void InstrumentModule()
7575
private void InstrumentType(TypeDefinition type)
7676
{
7777
if (type.CustomAttributes.Any(IsExcludeAttribute)
78-
&& InstrumentationHelper.IsTypeExcluded(type.FullName, _filters))
78+
|| InstrumentationHelper.IsTypeExcluded(_module, type.FullName, _filters))
7979
return;
8080

8181
foreach (var method in type.Methods)

0 commit comments

Comments
 (0)