Skip to content

Commit

Permalink
complete filter expression evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
tonerdo committed May 13, 2018
1 parent 1dd3b70 commit 9d09fde
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
34 changes: 21 additions & 13 deletions src/coverlet.core/Helpers/InstrumentationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,44 +83,49 @@ public static void DeleteHitsFile(string path)

public static bool IsModuleExcluded(string module, string[] filters)
{
if (filters == null || !filters.Any())
if (filters == null)
return false;

module = Path.GetFileNameWithoutExtension(module);
bool isMatch = false;
module = Path.GetFileNameWithoutExtension(module);

foreach (var filter in filters)
{
if (!IsValidFilterExpression(filter))
continue;

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

var regex = new Regex(pattern);
isMatch = regex.IsMatch(module);
modulePattern = WildcardToRegex(modulePattern);

var regex = new Regex(modulePattern);
isMatch = regex.IsMatch(module) && typePattern == "*";
}

return isMatch;
}

public static bool IsTypeExcluded(string type, string[] filters)
public static bool IsTypeExcluded(string module, string type, string[] filters)
{
if (filters == null || !filters.Any())
if (filters == null)
return false;

bool isMatch = false;
module = Path.GetFileNameWithoutExtension(module);

foreach (var filter in filters)
{
if (!IsValidFilterExpression(filter))
continue;

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

var regex = new Regex(pattern);
isMatch = regex.IsMatch(type);
typePattern = WildcardToRegex(typePattern);
modulePattern = WildcardToRegex(modulePattern);

isMatch = new Regex(typePattern).IsMatch(type) && new Regex(modulePattern).IsMatch(module);
}

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

if (new Regex(@"[^\w*]").IsMatch(filter.Replace("[", "").Replace("]", "")))
if (filter.EndsWith("]"))
return false;

if (new Regex(@"[^\w*]").IsMatch(filter.Replace(".", "").Replace("[", "").Replace("]", "")))
return false;

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/coverlet.core/Instrumentation/Instrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void InstrumentModule()
private void InstrumentType(TypeDefinition type)
{
if (type.CustomAttributes.Any(IsExcludeAttribute)
&& InstrumentationHelper.IsTypeExcluded(type.FullName, _filters))
|| InstrumentationHelper.IsTypeExcluded(_module, type.FullName, _filters))
return;

foreach (var method in type.Methods)
Expand Down

0 comments on commit 9d09fde

Please # to comment.