Skip to content

Commit

Permalink
Update PathTraverser only emit files matching a pattern in multiple w…
Browse files Browse the repository at this point in the history
…ays once

Closes #52
  • Loading branch information
kthompson committed Jul 3, 2020
1 parent fd33173 commit 371e62e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [1.1.7]
### Fixed
- Issue #52: Files matching a pattern in multiple ways are only emitted once

## [1.1.6]
### Fixed
Expand Down Expand Up @@ -79,6 +82,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fix issue where Glob.Directories did not always match properly

[Unreleased]: https://github.com/kthompson/glob/compare/1.1.6...HEAD
[1.1.7]: https://github.com/kthompson/glob/compare/1.1.6...1.1.7
[1.1.6]: https://github.com/kthompson/glob/compare/1.1.5...1.1.6
[1.1.5]: https://github.com/kthompson/glob/compare/1.1.4...1.1.5
[1.1.4]: https://github.com/kthompson/glob/compare/1.1.3...1.1.4
Expand Down
5 changes: 2 additions & 3 deletions src/Glob/Glob.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
<PackageId>Glob</PackageId>
<PackageTags>C#;glob;minimatch</PackageTags>
<Description>A C# Glob library for .NET and .NET Core.</Description>
<PackageReleaseNotes>Fixed
- Issue #57: Support parentheses, equals, and other miscellaneous characters in file names</PackageReleaseNotes>
<PackageReleaseNotes>Files matching a pattern in multiple ways are only emitted once</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/kthompson/glob/</PackageProjectUrl>
<PackageLicenseUrl>https://raw.githubusercontent.com/kthompson/glob/master/LICENSE</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
Expand All @@ -26,7 +25,7 @@
<GenerateAssemblyFileVersionAttribute>true</GenerateAssemblyFileVersionAttribute>
<RootNamespace>GlobExpressions</RootNamespace>
<LangVersion>latest</LangVersion>
<Version>1.1.6</Version>
<Version>1.1.7</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netstandard1.3|AnyCPU'">
Expand Down
10 changes: 8 additions & 2 deletions src/Glob/PathTraverser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private static IEnumerable<FileSystemInfo> Traverse(List<DirectoryInfo> roots, S
var segmentIndex = 0;
var emitDirectories = options.EmitDirectories;
var emitFiles = options.EmitFiles;
var cache = new HashSet<string>();

void Swap(ref List<DirectoryInfo> other)
{
Expand Down Expand Up @@ -77,8 +78,9 @@ where directorySegment.MatchesSegment(directory.Name, options.CaseSensitive)
var noMoreSegments = segmentIndex == segmentsLength;
if (emitDirectories && noMoreSegments)
{
foreach (var info in roots)
foreach (var info in roots.Where(info => !cache.Contains(info.FullName)))
{
cache.Add(info.FullName);
yield return info;
}
}
Expand Down Expand Up @@ -108,7 +110,11 @@ from file in FilesMatchingSegment(children, segment, options.CaseSensitive)

foreach (var info in allFiles)
{
yield return info;
if (!cache.Contains(info.FullName))
{
cache.Add(info.FullName);
yield return info;
}
}
}
rootCache.Clear();
Expand Down
24 changes: 24 additions & 0 deletions test/Glob.Tests/GlobTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,30 @@ public void StarStarFiles()
}
}

[Fact]
public void StarStarFilesIssue52()
{
Action<string> AssertEqual(string expected) => actual => Assert.Equal(expected, actual);

var testRoot = Path.Combine(Path.GetTempPath(), "Glob", "PathTraverserTests", "StarStarFilesIssue52");
try
{
CreateFiles(testRoot, "a/a/a/a/b.txt");

// Verify files exist before
Assert.True(File.Exists(Path.Combine(testRoot, "a/a/a/a/b.txt")));

Assert.Collection(Glob.Files(testRoot, "**/a/**/b.txt").OrderBy(x => x),
AssertEqual(Path.Combine("a", "a", "a", "a", "b.txt"))
);
}
finally
{
// Cleanup test
Directory.Delete(testRoot, true);
}
}

private void CreateFiles(string testRoot, string files)
{
Directory.CreateDirectory(testRoot);
Expand Down
4 changes: 4 additions & 0 deletions test/Glob.Tests/PathTraverserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ public void TestGlobExpressions(string pattern, string positiveMatch, string neg
[Theory]
// Double wildcard tests
[InlineData("**/a", @"ab/a/a.cs a/taco.cs b/taco.cs b/ab/a/hat.taco", @"ab\a a b\ab\a")]

// Issue 52
[InlineData("**/a/**/b", @"a/a/a/b", @"a\a\a\b")]
[InlineData("**/a/**/b", @"a/a/a/a/b", @"a\a\a\a\b")]
public void TestGlobExpressionsWithEmitDirectories(string pattern, string files, string matches)
{
var parser = new Parser(pattern);
Expand Down

0 comments on commit 371e62e

Please sign in to comment.