Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ public void Move_DirectoryExistsWithDifferentCase_DirectorySuccessfullyMoved()
}

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

[Test]
public void MockDirectory_Move_ShouldMoveFiles()
{
string sourceFilePath = XFS.Path(@"c:\demo.txt");
string sourceFileContent = "this is some content";

var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ sourceFilePath, new MockFileData(sourceFileContent) }
});

string destFilePath = XFS.Path(@"c:\demo1.txt");

fileSystem.Directory.Move(sourceFilePath, destFilePath);

Assert.That(fileSystem.FileExists(destFilePath), Is.True);
Assert.That(fileSystem.FileExists(sourceFilePath), Is.False);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm usually against multiple asserts, but since this is more about state after a move, it seems OK. I had a slight issue with this because it was initially kinda confusing. Maybe we move the FileExists check to the top so we're validating state together? i.e. FileExists destFile (true) and FileExists sourceFile (false) need to be together to validate the file moved on the file system. The GetFile makes sure the source and dest were the same.

Copy link
Contributor Author

@fgreinacher fgreinacher Jan 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, will reorder!

Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent));
}

[Test]
public void MockDirectory_Move_ShouldMoveDirectoryAtrributes()
{
Expand Down
14 changes: 11 additions & 3 deletions System.IO.Abstractions.TestingHelpers/MockDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public override void Delete(string path, bool recursive)

var stringOps = mockFileDataAccessor.StringOperations;
var pathWithDirectorySeparatorChar = $"{path}{Path.DirectorySeparatorChar}";

var affectedPaths = mockFileDataAccessor
.AllPaths
.Where(p => stringOps.Equals(p, path) || stringOps.StartsWith(p, pathWithDirectorySeparatorChar))
Expand All @@ -87,12 +87,12 @@ public override void Delete(string path, bool recursive)
{
throw new DirectoryNotFoundException(path + " does not exist or could not be found.");
}

if (!recursive && affectedPaths.Count > 1)
{
throw new IOException("The directory specified by " + path + " is read-only, or recursive is false and " + path + " is not an empty directory.");
}

foreach (var affectedPath in affectedPaths)
{
mockFileDataAccessor.RemoveFile(affectedPath);
Expand Down Expand Up @@ -378,6 +378,14 @@ public override void Move(string sourceDirName, string destDirName)
throw new IOException("Source and destination path must be different.");
}

//if we're moving a file, not a directory, call the appropriate file moving function.
var fileData = mockFileDataAccessor.GetFile(fullSourcePath);
if (fileData?.Attributes.HasFlag(FileAttributes.Directory) == false)
{
mockFileDataAccessor.File.Move(fullSourcePath, fullDestPath);
return;
}

var sourceRoot = mockFileDataAccessor.Path.GetPathRoot(fullSourcePath);
var destinationRoot = mockFileDataAccessor.Path.GetPathRoot(fullDestPath);

Expand Down