Skip to content

Commit

Permalink
Fix over-eager wildcard extension matching bug
Browse files Browse the repository at this point in the history
  • Loading branch information
markashleybell committed Jun 6, 2016
1 parent 6c28a2b commit 9869170
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
40 changes: 39 additions & 1 deletion MAB.DotIgnore.Test/IgnoreRuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void ToString_Returns_Correct_Pattern_Info()
{
var rule = new IgnoreRule("sub2/**.txt");
// Should return original glob pattern > modified glob pattern > translated regex
Assert.IsTrue(rule.ToString() == "sub2/**.txt > SUB2/**.TXT > SUB2/.*\\.TXT");
Assert.IsTrue(rule.ToString() == "sub2/**.txt > SUB2/**.TXT > SUB2/.*\\.TXT$");
}

[Test]
Expand Down Expand Up @@ -82,10 +82,12 @@ public void Match_Relative_No_Wildcards()
var rule = new IgnoreRule("test.txt");
Assert.IsFalse(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test.txt", false));
Assert.IsFalse(rule.IsMatch("/test.txt2", false));
Assert.IsTrue(rule.IsMatch("/sub1/test.txt", false));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test.txt", false));
// Should match directory as well
Assert.IsTrue(rule.IsMatch("/test.txt", true));
Assert.IsFalse(rule.IsMatch("/test.txt2", true));
}

[Test]
Expand All @@ -94,10 +96,12 @@ public void Match_Absolute_No_Wildcards()
var rule = new IgnoreRule("/test.txt");
Assert.IsFalse(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test.txt", false));
Assert.IsFalse(rule.IsMatch("/test.txt2", false));
Assert.IsFalse(rule.IsMatch("/sub1/test.txt", false));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test.txt", false));
// Should match directory as well
Assert.IsTrue(rule.IsMatch("/test.txt", true));
Assert.IsFalse(rule.IsMatch("/test.txt2", true));
}

