Skip to content

Commit 36e5b3f

Browse files
jfleppFlepp Jannfgreinacher
authored
fix: file move after directory move doesn't work correctly (#700)
Co-authored-by: Flepp Jann <JFlepp@Hamilton.ch> Co-authored-by: Florian Greinacher <florian@greinacher.de>
1 parent 6aca37a commit 36e5b3f

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ public void MoveDirectory(string sourcePath, string destPath)
249249
foreach (var path in affectedPaths)
250250
{
251251
var newPath = StringOperations.Replace(path, sourcePath, destPath);
252-
files[newPath] = files[path];
252+
var entry = files[path];
253+
entry.Path = newPath;
254+
files[newPath] = entry;
253255
files.Remove(path);
254256
}
255257
}

tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.Generic;
22
using System.Collections.ObjectModel;
33
using System.ComponentModel;
4-
using System.Diagnostics;
54
using System.Linq;
65
using System.Reflection;
76
using System.Text;
@@ -250,6 +249,20 @@ public void MockFileSystem_AddFilesFromEmbeddedResource_ShouldAddAllTheFiles()
250249
Assert.Contains(XFS.Path(@"C:\SecondTestFile.txt"), fileSystem.AllFiles.ToList());
251250
}
252251

252+
[Test]
253+
public void MockFileSystem_MoveDirectoryAndFile_ShouldMoveCorrectly()
254+
{
255+
var fileSystem = new MockFileSystem();
256+
fileSystem.AddFile(XFS.Path(@"C:\source\project.txt"), string.Empty);
257+
fileSystem.AddFile(XFS.Path(@"C:\source\subdir\other.txt"), string.Empty);
258+
259+
fileSystem.Directory.Move(XFS.Path(@"C:\source"), XFS.Path(@"C:\target"));
260+
fileSystem.File.Move(XFS.Path(@"C:\target\project.txt"), XFS.Path(@"C:\target\proj.txt"));
261+
262+
var expected = new[] { XFS.Path(@"C:\target\proj.txt"), XFS.Path(@"C:\target\subdir\other.txt") };
263+
CollectionAssert.AreEquivalent(expected, fileSystem.AllFiles);
264+
}
265+
253266
[Test]
254267
public void MockFileSystem_RemoveFile_RemovesFiles()
255268
{

0 commit comments

Comments
 (0)