/
MockFileSystemTests.cs
421 lines (345 loc) · 17 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using XFS = System.IO.Abstractions.TestingHelpers.MockUnixSupport;
namespace System.IO.Abstractions.TestingHelpers.Tests
{
[TestFixture]
public class MockFileSystemTests
{
[Test]
public void MockFileSystem_GetFile_ShouldReturnNullWhenFileIsNotRegistered()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\something\demo.txt", new MockFileData("Demo\r\ntext\ncontent\rvalue") },
{ @"c:\something\other.gif", new MockFileData("gif content") }
});
var result = fileSystem.GetFile(@"c:\something\else.txt");
Assert.IsNull(result);
}
[Test]
public void MockFileSystem_GetFile_ShouldReturnFileRegisteredInConstructor()
{
var file1 = new MockFileData("Demo\r\ntext\ncontent\rvalue");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\something\demo.txt", file1 },
{ @"c:\something\other.gif", new MockFileData("gif content") }
});
var result = fileSystem.GetFile(@"c:\something\demo.txt");
Assert.AreEqual(file1, result);
}
[Test]
[WindowsOnly(WindowsSpecifics.CaseInsensitivity)]
public void MockFileSystem_GetFile_ShouldReturnFileRegisteredInConstructorWhenPathsDifferByCase()
{
var file1 = new MockFileData("Demo\r\ntext\ncontent\rvalue");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\something\demo.txt", file1 },
{ @"c:\something\other.gif", new MockFileData("gif content") }
});
var result = fileSystem.GetFile(@"c:\SomeThing\DEMO.txt");
Assert.AreEqual(file1, result);
}
[Test]
[UnixOnly(UnixSpecifics.CaseSensitivity)]
public void MockFileSystem_GetFile_ShouldNotReturnFileRegisteredInConstructorWhenPathsDifferByCase()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ "/something/demo.txt", new MockFileData("Demo\r\ntext\ncontent\rvalue") },
{ "/something/other.gif", new MockFileData("gif content") }
});
var result = fileSystem.GetFile("/SomeThing/DEMO.txt");
Assert.IsNull(result);
}
[Test]
public void MockFileSystem_AddFile_ShouldHandleUnnormalizedSlashes()
{
var path = XFS.Path(@"c:\d1\d2\file.txt");
var alternatePath = XFS.Path("c:/d1/d2/file.txt");
var alternateParentPath = XFS.Path("c://d1//d2/");
var fs = new MockFileSystem();
fs.AddFile(path, new MockFileData("Hello"));
var fileCount = fs.Directory.GetFiles(alternateParentPath).Length;
var fileExists = fs.File.Exists(alternatePath);
Assert.AreEqual(1, fileCount);
Assert.IsTrue(fileExists);
}
[Test]
public void MockFileSystem_AddFile_ShouldHandleNullFileDataAsEmpty()
{
var path = XFS.Path(@"c:\something\nullish.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, null }
});
var result = fileSystem.File.ReadAllText(path);
Assert.IsEmpty(result, "Null MockFileData should be allowed for and result in an empty file.");
}
[Test]
public void MockFileSystem_AddFile_ShouldRepaceExistingFile()
{
var path = XFS.Path(@"c:\some\file.txt");
const string existingContent = "Existing content";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData(existingContent) }
});
Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(existingContent));
const string newContent = "New content";
fileSystem.AddFile(path, new MockFileData(newContent));
Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(newContent));
}
[Test]
public void MockFileSystem_ByDefault_IsSerializable()
{
var file1 = new MockFileData("Demo\r\ntext\ncontent\rvalue");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\something\demo.txt", file1 },
{ @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) }
});
var memoryStream = new MemoryStream();
var serializer = new Runtime.Serialization.Formatters.Binary.BinaryFormatter();
serializer.Serialize(memoryStream, fileSystem);
Assert.That(memoryStream.Length > 0, "Length didn't increase after serialization task.");
}
[Test]
public void MockFileSystem_AddDirectory_ShouldCreateDirectory()
{
string baseDirectory = XFS.Path(@"C:\Test");
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(baseDirectory);
Assert.IsTrue(fileSystem.Directory.Exists(baseDirectory));
}
[Test]
public void MockFileSystem_AddDirectory_ShouldThrowExceptionIfDirectoryIsReadOnly()
{
string baseDirectory = XFS.Path(@"C:\Test");
var fileSystem = new MockFileSystem();
fileSystem.AddFile(baseDirectory, new MockFileData(string.Empty));
fileSystem.File.SetAttributes(baseDirectory, FileAttributes.ReadOnly);
TestDelegate action = () => fileSystem.AddDirectory(baseDirectory);
Assert.Throws<UnauthorizedAccessException>(action);
}
[Test]
public void MockFileSystem_DriveInfo_ShouldNotThrowAnyException()
{
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(XFS.Path(@"C:\Test"));
var actualResults = fileSystem.DriveInfo.GetDrives();
Assert.IsNotNull(actualResults);
}
[Test]
public void MockFileSystem_AddFile_ShouldMatchCapitalization_PerfectMatch()
{
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(XFS.Path(@"C:\test"));
fileSystem.AddDirectory(XFS.Path(@"C:\LOUD"));
fileSystem.AddFile(XFS.Path(@"C:\test\file.txt"), "foo");
fileSystem.AddFile(XFS.Path(@"C:\LOUD\file.txt"), "foo");
fileSystem.AddDirectory(XFS.Path(@"C:\test\SUBDirectory"));
fileSystem.AddDirectory(XFS.Path(@"C:\LOUD\SUBDirectory"));
Assert.Contains(XFS.Path(@"C:\test\file.txt"), fileSystem.AllFiles.ToList());
Assert.Contains(XFS.Path(@"C:\LOUD\file.txt"), fileSystem.AllFiles.ToList());
Assert.Contains(XFS.Path(@"C:\test\SUBDirectory"), fileSystem.AllDirectories.ToList());
Assert.Contains(XFS.Path(@"C:\LOUD\SUBDirectory"), fileSystem.AllDirectories.ToList());
}
[Test]
[WindowsOnly(WindowsSpecifics.CaseInsensitivity)]
public void MockFileSystem_AddFile_ShouldMatchCapitalization_PartialMatch()
{
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(XFS.Path(@"C:\test\subtest"));
fileSystem.AddDirectory(XFS.Path(@"C:\LOUD\SUBLOUD"));
fileSystem.AddFile(XFS.Path(@"C:\test\SUBTEST\file.txt"), "foo");
fileSystem.AddFile(XFS.Path(@"C:\LOUD\subloud\file.txt"), "foo");
fileSystem.AddDirectory(XFS.Path(@"C:\test\SUBTEST\SUBDirectory"));
fileSystem.AddDirectory(XFS.Path(@"C:\LOUD\subloud\SUBDirectory"));
Assert.Contains(XFS.Path(@"C:\test\subtest\file.txt"), fileSystem.AllFiles.ToList());
Assert.Contains(XFS.Path(@"C:\LOUD\SUBLOUD\file.txt"), fileSystem.AllFiles.ToList());
Assert.Contains(XFS.Path(@"C:\test\subtest\SUBDirectory"), fileSystem.AllDirectories.ToList());
Assert.Contains(XFS.Path(@"C:\LOUD\SUBLOUD\SUBDirectory"), fileSystem.AllDirectories.ToList());
}
[Test]
[WindowsOnly(WindowsSpecifics.CaseInsensitivity)]
public void MockFileSystem_AddFile_ShouldMatchCapitalization_PartialMatch_FurtherLeft()
{
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(XFS.Path(@"C:\test\subtest"));
fileSystem.AddDirectory(XFS.Path(@"C:\LOUD\SUBLOUD"));
fileSystem.AddFile(XFS.Path(@"C:\test\SUBTEST\new\file.txt"), "foo");
fileSystem.AddFile(XFS.Path(@"C:\LOUD\subloud\new\file.txt"), "foo");
fileSystem.AddDirectory(XFS.Path(@"C:\test\SUBTEST\new\SUBDirectory"));
fileSystem.AddDirectory(XFS.Path(@"C:\LOUD\subloud\new\SUBDirectory"));
Assert.Contains(XFS.Path(@"C:\test\subtest\new\file.txt"), fileSystem.AllFiles.ToList());
Assert.Contains(XFS.Path(@"C:\LOUD\SUBLOUD\new\file.txt"), fileSystem.AllFiles.ToList());
Assert.Contains(XFS.Path(@"C:\test\subtest\new\SUBDirectory"), fileSystem.AllDirectories.ToList());
Assert.Contains(XFS.Path(@"C:\LOUD\SUBLOUD\new\SUBDirectory"), fileSystem.AllDirectories.ToList());
}
[Test]
public void MockFileSystem_AddFileFromEmbeddedResource_ShouldAddTheFile()
{
var fileSystem = new MockFileSystem();
fileSystem.AddFileFromEmbeddedResource(XFS.Path(@"C:\TestFile.txt"), Assembly.GetExecutingAssembly(), "System.IO.Abstractions.TestingHelpers.Tests.TestFiles.TestFile.txt");
var result = fileSystem.GetFile(XFS.Path(@"C:\TestFile.txt"));
Assert.AreEqual(new UTF8Encoding().GetBytes("This is a test file."), result.Contents);
}
[Test]
public void MockFileSystem_AddFilesFromEmbeddedResource_ShouldAddAllTheFiles()
{
var fileSystem = new MockFileSystem();
fileSystem.AddFilesFromEmbeddedNamespace(XFS.Path(@"C:\"), Assembly.GetExecutingAssembly(), "System.IO.Abstractions.TestingHelpers.Tests.TestFiles");
Assert.Contains(XFS.Path(@"C:\TestFile.txt"), fileSystem.AllFiles.ToList());
Assert.Contains(XFS.Path(@"C:\SecondTestFile.txt"), fileSystem.AllFiles.ToList());
}
[Test]
public void MockFileSystem_RemoveFile_RemovesFiles()
{
var fileSystem = new MockFileSystem();
fileSystem.AddFile(@"C:\file.txt", new MockFileData("Content"));
fileSystem.RemoveFile(@"C:\file.txt");
Assert.False(fileSystem.FileExists(@"C:\file.txt"));
}
[Test]
public void MockFileSystem_RemoveFile_ThrowsUnauthorizedAccessExceptionIfFileIsReadOnly()
{
var path = XFS.Path(@"C:\file.txt");
var readOnlyFile = new MockFileData("")
{
Attributes = FileAttributes.ReadOnly
};
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, readOnlyFile },
});
TestDelegate action = () => fileSystem.RemoveFile(path);
Assert.Throws<UnauthorizedAccessException>(action);
}
[Test]
public void MockFileSystem_AllNodes_ShouldReturnAllNodes()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"c:\something\demo.txt"), MockFileData.NullObject },
{ XFS.Path(@"c:\something\other.gif"), MockFileData.NullObject },
{ XFS.Path(@"d:\foobar\"), new MockDirectoryData() },
{ XFS.Path(@"d:\foo\bar"), new MockDirectoryData( )}
});
var expectedNodes = new[]
{
XFS.Path(@"c:\something\demo.txt"),
XFS.Path(@"c:\something\other.gif"),
XFS.Path(@"d:\foobar"),
XFS.Path(@"d:\foo\bar"),
XFS.Path(@"C:\temp")
};
var result = fileSystem.AllNodes;
Assert.AreEqual(expectedNodes, result);
}
[Test]
[TestCase(@"C:\path")]
[TestCase(@"C:\path\")]
public void MockFileSystem_AddDirectory_TrailingSlashAllowedButNotRequired(string path)
{
var fileSystem = new MockFileSystem();
var path2 = XFS.Path(path);
fileSystem.AddDirectory(path2);
Assert.IsTrue(fileSystem.FileExists(path2));
}
[Test]
public void MockFileSystem_GetFiles_ThrowsArgumentExceptionForInvalidCharacters()
{
// Arrange
const string path = @"c:\";
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(XFS.Path(path));
// Act
TestDelegate getFilesWithInvalidCharacterInPath = () => fileSystem.Directory.GetFiles($"{path}{'\0'}.txt");
// Assert
Assert.Throws<ArgumentException>(getFilesWithInvalidCharacterInPath);
}
[Test]
[TestCase(null)]
[TestCase(@"C:\somepath")]
public void MockFileSystem_DefaultState_CurrentDirectoryExists(string currentDirectory)
{
var fs = new MockFileSystem(null, XFS.Path(currentDirectory));
var actualCurrentDirectory = fs.DirectoryInfo.FromDirectoryName(".");
Assert.IsTrue(actualCurrentDirectory.Exists);
}
[Test]
public void MockFileSystem_Constructor_ThrowsForNonRootedCurrentDirectory()
{
var ae = Assert.Throws<ArgumentException>(() =>
new MockFileSystem(null, "non-rooted")
);
Assert.AreEqual("currentDirectory", ae.ParamName);
}
[Test]
public void MockFileSystem_DefaultState_DefaultTempDirectoryExists()
{
var tempDirectory = XFS.Path(@"C:\temp");
var mockFileSystem = new MockFileSystem();
var mockFileSystemOverload = new MockFileSystem(null, string.Empty);
Assert.IsTrue(mockFileSystem.Directory.Exists(tempDirectory));
Assert.IsTrue(mockFileSystemOverload.Directory.Exists(tempDirectory));
}
[Test]
public void MockFileSystem_FileSystemWatcher_PathShouldBeAssignable()
{
var path = XFS.Path(@"C:\root");
var fileSystem = new MockFileSystem { FileSystemWatcher = new TestFileSystemWatcherFactory() };
var watcher = fileSystem.FileSystemWatcher.CreateNew(path);
Assert.AreEqual(path, watcher.Path);
}
[Test]
public void MockFileSystem_FileSystemWatcher_PathAndFilterShouldBeAssignable()
{
var path = XFS.Path(@"C:\root");
var filter = "*.txt";
var fileSystem = new MockFileSystem { FileSystemWatcher = new TestFileSystemWatcherFactory() };
var watcher = fileSystem.FileSystemWatcher.CreateNew(path, filter);
Assert.AreEqual(path, watcher.Path);
Assert.AreEqual(filter, watcher.Filter);
}
private class TestFileSystemWatcherFactory : IFileSystemWatcherFactory
{
public IFileSystemWatcher CreateNew() => new TestFileSystemWatcher(null);
public IFileSystemWatcher CreateNew(string path) => new TestFileSystemWatcher(path);
public IFileSystemWatcher CreateNew(string path, string filter) => new TestFileSystemWatcher(path, filter);
public IFileSystemWatcher FromPath(string path) => new TestFileSystemWatcher(path);
}
private class TestFileSystemWatcher : FileSystemWatcherBase
{
public TestFileSystemWatcher(string path) => Path = path;
public TestFileSystemWatcher(string path, string filter)
{
Path = path;
Filter = filter;
}
public override string Path { get; set; }
public override bool IncludeSubdirectories { get; set; }
public override bool EnableRaisingEvents { get; set; }
public override string Filter { get; set; }
public override int InternalBufferSize { get; set; }
public override NotifyFilters NotifyFilter { get; set; }
public override ISite Site { get; set; }
public override ISynchronizeInvoke SynchronizingObject { get; set; }
#if FEATURE_FILE_SYSTEM_WATCHER_FILTERS
public override Collection<string> Filters { get; }
#endif
public override void BeginInit() { }
public override void EndInit() { }
public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType) => default(WaitForChangedResult);
public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout) => default(WaitForChangedResult);
}
}
}