forked from TestableIO/System.IO.Abstractions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMockFileInfo.cs
306 lines (266 loc) · 9.59 KB
/
MockFileInfo.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
using System.Security.AccessControl;
namespace System.IO.Abstractions.TestingHelpers
{
[Serializable]
public class MockFileInfo : FileInfoBase
{
private readonly IMockFileDataAccessor mockFileSystem;
private string path;
public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path)
{
if (mockFileSystem == null)
{
throw new ArgumentNullException("mockFileSystem");
}
this.mockFileSystem = mockFileSystem;
this.path = path;
}
MockFileData MockFileData
{
get { return mockFileSystem.GetFile(path); }
}
public override void Delete()
{
mockFileSystem.RemoveFile(path);
}
public override void Refresh()
{
}
public override FileAttributes Attributes
{
get
{
if (MockFileData == null)
throw new FileNotFoundException("File not found", path);
return MockFileData.Attributes;
}
set { MockFileData.Attributes = value; }
}
public override DateTime CreationTime
{
get
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
return MockFileData.CreationTime.DateTime;
}
set
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
MockFileData.CreationTime = value;
}
}
public override DateTime CreationTimeUtc
{
get
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
return MockFileData.CreationTime.UtcDateTime;
}
set
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
MockFileData.CreationTime = value.ToLocalTime();
}
}
public override bool Exists
{
get { return MockFileData != null; }
}
public override string Extension
{
get
{
// System.IO.Path.GetExtension does only string manipulation,
// so it's safe to delegate.
return Path.GetExtension(path);
}
}
public override string FullName
{
get { return path; }
}
public override DateTime LastAccessTime
{
get
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
return MockFileData.LastAccessTime.DateTime;
}
set
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
MockFileData.LastAccessTime = value;
}
}
public override DateTime LastAccessTimeUtc
{
get
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
return MockFileData.LastAccessTime.UtcDateTime;
}
set
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
MockFileData.LastAccessTime = value;
}
}
public override DateTime LastWriteTime
{
get
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
return MockFileData.LastWriteTime.DateTime;
}
set
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
MockFileData.LastWriteTime = value;
}
}
public override DateTime LastWriteTimeUtc
{
get
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
return MockFileData.LastWriteTime.UtcDateTime;
}
set
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
MockFileData.LastWriteTime = value.ToLocalTime();
}
}
public override string Name {
get { return new MockPath(mockFileSystem).GetFileName(path); }
}
public override StreamWriter AppendText()
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
return new StreamWriter(new MockFileStream(mockFileSystem, FullName, true));
//return ((MockFileDataModifier) MockFileData).AppendText();
}
public override FileInfoBase CopyTo(string destFileName)
{
new MockFile(mockFileSystem).Copy(FullName, destFileName);
return mockFileSystem.FileInfo.FromFileName(destFileName);
}
public override FileInfoBase CopyTo(string destFileName, bool overwrite)
{
new MockFile(mockFileSystem).Copy(FullName, destFileName, overwrite);
return mockFileSystem.FileInfo.FromFileName(destFileName);
}
public override Stream Create()
{
return new MockFile(mockFileSystem).Create(FullName);
}
public override StreamWriter CreateText()
{
return new MockFile(mockFileSystem).CreateText(FullName);
}
public override void Decrypt()
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
var contents = MockFileData.Contents;
for (var i = 0; i < contents.Length; i++)
contents[i] ^= (byte)(i % 256);
}
public override void Encrypt()
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
var contents = MockFileData.Contents;
for(var i = 0; i < contents.Length; i++)
contents[i] ^= (byte) (i % 256);
}
public override FileSecurity GetAccessControl()
{
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
}
public override FileSecurity GetAccessControl(AccessControlSections includeSections)
{
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
}
public override void MoveTo(string destFileName)
{
var movedFileInfo = CopyTo(destFileName);
Delete();
path = movedFileInfo.FullName;
}
public override Stream Open(FileMode mode)
{
return new MockFile(mockFileSystem).Open(FullName, mode);
}
public override Stream Open(FileMode mode, FileAccess access)
{
return new MockFile(mockFileSystem).Open(FullName, mode, access);
}
public override Stream Open(FileMode mode, FileAccess access, FileShare share)
{
return new MockFile(mockFileSystem).Open(FullName, mode, access, share);
}
public override Stream OpenRead()
{
return new MockFileStream(mockFileSystem, path);
}
public override StreamReader OpenText()
{
return new StreamReader(OpenRead());
}
public override Stream OpenWrite()
{
return new MockFileStream(mockFileSystem, path);
}
public override FileInfoBase Replace(string destinationFileName, string destinationBackupFileName)
{
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
}
public override FileInfoBase Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
{
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
}
public override void SetAccessControl(FileSecurity fileSecurity)
{
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
}
public override DirectoryInfoBase Directory
{
get
{
return mockFileSystem.DirectoryInfo.FromDirectoryName(DirectoryName);
}
}
public override string DirectoryName
{
get
{
// System.IO.Path.GetDirectoryName does only string manipulation,
// so it's safe to delegate.
return Path.GetDirectoryName(path);
}
}
public override bool IsReadOnly
{
get
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
return (MockFileData.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
}
set
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
if(value)
MockFileData.Attributes |= FileAttributes.ReadOnly;
else
MockFileData.Attributes &= ~FileAttributes.ReadOnly;
}
}
public override long Length
{
get
{
if (MockFileData == null) throw new FileNotFoundException("File not found", path);
return MockFileData.Contents.LongLength;
}
}
}
}