Skip to content

Commit 9885122

Browse files
meisenringadvmeisenringfgreinacher
authored
fix: do not move unrelated files with same prefix (#665)
Fixes #664 Co-authored-by: Marco Eisenring <Marco.Eisenring@advellence.com> Co-authored-by: Florian Greinacher <fgreinacher@users.noreply.github.com> Co-authored-by: Florian Greinacher <florian@greinacher.de>
1 parent 65de5f1 commit 9885122

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,12 @@ public void MoveDirectory(string sourcePath, string destPath)
239239
sourcePath = FixPath(sourcePath);
240240
destPath = FixPath(destPath);
241241

242+
var sourcePathSequence = sourcePath.Split(new[] {Path.DirectorySeparatorChar}, StringSplitOptions.RemoveEmptyEntries);
243+
242244
lock (files)
243245
{
244246
var affectedPaths = files.Keys
245-
.Where(p => StringOperations.StartsWith(p, sourcePath))
247+
.Where(p => PathStartsWith(p, sourcePathSequence))
246248
.ToList();
247249

248250
foreach (var path in affectedPaths)
@@ -252,6 +254,25 @@ public void MoveDirectory(string sourcePath, string destPath)
252254
files.Remove(path);
253255
}
254256
}
257+
258+
bool PathStartsWith(string path, string[] minMatch)
259+
{
260+
var pathSequence = path.Split(new[] {Path.DirectorySeparatorChar}, StringSplitOptions.RemoveEmptyEntries);
261+
if (pathSequence.Length < minMatch.Length)
262+
{
263+
return false;
264+
}
265+
266+
for (var i = 0; i < minMatch.Length; i++)
267+
{
268+
if (!StringOperations.Equals(minMatch[i], pathSequence[i]))
269+
{
270+
return false;
271+
}
272+
}
273+
274+
return true;
275+
}
255276
}
256277

257278
public void RemoveFile(string path)

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,27 @@ public void MockDirectory_Move_ShouldMoveDirectoryWithReadOnlySubDirectory()
14611461
Assert.IsTrue(fileSystem.FileExists(destSubDirName));
14621462
}
14631463

1464+
[Test]
1465+
public void MockDirectory_Move_ShouldOnlyMoveDirAndFilesWithinDir()
1466+
{
1467+
// Arrange
1468+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
1469+
{
1470+
{XFS.Path(@"c:\source\dummy"), new MockDirectoryData()},
1471+
{XFS.Path(@"c:\source\dummy\content.txt"), new MockFileData(new byte[] {0})},
1472+
{XFS.Path(@"c:\source\dummy.txt"), new MockFileData(new byte[] {0})},
1473+
{XFS.Path(@"c:\source\dummy2"), new MockDirectoryData()},
1474+
{XFS.Path(@"c:\destination"), new MockDirectoryData()},
1475+
});
1476+
1477+
// Act
1478+
fileSystem.Directory.Move(XFS.Path(@"c:\source\dummy"), XFS.Path(@"c:\destination\dummy"));
1479+
1480+
// Assert
1481+
Assert.That(fileSystem.FileExists(XFS.Path(@"c:\source\dummy.txt")), Is.True);
1482+
Assert.That(fileSystem.Directory.Exists(XFS.Path(@"c:\source\dummy2")), Is.True);
1483+
}
1484+
14641485
[Test]
14651486
public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemConstructor()
14661487
{

0 commit comments

Comments
 (0)