From cd92acc71b99c919f8386e2dc5bad2397fefba6d Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Mon, 7 Jan 2019 21:12:10 +0100 Subject: [PATCH 1/2] Enable MockDirectory.Move to also move files Fixes #187 Supersedes #188 --- .../MockDirectoryTests.cs | 23 ++++++++++++++++++- .../MockDirectory.cs | 14 ++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs index 72cf08c7e..e61eb91c1 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,27 @@ 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"), MockFileData.NullObject } + }); + + 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_Move_ShouldMoveDirectoryAtrributes() { diff --git a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs index 280d88948..75bf501f3 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 != null && (fileData.Attributes & FileAttributes.Directory) == 0) + { + mockFileDataAccessor.File.Move(fullSourcePath, fullDestPath); + return; + } + var sourceRoot = mockFileDataAccessor.Path.GetPathRoot(fullSourcePath); var destinationRoot = mockFileDataAccessor.Path.GetPathRoot(fullDestPath); From e74649dedd39f01b1fe529d0b2692a65171efbaf Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Sun, 27 Jan 2019 20:50:41 +0100 Subject: [PATCH 2/2] Address review feedback --- .../MockDirectoryTests.cs | 9 ++++----- System.IO.Abstractions.TestingHelpers/MockDirectory.cs | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs index e61eb91c1..7b3372b63 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs @@ -1185,22 +1185,21 @@ public void MockDirectory_Move_ShouldMoveDirectories(string sourceDirName, strin [Test] public void MockDirectory_Move_ShouldMoveFiles() { - string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); + string sourceFilePath = XFS.Path(@"c:\demo.txt"); string sourceFileContent = "this is some content"; var fileSystem = new MockFileSystem(new Dictionary { - { sourceFilePath, new MockFileData(sourceFileContent) }, - { XFS.Path(@"c:\somethingelse\dummy.txt"), MockFileData.NullObject } + { sourceFilePath, new MockFileData(sourceFileContent) } }); - string destFilePath = XFS.Path(@"c:\somethingelse\demo1.txt"); + string destFilePath = XFS.Path(@"c:\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); + Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent)); } [Test] diff --git a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs index 75bf501f3..cd3b2519b 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs @@ -380,7 +380,7 @@ public override void Move(string sourceDirName, string destDirName) //if we're moving a file, not a directory, call the appropriate file moving function. var fileData = mockFileDataAccessor.GetFile(fullSourcePath); - if (fileData != null && (fileData.Attributes & FileAttributes.Directory) == 0) + if (fileData?.Attributes.HasFlag(FileAttributes.Directory) == false) { mockFileDataAccessor.File.Move(fullSourcePath, fullDestPath); return;