-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Exists.cs
181 lines (155 loc) · 6.32 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Linq;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
namespace System.IO.Tests
{
public class FileInfo_Exists : FileSystemTest
{
[Fact]
public void InitializeExistsBeforeCreation()
{
string fileName = GetTestFilePath();
FileInfo di = new FileInfo(fileName);
// don't check it, data has not yet been init'ed
File.Create(fileName).Dispose();
// data will be init'ed at the time of calling exists
Assert.True(di.Exists);
}
[Fact]
public void InitializeExistsAfterCreation()
{
string fileName = GetTestFilePath();
FileInfo di = new FileInfo(fileName);
Assert.False(di.Exists);
File.Create(fileName).Dispose();
// data should be stale
Assert.False(di.Exists);
// force refresh
di.Refresh();
Assert.True(di.Exists);
}
[Fact]
public void NonExistentFile()
{
Assert.False(new FileInfo("Da drar vi til fjells").Exists);
}
[Theory, MemberData(nameof(TrailingCharacters))]
public void MissingDirectory(char trailingChar)
{
string path = GetTestFilePath();
FileInfo info = new FileInfo(Path.Combine(path, "file" + trailingChar));
Assert.False(info.Exists);
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsCaseInsensitiveOS))]
public void CaseInsensitivity()
{
string path = GetTestFilePath();
File.Create(path).Dispose();
Assert.True(new FileInfo(path.ToUpperInvariant()).Exists);
Assert.True(new FileInfo(path.ToLowerInvariant()).Exists);
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsCaseSensitiveOS))]
public void CaseSensitivity()
{
string path = GetTestFilePath();
File.Create(path).Dispose();
Assert.False(new FileInfo(path.ToUpperInvariant()).Exists);
Assert.False(new FileInfo(path.ToLowerInvariant()).Exists);
}
[Fact]
public void TrueForNewFileInfo()
{
string fileName = GetTestFilePath();
File.Create(fileName).Dispose();
FileInfo di = new FileInfo(fileName);
Assert.True(di.Exists);
}
[Fact]
public void FalseForDirectory()
{
string fileName = GetTestFilePath();
Directory.CreateDirectory(fileName);
FileInfo di = new FileInfo(fileName);
Assert.False(di.Exists);
}
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)] // Uses P/Invokes
[ActiveIssue("https://github.com/dotnet/runtime/issues/67853", TestPlatforms.iOS | TestPlatforms.tvOS)]
public void TrueForNonRegularFile()
{
string fileName = GetTestFilePath();
Assert.Equal(0, mkfifo(fileName, 0));
FileInfo fi = new FileInfo(fileName);
Assert.True(fi.Exists);
}
[ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))]
public void SymLinksMayExistIndependentlyOfTarget()
{
var path = GetTestFilePath();
var linkPath = GetRandomLinkPath();
var pathFI = new FileInfo(path);
var linkPathFI = new FileInfo(linkPath);
pathFI.Create().Dispose();
Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: false));
// Both the symlink and the target exist
pathFI.Refresh();
linkPathFI.Refresh();
Assert.True(pathFI.Exists, "path should exist");
Assert.True(linkPathFI.Exists, "linkPath should exist");
// Delete the target. The symlink should still exist
pathFI.Delete();
pathFI.Refresh();
linkPathFI.Refresh();
Assert.False(pathFI.Exists, "path should now not exist");
Assert.True(linkPathFI.Exists, "linkPath should still exist");
// Now delete the symlink.
linkPathFI.Delete();
linkPathFI.Refresh();
Assert.False(linkPathFI.Exists, "linkPath should no longer exist");
}
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void UnsharedFileExists()
{
string path = GetTestFilePath();
using (FileStream stream = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None))
{
RemoteExecutor.Invoke((p) =>
{
FileInfo info = new FileInfo(p);
Assert.True(info.Exists);
}, path).Dispose();
}
}
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[SkipOnPlatform(TestPlatforms.OSX, "Not supported on OSX.")]
public void LockedFileExists()
{
string path = GetTestFilePath();
File.WriteAllBytes(path, new byte[10]);
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
stream.Lock(0, 10);
RemoteExecutor.Invoke((p) =>
{
FileInfo info = new FileInfo(p);
Assert.True(info.Exists);
}, path).Dispose();
stream.Unlock(0, 10);
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotInAppContainer))] // Can't read root in appcontainer
[PlatformSpecific(TestPlatforms.Windows)]
public void PageFileExists()
{
// Typically there is a page file on the C: drive, if not, don't bother trying to track it down.
string pageFilePath = Directory.EnumerateFiles(@"C:\", "pagefile.sys").FirstOrDefault();
if (pageFilePath != null)
{
FileInfo info = new FileInfo(pageFilePath);
Assert.True(info.Exists);
}
}
}
}