diff --git a/TestHelpers.Tests/MockFileCopyTests.cs b/TestHelpers.Tests/MockFileCopyTests.cs index c70e1674b..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() { @@ -271,5 +269,27 @@ public void MockFile_Copy_ShouldThrowArgumentExceptionWhenTargetIsEmpty_Message( Assert.That(exception.Message, Does.StartWith("Empty file name is not legal.")); } + + [Test] + public void MockFile_Copy_ShouldThrowExceptionWhenSourceDoesNotExist() + { + string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); + var fileSystem = new MockFileSystem(); + + TestDelegate action = () => fileSystem.File.Copy(sourceFilePath, XFS.Path(@"c:\something\demo2.txt")); + + Assert.Throws(action); + } + + [Test] + public void MockFile_Copy_ShouldThrowExceptionWhenSourceDoesNotExist_EvenWhenCopyingToItself() + { + string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); + var fileSystem = new MockFileSystem(); + + 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 10e9b600f..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,19 +402,67 @@ 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); Assert.AreEqual(fileInfo.FullName, destination); } + + [Test] + public void MockFileInfo_MoveTo_SameSourceAndTargetIsANoOp() + { + 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")); + string destination = XFS.Path(XFS.Path(@"c:\temp\file.txt")); + + fileInfo.MoveTo(destination); + + Assert.AreEqual(fileInfo.FullName, destination); + Assert.True(fileInfo.Exists); + } + + [Test] + public void MockFileInfo_MoveTo_SameSourceAndTargetThrowsExceptionIfSourceDoesntExist() + { + var fileSystem = new MockFileSystem(); + var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); + string destination = XFS.Path(XFS.Path(@"c:\temp\file.txt")); + + TestDelegate action = () => fileInfo.MoveTo(destination); + + Assert.Throws(action); + } + + [Test] + public void MockFileInfo_MoveTo_ThrowsExceptionIfSourceDoesntExist() + { + var fileSystem = new MockFileSystem(); + var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); + string destination = XFS.Path(XFS.Path(@"c:\temp\file2.txt")); + + TestDelegate action = () => fileInfo.MoveTo(destination); + + Assert.Throws(action); + } + + [Test] + public void MockFileInfo_CopyTo_ThrowsExceptionIfSourceDoesntExist() + { + var fileSystem = new MockFileSystem(); + var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); + string destination = XFS.Path(XFS.Path(@"c:\temp\file2.txt")); + + TestDelegate action = () => fileInfo.CopyTo(destination); + + Assert.Throws(action); + } } } diff --git a/TestHelpers.Tests/MockFileMoveTests.cs b/TestHelpers.Tests/MockFileMoveTests.cs index 781355adc..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() { @@ -29,6 +27,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() { @@ -241,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(); @@ -263,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(); @@ -286,5 +305,27 @@ 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] + public void MockFile_Move_ShouldThrowExceptionWhenSourceDoesNotExist() + { + string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); + var fileSystem = new MockFileSystem(); + + TestDelegate action = () => fileSystem.File.Move(sourceFilePath, XFS.Path(@"c:\something\demo2.txt")); + + Assert.Throws(action); + } + + [Test] + public void MockFile_Move_ShouldThrowExceptionWhenSourceDoesNotExist_EvenWhenCopyingToItself() + { + string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); + var fileSystem = new MockFileSystem(); + + TestDelegate action = () => fileSystem.File.Move(sourceFilePath, XFS.Path(@"c:\something\demo.txt")); + + Assert.Throws(action); + } } } \ No newline at end of file diff --git a/TestingHelpers/MockFile.cs b/TestingHelpers/MockFile.cs index fcb18b113..fa6485a92 100644 --- a/TestingHelpers/MockFile.cs +++ b/TestingHelpers/MockFile.cs @@ -112,6 +112,11 @@ public override void Copy(string sourceFileName, string destFileName, bool overw 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)) { @@ -339,7 +344,17 @@ 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 0b4f29fbe..7aa9db32c 100644 --- a/TestingHelpers/MockFileInfo.cs +++ b/TestingHelpers/MockFileInfo.cs @@ -156,17 +156,23 @@ 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) { - 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); } @@ -218,6 +224,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; }