Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace System.IO.Abstractions.TestingHelpers;
Expand All @@ -15,6 +16,9 @@ namespace System.IO.Abstractions.TestingHelpers;
#endif
public class MockDirectory : DirectoryBase
{
private static readonly bool IsNetFramework = RuntimeInformation.FrameworkDescription
.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase);

private readonly IMockFileDataAccessor mockFileDataAccessor;
private string currentDirectory;

Expand Down Expand Up @@ -340,10 +344,12 @@ private string[] GetFilesInternal(
.Replace(@"\*", isUnix ? @"[^<>:""/|?*]*?" : @"[^<>:""/\\|?*]*?")
.Replace(@"\?", isUnix ? @"[^<>:""/|?*]?" : @"[^<>:""/\\|?*]?");

// Only .NET Framework also matches a three-character extension against longer ones
// ("*.xls" finds "book.xlsx"); .NET Core and .NET 5+ match the exact extension.
var extension = Path.GetExtension(searchPattern);
bool hasExtensionLengthOfThree = extension != null && extension.Length == 4 &&
!extension.Contains("*") && !extension.Contains("?");
if (hasExtensionLengthOfThree)
if (IsNetFramework && hasExtensionLengthOfThree)
{
var fileNamePatternSpecial =
string.Format(CultureInfo.InvariantCulture, "{0}[^.]", fileNamePattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public async Task MockDirectory_GetFiles_ShouldFilterByExtensionBasedSearchPatte
await That(result).IsEqualTo(expected).InAnyOrder();
}

#if NETFRAMEWORK
[Test]
public async Task MockDirectory_GetFiles_ShouldFilterByExtensionBasedSearchPatternWithThreeCharacterLongFileExtension_RespectingAllDirectorySearchOption()
{
Expand Down Expand Up @@ -208,6 +209,81 @@ public async Task MockDirectory_GetFiles_ShouldFilterByExtensionBasedSearchPatte
// Assert
await That(result).IsEqualTo(expected).InAnyOrder();
}
#else
[Test]
public async Task MockDirectory_GetFiles_ShouldNotMatchLongerFileExtensionsWithThreeCharacterLongFileExtension_RespectingAllDirectorySearchOption()
{
// Arrange
var fileSystem = SetupFileSystem();
fileSystem.AddFile(XFS.Path(@"c:\a\a\c.gifx"), new MockFileData(string.Empty));
fileSystem.AddFile(XFS.Path(@"c:\a\a\c.gifx.xyz"), new MockFileData(string.Empty));
fileSystem.AddFile(XFS.Path(@"c:\a\a\c.gifz\xyz"), new MockFileData(string.Empty));
var expected = new[]
{
XFS.Path(@"c:\a.gif"),
XFS.Path(@"c:\a\b.gif"),
XFS.Path(@"c:\a\a\c.gif")
};

// Act
var result = fileSystem.Directory.GetFiles(XFS.Path(@"c:\"), "*.gif", SearchOption.AllDirectories);

// Assert
await That(result).IsEqualTo(expected).InAnyOrder();
}

[Test]
public async Task MockDirectory_GetFiles_ShouldNotMatchLongerFileExtensionsWithThreeCharacterLongFileExtension_RespectingTopDirectorySearchOption()
{
// Arrange
var fileSystem = SetupFileSystem();
fileSystem.AddFile(XFS.Path(@"c:\a\c.gifx"), new MockFileData(string.Empty));
fileSystem.AddFile(XFS.Path(@"c:\a\a\c.gifx.xyz"), new MockFileData(string.Empty));
fileSystem.AddFile(XFS.Path(@"c:\a\a\c.gifx"), new MockFileData(string.Empty));
var expected = new[]
{
XFS.Path(@"c:\a\b.gif")
};

// Act
var result = fileSystem.Directory.GetFiles(XFS.Path(@"c:\a"), "*.gif", SearchOption.TopDirectoryOnly);

// Assert
await That(result).IsEqualTo(expected).InAnyOrder();
}

[TestCase("*.txt", "a.txt", "b.txt2")]
[TestCase("*.xls", "book.xls", "book.xlsx")]
public async Task MockDirectory_EnumerateFiles_ShouldNotMatchLongerFileExtensionsWithThreeCharacterLongFileExtension(
string searchPattern, string matchingFileName, string fileNameWithLongerExtension)
{
// Arrange
var fileSystem = new MockFileSystem();
fileSystem.AddFile(XFS.Path(@"c:\a\" + matchingFileName), new MockFileData(string.Empty));
fileSystem.AddFile(XFS.Path(@"c:\a\" + fileNameWithLongerExtension), new MockFileData(string.Empty));

// Act
var result = fileSystem.Directory.EnumerateFiles(XFS.Path(@"c:\a"), searchPattern, SearchOption.AllDirectories);

// Assert
await That(result).IsEqualTo(new[] { XFS.Path(@"c:\a\" + matchingFileName) });
}

[Test]
public async Task MockDirectory_GetDirectories_ShouldNotMatchLongerExtensionsWithThreeCharacterLongExtension()
{
// Arrange
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(XFS.Path(@"c:\a\b.gif"));
fileSystem.AddDirectory(XFS.Path(@"c:\a\b.gifx"));

// Act
var result = fileSystem.Directory.GetDirectories(XFS.Path(@"c:\a"), "*.gif");

// Assert
await That(result).IsEqualTo(new[] { XFS.Path(@"c:\a\b.gif") });
}
#endif

[Test]
public async Task MockDirectory_GetFiles_ShouldFilterByExtensionBasedSearchPatternOnlyIfTheFileExtensionIsThreeCharacterLong()
Expand Down
Loading