Skip to content

Commit 19e1b6c

Browse files
authored
Enable MockDirectory.Move to also move files (#438)
Fixes #187 Supersedes #188
1 parent 8f429cf commit 19e1b6c

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ public void Move_DirectoryExistsWithDifferentCase_DirectorySuccessfullyMoved()
11641164
}
11651165

11661166
[TestCaseSource("GetPathsForMoving")]
1167-
public void MockDirectory_Move_ShouldMove(string sourceDirName, string destDirName, string filePathOne, string filePathTwo)
1167+
public void MockDirectory_Move_ShouldMoveDirectories(string sourceDirName, string destDirName, string filePathOne, string filePathTwo)
11681168
{
11691169
// Arrange
11701170
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
@@ -1182,6 +1182,26 @@ public void MockDirectory_Move_ShouldMove(string sourceDirName, string destDirNa
11821182
Assert.IsTrue(fileSystem.File.Exists(XFS.Path(destDirName + filePathTwo)));
11831183
}
11841184

1185+
[Test]
1186+
public void MockDirectory_Move_ShouldMoveFiles()
1187+
{
1188+
string sourceFilePath = XFS.Path(@"c:\demo.txt");
1189+
string sourceFileContent = "this is some content";
1190+
1191+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
1192+
{
1193+
{ sourceFilePath, new MockFileData(sourceFileContent) }
1194+
});
1195+
1196+
string destFilePath = XFS.Path(@"c:\demo1.txt");
1197+
1198+
fileSystem.Directory.Move(sourceFilePath, destFilePath);
1199+
1200+
Assert.That(fileSystem.FileExists(destFilePath), Is.True);
1201+
Assert.That(fileSystem.FileExists(sourceFilePath), Is.False);
1202+
Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent));
1203+
}
1204+
11851205
[Test]
11861206
public void MockDirectory_Move_ShouldMoveDirectoryAtrributes()
11871207
{

System.IO.Abstractions.TestingHelpers/MockDirectory.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public override void Delete(string path, bool recursive)
7777

7878
var stringOps = mockFileDataAccessor.StringOperations;
7979
var pathWithDirectorySeparatorChar = $"{path}{Path.DirectorySeparatorChar}";
80-
80+
8181
var affectedPaths = mockFileDataAccessor
8282
.AllPaths
8383
.Where(p => stringOps.Equals(p, path) || stringOps.StartsWith(p, pathWithDirectorySeparatorChar))
@@ -87,12 +87,12 @@ public override void Delete(string path, bool recursive)
8787
{
8888
throw new DirectoryNotFoundException(path + " does not exist or could not be found.");
8989
}
90-
90+
9191
if (!recursive && affectedPaths.Count > 1)
9292
{
9393
throw new IOException("The directory specified by " + path + " is read-only, or recursive is false and " + path + " is not an empty directory.");
9494
}
95-
95+
9696
foreach (var affectedPath in affectedPaths)
9797
{
9898
mockFileDataAccessor.RemoveFile(affectedPath);
@@ -378,6 +378,14 @@ public override void Move(string sourceDirName, string destDirName)
378378
throw new IOException("Source and destination path must be different.");
379379
}
380380

381+
//if we're moving a file, not a directory, call the appropriate file moving function.
382+
var fileData = mockFileDataAccessor.GetFile(fullSourcePath);
383+
if (fileData?.Attributes.HasFlag(FileAttributes.Directory) == false)
384+
{
385+
mockFileDataAccessor.File.Move(fullSourcePath, fullDestPath);
386+
return;
387+
}
388+
381389
var sourceRoot = mockFileDataAccessor.Path.GetPathRoot(fullSourcePath);
382390
var destinationRoot = mockFileDataAccessor.Path.GetPathRoot(fullDestPath);
383391

0 commit comments

Comments
 (0)