-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathMockFileSystemTests.cs
More file actions
630 lines (508 loc) · 24.6 KB
/
MockFileSystemTests.cs
File metadata and controls
630 lines (508 loc) · 24.6 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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
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.That(result, Is.Null);
}
[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.That(result, Is.EqualTo(file1));
}
[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.That(result, Is.EqualTo(file1));
}
[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.That(result, Is.Null);
}
[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.That(fileCount, Is.EqualTo(1));
Assert.That(fileExists, Is.True);
}
[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.That(result, Is.Empty, "Null MockFileData should be allowed for and result in an empty file.");
}
[Test]
public void MockFileSystem_AddFile_ShouldReplaceExistingFile()
{
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_AddEmptyFile_ShouldBeEmpty()
{
var path = XFS.Path(@"c:\some\file.txt");
var fileSystem = new MockFileSystem();
fileSystem.AddEmptyFile(path);
Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(""));
}
[Test]
public void MockFileSystem_AddEmptyFile_ShouldExist()
{
var fileSystem = new MockFileSystem();
var path = fileSystem.FileInfo.New(XFS.Path(@"c:\some\file.txt"));
fileSystem.AddEmptyFile(path);
Assert.That(path.Exists, Is.True);
}
[Test]
public void MockFileSystem_AddFile_ShouldExist()
{
var fileSystem = new MockFileSystem();
var path = fileSystem.FileInfo.New(XFS.Path(@"c:\some\file.txt"));
fileSystem.AddFile(path, new MockFileData("stuff"));
Assert.That(path.Exists, Is.True);
}
[Test]
public void MockFileSystem_AddDirectory_ShouldExist()
{
var fileSystem = new MockFileSystem();
var path = fileSystem.DirectoryInfo.New(XFS.Path(@"c:\thedir"));
fileSystem.AddDirectory(path);
Assert.That(path.Exists, Is.True);
}
#if !NET9_0_OR_GREATER
[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();
#pragma warning disable SYSLIB0011
var serializer = new Runtime.Serialization.Formatters.Binary.BinaryFormatter();
serializer.Serialize(memoryStream, fileSystem);
#pragma warning restore SYSLIB0011
Assert.That(memoryStream.Length > 0, "Length didn't increase after serialization task.");
}
#endif
[Test]
public void MockFileSystem_AddDirectory_ShouldCreateDirectory()
{
string baseDirectory = XFS.Path(@"C:\Test");
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(baseDirectory);
Assert.That(fileSystem.Directory.Exists(baseDirectory), Is.True);
}
[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_AddDrive_ShouldExist()
{
string name = @"D:\";
var fileSystem = new MockFileSystem();
fileSystem.AddDrive(name, new MockDriveData());
var actualResults = fileSystem.DriveInfo.GetDrives().Select(d => d.Name);
Assert.That(actualResults, Does.Contain(name));
}
[Test]
public void MockFileSystem_DriveInfo_ShouldNotThrowAnyException()
{
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(XFS.Path(@"C:\Test"));
var actualResults = fileSystem.DriveInfo.GetDrives();
Assert.That(actualResults, Is.Not.Null);
}
[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.That(fileSystem.AllFiles.ToList(), Does.Contain(XFS.Path(@"C:\test\file.txt")));
Assert.That(fileSystem.AllFiles.ToList(), Does.Contain(XFS.Path(@"C:\LOUD\file.txt")));
Assert.That(fileSystem.AllDirectories.ToList(), Does.Contain(XFS.Path(@"C:\test\SUBDirectory")));
Assert.That(fileSystem.AllDirectories.ToList(), Does.Contain(XFS.Path(@"C:\LOUD\SUBDirectory")));
}
[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.That(fileSystem.AllFiles.ToList(), Does.Contain(XFS.Path(@"C:\test\subtest\file.txt")));
Assert.That(fileSystem.AllFiles.ToList(), Does.Contain(XFS.Path(@"C:\LOUD\SUBLOUD\file.txt")));
Assert.That(fileSystem.AllDirectories.ToList(), Does.Contain(XFS.Path(@"C:\test\subtest\SUBDirectory")));
Assert.That(fileSystem.AllDirectories.ToList(), Does.Contain(XFS.Path(@"C:\LOUD\SUBLOUD\SUBDirectory")));
}
[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.That(fileSystem.AllFiles.ToList(), Does.Contain(XFS.Path(@"C:\test\subtest\new\file.txt")));
Assert.That(fileSystem.AllFiles.ToList(), Does.Contain(XFS.Path(@"C:\LOUD\SUBLOUD\new\file.txt")));
Assert.That(fileSystem.AllDirectories.ToList(), Does.Contain(XFS.Path(@"C:\test\subtest\new\SUBDirectory")));
Assert.That(fileSystem.AllDirectories.ToList(), Does.Contain(XFS.Path(@"C:\LOUD\SUBLOUD\new\SUBDirectory")));
}
[Test]
public void MockFileSystem_AddFile_InitializesMockFileDataFileVersionInfoIfNull()
{
// Arrange
var fileSystem = new MockFileSystem();
// Act
fileSystem.AddFile(XFS.Path(@"C:\file.txt"), string.Empty);
// Assert
IFileVersionInfo fileVersionInfo = fileSystem.FileVersionInfo.GetVersionInfo(XFS.Path(@"C:\file.txt"));
Assert.That(fileVersionInfo, Is.Not.Null);
Assert.That(fileVersionInfo.FileName, Is.EqualTo(XFS.Path(@"C:\file.txt")));
}
[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.That(result.Contents, Is.EqualTo(new UTF8Encoding().GetBytes("This is a test file.")));
}
[Test]
public void MockFileSystem_AddFilesFromEmbeddedResource_ShouldAddAllTheFiles()
{
var fileSystem = new MockFileSystem();
fileSystem.AddFilesFromEmbeddedNamespace(XFS.Path(@"C:\"), Assembly.GetExecutingAssembly(), "System.IO.Abstractions.TestingHelpers.Tests.TestFiles");
Assert.That(fileSystem.AllFiles.ToList(), Does.Contain(XFS.Path(@"C:\TestFile.txt")));
Assert.That(fileSystem.AllFiles.ToList(), Does.Contain(XFS.Path(@"C:\SecondTestFile.txt")));
}
[Test]
public void MockFileSystem_MoveDirectory_MovesDirectoryWithoutRenamingFiles()
{
var fileSystem = new MockFileSystem();
fileSystem.AddFile(XFS.Path(@"C:\dir1\dir1\dir1.txt"), string.Empty);
fileSystem.MoveDirectory(XFS.Path(@"C:\dir1"), XFS.Path(@"C:\dir2"));
var expected = new[] { XFS.Path(@"C:\dir2\dir1\dir1.txt") };
Assert.That(fileSystem.AllFiles, Is.EqualTo(expected));
}
[Test]
public void MockFileSystem_MoveDirectoryAndFile_ShouldMoveCorrectly()
{
var fileSystem = new MockFileSystem();
fileSystem.AddFile(XFS.Path(@"C:\source\project.txt"), string.Empty);
fileSystem.AddFile(XFS.Path(@"C:\source\subdir\other.txt"), string.Empty);
fileSystem.Directory.Move(XFS.Path(@"C:\source"), XFS.Path(@"C:\target"));
fileSystem.File.Move(XFS.Path(@"C:\target\project.txt"), XFS.Path(@"C:\target\proj.txt"));
var expected = new[] { XFS.Path(@"C:\target\proj.txt"), XFS.Path(@"C:\target\subdir\other.txt") };
Assert.That(fileSystem.AllFiles, Is.EqualTo(expected));
}
[Test]
public void MockFileSystem_RemoveFile_RemovesFiles()
{
var fileSystem = new MockFileSystem();
fileSystem.AddFile(@"C:\file.txt", new MockFileData("Content"));
fileSystem.RemoveFile(@"C:\file.txt");
Assert.That(fileSystem.FileExists(@"C:\file.txt"), Is.False);
}
[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"), string.Empty },
{ XFS.Path(@"c:\something\other.gif"), string.Empty },
{ 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.That(result, Is.EqualTo(expectedNodes));
}
[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.That(fileSystem.FileExists(path2), Is.True);
}
[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.New(".");
Assert.That(actualCurrentDirectory.Exists, Is.True);
}
[Test]
public void MockFileSystem_Constructor_ThrowsForNonRootedCurrentDirectory()
{
var ae = Assert.Throws<ArgumentException>(() =>
new MockFileSystem(null, "non-rooted")
);
Assert.That(ae.ParamName, Is.EqualTo("currentDirectory"));
}
[Test]
[WindowsOnly(WindowsSpecifics.Drives)]
public void MockFileSystem_Constructor_ShouldSupportDifferentRootDrives()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
[@"c:\"] = new MockDirectoryData(),
[@"z:\"] = new MockDirectoryData(),
[@"d:\"] = new MockDirectoryData(),
});
var cExists = fileSystem.Directory.Exists(@"c:\");
var zExists = fileSystem.Directory.Exists(@"z:\");
var dExists = fileSystem.Directory.Exists(@"d:\");
Assert.That(fileSystem, Is.Not.Null);
Assert.That(cExists, Is.True);
Assert.That(zExists, Is.True);
Assert.That(dExists, Is.True);
}
[Test]
[WindowsOnly(WindowsSpecifics.Drives)]
public void MockFileSystem_Constructor_ShouldAddDifferentDrivesIfNotExist()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
[@"d:\foo\bar\"] = new MockDirectoryData(),
});
var drivesInfo = fileSystem.DriveInfo.GetDrives();
var fooExists = fileSystem.Directory.Exists(@"d:\foo\");
var barExists = fileSystem.Directory.Exists(@"d:\foo\bar\");
Assert.That(drivesInfo.Any(d => string.Equals(d.Name, @"D:\", StringComparison.InvariantCultureIgnoreCase)), Is.True);
Assert.That(fooExists, Is.True);
Assert.That(barExists, Is.True);
}
[Test]
[WindowsOnly(WindowsSpecifics.Drives)]
public void MockFileSystem_Constructor_ShouldNotDuplicateDrives()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
[@"d:\foo\bar\"] = new MockDirectoryData(),
[@"d:\"] = new MockDirectoryData()
});
var drivesInfo = fileSystem.DriveInfo.GetDrives();
Assert.That(drivesInfo.Where(d => string.Equals(d.Name, @"D:\", StringComparison.InvariantCultureIgnoreCase)), Has.Exactly(1).Items);
}
[Test]
public void MockFileSystem_DefaultState_DefaultTempDirectoryExists()
{
var tempDirectory = XFS.Path(@"C:\temp");
var mockFileSystem = new MockFileSystem();
var mockFileSystemOverload = new MockFileSystem(null, string.Empty);
Assert.That(mockFileSystem.Directory.Exists(tempDirectory), Is.True);
Assert.That(mockFileSystemOverload.Directory.Exists(tempDirectory), Is.True);
}
[Test]
public void MockFileSystem_FileSystemWatcher_Can_Be_Overridden()
{
var path = XFS.Path(@"C:\root");
var fileSystem = new TestFileSystem(new TestFileSystemWatcherFactory());
var watcher = fileSystem.FileSystemWatcher.New(path);
Assert.That(watcher.Path, Is.EqualTo(path));
}
[Test]
public void MockFileSystem_DeleteDirectoryRecursive_WithReadOnlyFile_ShouldThrowUnauthorizedException()
{
string baseDirectory = XFS.Path(@"C:\Test");
string textFile = XFS.Path(@"C:\Test\file.txt");
var fileSystem = new MockFileSystem();
fileSystem.AddFile(baseDirectory, new MockFileData(string.Empty));
fileSystem.AddFile(textFile, new MockFileData("Content"));
fileSystem.File.SetAttributes(textFile, FileAttributes.ReadOnly);
TestDelegate action = () => fileSystem.Directory.Delete(baseDirectory, true);
Assert.Throws<UnauthorizedAccessException>(action);
Assert.That(fileSystem.File.Exists(textFile), Is.True);
Assert.That(fileSystem.Directory.Exists(baseDirectory), Is.True);
}
private class TestFileSystem : MockFileSystem
{
private readonly IFileSystemWatcherFactory fileSystemWatcherFactory;
public TestFileSystem(IFileSystemWatcherFactory fileSystemWatcherFactory)
{
this.fileSystemWatcherFactory = fileSystemWatcherFactory;
}
public override IFileSystemWatcherFactory FileSystemWatcher => fileSystemWatcherFactory;
}
private class TestFileSystemWatcherFactory : IFileSystemWatcherFactory
{
public IFileSystemWatcher CreateNew() => New();
public IFileSystemWatcher CreateNew(string path) => New(path);
public IFileSystemWatcher CreateNew(string path, string filter) => New(path, filter);
public IFileSystemWatcher New()
=> new TestFileSystemWatcher(null);
public IFileSystemWatcher New(string path)
=> new TestFileSystemWatcher(path);
public IFileSystemWatcher New(string path, string filter)
=> new TestFileSystemWatcher(path, filter);
public IFileSystemWatcher Wrap(FileSystemWatcher fileSystemWatcher)
=> new TestFileSystemWatcher(fileSystemWatcher.Path, fileSystemWatcher.Filter);
public IFileSystemWatcher FromPath(string path) => new TestFileSystemWatcher(path);
public IFileSystem FileSystem => null!;
}
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 IFileSystem FileSystem { get; }
public override bool IncludeSubdirectories { get; set; }
public override IContainer Container { get; }
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 IWaitForChangedResult WaitForChanged(WatcherChangeTypes changeType)
{
_ = changeType;
return default(IWaitForChangedResult);
}
public override IWaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout)
{
_ = changeType;
_ = timeout;
return default(IWaitForChangedResult);
}
#if FEATURE_FILE_SYSTEM_WATCHER_WAIT_WITH_TIMESPAN
public override IWaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, TimeSpan timeout)
{
_ = changeType;
_ = timeout;
return default(IWaitForChangedResult);
}
#endif
}
}
}