[Test]
Expand All @@ -106,6 +110,7 @@ public void Match_Directory_Relative_No_Wildcards()
var rule = new IgnoreRule("test/");
Assert.IsFalse(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test", true));
Assert.IsFalse(rule.IsMatch("/test2", true));
Assert.IsTrue(rule.IsMatch("/sub1/test", true));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test", true));
// Should not match file called 'test' with no extension
Expand All @@ -118,6 +123,7 @@ public void Match_Directory_Absolute_No_Wildcards()
var rule = new IgnoreRule("/test/");
Assert.IsFalse(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test", true));
Assert.IsFalse(rule.IsMatch("/test2", true));
Assert.IsFalse(rule.IsMatch("/sub1/test", true));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test", true));
// Should not match file called 'test' with no extension
Expand All @@ -130,10 +136,12 @@ public void Negated_Match_Relative_No_Wildcards()
var rule = new IgnoreRule("!test.txt");
Assert.IsTrue(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test.txt", false));
Assert.IsFalse(rule.IsMatch("/test.txt2", false));
Assert.IsTrue(rule.IsMatch("/sub1/test.txt", false));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test.txt", false));
// Should match directory as well
Assert.IsTrue(rule.IsMatch("/test.txt", true));
Assert.IsFalse(rule.IsMatch("/test.txt2", true));
}

[Test]
Expand All @@ -142,10 +150,12 @@ public void Negated_Match_Absolute_No_Wildcards()
var rule = new IgnoreRule("!/test.txt");
Assert.IsTrue(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test.txt", false));
Assert.IsFalse(rule.IsMatch("/test.txt2", false));
Assert.IsFalse(rule.IsMatch("/sub1/test.txt", false));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test.txt", false));
// Should match directory as well
Assert.IsTrue(rule.IsMatch("/test.txt", true));
Assert.IsFalse(rule.IsMatch("/test.txt2", true));
}

[Test]
Expand All @@ -154,6 +164,7 @@ public void Negated_Match_Directory_Relative_No_Wildcards()
var rule = new IgnoreRule("!test/");
Assert.IsTrue(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test", true));
Assert.IsFalse(rule.IsMatch("/test2", true));
Assert.IsTrue(rule.IsMatch("/sub1/test", true));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test", true));
// Should not match file called 'test' with no extension
Expand All @@ -166,6 +177,7 @@ public void Negated_Match_Directory_Absolute_No_Wildcards()
var rule = new IgnoreRule("!/test/");
Assert.IsTrue(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test", true));
Assert.IsFalse(rule.IsMatch("/test2", true));
Assert.IsFalse(rule.IsMatch("/sub1/test", true));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test", true));
// Should not match file called 'test' with no extension
Expand All @@ -178,10 +190,12 @@ public void Match_Global_Wildcards()
var rule = new IgnoreRule("*.txt");
Assert.IsFalse(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test.txt", false));
Assert.IsFalse(rule.IsMatch("/test.txt2", false));
Assert.IsTrue(rule.IsMatch("/sub1/test.txt", false));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test.txt", false));
// Should match directory as well
Assert.IsTrue(rule.IsMatch("/test.txt", true));
Assert.IsFalse(rule.IsMatch("/test.txt2", true));
}

[Test]
Expand All @@ -190,10 +204,12 @@ public void Negated_Match_Global_Wildcards()
var rule = new IgnoreRule("!*.txt");
Assert.IsTrue(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test.txt", false));
Assert.IsFalse(rule.IsMatch("/test.txt2", false));
Assert.IsTrue(rule.IsMatch("/sub1/test.txt", false));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test.txt", false));
// Should match directory as well
Assert.IsTrue(rule.IsMatch("/test.txt", true));
Assert.IsFalse(rule.IsMatch("/test.txt2", true));
}

[Test]
Expand All @@ -202,10 +218,12 @@ public void Match_Global_Star_Star_Wildcard()
var rule = new IgnoreRule("**.txt");
Assert.IsFalse(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test.txt", false));
Assert.IsFalse(rule.IsMatch("/test.txt2", false));
Assert.IsTrue(rule.IsMatch("/sub1/test.txt", false));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test.txt", false));
// Should match directory as well
Assert.IsTrue(rule.IsMatch("/test.txt", true));
Assert.IsFalse(rule.IsMatch("/test.txt2", true));
}

[Test]
Expand All @@ -214,6 +232,7 @@ public void Match_Leading_Star_Star_Wildcard()
var rule = new IgnoreRule("**/test");
Assert.IsFalse(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test", true));
Assert.IsFalse(rule.IsMatch("/test2", true));
Assert.IsTrue(rule.IsMatch("/sub1/test", true));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test", true));
// Should match directory as well
Expand All @@ -228,8 +247,10 @@ public void Match_Relative_Star_Star_Wildcard()
Assert.IsFalse(rule.IsMatch("/test.txt", false));
Assert.IsFalse(rule.IsMatch("/sub1/test.txt", false));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test.txt", false));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test.txt2", false));
// Should match directory as well
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test.txt", true));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test.txt2", true));
}

[Test]
Expand All @@ -240,9 +261,11 @@ public void Match_Absolute_Star_Star_Wildcard()
Assert.IsFalse(rule.IsMatch("/test.txt", false));
Assert.IsTrue(rule.IsMatch("/sub1/test.txt", false));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test.txt", false));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test.txt2", false));
Assert.IsFalse(rule.IsMatch("/sub0/sub1/test.txt", false));
// Should match directory as well
Assert.IsTrue(rule.IsMatch("/sub1/test.txt", true));
Assert.IsFalse(rule.IsMatch("/sub1/test.txt2", true));
}

