-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathDelete.cs
More file actions
235 lines (201 loc) · 8.28 KB
/
Copy pathDelete.cs
File metadata and controls
235 lines (201 loc) · 8.28 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Xunit;
using Microsoft.DotNet.XUnitExtensions;
namespace System.IO.Tests
{
public class File_Delete : FileSystemTest
{
static bool IsBindMountSupported => OperatingSystem.IsLinux() && !PlatformDetection.IsInContainer;
private static bool IsBindMountSupportedAndPrivilegedProcess => IsBindMountSupported && PlatformDetection.IsPrivilegedProcess;
protected virtual void Delete(string path)
{
File.Delete(path);
}
protected virtual FileInfo Create(string path)
{
var ret = new FileInfo(path);
ret.Create().Dispose();
return ret;
}
#region UniversalTests
[Fact]
public void NullParameters()
{
Assert.Throws<ArgumentNullException>(() => Delete(null));
}
[Fact]
public void InvalidParameters()
{
Assert.Throws<ArgumentException>(() => Delete(string.Empty));
}
[Fact]
public void DeleteDot_ThrowsUnauthorizedAccessException()
{
Assert.Throws<UnauthorizedAccessException>(() => Delete("."));
}
[Fact]
public void ShouldBeAbleToDeleteHiddenFile()
{
FileInfo testFile = Create(GetTestFilePath());
testFile.Attributes = FileAttributes.Hidden;
Delete(testFile.FullName);
Assert.False(testFile.Exists);
}
[Fact]
public void DeleteNonEmptyFile()
{
FileInfo testFile = Create(GetTestFilePath());
File.WriteAllText(testFile.FullName, "This is content");
Delete(testFile.FullName);
Assert.False(testFile.Exists);
}
[Fact]
public void PositiveTest()
{
FileInfo testFile = Create(GetTestFilePath());
Delete(testFile.FullName);
Assert.False(testFile.Exists);
}
[Fact]
public void NonExistentFile()
{
Delete(Path.Combine(Path.GetPathRoot(TestDirectory), Path.GetRandomFileName()));
Delete(GetTestFilePath());
}
[Fact]
public void ShouldThrowIOExceptionDeletingDirectory()
{
Assert.Throws<UnauthorizedAccessException>(() => Delete(TestDirectory));
}
[ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))]
public void DeletingSymLinkDoesntDeleteTarget()
{
var path = GetTestFilePath();
var linkPath = GetRandomLinkPath();
File.Create(path).Dispose();
Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: false));
// Both the symlink and the target exist
Assert.True(File.Exists(path), "path should exist");
Assert.True(File.Exists(linkPath), "linkPath should exist");
// Delete the symlink
File.Delete(linkPath);
// Target should still exist
Assert.True(File.Exists(path), "path should still exist");
Assert.False(File.Exists(linkPath), "linkPath should no longer exist");
}
[Fact]
public void NonExistentPath_Throws_DirectoryNotFoundException()
{
Assert.Throws<DirectoryNotFoundException>(() => Delete(Path.Combine(Path.GetRandomFileName(), "C")));
Assert.Throws<DirectoryNotFoundException>(() => Delete(Path.Combine(Path.GetPathRoot(TestDirectory), Path.GetRandomFileName(), "C")));
Assert.Throws<DirectoryNotFoundException>(() => Delete(Path.Combine(TestDirectory, GetTestFileName(), "C")));
}
#endregion
#region PlatformSpecific
[ConditionalFact(typeof(File_Delete), nameof(IsBindMountSupportedAndPrivilegedProcess))]
[OuterLoop("Needs sudo access")]
[PlatformSpecific(TestPlatforms.Linux)]
public void Unix_NonExistentPath_ReadOnlyVolume()
{
ReadOnly_FileSystemHelper(readOnlyDirectory =>
{
Delete(Path.Combine(readOnlyDirectory, "DoesNotExist"));
});
}
[ConditionalFact(typeof(File_Delete), nameof(IsBindMountSupportedAndPrivilegedProcess))]
[OuterLoop("Needs sudo access")]
[PlatformSpecific(TestPlatforms.Linux)]
public void Unix_ExistingDirectory_ReadOnlyVolume()
{
ReadOnly_FileSystemHelper(readOnlyDirectory =>
{
Assert.Throws<IOException>(() => Delete(Path.Combine(readOnlyDirectory, "subdir")));
}, subDirectoryName: "subdir");
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Deleting already-open file throws
public void Windows_File_Already_Open_Throws_IOException()
{
string path = GetTestFilePath();
using (File.Create(path))
{
Assert.Throws<IOException>(() => Delete(path));
}
}
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix)] // Deleting already-open file allowed
public void Unix_File_Already_Open_Allowed()
{
string path = GetTestFilePath();
using (File.Create(path))
{
Delete(path);
Assert.False(File.Exists(path));
}
Assert.False(File.Exists(path));
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Deleting readonly file throws
public void WindowsDeleteReadOnlyFile()
{
string path = GetTestFilePath();
File.Create(path).Dispose();
File.SetAttributes(path, FileAttributes.ReadOnly);
Assert.Throws<UnauthorizedAccessException>(() => Delete(path));
Assert.True(File.Exists(path));
File.SetAttributes(path, FileAttributes.Normal);
}
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix)] // Deleting readonly file allowed
public void UnixDeleteReadOnlyFile()
{
FileInfo testFile = Create(GetTestFilePath());
testFile.Attributes = FileAttributes.ReadOnly;
Delete(testFile.FullName);
Assert.False(testFile.Exists);
}
[ConditionalTheory(typeof(FileSystemTest), nameof(FileSystemTest.SupportsAlternateDataStreams))]
[InlineData(":bar")]
[InlineData(":bar:$DATA")]
[PlatformSpecific(TestPlatforms.Windows)]
public void WindowsDeleteAlternateDataStream(string streamName)
{
FileInfo testFile = Create(GetTestFilePath());
testFile.Create().Dispose();
streamName = testFile.FullName + streamName;
File.Create(streamName).Dispose();
Assert.True(File.Exists(streamName));
Delete(streamName);
Assert.False(File.Exists(streamName));
testFile.Refresh();
Assert.True(testFile.Exists);
}
[Theory]
[MemberData(nameof(TestData.ValidFileNames), MemberType = typeof(TestData))]
public void DeleteWithProblematicNames(string fileName)
{
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
string filePath = Path.Combine(testDir.FullName, fileName);
File.Create(filePath).Dispose();
Assert.True(File.Exists(filePath));
Delete(filePath);
Assert.False(File.Exists(filePath));
}
[Theory]
[MemberData(nameof(TestData.WindowsTrailingProblematicFileNames), MemberType = typeof(TestData))]
[PlatformSpecific(TestPlatforms.Windows)]
public void WindowsDeleteWithTrailingSpacePeriod_ViaExtendedSyntax(string fileName)
{
// Files with trailing spaces/periods require \\?\ syntax on Windows
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
string filePath = Path.Combine(testDir.FullName, fileName);
string extendedPath = @"\\?\" + filePath;
File.Create(extendedPath).Dispose();
Assert.True(File.Exists(extendedPath));
Delete(extendedPath);
Assert.False(File.Exists(extendedPath));
}
#endregion
}
}