From 34b193e3f664365d7725999d982171e336560fff Mon Sep 17 00:00:00 2001 From: Marco Eisenring Date: Sat, 31 Oct 2020 16:01:19 +0100 Subject: [PATCH 1/2] Fix #664 --- .../MockFileSystem.cs | 23 ++++++++++++++++++- .../MockDirectoryTests.cs | 21 +++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs b/src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs index d70b6a7cd..30a845213 100644 --- a/src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs +++ b/src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs @@ -236,10 +236,12 @@ public void MoveDirectory(string sourcePath, string destPath) sourcePath = FixPath(sourcePath); destPath = FixPath(destPath); + var sourcePathSequence = sourcePath.Split(new[] {Path.DirectorySeparatorChar}, StringSplitOptions.RemoveEmptyEntries); + lock (files) { var affectedPaths = files.Keys - .Where(p => StringOperations.StartsWith(p, sourcePath)) + .Where(p => PathStartsWith(p, sourcePathSequence)) .ToList(); foreach (var path in affectedPaths) @@ -249,6 +251,25 @@ public void MoveDirectory(string sourcePath, string destPath) files.Remove(path); } } + + bool PathStartsWith(string path, string[] minMatch) + { + var pathSequence = path.Split(new[] {Path.DirectorySeparatorChar}, StringSplitOptions.RemoveEmptyEntries); + if (pathSequence.Length < minMatch.Length) + { + return false; + } + + for (var i = 0; i < minMatch.Length; i++) + { + if (!StringOperations.Equals(minMatch[i], pathSequence[i])) + { + return false; + } + } + + return true; + } } public void RemoveFile(string path) diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs index cabcdb441..870cb52b4 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs @@ -1461,6 +1461,27 @@ public void MockDirectory_Move_ShouldMoveDirectoryWithReadOnlySubDirectory() Assert.IsTrue(fileSystem.FileExists(destSubDirName)); } + [Test] + public void MockDirectory_Move_ShouldOnlyMoveDirAndFilesWithinDir() + { + // Arrange + var fileSystem = new MockFileSystem(new Dictionary + { + {XFS.Path(@"c:\source\dummy"), new MockDirectoryData()}, + {XFS.Path(@"c:\source\dummy\content.txt"), new MockFileData(new byte[] {0})}, + {XFS.Path(@"c:\source\dummy.txt"), new MockFileData(new byte[] {0})}, + {XFS.Path(@"c:\source\dummy2"), new MockDirectoryData()}, + {XFS.Path(@"c:\destination"), new MockDirectoryData()}, + }); + + // Act + fileSystem.Directory.Move(@"c:\source\dummy", @"c:\destination\dummy"); + + // Assert + Assert.That(fileSystem.FileExists(@"c:\source\dummy.txt"), Is.True); + Assert.That(fileSystem.Directory.Exists(@"c:\source\dummy2"), Is.True); + } + [Test] public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemConstructor() { From 90749f871c1b4de0460a3bebe5fb52612f51c4f5 Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Sat, 21 Nov 2020 22:18:12 +0100 Subject: [PATCH 2/2] Apply suggestions from code review --- .../MockDirectoryTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs index 870cb52b4..cc5835976 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs @@ -1475,11 +1475,11 @@ public void MockDirectory_Move_ShouldOnlyMoveDirAndFilesWithinDir() }); // Act - fileSystem.Directory.Move(@"c:\source\dummy", @"c:\destination\dummy"); + fileSystem.Directory.Move(XFS.Path(@"c:\source\dummy"), XFS.Path(@"c:\destination\dummy")); // Assert - Assert.That(fileSystem.FileExists(@"c:\source\dummy.txt"), Is.True); - Assert.That(fileSystem.Directory.Exists(@"c:\source\dummy2"), Is.True); + Assert.That(fileSystem.FileExists(XFS.Path(@"c:\source\dummy.txt")), Is.True); + Assert.That(fileSystem.Directory.Exists(XFS.Path(@"c:\source\dummy2")), Is.True); } [Test]