From fb9d192e0165bea560f79acd58bea8e32e5a65c8 Mon Sep 17 00:00:00 2001 From: Alexander Pruss Date: Fri, 18 Nov 2016 13:54:00 +0100 Subject: [PATCH 01/11] 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 445b0c07352d9246420cd3f3c6904f61021b6060 Mon Sep 17 00:00:00 2001 From: Alexander Pruss Date: Mon, 21 Nov 2016 15:40:59 +0100 Subject: [PATCH 02/11] #181: MockFileInfo and MockFile no longer throw exceptions if moving a file to itself. See http://stackoverflow.com/questions/40722205/should-mscorlibs-fileinfo-moveto-throw-an-exception-if-trying-to-move-a-file --- TestHelpers.Tests/MockFileInfoTests.cs | 15 +++++++++++++++ TestHelpers.Tests/MockFileMoveTests.cs | 19 +++++++++++++++++++ TestingHelpers/MockFile.cs | 11 ++++++++++- TestingHelpers/MockFileInfo.cs | 2 +- 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/TestHelpers.Tests/MockFileInfoTests.cs b/TestHelpers.Tests/MockFileInfoTests.cs index 659820d56..e9752e3ab 100644 --- a/TestHelpers.Tests/MockFileInfoTests.cs +++ b/TestHelpers.Tests/MockFileInfoTests.cs @@ -476,5 +476,20 @@ public void MockFileInfo_MoveTo_ShouldUpdateFileInfoDirectoryAndFullName() Assert.AreEqual(fileInfo.DirectoryName, destinationFolder); Assert.AreEqual(fileInfo.FullName, destination); } + + [Test] + public void MockFileInfo_MoveTo_SameSourceAndTargetIsANoOp() + { + // Arrange + var fileSystem = new MockFileSystem(); + fileSystem.AddFile(XFS.Path(@"c:\temp\file.txt"), new MockFileData(@"line 1\r\nline 2")); + var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); + + // Act + string destination = XFS.Path(XFS.Path(@"c:\temp\file.txt")); + fileInfo.MoveTo(destination); + + Assert.AreEqual(fileInfo.FullName, destination); + } } } \ No newline at end of file diff --git a/TestHelpers.Tests/MockFileMoveTests.cs b/TestHelpers.Tests/MockFileMoveTests.cs index 090a6b23f..c86283849 100644 --- a/TestHelpers.Tests/MockFileMoveTests.cs +++ b/TestHelpers.Tests/MockFileMoveTests.cs @@ -29,6 +29,25 @@ public void MockFile_Move_ShouldMoveFileWithinMemoryFileSystem() Assert.That(fileSystem.FileExists(sourceFilePath), Is.False); } + [Test] + public void MockFile_Move_SameSourceAndTargetIsANoOp() + { + 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\demo.txt"); + + fileSystem.File.Move(sourceFilePath, destFilePath); + + Assert.That(fileSystem.FileExists(destFilePath), Is.True); + Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent)); + } + [Test] public void MockFile_Move_ShouldThrowIOExceptionWhenTargetAlreadyExists() { diff --git a/TestingHelpers/MockFile.cs b/TestingHelpers/MockFile.cs index affc56da4..b89ba4fee 100644 --- a/TestingHelpers/MockFile.cs +++ b/TestingHelpers/MockFile.cs @@ -323,7 +323,16 @@ public override void Move(string sourceFileName, string destFileName) mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, "destFileName"); if (mockFileDataAccessor.GetFile(destFileName) != null) - throw new IOException("A file can not be created if it already exists."); + { + if (destFileName.Equals(sourceFileName)) + { + return; + } else + { + throw new IOException("A file can not be created if it already exists."); + } + } + var sourceFile = mockFileDataAccessor.GetFile(sourceFileName); diff --git a/TestingHelpers/MockFileInfo.cs b/TestingHelpers/MockFileInfo.cs index a4ffedf9f..8a21a8d6d 100644 --- a/TestingHelpers/MockFileInfo.cs +++ b/TestingHelpers/MockFileInfo.cs @@ -209,7 +209,7 @@ public override FileSecurity GetAccessControl(AccessControlSections includeSecti public override void MoveTo(string destFileName) { - var movedFileInfo = CopyTo(destFileName); + var movedFileInfo = CopyTo(destFileName, true); Delete(); path = movedFileInfo.FullName; } From 7b075cfa3663502737499ac75d901104e6327cb8 Mon Sep 17 00:00:00 2001 From: Alexander Pruss Date: Mon, 21 Nov 2016 16:03:10 +0100 Subject: [PATCH 03/11] Returning early if destfileName == FullName to prevent side effects. --- TestingHelpers/MockFileInfo.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/TestingHelpers/MockFileInfo.cs b/TestingHelpers/MockFileInfo.cs index 8a21a8d6d..16cdf69c6 100644 --- a/TestingHelpers/MockFileInfo.cs +++ b/TestingHelpers/MockFileInfo.cs @@ -209,7 +209,11 @@ public override FileSecurity GetAccessControl(AccessControlSections includeSecti public override void MoveTo(string destFileName) { - var movedFileInfo = CopyTo(destFileName, true); + if (destFileName == FullName) + { + return; + } + var movedFileInfo = CopyTo(destFileName); Delete(); path = movedFileInfo.FullName; } From 6ddc87dbc4f9b5c595009e64557e3b86065d0922 Mon Sep 17 00:00:00 2001 From: Alexander Pruss Date: Mon, 21 Nov 2016 16:46:24 +0100 Subject: [PATCH 04/11] #181: Moved existence and same target-source check further down to make sure all related calls to Move and Copy made the same checks. Copying and moving nonexistent sources now correctly throws an exception. --- TestHelpers.Tests/MockFileCopyTests.cs | 20 ++++++++++++ TestHelpers.Tests/MockFileInfoTests.cs | 45 ++++++++++++++++++++++++++ TestHelpers.Tests/MockFileMoveTests.cs | 20 ++++++++++++ TestingHelpers/MockFile.cs | 3 ++ TestingHelpers/MockFileInfo.cs | 15 +++++---- 5 files changed, 97 insertions(+), 6 deletions(-) diff --git a/TestHelpers.Tests/MockFileCopyTests.cs b/TestHelpers.Tests/MockFileCopyTests.cs index 7dffee39f..af446a884 100644 --- a/TestHelpers.Tests/MockFileCopyTests.cs +++ b/TestHelpers.Tests/MockFileCopyTests.cs @@ -271,5 +271,25 @@ public void MockFile_Copy_ShouldThrowArgumentExceptionWhenTargetIsEmpty_Message( Assert.That(exception.Message, Is.StringStarting("Empty file name is not legal.")); } + + [Test] + [ExpectedException(typeof(FileNotFoundException))] + public void MockFile_Copy_ShouldThrowExceptionWhenSourceDoesNotExist() + { + string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); + var fileSystem = new MockFileSystem(); + + fileSystem.File.Copy(sourceFilePath, XFS.Path(@"c:\something\demo2.txt")); + } + + [Test] + [ExpectedException(typeof(FileNotFoundException))] + public void MockFile_Copy_ShouldThrowExceptionWhenSourceDoesNotExist_EvenWhenCopyingToItself() + { + string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); + var fileSystem = new MockFileSystem(); + + fileSystem.File.Copy(sourceFilePath, XFS.Path(@"c:\something\demo.txt")); + } } } \ No newline at end of file diff --git a/TestHelpers.Tests/MockFileInfoTests.cs b/TestHelpers.Tests/MockFileInfoTests.cs index e9752e3ab..d7c8a1dcf 100644 --- a/TestHelpers.Tests/MockFileInfoTests.cs +++ b/TestHelpers.Tests/MockFileInfoTests.cs @@ -491,5 +491,50 @@ public void MockFileInfo_MoveTo_SameSourceAndTargetIsANoOp() Assert.AreEqual(fileInfo.FullName, destination); } + + [Test] + [ExpectedException(typeof(FileNotFoundException))] + public void MockFileInfo_MoveTo_SameSourceAndTargetThrowsExceptionIfSourceDoesntExist() + { + // Arrange + var fileSystem = new MockFileSystem(); + var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); + + // Act + string destination = XFS.Path(XFS.Path(@"c:\temp\file.txt")); + fileInfo.MoveTo(destination); + + Assert.AreEqual(fileInfo.FullName, destination); + } + + [Test] + [ExpectedException(typeof(FileNotFoundException))] + public void MockFileInfo_MoveTo_ThrowsExceptionIfSourceDoesntExist() + { + // Arrange + var fileSystem = new MockFileSystem(); + var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); + + // Act + string destination = XFS.Path(XFS.Path(@"c:\temp\file2.txt")); + fileInfo.MoveTo(destination); + + Assert.AreEqual(fileInfo.FullName, destination); + } + + [Test] + [ExpectedException(typeof(FileNotFoundException))] + public void MockFileInfo_CopyTo_ThrowsExceptionIfSourceDoesntExist() + { + // Arrange + var fileSystem = new MockFileSystem(); + var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); + + // Act + string destination = XFS.Path(XFS.Path(@"c:\temp\file2.txt")); + fileInfo.CopyTo(destination); + + Assert.AreEqual(fileInfo.FullName, destination); + } } } \ No newline at end of file diff --git a/TestHelpers.Tests/MockFileMoveTests.cs b/TestHelpers.Tests/MockFileMoveTests.cs index c86283849..71da040d5 100644 --- a/TestHelpers.Tests/MockFileMoveTests.cs +++ b/TestHelpers.Tests/MockFileMoveTests.cs @@ -305,5 +305,25 @@ public void MockFile_Move_ShouldThrowDirectoryNotFoundExceptionWhenSourcePathDoe Assert.That(() => fileSystem.File.Move(sourceFilePath, destFilePath), Throws.InstanceOf().With.Message.StartsWith(@"Could not find a part of the path")); } + + [Test] + [ExpectedException(typeof(FileNotFoundException))] + public void MockFile_Move_ShouldThrowExceptionWhenSourceDoesNotExist() + { + string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); + var fileSystem = new MockFileSystem(); + + fileSystem.File.Move(sourceFilePath, XFS.Path(@"c:\something\demo2.txt")); + } + + [Test] + [ExpectedException(typeof(FileNotFoundException))] + public void MockFile_Move_ShouldThrowExceptionWhenSourceDoesNotExist_EvenWhenCopyingToItself() + { + string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); + var fileSystem = new MockFileSystem(); + + fileSystem.File.Move(sourceFilePath, XFS.Path(@"c:\something\demo.txt")); + } } } \ No newline at end of file diff --git a/TestingHelpers/MockFile.cs b/TestingHelpers/MockFile.cs index b89ba4fee..3b9c49cbc 100644 --- a/TestingHelpers/MockFile.cs +++ b/TestingHelpers/MockFile.cs @@ -99,6 +99,9 @@ public override void Copy(string sourceFileName, string destFileName) public override void Copy(string sourceFileName, string destFileName, bool overwrite) { + if (!Exists(sourceFileName)) + { + } if (sourceFileName == null) { throw new ArgumentNullException("sourceFileName", Properties.Resources.FILENAME_CANNOT_BE_NULL); diff --git a/TestingHelpers/MockFileInfo.cs b/TestingHelpers/MockFileInfo.cs index 16cdf69c6..e17da6416 100644 --- a/TestingHelpers/MockFileInfo.cs +++ b/TestingHelpers/MockFileInfo.cs @@ -161,12 +161,19 @@ public override StreamWriter AppendText() public override FileInfoBase CopyTo(string destFileName) { - new MockFile(mockFileSystem).Copy(FullName, destFileName); - return mockFileSystem.FileInfo.FromFileName(destFileName); + return CopyTo(destFileName, false); } public override FileInfoBase CopyTo(string destFileName, bool overwrite) { + if (!Exists) + { + throw new FileNotFoundException("The file does not exist and can't be moved or copied.", FullName); + } + if (destFileName == FullName) + { + return this; + } new MockFile(mockFileSystem).Copy(FullName, destFileName, overwrite); return mockFileSystem.FileInfo.FromFileName(destFileName); } @@ -209,10 +216,6 @@ public override FileSecurity GetAccessControl(AccessControlSections includeSecti public override void MoveTo(string destFileName) { - if (destFileName == FullName) - { - return; - } var movedFileInfo = CopyTo(destFileName); Delete(); path = movedFileInfo.FullName; From 3c8da79b7f478dc71b45cb6c0fe2c4ccdb5ced73 Mon Sep 17 00:00:00 2001 From: Alexander Pruss Date: Mon, 21 Nov 2016 16:48:37 +0100 Subject: [PATCH 05/11] Added missing change. --- TestingHelpers/MockFile.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/TestingHelpers/MockFile.cs b/TestingHelpers/MockFile.cs index 3b9c49cbc..1de801363 100644 --- a/TestingHelpers/MockFile.cs +++ b/TestingHelpers/MockFile.cs @@ -101,6 +101,7 @@ public override void Copy(string sourceFileName, string destFileName, bool overw { if (!Exists(sourceFileName)) { + throw new FileNotFoundException(); } if (sourceFileName == null) { From eca251965c6d0aea6b2f9416fbf99cbc35f0478f Mon Sep 17 00:00:00 2001 From: Alexander Pruss Date: Mon, 21 Nov 2016 17:12:47 +0100 Subject: [PATCH 06/11] Rearranged order of checks. --- TestingHelpers/MockFile.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/TestingHelpers/MockFile.cs b/TestingHelpers/MockFile.cs index 1de801363..75dd86935 100644 --- a/TestingHelpers/MockFile.cs +++ b/TestingHelpers/MockFile.cs @@ -99,10 +99,7 @@ public override void Copy(string sourceFileName, string destFileName) public override void Copy(string sourceFileName, string destFileName, bool overwrite) { - if (!Exists(sourceFileName)) - { - throw new FileNotFoundException(); - } + //This is the order that checks are executed in MSCORLIB. if (sourceFileName == null) { throw new ArgumentNullException("sourceFileName", Properties.Resources.FILENAME_CANNOT_BE_NULL); @@ -112,9 +109,12 @@ public override void Copy(string sourceFileName, string destFileName, bool overw { throw new ArgumentNullException("destFileName", Properties.Resources.FILENAME_CANNOT_BE_NULL); } - mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, "sourceFileName"); mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, "destFileName"); + if (!Exists(sourceFileName)) + { + throw new FileNotFoundException(); + } var directoryNameOfDestination = mockPath.GetDirectoryName(destFileName); if (!mockFileDataAccessor.Directory.Exists(directoryNameOfDestination)) From de8908731221e1dfe849c6dae6fbfd78f7812cd9 Mon Sep 17 00:00:00 2001 From: Alexander Pruss Date: Tue, 22 Nov 2016 10:24:11 +0100 Subject: [PATCH 07/11] Caught another edge case. --- TestHelpers.Tests/MockFileInfoTests.cs | 1 + TestingHelpers/MockFileInfo.cs | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/TestHelpers.Tests/MockFileInfoTests.cs b/TestHelpers.Tests/MockFileInfoTests.cs index d7c8a1dcf..6c57e73b7 100644 --- a/TestHelpers.Tests/MockFileInfoTests.cs +++ b/TestHelpers.Tests/MockFileInfoTests.cs @@ -490,6 +490,7 @@ public void MockFileInfo_MoveTo_SameSourceAndTargetIsANoOp() fileInfo.MoveTo(destination); Assert.AreEqual(fileInfo.FullName, destination); + Assert.True(fileInfo.Exists); } [Test] diff --git a/TestingHelpers/MockFileInfo.cs b/TestingHelpers/MockFileInfo.cs index e17da6416..b4144f7e3 100644 --- a/TestingHelpers/MockFileInfo.cs +++ b/TestingHelpers/MockFileInfo.cs @@ -217,6 +217,10 @@ public override FileSecurity GetAccessControl(AccessControlSections includeSecti public override void MoveTo(string destFileName) { var movedFileInfo = CopyTo(destFileName); + if (destFileName == FullName) + { + return; + } Delete(); path = movedFileInfo.FullName; } From afd95c43e47dc3d42e09dd071099a91724bfae10 Mon Sep 17 00:00:00 2001 From: John Reese Date: Sat, 7 Jul 2018 12:06:03 -0400 Subject: [PATCH 08/11] Leverage Assert.Throws in tests --- TestHelpers.Tests/MockFileCopyTests.cs | 10 ++++++---- TestHelpers.Tests/MockFileInfoTests.cs | 15 ++++++--------- TestHelpers.Tests/MockFileMoveTests.cs | 10 ++++++---- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/TestHelpers.Tests/MockFileCopyTests.cs b/TestHelpers.Tests/MockFileCopyTests.cs index 00655b399..02028c487 100644 --- a/TestHelpers.Tests/MockFileCopyTests.cs +++ b/TestHelpers.Tests/MockFileCopyTests.cs @@ -273,23 +273,25 @@ public void MockFile_Copy_ShouldThrowArgumentExceptionWhenTargetIsEmpty_Message( } [Test] - [ExpectedException(typeof(FileNotFoundException))] public void MockFile_Copy_ShouldThrowExceptionWhenSourceDoesNotExist() { string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); var fileSystem = new MockFileSystem(); - fileSystem.File.Copy(sourceFilePath, XFS.Path(@"c:\something\demo2.txt")); + TestDelegate action = () => fileSystem.File.Copy(sourceFilePath, XFS.Path(@"c:\something\demo2.txt")); + + Assert.Throws(action); } [Test] - [ExpectedException(typeof(FileNotFoundException))] public void MockFile_Copy_ShouldThrowExceptionWhenSourceDoesNotExist_EvenWhenCopyingToItself() { string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); var fileSystem = new MockFileSystem(); - fileSystem.File.Copy(sourceFilePath, XFS.Path(@"c:\something\demo.txt")); + TestDelegate action = () => fileSystem.File.Copy(sourceFilePath, XFS.Path(@"c:\something\demo.txt")); + + Assert.Throws(action); } } } \ No newline at end of file diff --git a/TestHelpers.Tests/MockFileInfoTests.cs b/TestHelpers.Tests/MockFileInfoTests.cs index 316304759..dbf9cdc5c 100644 --- a/TestHelpers.Tests/MockFileInfoTests.cs +++ b/TestHelpers.Tests/MockFileInfoTests.cs @@ -495,7 +495,6 @@ public void MockFileInfo_MoveTo_SameSourceAndTargetIsANoOp() } [Test] - [ExpectedException(typeof(FileNotFoundException))] public void MockFileInfo_MoveTo_SameSourceAndTargetThrowsExceptionIfSourceDoesntExist() { // Arrange @@ -504,13 +503,12 @@ public void MockFileInfo_MoveTo_SameSourceAndTargetThrowsExceptionIfSourceDoesnt // Act string destination = XFS.Path(XFS.Path(@"c:\temp\file.txt")); - fileInfo.MoveTo(destination); + TestDelegate action = () => fileInfo.MoveTo(destination); - Assert.AreEqual(fileInfo.FullName, destination); + Assert.Throws(action); } [Test] - [ExpectedException(typeof(FileNotFoundException))] public void MockFileInfo_MoveTo_ThrowsExceptionIfSourceDoesntExist() { // Arrange @@ -519,13 +517,12 @@ public void MockFileInfo_MoveTo_ThrowsExceptionIfSourceDoesntExist() // Act string destination = XFS.Path(XFS.Path(@"c:\temp\file2.txt")); - fileInfo.MoveTo(destination); + TestDelegate action = () => fileInfo.MoveTo(destination); - Assert.AreEqual(fileInfo.FullName, destination); + Assert.Throws(action); } [Test] - [ExpectedException(typeof(FileNotFoundException))] public void MockFileInfo_CopyTo_ThrowsExceptionIfSourceDoesntExist() { // Arrange @@ -534,9 +531,9 @@ public void MockFileInfo_CopyTo_ThrowsExceptionIfSourceDoesntExist() // Act string destination = XFS.Path(XFS.Path(@"c:\temp\file2.txt")); - fileInfo.CopyTo(destination); + TestDelegate action = () => fileInfo.CopyTo(destination); - Assert.AreEqual(fileInfo.FullName, destination); + Assert.Throws(action); } } } diff --git a/TestHelpers.Tests/MockFileMoveTests.cs b/TestHelpers.Tests/MockFileMoveTests.cs index ad2c12565..39c086b69 100644 --- a/TestHelpers.Tests/MockFileMoveTests.cs +++ b/TestHelpers.Tests/MockFileMoveTests.cs @@ -307,23 +307,25 @@ public void MockFile_Move_ShouldThrowDirectoryNotFoundExceptionWhenSourcePathDoe } [Test] - [ExpectedException(typeof(FileNotFoundException))] public void MockFile_Move_ShouldThrowExceptionWhenSourceDoesNotExist() { string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); var fileSystem = new MockFileSystem(); - fileSystem.File.Move(sourceFilePath, XFS.Path(@"c:\something\demo2.txt")); + TestDelegate action = () => fileSystem.File.Move(sourceFilePath, XFS.Path(@"c:\something\demo2.txt")); + + Assert.Throws(action); } [Test] - [ExpectedException(typeof(FileNotFoundException))] public void MockFile_Move_ShouldThrowExceptionWhenSourceDoesNotExist_EvenWhenCopyingToItself() { string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); var fileSystem = new MockFileSystem(); - fileSystem.File.Move(sourceFilePath, XFS.Path(@"c:\something\demo.txt")); + TestDelegate action = () => fileSystem.File.Move(sourceFilePath, XFS.Path(@"c:\something\demo.txt")); + + Assert.Throws(action); } } } \ No newline at end of file From 11715945fd93f068d61eba794dbbba1f52876530 Mon Sep 17 00:00:00 2001 From: John Reese Date: Sat, 7 Jul 2018 12:10:39 -0400 Subject: [PATCH 09/11] General cleanup --- TestHelpers.Tests/MockFileMoveTests.cs | 12 ++++++------ TestingHelpers/MockFile.cs | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/TestHelpers.Tests/MockFileMoveTests.cs b/TestHelpers.Tests/MockFileMoveTests.cs index 39c086b69..4dab33fff 100644 --- a/TestHelpers.Tests/MockFileMoveTests.cs +++ b/TestHelpers.Tests/MockFileMoveTests.cs @@ -1,14 +1,12 @@ namespace System.IO.Abstractions.TestingHelpers.Tests { using Collections.Generic; - using Linq; - using NUnit.Framework; - using XFS = MockUnixSupport; - public class MockFileMoveTests { + public class MockFileMoveTests + { [Test] public void MockFile_Move_ShouldMoveFileWithinMemoryFileSystem() { @@ -260,7 +258,8 @@ public void MockFile_Move_ShouldThrowArgumentExceptionWhenTargetIsEmpty_Message( } [Test] - public void MockFile_Move_ShouldThrowArgumentExceptionWhenTargetIsEmpty_ParamName() { + public void MockFile_Move_ShouldThrowArgumentExceptionWhenTargetIsEmpty_ParamName() + { string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); var fileSystem = new MockFileSystem(); @@ -282,7 +281,8 @@ public void MockFile_Move_ShouldThrowFileNotFoundExceptionWhenSourceDoesNotExist } [Test] - public void MockFile_Move_ShouldThrowFileNotFoundExceptionWhenSourceDoesNotExist_FileName() { + public void MockFile_Move_ShouldThrowFileNotFoundExceptionWhenSourceDoesNotExist_FileName() + { string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); string destFilePath = XFS.Path(@"c:\something\demo1.txt"); var fileSystem = new MockFileSystem(); diff --git a/TestingHelpers/MockFile.cs b/TestingHelpers/MockFile.cs index aa9d61030..5ab79635d 100644 --- a/TestingHelpers/MockFile.cs +++ b/TestingHelpers/MockFile.cs @@ -99,7 +99,6 @@ public override void Copy(string sourceFileName, string destFileName) public override void Copy(string sourceFileName, string destFileName, bool overwrite) { - //This is the order that checks are executed in MSCORLIB. if (sourceFileName == null) { throw new ArgumentNullException("sourceFileName", StringResources.Manager.GetString("FILENAME_CANNOT_BE_NULL")); @@ -109,8 +108,10 @@ public override void Copy(string sourceFileName, string destFileName, bool overw { throw new ArgumentNullException("destFileName", StringResources.Manager.GetString("FILENAME_CANNOT_BE_NULL")); } + mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, "sourceFileName"); mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, "destFileName"); + if (!Exists(sourceFileName)) { throw new FileNotFoundException(); From bf0c5fec9f0b794fbffe79bff8ed4870eb2931b1 Mon Sep 17 00:00:00 2001 From: John Reese Date: Sat, 7 Jul 2018 12:12:47 -0400 Subject: [PATCH 10/11] Fix brace for else statement --- TestingHelpers/MockFile.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TestingHelpers/MockFile.cs b/TestingHelpers/MockFile.cs index 5ab79635d..fa6485a92 100644 --- a/TestingHelpers/MockFile.cs +++ b/TestingHelpers/MockFile.cs @@ -348,7 +348,8 @@ public override void Move(string sourceFileName, string destFileName) if (destFileName.Equals(sourceFileName)) { return; - } else + } + else { throw new IOException("A file can not be created if it already exists."); } From cd6b6c565f3b73d6b85599f8aabb90fa57c4667e Mon Sep 17 00:00:00 2001 From: John Reese Date: Sat, 7 Jul 2018 12:31:33 -0400 Subject: [PATCH 11/11] General cleanup while were here --- TestHelpers.Tests/MockFileCopyTests.cs | 8 +-- TestHelpers.Tests/MockFileInfoTests.cs | 99 ++++---------------------- TestingHelpers/MockFileInfo.cs | 1 - 3 files changed, 17 insertions(+), 91 deletions(-) diff --git a/TestHelpers.Tests/MockFileCopyTests.cs b/TestHelpers.Tests/MockFileCopyTests.cs index 02028c487..6e78bf086 100644 --- a/TestHelpers.Tests/MockFileCopyTests.cs +++ b/TestHelpers.Tests/MockFileCopyTests.cs @@ -1,16 +1,14 @@ namespace System.IO.Abstractions.TestingHelpers.Tests { using Collections.Generic; - using Globalization; - using Linq; - using NUnit.Framework; - using XFS = MockUnixSupport; - public class MockFileCopyTests { + public class MockFileCopyTests + { + [Test] public void MockFile_Copy_ShouldOverwriteFileWhenOverwriteFlagIsTrue() { diff --git a/TestHelpers.Tests/MockFileInfoTests.cs b/TestHelpers.Tests/MockFileInfoTests.cs index dbf9cdc5c..aedd6d836 100644 --- a/TestHelpers.Tests/MockFileInfoTests.cs +++ b/TestHelpers.Tests/MockFileInfoTests.cs @@ -12,7 +12,6 @@ public class MockFileInfoTests [Test] public void MockFileInfo_Exists_ShouldReturnTrueIfFileExistsInMemoryFileSystem() { - // Arrange var fileSystem = new MockFileSystem(new Dictionary { { XFS.Path(@"c:\a.txt"), new MockFileData("Demo text content") }, @@ -20,17 +19,14 @@ public void MockFileInfo_Exists_ShouldReturnTrueIfFileExistsInMemoryFileSystem() }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act var result = fileInfo.Exists; - // Assert Assert.IsTrue(result); } [Test] public void MockFileInfo_Exists_ShouldReturnFalseIfFileDoesNotExistInMemoryFileSystem() { - // Arrange var fileSystem = new MockFileSystem(new Dictionary { { XFS.Path(@"c:\a.txt"), new MockFileData("Demo text content") }, @@ -38,17 +34,14 @@ public void MockFileInfo_Exists_ShouldReturnFalseIfFileDoesNotExistInMemoryFileS }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\foo.txt")); - // Act var result = fileInfo.Exists; - // Assert Assert.IsFalse(result); } [Test] public void MockFileInfo_Length_ShouldReturnLengthOfFileInMemoryFileSystem() { - // Arrange const string fileContent = "Demo text content"; var fileSystem = new MockFileSystem(new Dictionary { @@ -57,17 +50,14 @@ public void MockFileInfo_Length_ShouldReturnLengthOfFileInMemoryFileSystem() }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act var result = fileInfo.Length; - // Assert Assert.AreEqual(fileContent.Length, result); } [Test] public void MockFileInfo_Length_ShouldThrowFileNotFoundExceptionIfFileDoesNotExistInMemoryFileSystem() { - // Arrange const string fileContent = "Demo text content"; var fileSystem = new MockFileSystem(new Dictionary { @@ -76,16 +66,14 @@ public void MockFileInfo_Length_ShouldThrowFileNotFoundExceptionIfFileDoesNotExi }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\foo.txt")); -// ReSharper disable ReturnValueOfPureMethodIsNotUsed var ex = Assert.Throws(() => fileInfo.Length.ToString(CultureInfo.InvariantCulture)); -// ReSharper restore ReturnValueOfPureMethodIsNotUsed + Assert.AreEqual(XFS.Path(@"c:\foo.txt"), ex.FileName); } [Test] public void MockFileInfo_CreationTimeUtc_ShouldReturnCreationTimeUtcOfFileInMemoryFileSystem() { - // Arrange var creationTime = DateTime.Now.AddHours(-4); var fileData = new MockFileData("Demo text content") { CreationTime = creationTime }; var fileSystem = new MockFileSystem(new Dictionary @@ -94,17 +82,14 @@ public void MockFileInfo_CreationTimeUtc_ShouldReturnCreationTimeUtcOfFileInMemo }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act var result = fileInfo.CreationTimeUtc; - // Assert Assert.AreEqual(creationTime.ToUniversalTime(), result); } [Test] public void MockFileInfo_CreationTimeUtc_ShouldSetCreationTimeUtcOfFileInMemoryFileSystem() { - // Arrange var creationTime = DateTime.Now.AddHours(-4); var fileData = new MockFileData("Demo text content") { CreationTime = creationTime }; var fileSystem = new MockFileSystem(new Dictionary @@ -113,11 +98,9 @@ public void MockFileInfo_CreationTimeUtc_ShouldSetCreationTimeUtcOfFileInMemoryF }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act var newUtcTime = DateTime.UtcNow; fileInfo.CreationTimeUtc = newUtcTime; - // Assert Assert.AreEqual(newUtcTime, fileInfo.CreationTimeUtc); } @@ -125,7 +108,6 @@ public void MockFileInfo_CreationTimeUtc_ShouldSetCreationTimeUtcOfFileInMemoryF [Test] public void MockFileInfo_CreationTime_ShouldReturnCreationTimeOfFileInMemoryFileSystem() { - // Arrange var creationTime = DateTime.Now.AddHours(-4); var fileData = new MockFileData("Demo text content") { CreationTime = creationTime }; var fileSystem = new MockFileSystem(new Dictionary @@ -134,17 +116,14 @@ public void MockFileInfo_CreationTime_ShouldReturnCreationTimeOfFileInMemoryFile }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act var result = fileInfo.CreationTime; - // Assert Assert.AreEqual(creationTime, result); } [Test] public void MockFileInfo_CreationTime_ShouldSetCreationTimeOfFileInMemoryFileSystem() { - // Arrange var creationTime = DateTime.Now.AddHours(-4); var fileData = new MockFileData("Demo text content") { CreationTime = creationTime }; var fileSystem = new MockFileSystem(new Dictionary @@ -152,19 +131,16 @@ public void MockFileInfo_CreationTime_ShouldSetCreationTimeOfFileInMemoryFileSys { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - - // Act var newTime = DateTime.Now; + fileInfo.CreationTime = newTime; - // Assert Assert.AreEqual(newTime, fileInfo.CreationTime); } [Test] public void MockFileInfo_IsReadOnly_ShouldSetReadOnlyAttributeOfFileInMemoryFileSystem() { - // Arrange var fileData = new MockFileData("Demo text content"); var fileSystem = new MockFileSystem(new Dictionary { @@ -172,17 +148,14 @@ public void MockFileInfo_IsReadOnly_ShouldSetReadOnlyAttributeOfFileInMemoryFile }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act fileInfo.IsReadOnly = true; - // Assert Assert.AreEqual(FileAttributes.ReadOnly, fileData.Attributes & FileAttributes.ReadOnly); } [Test] public void MockFileInfo_IsReadOnly_ShouldSetNotReadOnlyAttributeOfFileInMemoryFileSystem() { - // Arrange var fileData = new MockFileData("Demo text content") {Attributes = FileAttributes.ReadOnly}; var fileSystem = new MockFileSystem(new Dictionary { @@ -190,17 +163,14 @@ public void MockFileInfo_IsReadOnly_ShouldSetNotReadOnlyAttributeOfFileInMemoryF }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act fileInfo.IsReadOnly = false; - // Assert Assert.AreNotEqual(FileAttributes.ReadOnly, fileData.Attributes & FileAttributes.ReadOnly); } [Test] public void MockFileInfo_AppendText_ShouldAddTextToFileInMemoryFileSystem() { - // Arrange var fileData = new MockFileData("Demo text content"); var fileSystem = new MockFileSystem(new Dictionary { @@ -208,22 +178,21 @@ public void MockFileInfo_AppendText_ShouldAddTextToFileInMemoryFileSystem() }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act using (var file = fileInfo.AppendText()) file.WriteLine("This should be at the end"); string newcontents; using (var newfile = fileInfo.OpenText()) + { newcontents = newfile.ReadToEnd(); + } - // Assert Assert.AreEqual($"Demo text contentThis should be at the end{Environment.NewLine}", newcontents); } [Test] public void MockFileInfo_OpenWrite_ShouldAddDataToFileInMemoryFileSystem() { - // Arrange var fileData = new MockFileData("Demo text content"); var fileSystem = new MockFileSystem(new Dictionary { @@ -232,22 +201,24 @@ public void MockFileInfo_OpenWrite_ShouldAddDataToFileInMemoryFileSystem() var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); var bytesToAdd = new byte[] {65, 66, 67, 68, 69}; - // Act + using (var file = fileInfo.OpenWrite()) + { file.Write(bytesToAdd, 0, bytesToAdd.Length); + } string newcontents; using (var newfile = fileInfo.OpenText()) + { newcontents = newfile.ReadToEnd(); + } - // Assert Assert.AreEqual("ABCDEtext content", newcontents); } #if NET40 [Test] public void MockFileInfo_Encrypt_ShouldReturnXorOfFileInMemoryFileSystem() { - // Arrange var fileData = new MockFileData("Demo text content"); var fileSystem = new MockFileSystem(new Dictionary { @@ -255,23 +226,19 @@ public void MockFileInfo_Encrypt_ShouldReturnXorOfFileInMemoryFileSystem() }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act fileInfo.Encrypt(); - string newcontents; using (var newfile = fileInfo.OpenText()) { newcontents = newfile.ReadToEnd(); } - // Assert Assert.AreNotEqual("Demo text content", newcontents); } [Test] public void MockFileInfo_Decrypt_ShouldReturnCorrectContentsFileInMemoryFileSystem() { - // Arrange var fileData = new MockFileData("Demo text content"); var fileSystem = new MockFileSystem(new Dictionary { @@ -280,7 +247,6 @@ public void MockFileInfo_Decrypt_ShouldReturnCorrectContentsFileInMemoryFileSyst var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); fileInfo.Encrypt(); - // Act fileInfo.Decrypt(); string newcontents; @@ -289,7 +255,6 @@ public void MockFileInfo_Decrypt_ShouldReturnCorrectContentsFileInMemoryFileSyst newcontents = newfile.ReadToEnd(); } - // Assert Assert.AreEqual("Demo text content", newcontents); } #endif @@ -297,7 +262,6 @@ public void MockFileInfo_Decrypt_ShouldReturnCorrectContentsFileInMemoryFileSyst [Test] public void MockFileInfo_LastAccessTimeUtc_ShouldReturnLastAccessTimeUtcOfFileInMemoryFileSystem() { - // Arrange var lastAccessTime = DateTime.Now.AddHours(-4); var fileData = new MockFileData("Demo text content") { LastAccessTime = lastAccessTime }; var fileSystem = new MockFileSystem(new Dictionary @@ -306,17 +270,14 @@ public void MockFileInfo_LastAccessTimeUtc_ShouldReturnLastAccessTimeUtcOfFileIn }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act var result = fileInfo.LastAccessTimeUtc; - // Assert Assert.AreEqual(lastAccessTime.ToUniversalTime(), result); } [Test] public void MockFileInfo_LastAccessTimeUtc_ShouldSetCreationTimeUtcOfFileInMemoryFileSystem() { - // Arrange var lastAccessTime = DateTime.Now.AddHours(-4); var fileData = new MockFileData("Demo text content") { LastAccessTime = lastAccessTime }; var fileSystem = new MockFileSystem(new Dictionary @@ -325,18 +286,15 @@ public void MockFileInfo_LastAccessTimeUtc_ShouldSetCreationTimeUtcOfFileInMemor }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act var newUtcTime = DateTime.UtcNow; fileInfo.LastAccessTimeUtc = newUtcTime; - // Assert Assert.AreEqual(newUtcTime, fileInfo.LastAccessTimeUtc); } [Test] public void MockFileInfo_LastWriteTimeUtc_ShouldReturnLastWriteTimeUtcOfFileInMemoryFileSystem() { - // Arrange var lastWriteTime = DateTime.Now.AddHours(-4); var fileData = new MockFileData("Demo text content") { LastWriteTime = lastWriteTime }; var fileSystem = new MockFileSystem(new Dictionary @@ -345,17 +303,14 @@ public void MockFileInfo_LastWriteTimeUtc_ShouldReturnLastWriteTimeUtcOfFileInMe }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act var result = fileInfo.LastWriteTimeUtc; - // Assert Assert.AreEqual(lastWriteTime.ToUniversalTime(), result); } [Test] public void MockFileInfo_LastWriteTimeUtc_ShouldSetLastWriteTimeUtcOfFileInMemoryFileSystem() { - // Arrange var lastWriteTime = DateTime.Now.AddHours(-4); var fileData = new MockFileData("Demo text content") { LastWriteTime = lastWriteTime }; var fileSystem = new MockFileSystem(new Dictionary @@ -364,49 +319,39 @@ public void MockFileInfo_LastWriteTimeUtc_ShouldSetLastWriteTimeUtcOfFileInMemor }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act var newUtcTime = DateTime.UtcNow; fileInfo.LastWriteTime = newUtcTime; - // Assert Assert.AreEqual(newUtcTime, fileInfo.LastWriteTime); } [Test] public void MockFileInfo_GetExtension_ShouldReturnExtension() { - // Arrange var fileSystem = new MockFileSystem(new Dictionary()); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - // Act var result = fileInfo.Extension; - // Assert Assert.AreEqual(".txt", result); } [Test] public void MockFileInfo_GetExtensionWithoutExtension_ShouldReturnEmptyString() { - // Arrange var fileSystem = new MockFileSystem(new Dictionary()); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a")); - // Act var result = fileInfo.Extension; - // Assert Assert.AreEqual(string.Empty, result); } [Test] public void MockFileInfo_GetDirectoryName_ShouldReturnCompleteDirectoryPath() { - // Arrange var fileInfo = new MockFileInfo(new MockFileSystem(), XFS.Path(@"c:\temp\level1\level2\file.txt")); - // Act var result = fileInfo.DirectoryName; Assert.AreEqual(XFS.Path(@"c:\temp\level1\level2"), result); @@ -415,10 +360,8 @@ public void MockFileInfo_GetDirectoryName_ShouldReturnCompleteDirectoryPath() [Test] public void MockFileInfo_GetDirectory_ShouldReturnDirectoryInfoWithCorrectPath() { - // Arrange var fileInfo = new MockFileInfo(new MockFileSystem(), XFS.Path(@"c:\temp\level1\level2\file.txt")); - // Act var result = fileInfo.Directory; Assert.AreEqual(XFS.Path(@"c:\temp\level1\level2"), result.FullName); @@ -427,12 +370,10 @@ public void MockFileInfo_GetDirectory_ShouldReturnDirectoryInfoWithCorrectPath() [Test] public void MockFileInfo_OpenRead_ShouldReturnByteContentOfFile() { - // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddFile(XFS.Path(@"c:\temp\file.txt"), new MockFileData(new byte[] { 1, 2 })); var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); - // Act byte[] result = new byte[2]; using (var stream = fileInfo.OpenRead()) { @@ -445,12 +386,10 @@ public void MockFileInfo_OpenRead_ShouldReturnByteContentOfFile() [Test] public void MockFileInfo_OpenText_ShouldReturnStringContentOfFile() { - // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddFile(XFS.Path(@"c:\temp\file.txt"), new MockFileData(@"line 1\r\nline 2")); var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); - // Act string result; using (var streamReader = fileInfo.OpenText()) { @@ -463,15 +402,13 @@ public void MockFileInfo_OpenText_ShouldReturnStringContentOfFile() [Test] public void MockFileInfo_MoveTo_ShouldUpdateFileInfoDirectoryAndFullName() { - // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddFile(XFS.Path(@"c:\temp\file.txt"), new MockFileData(@"line 1\r\nline 2")); var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); - - // Act string destinationFolder = XFS.Path(@"c:\temp2"); string destination = XFS.Path(destinationFolder + @"\file.txt"); fileSystem.AddDirectory(destination); + fileInfo.MoveTo(destination); Assert.AreEqual(fileInfo.DirectoryName, destinationFolder); @@ -481,13 +418,11 @@ public void MockFileInfo_MoveTo_ShouldUpdateFileInfoDirectoryAndFullName() [Test] public void MockFileInfo_MoveTo_SameSourceAndTargetIsANoOp() { - // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddFile(XFS.Path(@"c:\temp\file.txt"), new MockFileData(@"line 1\r\nline 2")); var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); - - // Act string destination = XFS.Path(XFS.Path(@"c:\temp\file.txt")); + fileInfo.MoveTo(destination); Assert.AreEqual(fileInfo.FullName, destination); @@ -497,12 +432,10 @@ public void MockFileInfo_MoveTo_SameSourceAndTargetIsANoOp() [Test] public void MockFileInfo_MoveTo_SameSourceAndTargetThrowsExceptionIfSourceDoesntExist() { - // Arrange var fileSystem = new MockFileSystem(); var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); - - // Act string destination = XFS.Path(XFS.Path(@"c:\temp\file.txt")); + TestDelegate action = () => fileInfo.MoveTo(destination); Assert.Throws(action); @@ -511,12 +444,10 @@ public void MockFileInfo_MoveTo_SameSourceAndTargetThrowsExceptionIfSourceDoesnt [Test] public void MockFileInfo_MoveTo_ThrowsExceptionIfSourceDoesntExist() { - // Arrange var fileSystem = new MockFileSystem(); var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); - - // Act string destination = XFS.Path(XFS.Path(@"c:\temp\file2.txt")); + TestDelegate action = () => fileInfo.MoveTo(destination); Assert.Throws(action); @@ -525,12 +456,10 @@ public void MockFileInfo_MoveTo_ThrowsExceptionIfSourceDoesntExist() [Test] public void MockFileInfo_CopyTo_ThrowsExceptionIfSourceDoesntExist() { - // Arrange var fileSystem = new MockFileSystem(); var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); - - // Act string destination = XFS.Path(XFS.Path(@"c:\temp\file2.txt")); + TestDelegate action = () => fileInfo.CopyTo(destination); Assert.Throws(action); diff --git a/TestingHelpers/MockFileInfo.cs b/TestingHelpers/MockFileInfo.cs index d011e3bf1..97eac2386 100644 --- a/TestingHelpers/MockFileInfo.cs +++ b/TestingHelpers/MockFileInfo.cs @@ -156,7 +156,6 @@ public override StreamWriter AppendText() { if (MockFileData == null) throw new FileNotFoundException("File not found", path); return new StreamWriter(new MockFileStream(mockFileSystem, FullName, MockFileStream.StreamType.APPEND)); - //return ((MockFileDataModifier) MockFileData).AppendText(); } public override FileInfoBase CopyTo(string destFileName)