From fb9d192e0165bea560f79acd58bea8e32e5a65c8 Mon Sep 17 00:00:00 2001 From: Alexander Pruss Date: Fri, 18 Nov 2016 13:54:00 +0100 Subject: [PATCH 1/3] Opening a MockFileStream to read from a nonexistent file will throw an exception. Implementation note - instead of using System.IO.FileAccess/FileMode, I ended up creating an internal StreamType enum instead to keep the interface simple. That's potentially something to change upon on review, but I think it's good the way it is. --- TestHelpers.Tests/MockFileStreamTests.cs | 16 +++++++++++++++- TestingHelpers/MockFile.cs | 2 +- TestingHelpers/MockFileInfo.cs | 7 ++++--- TestingHelpers/MockFileStream.cs | 15 +++++++++++++-- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/TestHelpers.Tests/MockFileStreamTests.cs b/TestHelpers.Tests/MockFileStreamTests.cs index 66b6412b9..a07f7e743 100644 --- a/TestHelpers.Tests/MockFileStreamTests.cs +++ b/TestHelpers.Tests/MockFileStreamTests.cs @@ -15,7 +15,7 @@ public void MockFileStream_Flush_WritesByteToFile() // Arrange var filepath = XFS.Path(@"c:\something\foo.txt"); var filesystem = new MockFileSystem(new Dictionary()); - var cut = new MockFileStream(filesystem, filepath); + var cut = new MockFileStream(filesystem, filepath, MockFileStream.StreamType.WRITE); // Act cut.WriteByte(255); @@ -44,5 +44,19 @@ public void MockFileStream_Dispose_ShouldNotResurrectFile() Assert.AreEqual(0, fileCount2, "File should have been deleted"); Assert.AreEqual(0, fileCount3, "Disposing stream should not have resurrected the file"); } + + [Test] + [ExpectedException(typeof(FileNotFoundException))] + public void MockFileStream_Constructor_Reading_Nonexistent_File_Throws_Exception() + { + // Arrange + var nonexistentFilePath = XFS.Path(@"c:\something\foo.txt"); + var filesystem = new MockFileSystem(new Dictionary()); + + // Act + var illegalFileStream = new MockFileStream(filesystem, nonexistentFilePath, MockFileStream.StreamType.READ); + + // Assert - expect an exception + } } } diff --git a/TestingHelpers/MockFile.cs b/TestingHelpers/MockFile.cs index 0d2506032..affc56da4 100644 --- a/TestingHelpers/MockFile.cs +++ b/TestingHelpers/MockFile.cs @@ -399,7 +399,7 @@ public override Stream OpenWrite(string path) { mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path"); - return new MockFileStream(mockFileDataAccessor, path); + return new MockFileStream(mockFileDataAccessor, path, MockFileStream.StreamType.WRITE); } public override byte[] ReadAllBytes(string path) diff --git a/TestingHelpers/MockFileInfo.cs b/TestingHelpers/MockFileInfo.cs index d881af155..a4ffedf9f 100644 --- a/TestingHelpers/MockFileInfo.cs +++ b/TestingHelpers/MockFileInfo.cs @@ -155,7 +155,7 @@ public override string Name { public override StreamWriter AppendText() { if (MockFileData == null) throw new FileNotFoundException("File not found", path); - return new StreamWriter(new MockFileStream(mockFileSystem, FullName, true)); + return new StreamWriter(new MockFileStream(mockFileSystem, FullName, MockFileStream.StreamType.APPEND)); //return ((MockFileDataModifier) MockFileData).AppendText(); } @@ -231,7 +231,8 @@ public override Stream Open(FileMode mode, FileAccess access, FileShare share) public override Stream OpenRead() { - return new MockFileStream(mockFileSystem, path); + if (MockFileData == null) throw new FileNotFoundException("File not found", path); + return new MockFileStream(mockFileSystem, path, MockFileStream.StreamType.READ); } public override StreamReader OpenText() @@ -241,7 +242,7 @@ public override StreamReader OpenText() public override Stream OpenWrite() { - return new MockFileStream(mockFileSystem, path); + return new MockFileStream(mockFileSystem, path, MockFileStream.StreamType.WRITE); } public override FileInfoBase Replace(string destinationFileName, string destinationBackupFileName) diff --git a/TestingHelpers/MockFileStream.cs b/TestingHelpers/MockFileStream.cs index 0af1ac962..88312b3c3 100644 --- a/TestingHelpers/MockFileStream.cs +++ b/TestingHelpers/MockFileStream.cs @@ -6,7 +6,14 @@ public class MockFileStream : MemoryStream private readonly IMockFileDataAccessor mockFileDataAccessor; private readonly string path; - public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, bool forAppend = false) + public enum StreamType + { + READ, + WRITE, + APPEND + } + + public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, StreamType streamType) { if (mockFileDataAccessor == null) { @@ -23,13 +30,17 @@ public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, b if (data != null && data.Length > 0) { Write(data, 0, data.Length); - Seek(0, forAppend + Seek(0, StreamType.APPEND.Equals(streamType) ? SeekOrigin.End : SeekOrigin.Begin); } } else { + if (StreamType.READ.Equals(streamType)) + { + throw new FileNotFoundException("File not found.", path); + } mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { })); } } From 4b3dc68b2bc0730cec909727be9462d03531acec Mon Sep 17 00:00:00 2001 From: Alexander Pruss Date: Wed, 23 Nov 2016 14:33:21 +0100 Subject: [PATCH 2/3] #187: Directory.move() can now also move files. --- TestHelpers.Tests/MockDirectoryTests.cs | 22 +++++++++++++++++++++- TestingHelpers/MockDirectory.cs | 12 +++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/TestHelpers.Tests/MockDirectoryTests.cs b/TestHelpers.Tests/MockDirectoryTests.cs index 412235433..6d5337d2a 100644 --- a/TestHelpers.Tests/MockDirectoryTests.cs +++ b/TestHelpers.Tests/MockDirectoryTests.cs @@ -992,7 +992,7 @@ public void Move_DirectoryExistsWithDifferentCase_DirectorySuccessfullyMoved() } [TestCaseSource("GetPathsForMoving")] - public void MockDirectory_Move_ShouldMove(string sourceDirName, string destDirName, string filePathOne, string filePathTwo) + public void MockDirectory_Move_ShouldMoveDirectories(string sourceDirName, string destDirName, string filePathOne, string filePathTwo) { // Arrange var fileSystem = new MockFileSystem(new Dictionary @@ -1010,6 +1010,26 @@ public void MockDirectory_Move_ShouldMove(string sourceDirName, string destDirNa Assert.IsTrue(fileSystem.File.Exists(XFS.Path(destDirName + filePathTwo))); } + [Test] + public void MockDirectory_Move_ShouldMoveFiles() + { + string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); + string sourceFileContent = "this is some content"; + var fileSystem = new MockFileSystem(new Dictionary + { + {sourceFilePath, new MockFileData(sourceFileContent)}, + {XFS.Path(@"c:\somethingelse\dummy.txt"), new MockFileData(new byte[] {0})} + }); + + string destFilePath = XFS.Path(@"c:\somethingelse\demo1.txt"); + + fileSystem.Directory.Move(sourceFilePath, destFilePath); + + Assert.That(fileSystem.FileExists(destFilePath), Is.True); + Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent)); + Assert.That(fileSystem.FileExists(sourceFilePath), Is.False); + } + [Test] public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemConstructor() { string directory = XFS.Path(@"D:\folder1\folder2"); diff --git a/TestingHelpers/MockDirectory.cs b/TestingHelpers/MockDirectory.cs index 455ee4e30..4523dd280 100644 --- a/TestingHelpers/MockDirectory.cs +++ b/TestingHelpers/MockDirectory.cs @@ -322,10 +322,16 @@ public override DirectoryInfoBase GetParent(string path) return parent; } - public override void Move(string sourceDirName, string destDirName) + public override void Move(string sourceName, string destName) { - var fullSourcePath = EnsurePathEndsWithDirectorySeparator(mockFileDataAccessor.Path.GetFullPath(sourceDirName)); - var fullDestPath = EnsurePathEndsWithDirectorySeparator(mockFileDataAccessor.Path.GetFullPath(destDirName)); + //if we're moving a file, not a directory, call the appropriate file moving function. + { + fileBase.Move(sourceName, destName); + return; + } + + var fullSourcePath = EnsurePathEndsWithDirectorySeparator(mockFileDataAccessor.Path.GetFullPath(sourceName)); + var fullDestPath = EnsurePathEndsWithDirectorySeparator(mockFileDataAccessor.Path.GetFullPath(destName)); if (string.Equals(fullSourcePath, fullDestPath, StringComparison.OrdinalIgnoreCase)) { From dcfa069262bd10a259f7967c98450dd2efac8fc0 Mon Sep 17 00:00:00 2001 From: Alexander Pruss Date: Wed, 23 Nov 2016 14:33:28 +0100 Subject: [PATCH 3/3] Added missing file. --- TestingHelpers/MockDirectory.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/TestingHelpers/MockDirectory.cs b/TestingHelpers/MockDirectory.cs index 4523dd280..4e542e87f 100644 --- a/TestingHelpers/MockDirectory.cs +++ b/TestingHelpers/MockDirectory.cs @@ -325,6 +325,8 @@ public override DirectoryInfoBase GetParent(string path) public override void Move(string sourceName, string destName) { //if we're moving a file, not a directory, call the appropriate file moving function. + var fileData = mockFileDataAccessor.GetFile((mockFileDataAccessor.Path.GetFullPath(sourceName))); + if (fileData != null && (fileData.Attributes & FileAttributes.Directory) == 0) { fileBase.Move(sourceName, destName); return;