-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMockFileSystemTests.cs
337 lines (276 loc) · 9.09 KB
/
MockFileSystemTests.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using System.IO;
using System.Linq;
using Testably.Abstractions.Testing.FileSystem;
using Testably.Abstractions.Testing.Helpers;
using Testably.Abstractions.Testing.Tests.TestHelpers;
#if NET6_0_OR_GREATER
using Microsoft.Win32.SafeHandles;
#endif
namespace Testably.Abstractions.Testing.Tests;
public class MockFileSystemTests
{
[SkippableTheory]
[AutoData]
public void FileSystemMock_File_Decrypt(string path, string contents)
{
MockFileSystem sut = new();
sut.File.WriteAllText(path, contents);
#pragma warning disable CA1416
sut.File.Encrypt(path);
#pragma warning restore CA1416
#pragma warning disable CA1416
sut.File.Decrypt(path);
#pragma warning restore CA1416
sut.File.ReadAllText(path).Should().Be(contents);
}
[SkippableTheory]
[AutoData]
public void FileSystemMock_File_Encrypt(string path, string contents)
{
MockFileSystem sut = new();
sut.File.WriteAllText(path, contents);
#pragma warning disable CA1416
sut.File.Encrypt(path);
#pragma warning restore CA1416
sut.File.ReadAllText(path).Should().NotBe(contents);
}
[SkippableTheory]
[AutoData]
public void FileSystemMock_FileInfo_Decrypt(string path, string contents)
{
MockFileSystem sut = new();
sut.File.WriteAllText(path, contents);
#pragma warning disable CA1416
sut.FileInfo.New(path).Encrypt();
#pragma warning restore CA1416
#pragma warning disable CA1416
sut.FileInfo.New(path).Decrypt();
#pragma warning restore CA1416
sut.File.ReadAllText(path).Should().Be(contents);
}
[SkippableTheory]
[AutoData]
public void FileSystemMock_FileInfo_Encrypt(string path, string contents)
{
MockFileSystem sut = new();
sut.File.WriteAllText(path, contents);
#pragma warning disable CA1416
sut.FileInfo.New(path).Encrypt();
#pragma warning restore CA1416
sut.File.ReadAllText(path).Should().NotBe(contents);
}
[SkippableFact]
public void FileSystemMock_ShouldBeInitializedWithADefaultDrive()
{
MockFileSystem sut = new();
string expectedDriveName = "".PrefixRoot(sut);
IDriveInfo[] drives = sut.DriveInfo.GetDrives();
IDriveInfo drive = sut.GetDefaultDrive();
drives.Should().NotBeEmpty();
drive.Name.Should().Be(expectedDriveName);
drive.AvailableFreeSpace.Should().BeGreaterThan(0);
drive.DriveFormat.Should()
.Be(DriveInfoMock.DefaultDriveFormat);
drive.DriveType.Should()
.Be(DriveInfoMock.DefaultDriveType);
drive.VolumeLabel.Should().NotBeNullOrEmpty();
}
[SkippableTheory]
[InlineData("A:\\")]
[InlineData("G:\\")]
[InlineData("z:\\")]
public void FileSystemMock_ShouldInitializeDriveFromCurrentDirectory(string driveName)
{
Skip.If(!Test.RunsOnWindows);
MockFileSystem sut = new(o => o.UseCurrentDirectory($"{driveName}foo\\bar"));
IDriveInfo[] drives = sut.DriveInfo.GetDrives();
drives.Length.Should().Be(2);
drives.Should().Contain(d => d.Name == "C:\\");
drives.Should().Contain(d => d.Name == driveName);
}
[SkippableFact]
public void ToString_ShouldContainStorageInformation()
{
MockFileSystem sut = new();
sut.File.WriteAllText("foo", "bar");
string result = sut.ToString();
result.Should().Contain("directories: 0, files: 1");
}
[SkippableTheory]
[AutoData]
public void WithAccessControl_Denied_CreateDirectoryShouldThrowIOException(
string path)
{
MockFileSystem sut = new();
sut.Initialize();
sut.WithAccessControlStrategy(new DefaultAccessControlStrategy((_, _) => false));
Exception? exception = Record.Exception(() =>
{
sut.Directory.CreateDirectory(path);
});
exception.Should().BeOfType<IOException>();
}
[SkippableTheory]
[AutoData]
public void WithAccessControl_ShouldConsiderPath(
string allowedPath, string deniedPath)
{
MockFileSystem sut = new();
sut.Initialize();
sut.WithAccessControlStrategy(new DefaultAccessControlStrategy((p, _)
=> string.Equals(p, sut.Path.GetFullPath(allowedPath), StringComparison.Ordinal)));
sut.Directory.CreateDirectory(allowedPath);
Exception? exception = Record.Exception(() =>
{
sut.Directory.CreateDirectory(deniedPath);
});
exception.Should().BeOfType<IOException>();
}
[SkippableTheory]
[InlineData("D:\\")]
public void WithDrive_Duplicate_ShouldUpdateExistingDrive(string driveName)
{
Skip.IfNot(Test.RunsOnWindows, "Linux does not support different drives.");
MockFileSystem sut = new();
sut.WithDrive(driveName, d => d.SetTotalSize(100));
sut.DriveInfo.GetDrives().Length.Should().Be(2);
IDriveInfo drive = sut.DriveInfo.GetDrives()
.Single(x => string.Equals(x.Name, driveName, StringComparison.Ordinal));
drive.TotalSize.Should().Be(100);
sut.WithDrive(driveName, d => d.SetTotalSize(200));
sut.DriveInfo.GetDrives().Length.Should().Be(2);
drive.TotalSize.Should().Be(200);
}
[SkippableFact]
public void WithDrive_ExistingName_ShouldUpdateDrive()
{
MockFileSystem sut = new();
string driveName = "".PrefixRoot(sut);
sut.WithDrive(driveName);
IDriveInfo[] drives = sut.DriveInfo.GetDrives();
drives.Length.Should().BeGreaterOrEqualTo(1);
drives.Should().ContainSingle(d => d.Name == driveName);
}
[SkippableTheory]
[InlineData("D:\\")]
public void WithDrive_NewName_ShouldCreateNewDrives(string driveName)
{
Skip.IfNot(Test.RunsOnWindows, "Linux does not support different drives.");
MockFileSystem sut = new();
sut.WithDrive(driveName);
IDriveInfo[] drives = sut.DriveInfo.GetDrives();
drives.Length.Should().Be(2);
drives.Should().ContainSingle(d => d.Name == driveName);
}
[SkippableTheory]
[InlineData("D")]
[InlineData("D:")]
public void WithDrive_ShouldHavePathSeparatorSuffix(string driveName)
{
Skip.IfNot(Test.RunsOnWindows, "Linux does not support different drives.");
string expectedDriveName = $"D:{Path.DirectorySeparatorChar}";
MockFileSystem sut = new();
sut.WithDrive(driveName);
IDriveInfo[] drives = sut.DriveInfo.GetDrives();
drives.Length.Should().BeLessOrEqualTo(2);
drives.Should().ContainSingle(d => d.Name == expectedDriveName);
}
[SkippableTheory]
[AutoData]
public void WithDrive_WithCallback_ShouldUpdateDrive(long totalSize)
{
MockFileSystem sut = new();
sut.WithDrive(d => d.SetTotalSize(totalSize));
IDriveInfo drive = sut.GetDefaultDrive();
drive.TotalSize.Should().Be(totalSize);
drive.TotalFreeSpace.Should().Be(totalSize);
drive.AvailableFreeSpace.Should().Be(totalSize);
}
#if NET6_0_OR_GREATER
[SkippableTheory]
[AutoData]
public void
WithSafeFileHandleStrategy_DefaultStrategy_ShouldUseMappedSafeFileHandleMock(
string path, string contents)
{
MockFileSystem sut = new();
sut.File.WriteAllText(path, contents);
sut.WithSafeFileHandleStrategy(
new DefaultSafeFileHandleStrategy(_ => new SafeFileHandleMock(path)));
using FileSystemStream stream =
sut.FileStream.New(new SafeFileHandle(), FileAccess.Read);
using StreamReader streamReader = new(stream);
string result = streamReader.ReadToEnd();
result.Should().Be(contents);
}
#endif
#if NET6_0_OR_GREATER
[SkippableTheory]
[AutoData]
public void WithSafeFileHandleStrategy_NullStrategy_ShouldThrowException(
string path, string contents)
{
MockFileSystem sut = new();
sut.File.WriteAllText(path, contents);
Exception? exception = Record.Exception(() =>
{
sut.FileStream.New(new SafeFileHandle(), FileAccess.Read);
});
exception.Should().BeOfType<ArgumentException>()
.Which.ParamName.Should().Be("handle");
}
#endif
[SkippableTheory]
[AutoData]
public void WithUncDrive_ShouldCreateUncDrive(
string path, string contents)
{
MockFileSystem sut = new();
sut.WithUncDrive("UNC-Path");
string fullPath = sut.Path.Combine("//UNC-Path", path);
sut.File.WriteAllText(fullPath, contents);
string result = sut.File.ReadAllText(fullPath);
result.Should().Be(contents);
}
[SkippableTheory]
[AutoData]
public void WithUncDrive_ShouldNotBeIncludedInGetDrives(
string server)
{
MockFileSystem sut = new();
string uncPrefix = new(sut.Path.DirectorySeparatorChar, 2);
string uncDrive = $"{uncPrefix}{server}";
int expectedLogicalDrives = sut.Directory.GetLogicalDrives().Length;
int expectedDrives = sut.DriveInfo.GetDrives().Length;
sut.WithUncDrive(uncDrive);
sut.Directory.GetLogicalDrives().Length.Should().Be(expectedLogicalDrives);
sut.DriveInfo.GetDrives().Length.Should().Be(expectedDrives);
}
[SkippableTheory]
[AutoData]
public void WithUncDrive_WriteBytes_ShouldReduceAvailableFreeSpace(
string server, string path, byte[] bytes)
{
MockFileSystem sut = new();
string uncPrefix = new(sut.Path.DirectorySeparatorChar, 2);
string uncDrive = $"{uncPrefix}{server}";
sut.WithUncDrive(uncDrive);
IDriveInfo drive = sut.DriveInfo.New(uncDrive);
long previousFreeSpace = drive.AvailableFreeSpace;
sut.File.WriteAllBytes(Path.Combine(uncDrive, path), bytes);
drive.AvailableFreeSpace.Should().Be(previousFreeSpace - bytes.Length);
}
[SkippableTheory]
[AutoData]
public void WriteAllText_OnUncPath_ShouldThrowDirectoryNotFoundException(
string path, string contents)
{
MockFileSystem sut = new();
string fullPath = sut.Path.Combine("//UNC-Path", path);
Exception? exception = Record.Exception(() =>
{
sut.File.WriteAllText(fullPath, contents);
});
exception.Should().BeOfType<DirectoryNotFoundException>();
}
}