-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathMockFile.cs
1414 lines (1210 loc) · 57 KB
/
MockFile.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
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.Win32.SafeHandles;
namespace System.IO.Abstractions.TestingHelpers;
using XFS = MockUnixSupport;
/// <inheritdoc />
#if FEATURE_SERIALIZABLE
[Serializable]
#endif
public partial class MockFile : FileBase
{
private readonly IMockFileDataAccessor mockFileDataAccessor;
/// <inheritdoc />
public MockFile(IMockFileDataAccessor mockFileDataAccessor) : base(mockFileDataAccessor?.FileSystem)
{
this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
}
#if FEATURE_FILE_SPAN
/// <inheritdoc cref="IFile.AppendAllBytes(string,byte[])"/>
public override void AppendAllBytes(string path, byte[] bytes)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
if (!mockFileDataAccessor.FileExists(path))
{
VerifyDirectoryExists(path);
mockFileDataAccessor.AddFile(path, mockFileDataAccessor.AdjustTimes(new MockFileData(bytes), TimeAdjustments.All));
}
else
{
var file = mockFileDataAccessor.GetFile(path);
file.CheckFileAccess(path, FileAccess.Write);
mockFileDataAccessor.AdjustTimes(file, TimeAdjustments.LastAccessTime | TimeAdjustments.LastWriteTime);
file.Contents = file.Contents.Concat(bytes).ToArray();
}
}
/// <inheritdoc cref="IFile.AppendAllBytes(string,ReadOnlySpan{byte})"/>
public override void AppendAllBytes(string path, ReadOnlySpan<byte> bytes)
{
AppendAllBytes(path, bytes.ToArray());
}
#endif
/// <inheritdoc />
public override void AppendAllLines(string path, IEnumerable<string> contents)
{
AppendAllLines(path, contents, MockFileData.DefaultEncoding);
}
/// <inheritdoc />
public override void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
VerifyValueIsNotNull(contents, nameof(contents));
VerifyValueIsNotNull(encoding, nameof(encoding));
var concatContents = contents.Aggregate("", (a, b) => a + b + Environment.NewLine);
AppendAllText(path, concatContents, encoding);
}
/// <inheritdoc />
public override void AppendAllText(string path, string contents)
{
AppendAllText(path, contents, MockFileData.DefaultEncoding);
}
/// <inheritdoc />
public override void AppendAllText(string path, string contents, Encoding encoding)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
if (encoding == null)
{
throw new ArgumentNullException(nameof(encoding));
}
if (!mockFileDataAccessor.FileExists(path))
{
VerifyDirectoryExists(path);
mockFileDataAccessor.AddFile(path, mockFileDataAccessor.AdjustTimes(new MockFileData(contents, encoding), TimeAdjustments.All));
}
else
{
var file = mockFileDataAccessor.GetFile(path);
file.CheckFileAccess(path, FileAccess.Write);
mockFileDataAccessor.AdjustTimes(file, TimeAdjustments.LastAccessTime | TimeAdjustments.LastWriteTime);
var bytesToAppend = encoding.GetBytes(contents);
file.Contents = file.Contents.Concat(bytesToAppend).ToArray();
}
}
#if FEATURE_FILE_SPAN
/// <inheritdoc cref="IFile.AppendAllText(string,ReadOnlySpan{char})"/>
public override void AppendAllText(string path, ReadOnlySpan<char> contents)
{
AppendAllText(path, contents.ToString());
}
/// <inheritdoc cref="IFile.AppendAllText(string,ReadOnlySpan{char},Encoding)"/>
public override void AppendAllText(string path, ReadOnlySpan<char> contents, Encoding encoding)
{
AppendAllText(path, contents.ToString(), encoding);
}
#endif
/// <inheritdoc />
public override StreamWriter AppendText(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
if (mockFileDataAccessor.FileExists(path))
{
StreamWriter sw = new StreamWriter(OpenWrite(path));
sw.BaseStream.Seek(0, SeekOrigin.End); //push the stream pointer at the end for append.
return sw;
}
return new StreamWriter(Create(path));
}
/// <inheritdoc />
public override void Copy(string sourceFileName, string destFileName)
{
Copy(sourceFileName, destFileName, false);
}
/// <inheritdoc />
public override void Copy(string sourceFileName, string destFileName, bool overwrite)
{
if (sourceFileName == null)
{
throw CommonExceptions.FilenameCannotBeNull(nameof(sourceFileName));
}
if (destFileName == null)
{
throw CommonExceptions.FilenameCannotBeNull(nameof(destFileName));
}
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, nameof(sourceFileName));
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, nameof(destFileName));
if (!Exists(sourceFileName))
{
throw CommonExceptions.FileNotFound(sourceFileName);
}
VerifyDirectoryExists(destFileName);
var fileExists = mockFileDataAccessor.FileExists(destFileName);
if (fileExists)
{
if (!overwrite)
{
throw CommonExceptions.FileAlreadyExists(destFileName);
}
if (string.Equals(sourceFileName, destFileName, StringComparison.OrdinalIgnoreCase) && XFS.IsWindowsPlatform())
{
throw CommonExceptions.ProcessCannotAccessFileInUse(destFileName);
}
mockFileDataAccessor.RemoveFile(destFileName);
}
var sourceFileData = mockFileDataAccessor.GetFile(sourceFileName);
sourceFileData.CheckFileAccess(sourceFileName, FileAccess.Read);
var destFileData = new MockFileData(sourceFileData);
mockFileDataAccessor.AdjustTimes(destFileData, TimeAdjustments.CreationTime | TimeAdjustments.LastAccessTime);
mockFileDataAccessor.AddFile(destFileName, destFileData);
}
/// <inheritdoc />
public override FileSystemStream Create(string path) =>
Create(path, 4096);
/// <inheritdoc />
public override FileSystemStream Create(string path, int bufferSize) =>
Create(path, bufferSize, FileOptions.None);
/// <inheritdoc />
public override FileSystemStream Create(string path, int bufferSize, FileOptions options) =>
CreateInternal(path, FileAccess.ReadWrite, options);
private FileSystemStream CreateInternal(string path, FileAccess access, FileOptions options)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path), "Path cannot be null.");
}
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, nameof(path));
VerifyDirectoryExists(path);
var mockFileData = new MockFileData(new byte[0]);
mockFileDataAccessor.AdjustTimes(mockFileData, TimeAdjustments.All);
mockFileDataAccessor.AddFile(path, mockFileData);
return OpenInternal(path, FileMode.Open, access, options);
}
#if FEATURE_CREATE_SYMBOLIC_LINK
/// <inheritdoc />
public override IFileSystemInfo CreateSymbolicLink(string path, string pathToTarget)
{
if (path == null)
{
throw CommonExceptions.FilenameCannotBeNull(nameof(path));
}
if (pathToTarget == null)
{
throw CommonExceptions.FilenameCannotBeNull(nameof(pathToTarget));
}
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, nameof(path));
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(pathToTarget, nameof(pathToTarget));
if (Exists(path))
{
throw CommonExceptions.FileAlreadyExists(nameof(path));
}
VerifyDirectoryExists(path);
var fileExists = mockFileDataAccessor.FileExists(pathToTarget);
if (!fileExists)
{
throw CommonExceptions.FileNotFound(pathToTarget);
}
var sourceFileData = mockFileDataAccessor.GetFile(pathToTarget);
sourceFileData.CheckFileAccess(pathToTarget, FileAccess.Read);
var destFileData = new MockFileData(new byte[0]);
mockFileDataAccessor.AdjustTimes(destFileData, TimeAdjustments.CreationTime | TimeAdjustments.LastAccessTime);
destFileData.LinkTarget = pathToTarget;
mockFileDataAccessor.AddFile(path, destFileData);
var mockFileInfo = new MockFileInfo(mockFileDataAccessor, path);
mockFileInfo.Attributes |= FileAttributes.ReparsePoint;
return mockFileInfo;
}
#endif
/// <inheritdoc />
public override StreamWriter CreateText(string path)
{
return new StreamWriter(Create(path));
}
/// <inheritdoc />
public override void Decrypt(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
new MockFileInfo(mockFileDataAccessor, path).Decrypt();
}
/// <inheritdoc />
public override void Delete(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
// We mimic exact behavior of the standard File.Delete() method
// which throws exception only if the folder does not exist,
// but silently returns if deleting a non-existing file in an existing folder.
VerifyDirectoryExists(path);
var file = mockFileDataAccessor.GetFile(path);
if (file != null && !file.AllowedFileShare.HasFlag(FileShare.Delete))
{
throw CommonExceptions.ProcessCannotAccessFileInUse(path);
}
if (file != null && file.IsDirectory)
{
throw new UnauthorizedAccessException($"Access to the path '{path}' is denied.");
}
mockFileDataAccessor.RemoveFile(path);
}
/// <inheritdoc />
public override void Encrypt(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
new MockFileInfo(mockFileDataAccessor, path).Encrypt();
}
/// <inheritdoc />
public override bool Exists(string path)
{
if (path == null)
{
return false;
}
if (path.Trim() == string.Empty)
{
return false;
}
//Not handling exceptions here so that mock behaviour is as similar as possible to System.IO.File.Exists (See #810)
try
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, nameof(path));
var file = mockFileDataAccessor.GetFile(path);
return file != null && !file.IsDirectory;
}
catch (ArgumentException) { }
catch (NotSupportedException) { }
catch (IOException) { }
catch (UnauthorizedAccessException) { }
return false;
}
/// <summary>
/// Gets the <see cref="FileAttributes"/> of the file on the path.
/// </summary>
/// <param name="path">The path to the file.</param>
/// <returns>The <see cref="FileAttributes"/> of the file on the path.</returns>
/// <exception cref="ArgumentException"><paramref name="path"/> is empty, contains only white spaces, or contains invalid characters.</exception>
/// <exception cref="PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.</exception>
/// <exception cref="NotSupportedException"><paramref name="path"/> is in an invalid format.</exception>
/// <exception cref="FileNotFoundException"><paramref name="path"/> represents a file and is invalid, such as being on an unmapped drive, or the file cannot be found.</exception>
/// <exception cref="DirectoryNotFoundException"><paramref name="path"/> represents a directory and is invalid, such as being on an unmapped drive, or the directory cannot be found.</exception>
/// <exception cref="IOException">This file is being used by another process.</exception>
/// <exception cref="UnauthorizedAccessException">The caller does not have the required permission.</exception>
public override FileAttributes GetAttributes(string path)
{
if (path != null && path.Length == 0)
{
throw CommonExceptions.PathIsNotOfALegalForm(nameof(path));
}
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
var possibleFileData = mockFileDataAccessor.GetFile(path);
FileAttributes result;
if (possibleFileData != null)
{
result = possibleFileData.Attributes;
}
else
{
var directoryInfo = mockFileDataAccessor.DirectoryInfo.New(path);
if (directoryInfo.Exists)
{
result = directoryInfo.Attributes;
}
else
{
VerifyDirectoryExists(path);
throw CommonExceptions.FileNotFound(path);
}
}
return result;
}
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE
/// <inheritdoc />
public override FileAttributes GetAttributes(SafeFileHandle fileHandle)
{
throw CommonExceptions.NotImplemented();
}
#endif
/// <inheritdoc />
public override DateTime GetCreationTime(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
return GetTimeFromFile(path, data => data.CreationTime.LocalDateTime, () => MockFileData.DefaultDateTimeOffset.LocalDateTime);
}
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE
/// <inheritdoc />
public override DateTime GetCreationTime(SafeFileHandle fileHandle)
{
throw CommonExceptions.NotImplemented();
}
#endif
/// <inheritdoc />
public override DateTime GetCreationTimeUtc(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
return GetTimeFromFile(path, data => data.CreationTime.UtcDateTime, () => MockFileData.DefaultDateTimeOffset.UtcDateTime);
}
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE
/// <inheritdoc />
public override DateTime GetCreationTimeUtc(SafeFileHandle fileHandle)
{
throw CommonExceptions.NotImplemented();
}
#endif
/// <inheritdoc />
public override DateTime GetLastAccessTime(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
return GetTimeFromFile(path, data => data.LastAccessTime.LocalDateTime, () => MockFileData.DefaultDateTimeOffset.LocalDateTime);
}
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE
/// <inheritdoc />
public override DateTime GetLastAccessTime(SafeFileHandle fileHandle)
{
throw CommonExceptions.NotImplemented();
}
#endif
/// <inheritdoc />
public override DateTime GetLastAccessTimeUtc(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
return GetTimeFromFile(path, data => data.LastAccessTime.UtcDateTime, () => MockFileData.DefaultDateTimeOffset.UtcDateTime);
}
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE
/// <inheritdoc />
public override DateTime GetLastAccessTimeUtc(SafeFileHandle fileHandle)
{
throw CommonExceptions.NotImplemented();
}
#endif
/// <inheritdoc />
public override DateTime GetLastWriteTime(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
return GetTimeFromFile(path, data => data.LastWriteTime.LocalDateTime, () => MockFileData.DefaultDateTimeOffset.LocalDateTime);
}
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE
/// <inheritdoc />
public override DateTime GetLastWriteTime(SafeFileHandle fileHandle)
{
throw CommonExceptions.NotImplemented();
}
#endif
/// <inheritdoc />
public override DateTime GetLastWriteTimeUtc(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
return GetTimeFromFile(path, data => data.LastWriteTime.UtcDateTime, () => MockFileData.DefaultDateTimeOffset.UtcDateTime);
}
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE
/// <inheritdoc />
public override DateTime GetLastWriteTimeUtc(SafeFileHandle fileHandle)
{
throw CommonExceptions.NotImplemented();
}
#endif
#if FEATURE_UNIX_FILE_MODE
/// <inheritdoc />
public override UnixFileMode GetUnixFileMode(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
if (!mockFileDataAccessor.FileExists(path))
{
throw CommonExceptions.FileNotFound(path);
}
var mockFileData = mockFileDataAccessor.GetFile(path);
return mockFileData.UnixMode;
}
#endif
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE
/// <inheritdoc />
public override UnixFileMode GetUnixFileMode(SafeFileHandle fileHandle)
{
throw CommonExceptions.NotImplemented();
}
#endif
private DateTime GetTimeFromFile(string path, Func<MockFileData, DateTime> existingFileFunction, Func<DateTime> nonExistingFileFunction)
{
DateTime result;
MockFileData file = mockFileDataAccessor.GetFile(path);
if (file != null)
{
result = existingFileFunction(file);
}
else
{
result = nonExistingFileFunction();
}
return result;
}
/// <inheritdoc />
public override void Move(string sourceFileName, string destFileName)
{
if (sourceFileName == null)
{
throw CommonExceptions.FilenameCannotBeNull(nameof(sourceFileName));
}
if (destFileName == null)
{
throw CommonExceptions.FilenameCannotBeNull(nameof(destFileName));
}
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, nameof(sourceFileName));
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, nameof(destFileName));
var sourceFile = mockFileDataAccessor.GetFile(sourceFileName);
if (sourceFile == null)
{
throw CommonExceptions.FileNotFound(sourceFileName);
}
if (!sourceFile.AllowedFileShare.HasFlag(FileShare.Delete))
{
throw CommonExceptions.ProcessCannotAccessFileInUse();
}
VerifyDirectoryExists(destFileName);
if (mockFileDataAccessor.GetFile(destFileName) != null)
{
if (mockFileDataAccessor.StringOperations.Equals(destFileName, sourceFileName))
{
if (XFS.IsWindowsPlatform())
{
mockFileDataAccessor.RemoveFile(sourceFileName);
mockFileDataAccessor.AddFile(destFileName, mockFileDataAccessor.AdjustTimes(new MockFileData(sourceFile), TimeAdjustments.LastAccessTime), false);
}
return;
}
else
{
throw new IOException("A file can not be created if it already exists.");
}
}
mockFileDataAccessor.RemoveFile(sourceFileName, false);
mockFileDataAccessor.AddFile(destFileName, mockFileDataAccessor.AdjustTimes(new MockFileData(sourceFile), TimeAdjustments.LastAccessTime), false);
}
#if FEATURE_FILE_MOVE_WITH_OVERWRITE
/// <inheritdoc />
public override void Move(string sourceFileName, string destFileName, bool overwrite)
{
if (sourceFileName == null)
{
throw CommonExceptions.FilenameCannotBeNull(nameof(sourceFileName));
}
if (destFileName == null)
{
throw CommonExceptions.FilenameCannotBeNull(nameof(destFileName));
}
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, nameof(sourceFileName));
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, nameof(destFileName));
if (mockFileDataAccessor.GetFile(destFileName) != null)
{
if (destFileName.Equals(sourceFileName))
{
return;
}
else if (!overwrite)
{
throw new IOException("A file can not be created if it already exists.");
}
}
var sourceFile = mockFileDataAccessor.GetFile(sourceFileName);
if (sourceFile == null)
{
throw CommonExceptions.FileNotFound(sourceFileName);
}
if (!sourceFile.AllowedFileShare.HasFlag(FileShare.Delete))
{
throw CommonExceptions.ProcessCannotAccessFileInUse();
}
VerifyDirectoryExists(destFileName);
mockFileDataAccessor.RemoveFile(sourceFileName);
mockFileDataAccessor.AddFile(destFileName, mockFileDataAccessor.AdjustTimes(new MockFileData(sourceFile), TimeAdjustments.LastAccessTime));
}
#endif
/// <inheritdoc />
public override FileSystemStream Open(string path, FileMode mode)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
return Open(path, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None);
}
/// <inheritdoc />
public override FileSystemStream Open(string path, FileMode mode, FileAccess access)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
return Open(path, mode, access, FileShare.None);
}
/// <inheritdoc />
public override FileSystemStream Open(string path, FileMode mode, FileAccess access, FileShare share) =>
OpenInternal(path, mode, access, FileOptions.None);
#if FEATURE_FILESTREAM_OPTIONS
/// <inheritdoc />
public override FileSystemStream Open(string path, FileStreamOptions options)
{
return OpenInternal(path, options.Mode, options.Access, options.Options);
}
#endif
private FileSystemStream OpenInternal(
string path,
FileMode mode,
FileAccess access,
FileOptions options)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
bool exists = mockFileDataAccessor.FileExists(path);
if (mode == FileMode.CreateNew && exists)
{
throw CommonExceptions.FileAlreadyExists(path);
}
if ((mode == FileMode.Open || mode == FileMode.Truncate) && !exists)
{
throw CommonExceptions.FileNotFound(path);
}
if (!exists || mode == FileMode.CreateNew)
{
return CreateInternal(path, access, options);
}
if (mode == FileMode.Create || mode == FileMode.Truncate)
{
Delete(path);
return CreateInternal(path, access, options);
}
var mockFileData = mockFileDataAccessor.GetFile(path);
mockFileData.CheckFileAccess(path, access);
var timeAdjustments = TimeAdjustments.LastAccessTime;
if (access.HasFlag(FileAccess.Write))
{
timeAdjustments |= TimeAdjustments.LastWriteTime;
}
mockFileDataAccessor.AdjustTimes(mockFileData, timeAdjustments);
return new MockFileStream(mockFileDataAccessor, path, mode, access, options);
}
/// <inheritdoc />
public override FileSystemStream OpenRead(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
return Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
}
/// <inheritdoc />
public override StreamReader OpenText(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
return new StreamReader(OpenRead(path));
}
/// <inheritdoc />
public override FileSystemStream OpenWrite(string path) => OpenWriteInternal(path, FileOptions.None);
private FileSystemStream OpenWriteInternal(string path, FileOptions options)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
return OpenInternal(path, FileMode.OpenOrCreate, FileAccess.Write, options);
}
/// <inheritdoc />
public override byte[] ReadAllBytes(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
if (!mockFileDataAccessor.FileExists(path))
{
throw CommonExceptions.FileNotFound(path);
}
mockFileDataAccessor.GetFile(path).CheckFileAccess(path, FileAccess.Read);
var fileData = mockFileDataAccessor.GetFile(path);
mockFileDataAccessor.AdjustTimes(fileData, TimeAdjustments.LastAccessTime);
return fileData.Contents.ToArray();
}
/// <inheritdoc />
public override string[] ReadAllLines(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
if (!mockFileDataAccessor.FileExists(path))
{
throw CommonExceptions.FileNotFound(path);
}
var fileData = mockFileDataAccessor.GetFile(path);
fileData.CheckFileAccess(path, FileAccess.Read);
mockFileDataAccessor.AdjustTimes(fileData, TimeAdjustments.LastAccessTime);
return fileData
.TextContents
.SplitLines();
}
/// <inheritdoc />
public override string[] ReadAllLines(string path, Encoding encoding)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
if (encoding == null)
{
throw new ArgumentNullException(nameof(encoding));
}
if (!mockFileDataAccessor.FileExists(path))
{
throw CommonExceptions.FileNotFound(path);
}
var fileData = mockFileDataAccessor.GetFile(path);
fileData.CheckFileAccess(path, FileAccess.Read);
mockFileDataAccessor.AdjustTimes(fileData, TimeAdjustments.LastAccessTime);
using (var ms = new MemoryStream(fileData.Contents))
using (var sr = new StreamReader(ms, encoding))
{
return sr.ReadToEnd().SplitLines();
}
}
/// <inheritdoc />
public override string ReadAllText(string path)
{
return ReadAllText(path, MockFileData.DefaultEncoding);
}
/// <inheritdoc />
public override string ReadAllText(string path, Encoding encoding)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
if (!mockFileDataAccessor.FileExists(path))
{
throw CommonExceptions.FileNotFound(path);
}
if (encoding == null)
{
throw new ArgumentNullException(nameof(encoding));
}
return ReadAllTextInternal(path, encoding);
}
/// <inheritdoc />
public override IEnumerable<string> ReadLines(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
return ReadAllLines(path);
}
/// <inheritdoc />
public override IEnumerable<string> ReadLines(string path, Encoding encoding)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
VerifyValueIsNotNull(encoding, "encoding");
return ReadAllLines(path, encoding);
}
/// <inheritdoc />
public override void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName)
{
Replace(sourceFileName, destinationFileName, destinationBackupFileName, false);
}
/// <inheritdoc />
public override void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
{
if (sourceFileName == null)
{
throw new ArgumentNullException(nameof(sourceFileName));
}
if (destinationFileName == null)
{
throw new ArgumentNullException(nameof(destinationFileName));
}
if (!mockFileDataAccessor.FileExists(sourceFileName))
{
throw CommonExceptions.FileNotFound(sourceFileName);
}
if (!mockFileDataAccessor.FileExists(destinationFileName))
{
throw CommonExceptions.FileNotFound(destinationFileName);
}
if (mockFileDataAccessor.StringOperations.Equals(sourceFileName, destinationFileName) && XFS.IsWindowsPlatform())
{
throw CommonExceptions.ProcessCannotAccessFileInUse();
}
if (destinationBackupFileName != null)
{
Copy(destinationFileName, destinationBackupFileName, overwrite: true);
}
Delete(destinationFileName);
Move(sourceFileName, destinationFileName);
}
#if FEATURE_CREATE_SYMBOLIC_LINK
/// <inheritdoc />
public override IFileSystemInfo ResolveLinkTarget(string linkPath, bool returnFinalTarget)
{
var initialContainer = mockFileDataAccessor.GetFile(linkPath);
if (initialContainer.LinkTarget != null)
{
var nextLocation = initialContainer.LinkTarget;
var nextContainer = mockFileDataAccessor.GetFile(nextLocation);
if (returnFinalTarget)
{
// The maximum number of symbolic links that are followed:
// https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.resolvelinktarget?view=net-6.0#remarks
int maxResolveLinks = XFS.IsWindowsPlatform() ? 63 : 40;
for (int i = 1; i < maxResolveLinks; i++)
{
if (nextContainer.LinkTarget == null)
{
break;
}
nextLocation = nextContainer.LinkTarget;
nextContainer = mockFileDataAccessor.GetFile(nextLocation);
}
if (nextContainer.LinkTarget != null)
{
throw CommonExceptions.NameCannotBeResolvedByTheSystem(linkPath);
}
}
if (nextContainer.IsDirectory)
{
return new MockDirectoryInfo(mockFileDataAccessor, nextLocation);
}
else
{
return new MockFileInfo(mockFileDataAccessor, nextLocation);
}
}
throw CommonExceptions.NameCannotBeResolvedByTheSystem(linkPath);
}
#endif
/// <inheritdoc />
public override void SetAttributes(string path, FileAttributes fileAttributes)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
var possibleFileData = mockFileDataAccessor.GetFile(path);
if (possibleFileData == null)
{
var directoryInfo = mockFileDataAccessor.DirectoryInfo.New(path);
if (directoryInfo.Exists)
{
directoryInfo.Attributes = fileAttributes;
}
else
{
throw CommonExceptions.FileNotFound(path);
}
}
else
{
mockFileDataAccessor.AdjustTimes(possibleFileData, TimeAdjustments.LastAccessTime);
possibleFileData.Attributes = fileAttributes;
}
}
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE
/// <inheritdoc />
public override void SetAttributes(SafeFileHandle fileHandle, FileAttributes fileAttributes)
{
throw CommonExceptions.NotImplemented();
}
#endif
/// <inheritdoc />
public override void SetCreationTime(string path, DateTime creationTime)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
mockFileDataAccessor.GetFile(path).CreationTime = new DateTimeOffset(creationTime);
}
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE
/// <inheritdoc />
public override void SetCreationTime(SafeFileHandle fileHandle, DateTime creationTime)
{
throw CommonExceptions.NotImplemented();
}
#endif
/// <inheritdoc />
public override void SetCreationTimeUtc(string path, DateTime creationTimeUtc)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
mockFileDataAccessor.GetFile(path).CreationTime = new DateTimeOffset(creationTimeUtc, TimeSpan.Zero);
}
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE
/// <inheritdoc />
public override void SetCreationTimeUtc(SafeFileHandle fileHandle, DateTime creationTimeUtc)
{
throw CommonExceptions.NotImplemented();
}
#endif
/// <inheritdoc />
public override void SetLastAccessTime(string path, DateTime lastAccessTime)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
mockFileDataAccessor.GetFile(path).LastAccessTime = new DateTimeOffset(lastAccessTime);
}
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE
/// <inheritdoc />
public override void SetLastAccessTime(SafeFileHandle fileHandle, DateTime lastAccessTime)
{
throw CommonExceptions.NotImplemented();
}
#endif
/// <inheritdoc />
public override void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
mockFileDataAccessor.GetFile(path).LastAccessTime = new DateTimeOffset(lastAccessTimeUtc, TimeSpan.Zero);
}
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE
/// <inheritdoc />
public override void SetLastAccessTimeUtc(SafeFileHandle fileHandle, DateTime lastAccessTimeUtc)
{
throw CommonExceptions.NotImplemented();
}
#endif
/// <inheritdoc />
public override void SetLastWriteTime(string path, DateTime lastWriteTime)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
mockFileDataAccessor.GetFile(path).LastWriteTime = new DateTimeOffset(lastWriteTime);
}
#if FEATURE_FILE_ATTRIBUTES_VIA_HANDLE