public void EarlierRulesTakePrecedence_ExcludeBeforeInclude() { var hierarchy = new TestHierarchy( new SimpleMatchable("root", new SimpleMatchable("dev", new SimpleMatchable("hda"), new SimpleMatchable("hda1"), new SimpleMatchable("hdb"), new SimpleMatchable("hdb1"), new SimpleMatchable("hdb2")), new SimpleMatchable("home", new SimpleMatchable("me", new SimpleMatchable("hdmi")), new SimpleMatchable("you")), new SimpleMatchable("var"))); var parser = new GlobParser(); var enumerator = new MultiGlobMatchEnumerator().Include(parser.Parse("**")); var filter = new MultiGlobMatchFilter(hierarchy.CaseSensitive) .Exclude(parser.Parse("dev/**")) .Include(parser.Parse("**/hd[am]?")); var matches = enumerator.EnumerateMatches(hierarchy) .Where(filter.Filter) .ToArray(); Assert.That(matches.Select(m => m.Item.Name).ToArray(), Is.EquivalentTo(new [] { "hdmi", })); }
public void FindsAllUniqueMatchesOfIncludesNotMatchedByExcludes() { var hierarchy = new TestHierarchy( new SimpleMatchable("root", new SimpleMatchable("dev", new SimpleMatchable("hda"), new SimpleMatchable("hda1"), new SimpleMatchable("hdb"), new SimpleMatchable("hdb1"), new SimpleMatchable("hdb2")), new SimpleMatchable("home", new SimpleMatchable("me", new SimpleMatchable("hdmi")), new SimpleMatchable("you")), new SimpleMatchable("var"))); var parser = new GlobParser(); var matches = new MultiGlobMatchEnumerator() .Exclude(parser.Parse("home/*/**")) .Include(parser.Parse("**/h*")) .EnumerateMatches(hierarchy); Assert.That(matches.Select(m => m.Item.Name).ToArray(), Is.EquivalentTo(new [] { "hda", "hda1", "hdb", "hdb1", "hdb2", "home" })); }
public void ExerciseMultiGlobMatchEnumerator() { TestContext.WriteLine($"CLR Version: {Environment.Version}"); TestContext.WriteLine($"Runtime: {AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName}"); var parser = new GlobParser(); var includes = new [] { parser.Parse("**/Microsoft*/**/*.dll"), parser.Parse("Program*/**/*.exe"), }; var excludes = new [] { parser.Parse("**/SQL*/**") }; var hierarchy = new FileSystemHierarchy(new DirectoryInfo("C:\\"), false); var enumerator = new MultiGlobMatchEnumerator() .Exclude(excludes) .Include(includes); var count = enumerator.EnumerateMatches(hierarchy).Count(); TestContext.WriteLine($"{count} matches"); }
public void MatcherWithOnlyRecursiveExclusionRulesMatchingWithinASubtree_DoesNotMatchWithinThatSubtree() { var hierarchy = new TestHierarchy( new SimpleMatchable("root", new SimpleMatchable("dev", new SimpleMatchable("hda"), new SimpleMatchable("hda1"), new SimpleMatchable("hdb"), new SimpleMatchable("hdb1"), new SimpleMatchable("hdb2")), new SimpleMatchable("home", new SimpleMatchable("me", new SimpleMatchable("hdmi")), new SimpleMatchable("you")), new SimpleMatchable("var"))); var parser = new GlobParser(); var matches = new MultiGlobMatchEnumerator() .Exclude(parser.Parse("d*/**")) .Include(parser.Parse("d*")) .EnumerateMatches(hierarchy).ToArray(); Assert.That(matches.Select(m => m.Item.Name).ToArray(), Is.EquivalentTo(new [] { "dev", })); }
public void MatcherWithOnlyExclusionRulesMatchingInitially_MatchesNothing() { var hierarchy = new TestHierarchy( new SimpleMatchable("root", new SimpleMatchable("dev", new SimpleMatchable("hda"), new SimpleMatchable("hda1"), new SimpleMatchable("hdb"), new SimpleMatchable("hdb1"), new SimpleMatchable("hdb2")), new SimpleMatchable("home", new SimpleMatchable("me", new SimpleMatchable("hdmi")), new SimpleMatchable("you")), new SimpleMatchable("var"))); var parser = new GlobParser(); var matches = new MultiGlobMatchEnumerator() .Exclude(parser.Parse("dev/**")) .Exclude(parser.Parse("**/hd[am]?")) .Include(parser.Parse("nothing/**")) .EnumerateMatches(hierarchy).ToArray(); Assert.That(matches.Select(m => m.Item.Name).ToArray(), Is.Empty); }
public void NotEqual(string a, string b) { var parser = new GlobParser(); var aRoot = parser.Parse(a).Root; var bRoot = parser.Parse(b).Root; Assume.That(aRoot, Is.Not.Null); Assume.That(bRoot, Is.Not.Null); Assert.That(aRoot, Is.Not.EqualTo(bRoot).Using(comparer)); }
public void ContainerExclusionDoesNotApplyRecursively() { var hierarchy = new TestHierarchy( new SimpleMatchable("root", new SimpleMatchable("dev", new SimpleMatchable("hda"), new SimpleMatchable("hda1"), new SimpleMatchable("hdb"), new SimpleMatchable("hdb1"), new SimpleMatchable("hdb2")), new SimpleMatchable("home", new SimpleMatchable("me", new SimpleMatchable("hdmi")), new SimpleMatchable("you")), new SimpleMatchable("var"))); var parser = new GlobParser(); var includes = new [] { parser.Parse("**/h*") }; var excludes = new [] { parser.Parse("home") }; var enumerator = new MultiGlobMatchEnumerator().Include(parser.Parse("**")); var filter = new MultiGlobMatchFilter(hierarchy.CaseSensitive) .Exclude(excludes) .Include(includes); var matches = enumerator.EnumerateMatches(hierarchy) .Where(filter.Filter) .ToArray(); Assert.That(matches.Select(m => m.Item.Name).ToArray(), Is.EquivalentTo(new [] { "hda", "hda1", "hdb", "hdb1", "hdb2", "hdmi" })); }
public void YieldsDetailsForEveryMatchingIncludeGlob() { var hierarchy = new TestHierarchy( new SimpleMatchable("root", new SimpleMatchable("dev", new SimpleMatchable("hda"), new SimpleMatchable("hda1"), new SimpleMatchable("hdb"), new SimpleMatchable("hdb1"), new SimpleMatchable("hdb2")), new SimpleMatchable("home", new SimpleMatchable("me", new SimpleMatchable("hdmi")), new SimpleMatchable("you")), new SimpleMatchable("var"))); var parser = new GlobParser(); var includes = new [] { parser.Parse("**/hd[am]?"), parser.Parse("home/**") }; var matches = new MultiGlobMatchEnumerator() .Include(includes) .EnumerateMatches(hierarchy).ToArray(); Assume.That(matches.Select(m => m.Item.Name).ToArray(), Is.EquivalentTo(new [] { "hda1", "me", "hdmi", "you" })); var hdmiMatch = matches.Single(m => m.Item.Name == "hdmi"); Assert.That(hdmiMatch.Details.Select(s => s.Glob).ToArray(), Is.EquivalentTo(includes)); }
public void RejectsUndefinedVariableCaptures() { var parser = new GlobParser { Variables = { new GlobVariable("MM", @"\d{2}"), new GlobVariable("dd", @"\d{2}") } }; Assert.That(() => parser.Parse("./logs-{missing}/{MM}{dd}/*.log"), Throws.InstanceOf <GlobFormatException>()); }
/// <summary> /// Returns <see cref="Path" /> instances matching the specified pattern. /// </summary> /// <param name="pattern">The pattern to match.</param> /// <param name="predicate">The predicate used to filter directories based on file system information.</param> /// <returns> /// <see cref="Path" /> instances matching the specified pattern. /// </returns> public IEnumerable <Path> Match(string pattern, Func <IDirectory, bool> predicate) { if (pattern == null) { throw new ArgumentNullException("pattern"); } if (string.IsNullOrWhiteSpace(pattern)) { return(Enumerable.Empty <Path>()); } // Parse the pattern into an AST. var root = _parser.Parse(pattern, _environment.IsUnix()); // Visit all nodes in the parsed patterns and filter the result. return(_visitor.Walk(root, predicate) .Select(x => x.Path) .Distinct(_comparer)); }
/// <inheritdoc/> public IEnumerable <Path> Match(GlobPattern pattern, GlobberSettings settings) { if (pattern == null) { throw new ArgumentNullException(nameof(pattern)); } if (string.IsNullOrWhiteSpace(pattern?.Pattern)) { return(Enumerable.Empty <Path>()); } // Parse the pattern into an AST. var root = _parser.Parse(pattern, settings); // Visit all nodes in the parsed patterns and filter the result. return(_visitor.Walk(root, settings) .Select(x => x.Path) .Distinct(_comparer)); }
public void WildcardSegmentMatchYieldsVariables() { var parser = new GlobParser { Variables = { new GlobVariable("yyyy", @"\d{4}") } }; var glob = parser.Parse("directory{yyyy}"); var start = new GlobMatchFactory(true).Start(glob); var child = ApplyToHierarchy(start, "directory2020"); Assert.That(child.IsMatch, Is.True); Assert.That(child.CanContinue, Is.False); Assert.That(child.GetPathSegments().ToArray(), Is.EqualTo(new [] { "directory2020" })); Assert.That(child.GetVariables().ToArray(), Is.EqualTo(new [] { new MatchedVariable("yyyy", "2020") })); }
/// <inheritdoc/> public IEnumerable <Path> Match(string pattern, GlobberSettings settings) { if (pattern == null) { throw new ArgumentNullException(nameof(pattern)); } if (string.IsNullOrWhiteSpace(pattern)) { return(Enumerable.Empty <Path>()); } // Make sure we got some settings. settings ??= new GlobberSettings(); // Parse the pattern into an AST. var root = _parser.Parse(pattern, settings.Comparer ?? PathComparer.Default); // Visit all nodes in the parsed patterns and filter the result. return(_visitor.Walk(root, settings) .Select(x => x.Path) .Distinct(settings.Comparer ?? PathComparer.Default)); }
public void IncludesValidVariableCaptures() { var parser = new GlobParser { Variables = { new GlobVariable("yyyy", @"\d{4}"), new GlobVariable("MM", @"\d{2}"), new GlobVariable("dd", @"\d{2}") } }; var glob = parser.Parse("./logs-{yyyy}/{MM}{dd}/*.log"); var expected = new Glob(null, new WildcardSegment("logs-{yyyy}", @"^logs-(?<yyyy>(\d{4}))$", "logs-"), new WildcardSegment("{MM}{dd}", @"^(?<MM>(\d{2}))(?<dd>(\d{2}))$", ""), new WildcardSegment("*.log", @"^.*\.log$", "")); Assert.That(glob.Root, Is.EqualTo(expected.Root).Using(rootSegmentComparer)); Assert.That(glob.Segments, Is.EqualTo(expected.Segments).Using(segmentComparer)); Assert.That(glob, Is.EqualTo(expected).Using(globComparer)); }