Skip to content

Commit

Permalink
fix: do not move unrelated files with same prefix (#665)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
4 people committed Nov 26, 2020
1 parent 65de5f1 commit 9885122
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs
Expand Up @@ -239,10 +239,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)
Expand All @@ -252,6 +254,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)
Expand Down
Expand Up @@ -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<string, MockFileData>
{
{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(XFS.Path(@"c:\source\dummy"), XFS.Path(@"c:\destination\dummy"));

// Assert
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]
public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemConstructor()
{
Expand Down

0 comments on commit 9885122

Please sign in to comment.