Skip to content

Commit 8da344c

Browse files
committed
Merge pull request #77 from wtjones/filemove-should-validate-directory
file move should throw if dest dir doesn't exist.
2 parents a44f978 + 7b57002 commit 8da344c

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
@@ -629,7 +629,8 @@ public void MockFile_Move_ShouldMoveFileWithinMemoryFileSystem()
629629
const string sourceFileContent = "this is some content";
630630
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
631631
{
632-
{sourceFilePath, new MockFileData(sourceFileContent)}
632+
{sourceFilePath, new MockFileData(sourceFileContent)},
633+
{@"c:\somethingelse\dummy.txt", new MockFileData(new byte[] {0})}
633634
});
634635

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

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

TestingHelpers/MockFile.cs

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

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

0 commit comments

Comments
 (0)