Repository navigation
Expand file tree
/
Copy pathMockFileStream.cs
More file actions
124 lines (108 loc) · 4.1 KB
/
Copy pathMockFileStream.cs
File metadata and controls
124 lines (108 loc) · 4.1 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
namespace System.IO.Abstractions.TestingHelpers
{
/// <inheritdoc />
[Serializable]
public class MockFileStream : MemoryStream
{
private readonly IMockFileDataAccessor mockFileDataAccessor;
private readonly string path;
private readonly FileAccess access = FileAccess.ReadWrite;
private readonly FileOptions options;
private bool disposed;
/// <inheritdoc />
public MockFileStream(
IMockFileDataAccessor mockFileDataAccessor,
string path,
FileMode mode,
FileAccess access = FileAccess.ReadWrite,
FileOptions options = FileOptions.None)
{
this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
this.path = path;
this.options = options;
if (mockFileDataAccessor.FileExists(path))
{
if (mode.Equals(FileMode.CreateNew))
{
throw CommonExceptions.FileAlreadyExists(path);
}
var fileData = mockFileDataAccessor.GetFile(path);
fileData.CheckFileAccess(path, access);
var existingContents = fileData.Contents;
var keepExistingContents =
existingContents?.Length > 0 &&
mode != FileMode.Truncate && mode != FileMode.Create;
if (keepExistingContents)
{
Write(existingContents, 0, existingContents.Length);
Seek(0, mode == FileMode.Append
? SeekOrigin.End
: SeekOrigin.Begin);
}
}
else
{
var directoryPath = mockFileDataAccessor.Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(directoryPath) && !mockFileDataAccessor.Directory.Exists(directoryPath))
{
throw CommonExceptions.CouldNotFindPartOfPath(path);
}
if (mode.Equals(FileMode.Open) || mode.Equals(FileMode.Truncate))
{
throw CommonExceptions.FileNotFound(path);
}
mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { }));
}
this.access = access;
}
/// <inheritdoc />
public override bool CanRead => access.HasFlag(FileAccess.Read);
/// <inheritdoc />
public override bool CanWrite => access.HasFlag(FileAccess.Write);
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
if (disposed)
{
return;
}
InternalFlush();
base.Dispose(disposing);
OnClose();
disposed = true;
}
/// <inheritdoc />
public override void Flush()
{
InternalFlush();
}
private void InternalFlush()
{
if (mockFileDataAccessor.FileExists(path))
{
var mockFileData = mockFileDataAccessor.GetFile(path);
/* reset back to the beginning .. */
var position = Position;
Seek(0, SeekOrigin.Begin);
/* .. read everything out */
var data = new byte[Length];
Read(data, 0, (int)Length);
/* restore to original position */
Seek(position, SeekOrigin.Begin);
/* .. put it in the mock system */
mockFileData.Contents = data;
}
}
private void OnClose()
{
if (options.HasFlag(FileOptions.DeleteOnClose) && mockFileDataAccessor.FileExists(path))
{
mockFileDataAccessor.RemoveFile(path);
}
if (options.HasFlag(FileOptions.Encrypted) && mockFileDataAccessor.FileExists(path))
{
mockFileDataAccessor.FileInfo.FromFileName(path).Encrypt();
}
}
}
}