blob: e4c85d831e172b8fce28887d84ea30d3886617d7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
// 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.IO.Enumeration;
using Xunit;
namespace System.IO.Tests
{
public class ErrorHandlingTests : FileSystemTest
{
private class IgnoreErrors : FileSystemEnumerator<string>
{
public IgnoreErrors(string directory)
: base(directory)
{ }
public int ErrorCount { get; private set; }
public string DirectoryFinished { get; private set; }
protected override string TransformEntry(ref FileSystemEntry entry)
=> entry.FileName.ToString();
protected override bool ContinueOnError(int error)
{
ErrorCount++;
return true;
}
protected override void OnDirectoryFinished(ReadOnlySpan<char> directory)
=> DirectoryFinished = directory.ToString();
}
private class LastError : FileSystemEnumerator<string>
{
public LastError(string directory)
: base(directory)
{ }
public int Error { get; private set; }
protected override string TransformEntry(ref FileSystemEntry entry)
=> entry.FileName.ToString();
protected override bool ContinueOnError(int error)
{
Error = error;
return true;
}
}
[Fact]
public void OpenErrorDoesNotHappenAgainOnMoveNext()
{
// What we're checking for here is that we don't try to enumerate when we
// couldn't even open the root directory (e.g. open the handle again, try
// to get data, etc.)
using (IgnoreErrors ie = new IgnoreErrors(Path.GetRandomFileName()))
{
Assert.Equal(1, ie.ErrorCount);
Assert.False(ie.MoveNext());
Assert.Equal(1, ie.ErrorCount);
// Since we didn't start, the directory shouldn't finish.
Assert.Null(ie.DirectoryFinished);
}
}
[Fact]
public void NotFoundErrorIsExpected()
{
// Make sure we're returning the native error as expected (and not the PAL error on Unix)
using (LastError le = new LastError(Path.GetRandomFileName()))
{
// Conveniently ERROR_FILE_NOT_FOUND and ENOENT are both 0x2
Assert.Equal(2, le.Error);
}
}
[Fact]
public void DeleteDirectoryAfterOpening()
{
// We shouldn't prevent the directory from being deleted, even though we've
// opened (and are holding) the handle. On Windows this means we've opened
// the handle with file share of delete.
DirectoryInfo info = Directory.CreateDirectory(GetTestFilePath());
using (IgnoreErrors ie = new IgnoreErrors(info.FullName))
{
Assert.Equal(0, ie.ErrorCount);
Directory.Delete(info.FullName);
Assert.False(ie.MoveNext());
// This doesn't cause an error as the directory is still valid until the
// the enumerator is closed (as we have an open handle)
Assert.Equal(0, ie.ErrorCount);
Assert.Equal(info.FullName, ie.DirectoryFinished);
}
}
}
}
|