Skip to content

Commit 7b57002

Browse files
committed
file move should throw if dest dir doesn't exist.
1 parent 1f90af2 commit 7b57002

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

TestHelpers.Tests/MockFileTests.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,8 @@ public void MockFile_Move_ShouldMoveFileWithinMemoryFileSystem()
628628
const string sourceFileContent = "this is some content";
629629
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
630630
{
631-
{sourceFilePath, new MockFileData(sourceFileContent)}
631+
{sourceFilePath, new MockFileData(sourceFileContent)},
632+
{@"c:\somethingelse\dummy.txt", new MockFileData(new byte[] {0})}
632633
});
633634

634635
const string destFilePath = @"c:\somethingelse\demo1.txt";
@@ -840,6 +841,24 @@ public void MockFile_Move_ShouldThrowFileNotFoundExceptionWhenSourceDoesNotExist
840841
Assert.That(exception.FileName, Is.EqualTo(@"c:\something\demo.txt"));
841842
}
842843

844+
[Test]
845+
public void MockFile_Move_ShouldThrowDirectoryNotFoundExceptionWhenSourcePathDoesNotExist_Message()
846+
{
847+
const string sourceFilePath = @"c:\something\demo.txt";
848+
const string destFilePath = @"c:\somethingelse\demo.txt";
849+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
850+
{
851+
{sourceFilePath, new MockFileData(new byte[] {0})}
852+
});
853+
854+
//var exists = fileSystem.Directory.Exists(@"c:\something");
855+
//exists = fileSystem.Directory.Exists(@"c:\something22");
856+
857+
var exception = Assert.Throws<DirectoryNotFoundException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));
858+
//Message = "Could not find a part of the path."
859+
Assert.That(exception.Message, Is.EqualTo(@"Could not find a part of the path."));
860+
}
861+
843862
[Test]
844863
public void MockFile_OpenWrite_ShouldCreateNewFiles() {
845864
const string filePath = @"c:\something\demo.txt";

TestingHelpers/MockFile.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ public override void Move(string sourceFileName, string destFileName) {
197197
if (sourceFile == null)
198198
throw new FileNotFoundException(string.Format("The file \"{0}\" could not be found.", sourceFileName), sourceFileName);
199199

200+
var destDir = mockFileDataAccessor.Directory.GetParent(destFileName);
201+
if (!destDir.Exists)
202+
{
203+
throw new DirectoryNotFoundException("Could not find a part of the path.");
204+
}
205+
200206
mockFileDataAccessor.AddFile(destFileName, new MockFileData(sourceFile.Contents));
201207
mockFileDataAccessor.RemoveFile(sourceFileName);
202208
}

0 commit comments

Comments
 (0)