diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs index 72cf08c7e..7b3372b63 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs @@ -1164,7 +1164,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 @@ -1182,6 +1182,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:\demo.txt"); + string sourceFileContent = "this is some content"; + + var fileSystem = new MockFileSystem(new Dictionary + { + { sourceFilePath, new MockFileData(sourceFileContent) } + }); + + string destFilePath = XFS.Path(@"c:\demo1.txt"); + + fileSystem.Directory.Move(sourceFilePath, destFilePath); + + Assert.That(fileSystem.FileExists(destFilePath), Is.True); + Assert.That(fileSystem.FileExists(sourceFilePath), Is.False); + Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent)); + } + [Test] public void MockDirectory_Move_ShouldMoveDirectoryAtrributes() { diff --git a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs index 280d88948..cd3b2519b 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs @@ -77,7 +77,7 @@ public override void Delete(string path, bool recursive) var stringOps = mockFileDataAccessor.StringOperations; var pathWithDirectorySeparatorChar = $"{path}{Path.DirectorySeparatorChar}"; - + var affectedPaths = mockFileDataAccessor .AllPaths .Where(p => stringOps.Equals(p, path) || stringOps.StartsWith(p, pathWithDirectorySeparatorChar)) @@ -87,12 +87,12 @@ public override void Delete(string path, bool recursive) { throw new DirectoryNotFoundException(path + " does not exist or could not be found."); } - + if (!recursive && affectedPaths.Count > 1) { throw new IOException("The directory specified by " + path + " is read-only, or recursive is false and " + path + " is not an empty directory."); } - + foreach (var affectedPath in affectedPaths) { mockFileDataAccessor.RemoveFile(affectedPath); @@ -378,6 +378,14 @@ public override void Move(string sourceDirName, string destDirName) throw new IOException("Source and destination path must be different."); } + //if we're moving a file, not a directory, call the appropriate file moving function. + var fileData = mockFileDataAccessor.GetFile(fullSourcePath); + if (fileData?.Attributes.HasFlag(FileAttributes.Directory) == false) + { + mockFileDataAccessor.File.Move(fullSourcePath, fullDestPath); + return; + } + var sourceRoot = mockFileDataAccessor.Path.GetPathRoot(fullSourcePath); var destinationRoot = mockFileDataAccessor.Path.GetPathRoot(fullDestPath);