Skip to content

Commit 8cb3638

Browse files
willrawlstathamoddie
authored andcommitted
Added support in MockFileInfo for both get and set of properties including:
CreationTime, CreationTimeUtc, LastAccessTime, LastAccessTimeUtc, LastWriteTime, LastWriteTimeUtc, IsReadOnly. Encrypt and Decrypt will now respond by internally performing a XOR across the byte array. OpenWrite and AppendText now work. NOTE: No attempt is currently made to insure a file marked read only file is not written to. Tests added to cover these new features. MockFileStream supports append (defaults to no append)
1 parent 3ee7857 commit 8cb3638

3 files changed

Lines changed: 309 additions & 19 deletions

File tree

TestHelpers.Tests/MockFileInfoTests.cs

Lines changed: 233 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,198 @@ public void MockFileInfo_CreationTimeUtc_ShouldReturnCreationTimeUtcOfFileInMemo
101101
Assert.AreEqual(creationTime.ToUniversalTime(), result);
102102
}
103103

104+
[Test]
105+
public void MockFileInfo_CreationTimeUtc_ShouldSetCreationTimeUtcOfFileInMemoryFileSystem()
106+
{
107+
// Arrange
108+
var creationTime = DateTime.Now.AddHours(-4);
109+
var fileData = new MockFileData("Demo text content") { CreationTime = creationTime };
110+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
111+
{
112+
{ @"c:\a.txt", fileData }
113+
});
114+
var fileInfo = new MockFileInfo(fileSystem, @"c:\a.txt");
115+
116+
// Act
117+
var newUtcTime = DateTime.UtcNow;
118+
fileInfo.CreationTimeUtc = newUtcTime;
119+
120+
// Assert
121+
Assert.AreEqual(newUtcTime, fileInfo.CreationTimeUtc);
122+
}
123+
124+
125+
[Test]
126+
public void MockFileInfo_CreationTime_ShouldReturnCreationTimeOfFileInMemoryFileSystem()
127+
{
128+
// Arrange
129+
var creationTime = DateTime.Now.AddHours(-4);
130+
var fileData = new MockFileData("Demo text content") { CreationTime = creationTime };
131+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
132+
{
133+
{ @"c:\a.txt", fileData }
134+
});
135+
var fileInfo = new MockFileInfo(fileSystem, @"c:\a.txt");
136+
137+
// Act
138+
var result = fileInfo.CreationTime;
139+
140+
// Assert
141+
Assert.AreEqual(creationTime, result);
142+
}
143+
144+
[Test]
145+
public void MockFileInfo_CreationTime_ShouldSetCreationTimeOfFileInMemoryFileSystem()
146+
{
147+
// Arrange
148+
var creationTime = DateTime.Now.AddHours(-4);
149+
var fileData = new MockFileData("Demo text content") { CreationTime = creationTime };
150+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
151+
{
152+
{ @"c:\a.txt", fileData }
153+
});
154+
var fileInfo = new MockFileInfo(fileSystem, @"c:\a.txt");
155+
156+
// Act
157+
var newTime = DateTime.Now;
158+
fileInfo.CreationTime = newTime;
159+
160+
// Assert
161+
Assert.AreEqual(newTime, fileInfo.CreationTime);
162+
}
163+
164+
[Test]
165+
public void MockFileInfo_IsReadOnly_ShouldSetReadOnlyAttributeOfFileInMemoryFileSystem()
166+
{
167+
// Arrange
168+
var fileData = new MockFileData("Demo text content");
169+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
170+
{
171+
{ @"c:\a.txt", fileData }
172+
});
173+
var fileInfo = new MockFileInfo(fileSystem, @"c:\a.txt");
174+
175+
// Act
176+
fileInfo.IsReadOnly = true;
177+
178+
// Assert
179+
Assert.AreEqual(FileAttributes.ReadOnly, fileData.Attributes & FileAttributes.ReadOnly);
180+
}
181+
182+
[Test]
183+
public void MockFileInfo_IsReadOnly_ShouldSetNotReadOnlyAttributeOfFileInMemoryFileSystem()
184+
{
185+
// Arrange
186+
var fileData = new MockFileData("Demo text content") {Attributes = FileAttributes.ReadOnly};
187+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
188+
{
189+
{ @"c:\a.txt", fileData }
190+
});
191+
var fileInfo = new MockFileInfo(fileSystem, @"c:\a.txt");
192+
193+
// Act
194+
fileInfo.IsReadOnly = false;
195+
196+
// Assert
197+
Assert.AreNotEqual(FileAttributes.ReadOnly, fileData.Attributes & FileAttributes.ReadOnly);
198+
}
199+
200+
[Test]
201+
public void MockFileInfo_AppendText_ShouldAddTextToFileInMemoryFileSystem()
202+
{
203+
// Arrange
204+
var fileData = new MockFileData("Demo text content");
205+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
206+
{
207+
{ @"c:\a.txt", fileData }
208+
});
209+
var fileInfo = new MockFileInfo(fileSystem, @"c:\a.txt");
210+
211+
// Act
212+
using (var file = fileInfo.AppendText())
213+
file.WriteLine("This should be at the end");
214+
215+
string newcontents;
216+
using (var newfile = fileInfo.OpenText())
217+
newcontents = newfile.ReadToEnd();
218+
219+
// Assert
220+
Assert.AreEqual("Demo text contentThis should be at the end\r\n", newcontents);
221+
}
222+
223+
[Test]
224+
public void MockFileInfo_OpenWrite_ShouldAddDataToFileInMemoryFileSystem()
225+
{
226+
// Arrange
227+
var fileData = new MockFileData("Demo text content");
228+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
229+
{
230+
{ @"c:\a.txt", fileData }
231+
});
232+
var fileInfo = new MockFileInfo(fileSystem, @"c:\a.txt");
233+
var bytesToAdd = new byte[] {65, 66, 67, 68, 69};
234+
235+
// Act
236+
using (var file = fileInfo.OpenWrite())
237+
file.Write(bytesToAdd, 0, bytesToAdd.Length);
238+
239+
string newcontents;
240+
using (var newfile = fileInfo.OpenText())
241+
newcontents = newfile.ReadToEnd();
242+
243+
// Assert
244+
Assert.AreEqual("ABCDEtext content", newcontents);
245+
}
246+
247+
[Test]
248+
public void MockFileInfo_Encrypt_ShouldReturnXorOfFileInMemoryFileSystem()
249+
{
250+
// Arrange
251+
var fileData = new MockFileData("Demo text content");
252+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
253+
{
254+
{ @"c:\a.txt", fileData }
255+
});
256+
var fileInfo = new MockFileInfo(fileSystem, @"c:\a.txt");
257+
258+
// Act
259+
fileInfo.Encrypt();
260+
261+
string newcontents;
262+
using (var newfile = fileInfo.OpenText())
263+
{
264+
newcontents = newfile.ReadToEnd();
265+
}
266+
267+
// Assert
268+
Assert.AreNotEqual("Demo text content", newcontents);
269+
}
270+
271+
[Test]
272+
public void MockFileInfo_Decrypt_ShouldReturnCorrectContentsFileInMemoryFileSystem()
273+
{
274+
// Arrange
275+
var fileData = new MockFileData("Demo text content");
276+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
277+
{
278+
{ @"c:\a.txt", fileData }
279+
});
280+
var fileInfo = new MockFileInfo(fileSystem, @"c:\a.txt");
281+
fileInfo.Encrypt();
282+
283+
// Act
284+
fileInfo.Decrypt();
285+
286+
string newcontents;
287+
using (var newfile = fileInfo.OpenText())
288+
{
289+
newcontents = newfile.ReadToEnd();
290+
}
291+
292+
// Assert
293+
Assert.AreEqual("Demo text content", newcontents);
294+
}
295+
104296
[Test]
105297
public void MockFileInfo_LastAccessTimeUtc_ShouldReturnLastAccessTimeUtcOfFileInMemoryFileSystem()
106298
{
@@ -120,6 +312,26 @@ public void MockFileInfo_LastAccessTimeUtc_ShouldReturnLastAccessTimeUtcOfFileIn
120312
Assert.AreEqual(lastAccessTime.ToUniversalTime(), result);
121313
}
122314

