Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enforce GlobMatcherOptions.AllowDotMatch option for file's excludePatterns #9634

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Docfx.Glob/FileGlob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static IEnumerable<string> GetFiles(string cwd, IEnumerable<string> patte
var globArray = patterns.Select(s => new GlobMatcher(s, options)).ToArray();
var excludeGlobArray = excludePatterns == null ?
Array.Empty<GlobMatcher>() :
excludePatterns.Select(s => new GlobMatcher(s, options)).ToArray();
excludePatterns.Select(s => new GlobMatcher(s, options | GlobMatcherOptions.AllowDotMatch)).ToArray();
return GetFilesCore(cwd, globArray, excludeGlobArray);
}

Expand Down
76 changes: 76 additions & 0 deletions test/Docfx.Glob.Tests/GlobFileTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Docfx.Tests.Common;
using FluentAssertions;
using FluentAssertions.Execution;
using Xunit;

namespace Docfx.Glob.Tests;
Expand Down Expand Up @@ -81,6 +83,80 @@ public void TestGlobGetFilesShouldAbleToGetFiles()
Assert.Equal(2, result.Length);
}

[Fact]
public void TestGlobGetFilesWithDotStartedDirectoryFiles()
{
// - .Hidden/
// |- A.cs
// |- B.cs
// |- .Nested/
// | |- C.cs
// |- D/
// | |- E.cs
// - .NotHidden/
// |- F.cs
// |- G.cs
// |- .HiddenFile.cs
var files = new string[]
{
".Hidden/A.cs",
".Hidden/B.cs",
".Hidden/.Nested/C.cs",
".Hidden/D/E.cs",
"NotHidden/F.cs",
"NotHidden/G.cs",
"NotHidden/.HiddenFile.cs",
};
CreateFilesOrFolders(_workingDirectory, files);

var totalFileCount = files.Length;

using (var scope = new AssertionScope())
{
// Test wildcard pattern with AllowDotMatch option.
var result = FileGlob.GetFiles(
_workingDirectory,
patterns: new[] { "**.cs" },
excludePatterns: null,
options: GlobMatcher.DefaultOptions | GlobMatcherOptions.AllowDotMatch
).ToArray();
result.Should().HaveCount(totalFileCount); // All files are included when using AllowDotMatch option.

// Test wildcard pattern with default options.
result = FileGlob.GetFiles(
_workingDirectory,
patterns: new[] { "**.cs" },
excludePatterns: null
).ToArray();
result.Should().HaveCount(2); // Dot started directories/files are excluded when using DefaultOptions.

// Test explicitly specified wildcard pattern with default options.
result = FileGlob.GetFiles(
_workingDirectory,
patterns: new[] { "**.cs", ".Hidden/**.cs" },
excludePatterns: null
).ToArray();
result.Should().HaveCount(5); // Explicitly specified `.Hidden/**.cs` files are included. But `.Hidden/.Nested/**` files are excluded.

// Test `excludePatterns` with default option.
result = FileGlob.GetFiles(
_workingDirectory,
patterns: new[] { "**.cs", ".Hidden/**.cs" },
excludePatterns: new[] { ".Hidden/D/E.cs" }
).ToArray();
result.Should().HaveCount(4); // `.Hidden/D/E.cs` file is excluded.

// Test `excludePatterns` with AllowDotMatch option.
result = FileGlob.GetFiles(
_workingDirectory,
patterns: new string[] { "**.cs", },
excludePatterns: new string[] { "**/.Nested/C.cs" },
options: GlobMatcher.DefaultOptions | GlobMatcherOptions.AllowDotMatch
).ToArray();
result.Should().HaveCount(totalFileCount - 1); // `.Hidden/.Nested/C.cs` file is excluded.
}
}

private static void CreateFilesOrFolders(string cwd, params string[] items)
{
if (string.IsNullOrEmpty(cwd)) cwd = ".";
Expand Down