Skip to content

Commit

Permalink
Make MockFile.Delete throw exception when file does not exist. (#387)
Browse files Browse the repository at this point in the history
Fixes #284
  • Loading branch information
peter-sereda authored and fgreinacher committed Nov 5, 2018
1 parent 01a127c commit 127d89f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
28 changes: 26 additions & 2 deletions System.IO.Abstractions.TestingHelpers.Tests/MockFileDeleteTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace System.IO.Abstractions.TestingHelpers.Tests
{
using NUnit.Framework;
using System.Collections.Generic;
using NUnit.Framework;

using XFS = MockUnixSupport;

Expand All @@ -10,7 +11,7 @@ public class MockFileDeleteTests
public void MockFile_Delete_ShouldDeleteFile()
{
var fileSystem = new MockFileSystem();
var path = XFS.Path("C:\\test");
var path = XFS.Path("C:\\some_folder\\test");
var directory = fileSystem.Path.GetDirectoryName(path);
fileSystem.AddFile(path, new MockFileData("Bla"));

Expand All @@ -35,5 +36,28 @@ public void MockFile_Delete_ShouldThrowArgumentExceptionIfPathContainsOnlyWhites
// Assert
Assert.Throws<ArgumentException>(action);
}

[Test]
public void MockFile_Delete_ShouldThrowDirectoryNotFoundExceptionIfParentFolderAbsent()
{
var fileSystem = new MockFileSystem();
var path = XFS.Path("C:\\test\\somefile.txt");

Assert.Throws<DirectoryNotFoundException>(() => fileSystem.File.Delete(path));
}

[Test]
public void MockFile_Delete_ShouldSilentlyReturnIfNonExistingFileInExistingFolder()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()
{
{ XFS.Path("C:\\temp\\exist.txt"), new MockFileData("foobar") },
});

string filePath = XFS.Path("C:\\temp\\somefile.txt");

// Delete() returns void, so there is nothing to check here beside absense of an exception
Assert.DoesNotThrow(() => fileSystem.File.Delete(filePath));

This comment has been minimized.

Copy link
@borismod

borismod Nov 5, 2018

Contributor

Maybe it's too late to comment, but I would check also that the directory still exists after file deletion.

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public void MockFileStream_Flush_WritesByteToFile()
[Test]
public void MockFileStream_Dispose_ShouldNotResurrectFile()
{
// path in this test case is a subject to Directory.GetParent(path) Linux issue
// https://github.com/System-IO-Abstractions/System.IO.Abstractions/issues/395
var fileSystem = new MockFileSystem();
var path = XFS.Path("C:\\test");
var path = XFS.Path("C:\\some_folder\\test");
var directory = fileSystem.Path.GetDirectoryName(path);
fileSystem.AddFile(path, new MockFileData("Bla"));
var stream = fileSystem.File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Delete);
Expand Down
11 changes: 8 additions & 3 deletions System.IO.Abstractions.TestingHelpers.Tests/MockFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,14 @@ public void MockFile_Delete_Should_RemoveFiles()

[Test]
public void MockFile_Delete_No_File_Does_Nothing()
{
string filePath = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem();
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()
{
{ XFS.Path(@"c:\something\exist.txt"), new MockFileData("Demo text content") },
});

string filePath = XFS.Path(@"c:\something\not_exist.txt");

fileSystem.File.Delete(filePath);
}

Expand Down
11 changes: 10 additions & 1 deletion System.IO.Abstractions.TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ public override void Delete(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

// We mimic exact behavior of the standard File.Delete() method
// which throws exception only if the folder does not exist,
// but silently returns if deleting a non-existing file in an existing folder.
VerifyDirectoryExists(path);

mockFileDataAccessor.RemoveFile(path);
}

Expand Down Expand Up @@ -991,7 +996,11 @@ private void VerifyDirectoryExists(string path)
DirectoryInfoBase dir = mockFileDataAccessor.Directory.GetParent(path);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), dir));
throw new DirectoryNotFoundException(
string.Format(
CultureInfo.InvariantCulture,
StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"),
dir));
}
}
}
Expand Down

0 comments on commit 127d89f

Please sign in to comment.