diff options
author | Jeremy Kuhne <jeremy.kuhne@microsoft.com> | 2018-02-14 08:49:57 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-14 08:49:57 +0300 |
commit | 1c9cd8118abfa13165d88a4e660fbdcc6c2ebc4c (patch) | |
tree | 04a07b15d5ac1dd854271fc66c6fa0befce43c56 /src/System.IO.FileSystem/tests | |
parent | e6b5f11ae54b7605872bbcc5bd79da5a0610f072 (diff) |
API tweaks to match latest updates to spec (#27102)
* API tweaks to match latest updates to spec
Add a few new tests
See #25873
* Properly clear state when enumerating on Unix.
Make sure we don't include special directories in subdir processing. Add test.
Collapse helper that was only called in one place, and remove dead one.
Diffstat (limited to 'src/System.IO.FileSystem/tests')
5 files changed, 129 insertions, 5 deletions
diff --git a/src/System.IO.FileSystem/tests/Directory/GetFileSystemEntries_str.cs b/src/System.IO.FileSystem/tests/Directory/GetFileSystemEntries_str.cs index 8a4175705a..84b018607e 100644 --- a/src/System.IO.FileSystem/tests/Directory/GetFileSystemEntries_str.cs +++ b/src/System.IO.FileSystem/tests/Directory/GetFileSystemEntries_str.cs @@ -175,6 +175,31 @@ namespace System.IO.Tests } } + [Fact] + public void HiddenFilesAreReturned() + { + // Note that APIs that take EnumerationOptions do NOT find hidden files by default + + DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath()); + FileInfo fileOne = new FileInfo(Path.Combine(testDirectory.FullName, GetTestFileName())); + + // Put a period in front to make it hidden on Unix + FileInfo fileTwo = new FileInfo(Path.Combine(testDirectory.FullName, "." + GetTestFileName())); + fileOne.Create().Dispose(); + fileTwo.Create().Dispose(); + if (PlatformDetection.IsWindows) + fileTwo.Attributes = fileTwo.Attributes | FileAttributes.Hidden; + + if (TestFiles) + { + FSAssert.EqualWhenOrdered(new string[] { fileOne.FullName, fileTwo.FullName }, GetEntries(testDirectory.FullName)); + } + else + { + Assert.Empty(GetEntries(testDirectory.FullName)); + } + } + #endregion #region PlatformSpecific diff --git a/src/System.IO.FileSystem/tests/Enumeration/IncludePredicateTests.netcoreapp.cs b/src/System.IO.FileSystem/tests/Enumeration/IncludePredicateTests.netcoreapp.cs new file mode 100644 index 0000000000..0a6737e064 --- /dev/null +++ b/src/System.IO.FileSystem/tests/Enumeration/IncludePredicateTests.netcoreapp.cs @@ -0,0 +1,55 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.IO.Enumeration; +using System.Linq; +using Xunit; + +namespace System.IO.Tests.Enumeration +{ + public abstract class IncludePredicateTests : FileSystemTest + { + public static IEnumerable<string> GetFileFullPathsWithExtension(string directory, + bool recursive, params string[] extensions) + { + return new FileSystemEnumerable<string>( + directory, + (ref FileSystemEntry entry) => entry.ToFullPath(), + new EnumerationOptions() { RecurseSubdirectories = recursive }) + { + ShouldIncludePredicate = (ref FileSystemEntry entry) => + { + if (entry.IsDirectory) return false; + foreach (string extension in extensions) + { + if (Path.GetExtension(entry.FileName).EndsWith(extension)) + return true; + } + return false; + } + }; + } + + [Fact] + public void CustomExtensionMatch() + { + DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath()); + DirectoryInfo testSubdirectory = Directory.CreateDirectory(Path.Combine(testDirectory.FullName, "Subdirectory")); + FileInfo fileOne = new FileInfo(Path.Combine(testDirectory.FullName, "fileone.htm")); + FileInfo fileTwo = new FileInfo(Path.Combine(testDirectory.FullName, "filetwo.html")); + FileInfo fileThree = new FileInfo(Path.Combine(testSubdirectory.FullName, "filethree.doc")); + FileInfo fileFour = new FileInfo(Path.Combine(testSubdirectory.FullName, "filefour.docx")); + + fileOne.Create().Dispose(); + fileTwo.Create().Dispose(); + fileThree.Create().Dispose(); + fileFour.Create().Dispose(); + + string[] paths = GetFileFullPathsWithExtension(testDirectory.FullName, true, ".htm", ".doc").ToArray(); + + FSAssert.EqualWhenOrdered(new string[] { fileOne.FullName, fileThree.FullName }, paths); + } + } +} diff --git a/src/System.IO.FileSystem/tests/Enumeration/SkipAttributeTests.netcoreapp.cs b/src/System.IO.FileSystem/tests/Enumeration/SkipAttributeTests.netcoreapp.cs index 224e2e3404..d81766ef2d 100644 --- a/src/System.IO.FileSystem/tests/Enumeration/SkipAttributeTests.netcoreapp.cs +++ b/src/System.IO.FileSystem/tests/Enumeration/SkipAttributeTests.netcoreapp.cs @@ -42,10 +42,14 @@ namespace System.IO.Tests.Enumeration if (PlatformDetection.IsWindows) fileFour.Attributes = fileTwo.Attributes | FileAttributes.Hidden; - string[] paths = GetPaths(testDirectory.FullName, new EnumerationOptions { AttributesToSkip = FileAttributes.Hidden }); + // Default EnumerationOptions is to skip hidden + string[] paths = GetPaths(testDirectory.FullName, new EnumerationOptions()); Assert.Equal(new string[] { fileOne.FullName }, paths); - paths = GetPaths(testDirectory.FullName, new EnumerationOptions { AttributesToSkip = FileAttributes.Hidden, RecurseSubdirectories = true }); + paths = GetPaths(testDirectory.FullName, new EnumerationOptions { AttributesToSkip = 0 }); + FSAssert.EqualWhenOrdered(new string[] { fileOne.FullName, fileTwo.FullName }, paths); + + paths = GetPaths(testDirectory.FullName, new EnumerationOptions { RecurseSubdirectories = true }); Assert.Equal(new string[] { fileOne.FullName, fileThree.FullName }, paths); if (PlatformDetection.IsWindows) @@ -58,7 +62,7 @@ namespace System.IO.Tests.Enumeration Directory.Move(testSubdirectory.FullName, Path.Combine(testDirectory.FullName, "." + testSubdirectory.Name)); } - paths = GetPaths(testDirectory.FullName, new EnumerationOptions { AttributesToSkip = FileAttributes.Hidden, RecurseSubdirectories = true }); + paths = GetPaths(testDirectory.FullName, new EnumerationOptions { RecurseSubdirectories = true }); Assert.Equal(new string[] { fileOne.FullName }, paths); } } diff --git a/src/System.IO.FileSystem/tests/Enumeration/SpecialDirectoryTests.netcoreapp.cs b/src/System.IO.FileSystem/tests/Enumeration/SpecialDirectoryTests.netcoreapp.cs index 98f3a2098a..5043190cff 100644 --- a/src/System.IO.FileSystem/tests/Enumeration/SpecialDirectoryTests.netcoreapp.cs +++ b/src/System.IO.FileSystem/tests/Enumeration/SpecialDirectoryTests.netcoreapp.cs @@ -2,14 +2,52 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Collections.Generic; using System.IO.Enumeration; using System.Linq; using Xunit; namespace System.IO.Tests.Enumeration { + public class SpecialDirectoryTests : FileSystemTest { + private class DirectoryRecursed : FileSystemEnumerator<string> + { + public int ShouldRecurseCalls { get; private set; } + + public DirectoryRecursed(string directory, EnumerationOptions options) + : base(directory, options) + { + } + + protected override string TransformEntry(ref FileSystemEntry entry) + =>new string(entry.FileName); + + protected override bool ShouldRecurseIntoEntry(ref FileSystemEntry entry) + { + ShouldRecurseCalls++; + return base.ShouldRecurseIntoEntry(ref entry); + } + } + + [Fact] + public void SpecialDirectoriesAreNotUpForRecursion() + { + using (var recursed = new DirectoryRecursed(TestDirectory, new EnumerationOptions { ReturnSpecialDirectories = true, RecurseSubdirectories = true, AttributesToSkip = 0 })) + { + List<string> results = new List<string>(); + while (recursed.MoveNext()) + results.Add(recursed.Current); + + Assert.Equal(0, recursed.ShouldRecurseCalls); + Assert.Contains("..", results); + } + } + } + + public class SpecialDirectoryTests_Enumerable : FileSystemTest + { protected virtual string[] GetNames(string directory, EnumerationOptions options) { return new FileSystemEnumerable<string>( @@ -21,13 +59,14 @@ namespace System.IO.Tests.Enumeration [Fact] public void SkippingHiddenFiles() { - string[] paths = GetNames(TestDirectory, new EnumerationOptions { ReturnSpecialDirectories = true }); + // Files that begin with periods are considered hidden on Unix + string[] paths = GetNames(TestDirectory, new EnumerationOptions { ReturnSpecialDirectories = true, AttributesToSkip = 0 }); Assert.Contains(".", paths); Assert.Contains("..", paths); } } - public class SpecialDirectoryTests_DirectoryInfo_GetDirectories : SpecialDirectoryTests + public class SpecialDirectoryTests_DirectoryInfo_GetDirectories : SpecialDirectoryTests_Enumerable { protected override string[] GetNames(string directory, EnumerationOptions options) { diff --git a/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj b/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj index 63a4b2c7cf..51e22c988f 100644 --- a/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj +++ b/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj @@ -60,6 +60,7 @@ <Compile Include="Enumeration\MatchCasingTests.netcoreapp.cs" /> <Compile Include="Enumeration\TrimmedPaths.netcoreapp.cs" /> <Compile Include="Enumeration\ErrorHandlingTests.netcoreapp.cs" /> + <Compile Include="Enumeration\IncludePredicateTests.netcoreapp.cs" /> </ItemGroup> <ItemGroup> <!-- Rewritten --> |