315+
[Test]
316+
public void MockFileInfo_LastAccessTimeUtc_ShouldSetCreationTimeUtcOfFileInMemoryFileSystem()
317+
{
318+
// Arrange
319+
var lastAccessTime = DateTime.Now.AddHours(-4);
320+
var fileData = new MockFileData("Demo text content") { LastAccessTime = lastAccessTime };
321+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
322+
{
323+
{ @"c:\a.txt", fileData }
324+
});
325+
var fileInfo = new MockFileInfo(fileSystem, @"c:\a.txt");
326+
327+
// Act
328+
var newUtcTime = DateTime.UtcNow;
329+
fileInfo.LastAccessTimeUtc = newUtcTime;
330+
331+
// Assert
332+
Assert.AreEqual(newUtcTime, fileInfo.LastAccessTimeUtc);
333+
}
334+
123335
[Test]
124336
public void MockFileInfo_LastWriteTimeUtc_ShouldReturnLastWriteTimeUtcOfFileInMemoryFileSystem()
125337
{
@@ -138,7 +350,27 @@ public void MockFileInfo_LastWriteTimeUtc_ShouldReturnLastWriteTimeUtcOfFileInMe
138350
// Assert
139351
Assert.AreEqual(lastWriteTime.ToUniversalTime(), result);
140352
}
141-
353+
354+
[Test]
355+
public void MockFileInfo_LastWriteTimeUtc_ShouldSetLastWriteTimeUtcOfFileInMemoryFileSystem()
356+
{
357+
// Arrange
358+
var lastWriteTime = DateTime.Now.AddHours(-4);
359+
var fileData = new MockFileData("Demo text content") { LastWriteTime = lastWriteTime };
360+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
361+
{
362+
{ @"c:\a.txt", fileData }
363+
});
364+
var fileInfo = new MockFileInfo(fileSystem, @"c:\a.txt");
365+
366+
// Act
367+
var newUtcTime = DateTime.UtcNow;
368+
fileInfo.LastWriteTime = newUtcTime;
369+
370+
// Assert
371+
Assert.AreEqual(newUtcTime, fileInfo.LastWriteTime);
372+
}
373+
142374
[Test]
143375
public void MockFileInfo_GetExtension_ShouldReturnExtension()
144376
{

0 commit comments

Comments
 (0)