Skip to content
Merged
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
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="MinVer" Version="6.0.0" />
<PackageVersion Include="NUnit" Version="4.3.2" />
<PackageVersion Include="NUnit3TestAdapter" Version="5.0.0" />
<PackageVersion Include="NUnit" Version="4.4.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="5.1.0" />
<PackageVersion Include="Ramstack.Globbing" Version="2.3.2" />
</ItemGroup>
</Project>
</Project>
51 changes: 26 additions & 25 deletions src/Ramstack.FileProviders.Globbing/GlobbingFileProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Ramstack.Globbing;
using Ramstack.FileProviders.Internal;

namespace Ramstack.FileProviders;

Expand Down Expand Up @@ -60,18 +60,17 @@ public GlobbingFileProvider(IFileProvider provider, string[] patterns, string[]?
public IFileInfo GetFileInfo(string subpath)
{
subpath = FilePath.Normalize(subpath);
if (!IsExcluded(subpath) && IsIncluded(subpath))
return _provider.GetFileInfo(subpath);

return new NotFoundFileInfo(subpath);
return IsFileIncluded(subpath)
? _provider.GetFileInfo(subpath)
: new NotFoundFileInfo(subpath);
}

/// <inheritdoc />
public IDirectoryContents GetDirectoryContents(string subpath)
{
subpath = FilePath.Normalize(subpath);

if (!IsExcluded(subpath))
if (IsDirectoryIncluded(subpath))
{
var directory = _provider.GetDirectoryContents(subpath);
if (directory is not NotFoundDirectoryContents)
Expand All @@ -85,23 +84,26 @@ public IDirectoryContents GetDirectoryContents(string subpath)
public IChangeToken Watch(string filter) =>
_provider.Watch(filter);

private bool IsIncluded(string path)
{
foreach (var pattern in _patterns)
if (Matcher.IsMatch(path, pattern, MatchFlags.Unix))
return true;

return false;
}

private bool IsExcluded(string path)
{
foreach (var pattern in _excludes)
if (Matcher.IsMatch(path, pattern, MatchFlags.Unix))
return true;
/// <summary>
/// Determines if a file is included based on the specified patterns and exclusions.
/// </summary>
/// <param name="path">The path of the file.</param>
/// <returns>
/// <see langword="true" /> if the file is included;
/// otherwise, <see langword="false" />.
/// </returns>
private bool IsFileIncluded(string path) =>
!PathHelper.IsMatch(path, _excludes) && PathHelper.IsMatch(path, _patterns);

return false;
}
/// <summary>
/// Determines if a directory is included based on the specified exclusions.
/// </summary>
/// <param name="path">The path of the directory.</param>
/// <returns>
/// <see langword="true" /> if the directory is included; otherwise, <see langword="false" />.
/// </returns>
private bool IsDirectoryIncluded(string path) =>
path == "/" || !PathHelper.IsMatch(path, _excludes) && PathHelper.IsPartialMatch(path, _patterns);

#region Inner type: GlobbingDirectoryContents

Expand Down Expand Up @@ -136,9 +138,8 @@ public IEnumerator<IFileInfo> GetEnumerator()
foreach (var file in _directory)
{
var path = FilePath.Join(_directoryPath, file.Name);
if (!_provider.IsExcluded(path))
if (file.IsDirectory || _provider.IsIncluded(path))
yield return file;
if (file.IsDirectory ? _provider.IsDirectoryIncluded(path) : _provider.IsFileIncluded(path))
yield return file;
}
}

Expand Down
Loading
Loading