-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Exists.cs
266 lines (223 loc) · 9.24 KB
/
Exists.cs
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Xunit;
namespace System.IO.Tests
{
public class File_Exists : FileSystemTest
{
#region Utilities
public virtual bool Exists(string path)
{
return File.Exists(path);
}
#endregion
#region UniversalTests
[Fact]
public void NullAsPath_ReturnsFalse()
{
Assert.False(Exists(null));
}
[Fact]
public void EmptyAsPath_ReturnsFalse()
{
Assert.False(Exists(string.Empty));
}
[Theory,
MemberData(nameof(ValidPathComponentNames))]
public void NonExistentValidPath_ReturnsFalse(string path)
{
Assert.False(Exists(path), path);
}
[Theory,
MemberData(nameof(ValidPathComponentNames))]
public void ValidPathExists_ReturnsTrue(string component)
{
string path = Path.Combine(TestDirectory, component);
FileInfo testFile = new FileInfo(path);
testFile.Create().Dispose();
Assert.True(Exists(path));
}
[Theory, MemberData(nameof(PathsWithInvalidCharacters))]
public void PathWithInvalidCharactersAsPath_ReturnsFalse(string invalidPath)
{
// Checks that errors aren't thrown when calling Exists() on paths with impossible to create characters
Assert.False(Exists(invalidPath));
}
[Fact]
public void PathAlreadyExistsAsFile()
{
string path = GetTestFilePath();
File.Create(path).Dispose();
Assert.True(Exists(IOServices.RemoveTrailingSlash(path)));
Assert.True(Exists(IOServices.RemoveTrailingSlash(IOServices.RemoveTrailingSlash(path))));
Assert.True(Exists(IOServices.RemoveTrailingSlash(IOServices.AddTrailingSlashIfNeeded(path))));
}
[Fact]
public void PathEndsInTrailingSlash()
{
string path = GetTestFilePath() + Path.DirectorySeparatorChar;
Assert.False(Exists(path));
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
public void PathEndsInAltTrailingSlash_Windows()
{
string path = GetTestFilePath() + Path.AltDirectorySeparatorChar;
Assert.False(Exists(path));
}
[Fact]
public void PathEndsInTrailingSlash_AndExists()
{
string path = GetTestFilePath();
File.Create(path).Dispose();
Assert.False(Exists(path + Path.DirectorySeparatorChar));
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
public void PathEndsInAltTrailingSlash_AndExists_Windows()
{
string path = GetTestFilePath();
File.Create(path).Dispose();
Assert.False(Exists(path + Path.AltDirectorySeparatorChar));
}
[Fact]
public void DirectoryLongerThanMaxDirectoryAsPath_DoesntThrow()
{
Assert.All((IOInputs.GetPathsLongerThanMaxDirectory(GetTestFilePath())), (path) =>
{
Assert.False(Exists(path));
});
}
[Fact]
public void DirectoryLongerThanMaxPathAsPath_DoesntThrow()
{
Assert.All((IOInputs.GetPathsLongerThanMaxPath(GetTestFilePath())), (path) =>
{
Assert.False(Exists(path), path);
});
}
[ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))]
public void SymLinksMayExistIndependentlyOfTarget()
{
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 target. The symlink should still exist
File.Delete(path);
Assert.False(File.Exists(path), "path should now not exist");
Assert.True(File.Exists(linkPath), "linkPath should still exist");
// Now delete the symlink.
File.Delete(linkPath);
Assert.False(File.Exists(linkPath), "linkPath should no longer exist");
}
#endregion
#region PlatformSpecific
[Theory,
MemberData(nameof(WhiteSpace))]
[PlatformSpecific(TestPlatforms.Windows)] // Unix equivalent tested already in CreateDirectory
public void WindowsNonSignificantWhiteSpaceAsPath_ReturnsFalse(string component)
{
// Checks that errors aren't thrown when calling Exists() on impossible paths
Assert.False(Exists(component));
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsCaseInsensitiveOS))]
public void DoesCaseInsensitiveInvariantComparions()
{
FileInfo testFile = new FileInfo(GetTestFilePath());
testFile.Create().Dispose();
Assert.True(Exists(testFile.FullName));
Assert.True(Exists(testFile.FullName.ToUpperInvariant()));
Assert.True(Exists(testFile.FullName.ToLowerInvariant()));
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsCaseSensitiveOS))]
public void DoesCaseSensitiveComparisons()
{
FileInfo testFile = new FileInfo(GetTestFilePath());
testFile.Create().Dispose();
Assert.True(Exists(testFile.FullName));
Assert.False(Exists(testFile.FullName.ToUpperInvariant()));
Assert.False(Exists(testFile.FullName.ToLowerInvariant()));
}
[Theory,
MemberData(nameof(NonControlWhiteSpace))]
[PlatformSpecific(TestPlatforms.Windows)]
public void TrailingWhiteSpace_NotTrimmed(string component)
{
// In .NET Core we don't trim anything other than space (' ')
string path = GetTestFilePath() + component;
FileInfo testFile = new FileInfo(path);
testFile.Create().Dispose();
Assert.True(Exists(path));
}
[Theory,
MemberData(nameof(SimpleWhiteSpace))] //*Just* spaces
[PlatformSpecific(TestPlatforms.Windows)] // In Windows, trailing whitespace in a path is trimmed
public void TrailingSpace_Trimmed(string component)
{
FileInfo testFile = new FileInfo(GetTestFilePath());
testFile.Create().Dispose();
// Windows will trim trailing spaces
Assert.True(Exists(testFile.FullName + component));
}
[Theory,
MemberData(nameof(PathsWithColons))]
[PlatformSpecific(TestPlatforms.Windows)] // alternate data stream
public void PathWithAlternateDataStreams_ReturnsFalse(string component)
{
Assert.False(Exists(component));
}
[ConditionalTheory(nameof(ReservedDeviceNamesAreBlocked))] // device names
[MemberData(nameof(PathsWithReservedDeviceNames))]
[OuterLoop]
public void PathWithReservedDeviceNameAsPath_ReturnsFalse(string component)
{
Assert.False(Exists(component));
}
[ActiveIssue("https://github.com/dotnet/runtimelab/issues/901", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
[Theory,
MemberData(nameof(UncPathsWithoutShareName))]
public void UncPathWithoutShareNameAsPath_ReturnsFalse(string component)
{
Assert.False(Exists(component));
}
[Theory,
MemberData(nameof(PathsWithComponentLongerThanMaxComponent))]
[PlatformSpecific(TestPlatforms.Windows)] // max directory length not fixed on Unix
public void DirectoryWithComponentLongerThanMaxComponentAsPath_ReturnsFalse(string component)
{
Assert.False(Exists(component));
}
#endregion
}
public class File_ExistsAsDirectory : FileSystemTest
{
[Fact]
public void DotAsPathReturnsFalse()
{
Assert.False(File.Exists("."));
Assert.False(File.Exists(".."));
}
[Fact]
public void PathAlreadyExistsAsDirectory()
{
string path = GetTestFilePath();
Directory.CreateDirectory(path);
Assert.False(File.Exists(IOServices.RemoveTrailingSlash(path)));
Assert.False(File.Exists(IOServices.RemoveTrailingSlash(IOServices.RemoveTrailingSlash(path))));
Assert.False(File.Exists(IOServices.RemoveTrailingSlash(IOServices.AddTrailingSlashIfNeeded(path))));
}
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)] // Uses P/Invokes
[ActiveIssue("https://github.com/dotnet/runtime/issues/67853", TestPlatforms.iOS | TestPlatforms.tvOS)]
public void FalseForNonRegularFile()
{
string fileName = GetTestFilePath();
Assert.Equal(0, mkfifo(fileName, 0));
Assert.True(File.Exists(fileName));
}
}
}