forked from TestableIO/System.IO.Abstractions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockFileTests.cs
More file actions
749 lines (617 loc) · 24.2 KB
/
MockFileTests.cs
File metadata and controls
749 lines (617 loc) · 24.2 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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using NUnit.Framework;
namespace System.IO.Abstractions.TestingHelpers.Tests;
using XFS = MockUnixSupport;
[TestFixture]
public class MockFileTests
{
[Test]
public async Task MockFile_Constructor_ShouldThrowArgumentNullExceptionIfMockFileDataAccessorIsNull()
{
// Arrange
// nothing to do
// Act
Action action = () => new MockFile(null);
// Assert
await That(action).Throws<ArgumentNullException>();
}
[Test]
public async Task MockFile_GetSetCreationTime_ShouldPersist()
{
// Arrange
string path = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var creationTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetCreationTime(path, creationTime);
var result = file.GetCreationTime(path);
// Assert
await That(result).IsEqualTo(creationTime);
}
[Test]
public async Task MockFile_SetCreationTimeUtc_ShouldAffectCreationTime()
{
// Arrange
string path = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var creationTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetCreationTimeUtc(path, creationTime.ToUniversalTime());
var result = file.GetCreationTime(path);
// Assert
await That(result).IsEqualTo(creationTime);
}
[Test]
public async Task MockFile_SetCreationTime_ShouldAffectCreationTimeUtc()
{
// Arrange
string path = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var creationTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetCreationTime(path, creationTime);
var result = file.GetCreationTimeUtc(path);
// Assert
await That(result).IsEqualTo(creationTime.ToUniversalTime());
}
[Test]
public async Task MockFile_GetSetLastAccessTime_ShouldPersist()
{
// Arrange
string path = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var lastAccessTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetLastAccessTime(path, lastAccessTime);
var result = file.GetLastAccessTime(path);
// Assert
await That(result).IsEqualTo(lastAccessTime);
}
[Test]
public async Task MockFile_SetLastAccessTimeUtc_ShouldAffectLastAccessTime()
{
// Arrange
string path = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var lastAccessTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetLastAccessTimeUtc(path, lastAccessTime.ToUniversalTime());
var result = file.GetLastAccessTime(path);
// Assert
await That(result).IsEqualTo(lastAccessTime);
}
[Test]
public async Task MockFile_SetLastAccessTime_ShouldAffectLastAccessTimeUtc()
{
// Arrange
string path = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var lastAccessTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetLastAccessTime(path, lastAccessTime);
var result = file.GetLastAccessTimeUtc(path);
// Assert
await That(result).IsEqualTo(lastAccessTime.ToUniversalTime());
}
[Test]
public async Task MockFile_GetSetLastWriteTime_ShouldPersist()
{
// Arrange
string path = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var lastWriteTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetLastWriteTime(path, lastWriteTime);
var result = file.GetLastWriteTime(path);
// Assert
await That(result).IsEqualTo(lastWriteTime);
}
static async Task ExecuteDefaultValueTest(Func<MockFile, string, DateTime> getDateValue)
{
var expected = new DateTime(1601, 01, 01, 00, 00, 00, DateTimeKind.Utc);
string path = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem();
var file = new MockFile(fileSystem);
var actual = getDateValue(file, path);
await That(actual.ToUniversalTime()).IsEqualTo(expected);
}
[Test]
public async Task MockFile_GetLastWriteTimeOfNonExistentFile_ShouldReturnDefaultValue()
{
await ExecuteDefaultValueTest((f, p) => f.GetLastWriteTime(p));
}
[Test]
public async Task MockFile_GetLastWriteTimeUtcOfNonExistentFile_ShouldReturnDefaultValue()
{
await ExecuteDefaultValueTest((f, p) => f.GetLastWriteTimeUtc(p));
}
[Test]
public async Task MockFile_GetLastAccessTimeUtcOfNonExistentFile_ShouldReturnDefaultValue()
{
await ExecuteDefaultValueTest((f, p) => f.GetLastAccessTimeUtc(p));
}
[Test]
public async Task MockFile_GetLastAccessTimeOfNonExistentFile_ShouldReturnDefaultValue()
{
await ExecuteDefaultValueTest((f, p) => f.GetLastAccessTime(p));
}
[Test]
public async Task MockFile_GetAttributeOfNonExistentFileButParentDirectoryExists_ShouldThrowOneFileNotFoundException()
{
// Arrange
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(XFS.Path(@"c:\something"));
// Act
Action action = () => fileSystem.File.GetAttributes(XFS.Path(@"c:\something\demo.txt"));
// Assert
await That(action).Throws<FileNotFoundException>();
}
[Test]
public async Task MockFile_GetAttributeOfNonExistentFile_ShouldThrowOneDirectoryNotFoundException()
{
// Arrange
var fileSystem = new MockFileSystem();
// Act
Action action = () => fileSystem.File.GetAttributes(XFS.Path(@"c:\something\demo.txt"));
// Assert
await That(action).Throws<DirectoryNotFoundException>();
}
[Test]
public async Task MockFile_GetAttributeOfExistingFile_ShouldReturnCorrectValue()
{
var filedata = new MockFileData("test")
{
Attributes = FileAttributes.Hidden
};
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"c:\something\demo.txt"), filedata }
});
var attributes = fileSystem.File.GetAttributes(XFS.Path(@"c:\something\demo.txt"));
await That(attributes).IsEqualTo(FileAttributes.Hidden);
}
[Test]
[WindowsOnly(WindowsSpecifics.UNCPaths)]
public async Task MockFile_GetAttributeOfExistingUncDirectory_ShouldReturnCorrectValue()
{
var filedata = new MockFileData("test");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"\\share\folder\demo.txt"), filedata }
});
var attributes = fileSystem.File.GetAttributes(XFS.Path(@"\\share\folder"));
await That(attributes).IsEqualTo(FileAttributes.Directory);
}
[Test]
public async Task MockFile_GetAttributeWithEmptyParameter_ShouldThrowOneArgumentException()
{
// Arrange
var fileSystem = new MockFileSystem();
// Act
Action action = () => fileSystem.File.GetAttributes(string.Empty);
// Assert
var exception = await That(action).Throws<ArgumentException>();
await That(exception.Message).StartsWith("The path is not of a legal form.");
}
[Test]
public async Task MockFile_GetAttributeWithIllegalParameter_ShouldThrowOneArgumentException()
{
// Arrange
var fileSystem = new MockFileSystem();
// Act
Action action = () => fileSystem.File.GetAttributes(string.Empty);
// Assert
// Note: The actual type of the exception differs from the documentation.
// According to the documentation it should be of type NotSupportedException.
await That(action).Throws<ArgumentException>();
}
[Test]
public async Task MockFile_GetCreationTimeOfNonExistentFile_ShouldReturnDefaultValue()
{
await ExecuteDefaultValueTest((f, p) => f.GetCreationTime(p));
}
[Test]
public async Task MockFile_GetCreationTimeUtcOfNonExistentFile_ShouldReturnDefaultValue()
{
await ExecuteDefaultValueTest((f, p) => f.GetCreationTimeUtc(p));
}
[Test]
public async Task MockFile_SetLastWriteTimeUtc_ShouldAffectLastWriteTime()
{
// Arrange
string path = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var lastWriteTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetLastWriteTimeUtc(path, lastWriteTime.ToUniversalTime());
var result = file.GetLastWriteTime(path);
// Assert
await That(result).IsEqualTo(lastWriteTime);
}
[Test]
public async Task MockFile_SetLastWriteTime_ShouldAffectLastWriteTimeUtc()
{
// Arrange
string path = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
// Act
var lastWriteTime = new DateTime(2010, 6, 4, 13, 26, 42);
file.SetLastWriteTime(path, lastWriteTime);
var result = file.GetLastWriteTimeUtc(path);
// Assert
await That(result).IsEqualTo(lastWriteTime.ToUniversalTime());
}
[Test]
public async Task MockFile_ReadAllText_ShouldReturnOriginalTextData()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"c:\something\demo.txt"), new MockFileData("Demo text content") },
{ XFS.Path(@"c:\something\other.gif"), new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) }
});
var file = new MockFile(fileSystem);
// Act
var result = file.ReadAllText(XFS.Path(@"c:\something\demo.txt"));
// Assert
await That(result).IsEqualTo("Demo text content");
}
[Test]
public async Task MockFile_ReadAllText_ShouldReturnOriginalDataWithCustomEncoding()
{
// Arrange
string text = "Hello there!";
var encodedText = Encoding.BigEndianUnicode.GetBytes(text);
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"c:\something\demo.txt"), new MockFileData(encodedText) }
});
var file = new MockFile(fileSystem);
// Act
var result = file.ReadAllText(XFS.Path(@"c:\something\demo.txt"), Encoding.BigEndianUnicode);
// Assert
await That(result).IsEqualTo(text);
}
public static IEnumerable<Encoding> GetEncodingsForReadAllText()
{
// little endian
yield return new UTF32Encoding(false, true, true);
// big endian
yield return new UTF32Encoding(true, true, true);
yield return new UTF8Encoding(true, true);
yield return new ASCIIEncoding();
}
[TestCaseSource(typeof(MockFileTests), nameof(GetEncodingsForReadAllText))]
public async Task MockFile_ReadAllText_ShouldReturnTheOriginalContentWhenTheFileContainsDifferentEncodings(Encoding encoding)
{
// Arrange
string text = "Hello there!";
var encodedText = encoding.GetPreamble().Concat(encoding.GetBytes(text)).ToArray();
var path = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData(encodedText) }
});
// Act
var actualText = fileSystem.File.ReadAllText(path);
// Assert
await That(actualText).IsEqualTo(text);
}
[Test]
public async Task MockFile_OpenWrite_ShouldCreateNewFiles()
{
string filePath = XFS.Path(@"c:\something\demo.txt");
string fileContent = "this is some content";
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(XFS.Path(@"c:\something"));
var bytes = new UTF8Encoding(true).GetBytes(fileContent);
var stream = fileSystem.File.OpenWrite(filePath);
stream.Write(bytes, 0, bytes.Length);
stream.Dispose();
await That(fileSystem.FileExists(filePath)).IsTrue();
await That(fileSystem.GetFile(filePath).TextContents).IsEqualTo(fileContent);
}
[Test]
public async Task MockFile_OpenWrite_ShouldNotCreateFolders()
{
string filePath = XFS.Path(@"c:\something\demo.txt"); // c:\something does not exist: OpenWrite should fail
var fileSystem = new MockFileSystem();
await That(() => fileSystem.File.OpenWrite(filePath)).Throws<DirectoryNotFoundException>();
}
[Test]
public async Task MockFile_OpenWrite_ShouldOverwriteExistingFiles()
{
string filePath = XFS.Path(@"c:\something\demo.txt");
string startFileContent = "this is some content";
string endFileContent = "this is some other content";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{filePath, new MockFileData(startFileContent)}
});
var bytes = new UTF8Encoding(true).GetBytes(endFileContent);
var stream = fileSystem.File.OpenWrite(filePath);
stream.Write(bytes, 0, bytes.Length);
stream.Dispose();
await That(fileSystem.FileExists(filePath)).IsTrue();
await That(fileSystem.GetFile(filePath).TextContents).IsEqualTo(endFileContent);
}
[Test]
public async Task MockFile_Delete_ShouldRemoveFileFromFileSystem()
{
string fullPath = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ fullPath, new MockFileData("Demo text content") }
});
var file = new MockFile(fileSystem);
file.Delete(fullPath);
await That(fileSystem.FileExists(fullPath)).IsFalse();
}
[Test]
public async Task MockFile_Delete_Should_RemoveFiles()
{
string filePath = XFS.Path(@"c:\something\demo.txt");
string fileContent = "this is some content";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { filePath, new MockFileData(fileContent) } });
await That(fileSystem.AllFiles.Count()).IsEqualTo(1);
fileSystem.File.Delete(filePath);
await That(fileSystem.AllFiles.Count()).IsEqualTo(0);
}
[Test]
public async Task MockFile_Delete_No_File_Does_Nothing()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()
{
{ XFS.Path(@"c:\something\exist.txt"), new MockFileData("Demo text content") },
});
string filePath = XFS.Path(@"c:\something\not_exist.txt");
await That(() => fileSystem.File.Delete(filePath)).DoesNotThrow();
}
[Test]
public async Task MockFile_Delete_ShouldThrowUnauthorizedAccessException_WhenPathIsADirectory()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"c:\bar"), new MockDirectoryData() },
});
// Act
Action action = () => fileSystem.File.Delete(XFS.Path(@"c:\bar"));
// Assert
await That(action).Throws<UnauthorizedAccessException>();
}
[Test]
public async Task MockFile_AppendText_AppendTextToAnExistingFile()
{
string filepath = XFS.Path(@"c:\something\does\exist.txt");
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ filepath, new MockFileData("I'm here. ") }
});
var stream = filesystem.File.AppendText(filepath);
stream.Write("Me too!");
stream.Flush();
stream.Dispose();
var file = filesystem.GetFile(filepath);
await That(file.TextContents).IsEqualTo("I'm here. Me too!");
}
[Test]
public async Task MockFile_AppendText_CreatesNewFileForAppendToNonExistingFile()
{
string filepath = XFS.Path(@"c:\something\doesnt\exist.txt");
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>());
filesystem.AddDirectory(XFS.Path(@"c:\something\doesnt"));
var stream = filesystem.File.AppendText(filepath);
stream.Write("New too!");
stream.Flush();
stream.Dispose();
var file = filesystem.GetFile(filepath);
await That(file.TextContents).IsEqualTo("New too!");
await That(filesystem.FileExists(filepath)).IsTrue();
}
#if !NET9_0_OR_GREATER
[Test]
public void Serializable_works()
{
//Arrange
MockFileData data = new MockFileData("Text Contents");
//Act
#pragma warning disable SYSLIB0011
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
formatter.Serialize(stream, data);
#pragma warning restore SYSLIB0011
//Assert
Assert.Pass();
}
#endif
#if !NET9_0_OR_GREATER
[Test]
public async Task Serializable_can_deserialize()
{
//Arrange
string textContentStr = "Text Contents";
//Act
MockFileData data = new MockFileData(textContentStr);
#pragma warning disable SYSLIB0011
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
formatter.Serialize(stream, data);
stream.Seek(0, SeekOrigin.Begin);
MockFileData deserialized = (MockFileData)formatter.Deserialize(stream);
#pragma warning restore SYSLIB0011
//Assert
await That(deserialized.TextContents).IsEqualTo(textContentStr);
}
#endif
[Test]
public async Task MockFile_Encrypt_ShouldSetEncryptedAttribute()
{
// Arrange
var fileData = new MockFileData("Demo text content");
var filePath = XFS.Path(@"c:\a.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{filePath, fileData }
});
// Act
#pragma warning disable CA1416
fileSystem.File.Encrypt(filePath);
#pragma warning restore CA1416
var attributes = fileSystem.File.GetAttributes(filePath);
// Assert
await That(attributes & FileAttributes.Encrypted).IsEqualTo(FileAttributes.Encrypted);
}
[Test]
public async Task MockFile_Decrypt_ShouldRemoveEncryptedAttribute()
{
// Arrange
const string Content = "Demo text content";
var fileData = new MockFileData(Content);
var filePath = XFS.Path(@"c:\a.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{filePath, fileData }
});
#pragma warning disable CA1416
fileSystem.File.Encrypt(filePath);
#pragma warning restore CA1416
// Act
#pragma warning disable CA1416
fileSystem.File.Decrypt(filePath);
#pragma warning restore CA1416
var attributes = fileSystem.File.GetAttributes(filePath);
// Assert
await That(attributes & FileAttributes.Encrypted).IsNotEqualTo(FileAttributes.Encrypted);
}
[Test]
public async Task MockFile_Replace_ShouldReplaceFileContents()
{
// Arrange
var fileSystem = new MockFileSystem();
var path1 = XFS.Path(@"c:\temp\file1.txt");
var path2 = XFS.Path(@"c:\temp\file2.txt");
fileSystem.AddFile(path1, new MockFileData("1"));
fileSystem.AddFile(path2, new MockFileData("2"));
// Act
fileSystem.File.Replace(path1, path2, null);
await That(fileSystem.File.ReadAllText(path2)).IsEqualTo("1");
}
[Test]
public async Task MockFile_Replace_ShouldCreateBackup()
{
// Arrange
var fileSystem = new MockFileSystem();
var path1 = XFS.Path(@"c:\temp\file1.txt");
var path2 = XFS.Path(@"c:\temp\file2.txt");
var path3 = XFS.Path(@"c:\temp\file3.txt");
fileSystem.AddFile(path1, new MockFileData("1"));
fileSystem.AddFile(path2, new MockFileData("2"));
// Act
fileSystem.File.Replace(path1, path2, path3);
await That(fileSystem.File.ReadAllText(path3)).IsEqualTo("2");
}
[Test]
public async Task MockFile_Replace_ShouldThrowIfDirectoryOfBackupPathDoesNotExist()
{
// Arrange
var fileSystem = new MockFileSystem();
var path1 = XFS.Path(@"c:\temp\file1.txt");
var path2 = XFS.Path(@"c:\temp\file2.txt");
var path3 = XFS.Path(@"c:\temp\subdirectory\file3.txt");
fileSystem.AddFile(path1, new MockFileData("1"));
fileSystem.AddFile(path2, new MockFileData("2"));
// Act
await That(() => fileSystem.File.Replace(path1, path2, path3)).Throws<DirectoryNotFoundException>();
}
[Test]
public async Task MockFile_Replace_ShouldThrowIfSourceFileDoesNotExist()
{
// Arrange
var fileSystem = new MockFileSystem();
var path1 = XFS.Path(@"c:\temp\file1.txt");
var path2 = XFS.Path(@"c:\temp\file2.txt");
fileSystem.AddFile(path2, new MockFileData("2"));
await That(() => fileSystem.File.Replace(path1, path2, null)).Throws<FileNotFoundException>();
}
[Test]
public async Task MockFile_Replace_ShouldThrowIfDestinationFileDoesNotExist()
{
// Arrange
var fileSystem = new MockFileSystem();
var path1 = XFS.Path(@"c:\temp\file1.txt");
var path2 = XFS.Path(@"c:\temp\file2.txt");
fileSystem.AddFile(path1, new MockFileData("1"));
await That(() => fileSystem.File.Replace(path1, path2, null)).Throws<FileNotFoundException>();
}
[Test]
public async Task MockFile_OpenRead_ShouldReturnReadOnlyStream()
{
// Tests issue #230
// Arrange
string filePath = XFS.Path(@"c:\something\demo.txt");
string startContent = "hi there";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ filePath, new MockFileData(startContent) }
});
// Act
var stream = fileSystem.File.OpenRead(filePath);
// Assert
await That(stream.CanWrite).IsFalse();
await That(() => stream.WriteByte(0)).Throws<NotSupportedException>();
}
[Test]
[WindowsOnly(WindowsSpecifics.Drives)]
public async Task MockFile_Replace_SameSourceAndDestination_ShouldThrowIOException()
{
string sourceFilePath = @"c:\something\demo.txt";
string destFilePath = @"c:\something\Demo.txt";
string fileContent = "content";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ sourceFilePath, new MockFileData(fileContent) }
});
await That(() => fileSystem.File.Replace(sourceFilePath, destFilePath, null, true)).Throws<IOException>().HasMessage("The process cannot access the file because it is being used by another process.");
}
}