forked from TestableIO/System.IO.Abstractions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMockFileSystem.cs
224 lines (184 loc) · 7 KB
/
MockFileSystem.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
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace System.IO.Abstractions.TestingHelpers
{
using XFS = MockUnixSupport;
[Serializable]
public class MockFileSystem : IFileSystem, IMockFileDataAccessor
{
private readonly IDictionary<string, MockFileData> files;
private readonly FileBase file;
private readonly DirectoryBase directory;
private readonly IFileInfoFactory fileInfoFactory;
private readonly PathBase pathField;
private readonly IDirectoryInfoFactory directoryInfoFactory;
private readonly IDriveInfoFactory driveInfoFactory;
[NonSerialized]
private readonly PathVerifier pathVerifier;
public MockFileSystem() : this(null) { }
public MockFileSystem(IDictionary<string, MockFileData> files, string currentDirectory = "")
{
if (string.IsNullOrEmpty(currentDirectory))
currentDirectory = IO.Path.GetTempPath();
pathVerifier = new PathVerifier(this);
this.files = new Dictionary<string, MockFileData>(StringComparer.OrdinalIgnoreCase);
pathField = new MockPath(this);
file = new MockFile(this);
directory = new MockDirectory(this, file, currentDirectory);
fileInfoFactory = new MockFileInfoFactory(this);
directoryInfoFactory = new MockDirectoryInfoFactory(this);
driveInfoFactory = new MockDriveInfoFactory(this);
if (files != null)
{
foreach (var entry in files)
{
AddFile(entry.Key, entry.Value);
}
}
}
public FileBase File
{
get { return file; }
}
public DirectoryBase Directory
{
get { return directory; }
}
public IFileInfoFactory FileInfo
{
get { return fileInfoFactory; }
}
public PathBase Path
{
get { return pathField; }
}
public IDirectoryInfoFactory DirectoryInfo
{
get { return directoryInfoFactory; }
}
public IDriveInfoFactory DriveInfo
{
get { return driveInfoFactory; }
}
public PathVerifier PathVerifier
{
get { return pathVerifier; }
}
private string FixPath(string path)
{
var pathSeparatorFixed = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
return pathField.GetFullPath(pathSeparatorFixed);
}
public MockFileData GetFile(string path)
{
path = FixPath(path);
return GetFileWithoutFixingPath(path);
}
public void AddFile(string path, MockFileData mockFile)
{
var fixedPath = FixPath(path);
lock (files)
{
if (FileExists(fixedPath))
{
var isReadOnly = (files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
var isHidden = (files[fixedPath].Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
if (isReadOnly || isHidden)
{
throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ACCESS_TO_THE_PATH_IS_DENIED, path));
}
}
var directoryPath = Path.GetDirectoryName(fixedPath);
if (!directory.Exists(directoryPath))
{
AddDirectory(directoryPath);
}
files[fixedPath] = mockFile;
}
}
public void AddDirectory(string path)
{
var fixedPath = FixPath(path);
var separator = XFS.Separator();
lock (files)
{
if (FileExists(path) &&
(files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ACCESS_TO_THE_PATH_IS_DENIED, path));
var lastIndex = 0;
bool isUnc =
path.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase) ||
path.StartsWith(@"//", StringComparison.OrdinalIgnoreCase);
if (isUnc)
{
//First, confirm they aren't trying to create '\\server\'
lastIndex = path.IndexOf(separator, 2, StringComparison.OrdinalIgnoreCase);
if (lastIndex < 0)
throw new ArgumentException(@"The UNC path should be of the form \\server\share.", "path");
/*
* Although CreateDirectory(@"\\server\share\") is not going to work in real code, we allow it here for the purposes of setting up test doubles.
* See PR https://github.com/tathamoddie/System.IO.Abstractions/pull/90 for conversation
*/
}
while ((lastIndex = path.IndexOf(separator, lastIndex + 1, StringComparison.OrdinalIgnoreCase)) > -1)
{
var segment = path.Substring(0, lastIndex + 1);
if (!directory.Exists(segment))
{
files[segment] = new MockDirectoryData();
}
}
var s = path.EndsWith(separator, StringComparison.OrdinalIgnoreCase) ? path : path + separator;
files[s] = new MockDirectoryData();
}
}
public void RemoveFile(string path)
{
path = FixPath(path);
lock (files)
files.Remove(path);
}
public bool FileExists(string path)
{
if (string.IsNullOrEmpty(path))
return false;
path = FixPath(path);
lock (files)
return files.ContainsKey(path);
}
public IEnumerable<string> AllPaths
{
get
{
lock (files)
return files.Keys.ToArray();
}
}
public IEnumerable<string> AllFiles
{
get
{
lock (file)
return files.Where(f => !f.Value.IsDirectory).Select(f => f.Key).ToArray();
}
}
public IEnumerable<string> AllDirectories
{
get
{
lock (files)
return files.Where(f => f.Value.IsDirectory).Select(f => f.Key).ToArray();
}
}
private MockFileData GetFileWithoutFixingPath(string path)
{
lock (files)
{
MockFileData result;
files.TryGetValue(path, out result);
return result;
}
}
}
}