[Test]
Expand All @@ -264,8 +287,11 @@ public void Match_Directory_Global_Star_Star_Wildcard()
var rule = new IgnoreRule("**test/");
Assert.IsFalse(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test", true));
Assert.IsFalse(rule.IsMatch("/test2", true));
Assert.IsTrue(rule.IsMatch("/sub1/test", true));
Assert.IsFalse(rule.IsMatch("/sub1/test2", true));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test", true));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test2", true));
// Should not match file
Assert.IsFalse(rule.IsMatch("/test.txt", false));
}
Expand All @@ -278,6 +304,7 @@ public void Match_Directory_Relative_Star_Star_Wildcard()
Assert.IsFalse(rule.IsMatch("/test", true));
Assert.IsFalse(rule.IsMatch("/sub1/test", true));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test", true));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test2", true));
// Should not match file
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test", false));
}
Expand All @@ -289,7 +316,9 @@ public void Match_Directory_Absolute_Star_Star_Wildcard()
Assert.IsFalse(rule.Negation);
Assert.IsFalse(rule.IsMatch("/test.txt", true));
Assert.IsTrue(rule.IsMatch("/sub1/test.txt", true));
Assert.IsFalse(rule.IsMatch("/sub1/test.txt2", true));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test.txt", true));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test.txt2", true));
Assert.IsFalse(rule.IsMatch("/sub0/sub1/test.txt", true));
// Should not match file
Assert.IsFalse(rule.IsMatch("/sub1/test.txt", false));
Expand All @@ -314,10 +343,12 @@ public void Negated_Match_Global_Star_Star_Wildcard()
var rule = new IgnoreRule("!**.txt");
Assert.IsTrue(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test.txt", false));
Assert.IsFalse(rule.IsMatch("/test.txt2", false));
Assert.IsTrue(rule.IsMatch("/sub1/test.txt", false));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test.txt", false));
// Should match directory as well
Assert.IsTrue(rule.IsMatch("/test.txt", true));
Assert.IsFalse(rule.IsMatch("/test.txt2", true));
}

[Test]
Expand All @@ -326,10 +357,12 @@ public void Negated_Match_Leading_Star_Star_Wildcard()
var rule = new IgnoreRule("!**/test");
Assert.IsTrue(rule.Negation);
Assert.IsTrue(rule.IsMatch("/test", true));
Assert.IsFalse(rule.IsMatch("/test2", true));
Assert.IsTrue(rule.IsMatch("/sub1/test", true));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test", true));
// Should match directory as well
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test", false));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test2", false));
}

[Test]
Expand All @@ -340,8 +373,10 @@ public void Negated_Match_Relative_Star_Star_Wildcard()
Assert.IsFalse(rule.IsMatch("/test.txt", false));
Assert.IsFalse(rule.IsMatch("/sub1/test.txt", false));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test.txt", false));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test.txt2", false));
// Should match directory as well
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test.txt", true));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test.txt2", true));
}

[Test]
Expand All @@ -351,10 +386,12 @@ public void Negated_Match_Absolute_Star_Star_Wildcard()
Assert.IsTrue(rule.Negation);
Assert.IsFalse(rule.IsMatch("/test.txt", false));
Assert.IsTrue(rule.IsMatch("/sub1/test.txt", false));
Assert.IsFalse(rule.IsMatch("/sub1/test.txt2", false));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test.txt", false));
Assert.IsFalse(rule.IsMatch("/sub0/sub1/test.txt", false));
// Should match directory as well
Assert.IsTrue(rule.IsMatch("/sub1/test.txt", true));
Assert.IsFalse(rule.IsMatch("/sub1/test.txt2", true));
}

[Test]
Expand All @@ -378,6 +415,7 @@ public void Negated_Match_Directory_Relative_Star_Star_Wildcard()
Assert.IsFalse(rule.IsMatch("/test", true));
Assert.IsFalse(rule.IsMatch("/sub1/test", true));
Assert.IsTrue(rule.IsMatch("/sub1/sub2/test", true));
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test2", true));
// Should not match file
Assert.IsFalse(rule.IsMatch("/sub1/sub2/test", false));
}
Expand Down
5 changes: 4 additions & 1 deletion MAB.DotIgnore/IgnoreRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@ private Regex GlobPatternToRegex(string globPattern, bool singleAsteriskMatchesS
regexPattern = Regex.Replace(regexPattern, @"\.\{\@\}", ".*");
}

regexPattern = Regex.Replace(regexPattern, @"\?", ".");
// Add on the end of line pattern char ($) so that wildcard matches aren't too greedy
// Omitting this means that ignore pattern '*.cs' would match files with a '.cshtml'
// extension as well as '.cs' files, which is definitely not what we want!
regexPattern = Regex.Replace(regexPattern, @"\?", ".") + "$";

return new Regex(regexPattern, MatchFlags.HasFlag(MatchFlags.IGNORE_CASE) ? RegexOptions.IgnoreCase : RegexOptions.None);
}
Expand Down
6 changes: 3 additions & 3 deletions MAB.DotIgnore/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyVersion("1.0.2.0")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]

0 comments on commit 9869170

Please sign in to comment.