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 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 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 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 FindsAllMatches() { 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 glob = new GlobParser().Parse("**/hd??"); var matches = new GlobMatchEnumerator(glob).EnumerateMatches(hierarchy); Assert.That(matches.Select(m => m.Item.Name).ToArray(), Is.EquivalentTo(new [] { "hda1", "hdb1", "hdb2", "hdmi" })); }
public void AcceptsAnything(string value, bool caseSensitive) { var segment = new GlobParser().ParseSingleSegment <WildcardMultiSegment>("**"); var match = segment.Match(value, caseSensitive); Assert.That(match.Success, Is.True); }
public void ParsesValidPatterns(Case testCase) { var glob = new GlobParser().Parse(testCase.Pattern !); Assert.That(glob.Root, Is.EqualTo(testCase.Root).Using(rootSegmentComparer)); Assert.That(glob.Segments, Is.EqualTo(testCase.Segments).Using(segmentComparer)); Assert.That(glob, Is.EqualTo(new Glob(testCase.Root, testCase.Segments.ToArray())).Using(globComparer)); }
public void AcceptsMatchingCaseSensitivePathSegment() { var segment = new GlobParser().ParseSingleSegment <WildcardSegment>("dir*ory"); var match = segment.Match("directory", true); Assert.That(match.Success, Is.True); }
public void AcceptsMatchingCaseInsensitivePathSegment() { var segment = new GlobParser().ParseSingleSegment <FixedSegment>("directory"); var match = segment.Match("DIRectoRy", false); Assert.That(match.Success, Is.True); }
public void RejectsNonMatchingCaseInsensitivePathSegment() { var segment = new GlobParser().ParseSingleSegment <WildcardSegment>("dir*ory"); var match = segment.Match("something", false); Assert.That(match.Success, Is.False); }
public void RejectsNonMatchingCaseSensitivePathSegment() { var segment = new GlobParser().ParseSingleSegment <FixedSegment>("directory"); var match = segment.Match("DIRectoRy", true); Assert.That(match.Success, Is.False); }
public void WildcardSegmentYieldsPrefix() { var glob = new GlobParser().Parse("dir*ory"); var start = new GlobMatchFactory(true).Start(glob); var prefix = start.GetPrefixFilter(); Assert.That(prefix, Is.EqualTo("dir")); }
public void WildcardMultiSegmentDoesNotYieldPrefix() { var glob = new GlobParser().Parse("**"); var start = new GlobMatchFactory(true).Start(glob); var prefix = start.GetPrefixFilter(); Assert.That(prefix, Is.EqualTo("")); }
public void WildcardSegmentMatchesOnlyOnce() { var glob = new GlobParser().Parse("dir*ory"); var start = new GlobMatchFactory(true).Start(glob); var child = ApplyToHierarchy(start, "directory", "directory"); Assert.That(child.IsMatch, Is.False); Assert.That(child.CanContinue, Is.False); }
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 FixedSegmentMatches() { var glob = new GlobParser().Parse("directory"); var start = new GlobMatchFactory(true).Start(glob); var child = ApplyToHierarchy(start, "directory"); Assert.That(child.IsMatch, Is.True); Assert.That(child.CanContinue, Is.False); Assert.That(child.GetPathSegments().ToArray(), Is.EqualTo(new [] { "directory" })); }
public void WildcardMultiSegmentCanMatchNoSegments() { var glob = new GlobParser().Parse("a/**/b"); var start = new GlobMatchFactory(true).Start(glob); var child = ApplyToHierarchy(start, "a", "b"); Assert.That(child.IsMatch, Is.True); Assert.That(child.CanContinue, Is.True); Assert.That(child.GetPathSegments().ToArray(), Is.EqualTo(new [] { "a", "b" })); }
/// <summary> /// Initializes a new instance of the <see cref="Globber"/> class. /// </summary> /// <param name="env">The environment.</param> /// <exception cref="ArgumentNullException"><paramref name="env"/> is <see langword="null"/></exception> public Globber(IFileSystemEnvironment env) { if (env == null) { throw new ArgumentNullException(nameof(env)); } _env = env; _parser = new GlobParser(env); _visitor = new GlobVisitor(env); _comparer = new PathComparer(env.IsUnix()); }
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> /// Initializes a new instance of the <see cref="Globber"/> class. /// </summary> /// <param name="fileSystem">The file system.</param> /// <param name="environment">The environment.</param> public Globber(IFileSystem fileSystem, IEnvironment environment) { if (fileSystem == null) { throw new ArgumentNullException(nameof(fileSystem)); } if (environment is null) { throw new ArgumentNullException(nameof(environment)); } _parser = new GlobParser(environment); _visitor = new GlobVisitor(fileSystem, environment); }
public void CoalescesAdjacentWildcardMultiSegments() { var glob = new GlobParser().Parse("a/**/**/**/b/**/c"); var expected = new Glob(null, new FixedSegment("a"), new WildcardMultiSegment(), new FixedSegment("b"), new WildcardMultiSegment(), new FixedSegment("c")); 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)); }
/// <summary> /// Initializes a new instance of the <see cref="Globber"/> class. /// </summary> /// <param name="fileSystem">The file system.</param> /// <param name="environment">The environment.</param> public Globber(IFileSystem fileSystem, ICakeEnvironment environment) { if (fileSystem == null) { throw new ArgumentNullException("fileSystem"); } if (environment == null) { throw new ArgumentNullException("environment"); } _environment = environment; _parser = new GlobParser(environment); _visitor = new GlobVisitor(fileSystem, environment); _comparer = new PathComparer(environment.IsUnix()); }
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 WildcardMultiSegmentCanMatchMultipleDepths() { var glob = new GlobParser().Parse("a/**/b/**/c"); var start = new GlobMatchFactory(true).Start(glob); Assert.That(ApplyToHierarchy(start, "a", "b", "c").IsMatch, Is.True); Assert.That(ApplyToHierarchy(start, "a", "b", "c").GetPathSegments().ToArray(), Is.EqualTo(new [] { "a", "b", "c" })); Assert.That(ApplyToHierarchy(start, "a", "b", "c", "d").IsMatch, Is.False); Assert.That(ApplyToHierarchy(start, "a", "b", "c", "d", "c").IsMatch, Is.True); Assert.That(ApplyToHierarchy(start, "a", "b", "c", "d", "c").GetPathSegments().ToArray(), Is.EqualTo(new [] { "a", "b", "c", "d", "c" })); Assert.That(ApplyToHierarchy(start, "a", "c").IsMatch, Is.False); Assert.That(ApplyToHierarchy(start, "a", "d", "b", "c").IsMatch, Is.True); Assert.That(ApplyToHierarchy(start, "a", "d", "b", "c").GetPathSegments().ToArray(), Is.EqualTo(new [] { "a", "d", "b", "c" })); }
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") })); }
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 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)); }
public void ReturnsMatchedNamedGroupValues() { var parser = new GlobParser { Variables = { new GlobVariable("yyyy", @"\d{4}"), new GlobVariable("MM", @"\d{2}"), new GlobVariable("dd", @"\d{2}"), } }; var segment = parser.ParseSingleSegment <WildcardSegment>("{yyyy}-{MM}-{dd}"); var match = segment.Match("2020-01-10", false); Assert.That(match.Success, Is.True); Assert.That(match.Variables, Is.EqualTo(new [] { new MatchedVariable("yyyy", "2020"), new MatchedVariable("MM", "01"), new MatchedVariable("dd", "10"), })); }