Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b4dfbad

Browse files
authored
More FileSystem tests (#27955)
- Fix #27244 validation - Add more enumerable tests Fixes #27244
1 parent 32bbfca commit b4dfbad

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

src/System.IO.FileSystem/tests/Enumeration/ErrorHandlingTests.netcoreapp.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,55 @@ public IgnoreErrors(string directory)
1616
{ }
1717

1818
public int ErrorCount { get; private set; }
19+
public string DirectoryFinished { get; private set; }
1920

2021
protected override string TransformEntry(ref FileSystemEntry entry)
21-
{
22-
throw new NotImplementedException();
23-
}
22+
=> entry.FileName.ToString();
2423

2524
protected override bool ContinueOnError(int error)
2625
{
2726
ErrorCount++;
2827
return true;
2928
}
29+
30+
protected override void OnDirectoryFinished(ReadOnlySpan<char> directory)
31+
=> DirectoryFinished = directory.ToString();
3032
}
3133

3234
[Fact]
3335
public void OpenErrorDoesNotHappenAgainOnMoveNext()
3436
{
37+
// What we're checking for here is that we don't try to enumerate when we
38+
// couldn't even open the root directory (e.g. open the handle again, try
39+
// to get data, etc.)
3540
using (IgnoreErrors ie = new IgnoreErrors(Path.GetRandomFileName()))
3641
{
3742
Assert.Equal(1, ie.ErrorCount);
3843
Assert.False(ie.MoveNext());
3944
Assert.Equal(1, ie.ErrorCount);
45+
46+
// Since we didn't start, the directory shouldn't finish.
47+
Assert.Null(ie.DirectoryFinished);
48+
}
49+
}
50+
51+
[Fact]
52+
public void DeleteDirectoryAfterOpening()
53+
{
54+
// We shouldn't prevent the directory from being deleted, even though we've
55+
// opened (and are holding) the handle. On Windows this means we've opened
56+
// the handle with file share of delete.
57+
DirectoryInfo info = Directory.CreateDirectory(GetTestFilePath());
58+
using (IgnoreErrors ie = new IgnoreErrors(info.FullName))
59+
{
60+
Assert.Equal(0, ie.ErrorCount);
61+
Directory.Delete(info.FullName);
62+
Assert.False(ie.MoveNext());
63+
64+
// This doesn't cause an error as the directory is still valid until the
65+
// the enumerator is closed (as we have an open handle)
66+
Assert.Equal(0, ie.ErrorCount);
67+
Assert.Equal(info.FullName, ie.DirectoryFinished);
4068
}
4169
}
4270
}

src/System.IO.FileSystem/tests/Enumeration/Win32MatcherTests.netcoreapp.cs renamed to src/System.IO.FileSystem/tests/Enumeration/FileSystemNameTests.netcoreapp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace System.IO.Tests
99
{
10-
public class Win32MatcherTests
10+
public class FileSystemNameTests
1111
{
1212
[Theory,
1313
MemberData(nameof(SimpleMatchData)),

src/System.IO.FileSystem/tests/Enumeration/RootTests.netcoreapp.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.IO.Enumeration;
6-
using System.Linq;
76
using Xunit;
87

98
namespace System.IO.Tests.Enumeration
@@ -32,11 +31,11 @@ protected override bool ShouldRecurseIntoEntry(ref FileSystemEntry entry)
3231
}
3332
}
3433

35-
[ActiveIssue(27244)]
3634
[Fact]
3735
public void CanRecurseFromRoot()
3836
{
3937
string root = Path.GetPathRoot(Path.GetTempPath());
38+
4039
using (var recursed = new DirectoryRecursed(root, new EnumerationOptions { AttributesToSkip = FileAttributes.System, RecurseSubdirectories = true }))
4140
{
4241
while (recursed.MoveNext())
@@ -47,9 +46,10 @@ public void CanRecurseFromRoot()
4746
return;
4847
}
4948

50-
// Should not get back a full path without a single separator (C:\foo.txt or /foo.txt)
51-
int count = recursed.Current.Count(c => c == Path.DirectorySeparatorChar);
52-
Assert.True(count <= 1, $"expected to find just one separator in '{recursed.Current}'");
49+
// Should start with the root and shouldn't have a separator after the root
50+
Assert.StartsWith(root, recursed.Current);
51+
Assert.True(recursed.Current.LastIndexOf(Path.DirectorySeparatorChar) < root.Length,
52+
$"should have no separators pasth the root '{root}' in in '{recursed.Current}'");
5353
}
5454

5555
Assert.NotNull(recursed.LastDirectory);

src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<Compile Include="Enumeration\ConstructionTests.netcoreapp.cs" />
5757
<Compile Include="Enumeration\SpecialDirectoryTests.netcoreapp.cs" />
5858
<Compile Include="Enumeration\SkipAttributeTests.netcoreapp.cs" />
59-
<Compile Include="Enumeration\Win32MatcherTests.netcoreapp.cs" />
59+
<Compile Include="Enumeration\FileSystemNameTests.netcoreapp.cs" />
6060
<Compile Include="Enumeration\MatchCasingTests.netcoreapp.cs" />
6161
<Compile Include="Enumeration\TrimmedPaths.netcoreapp.cs" />
6262
<Compile Include="Enumeration\ErrorHandlingTests.netcoreapp.cs" />

0 commit comments

Comments
 (0)