Skip to content

Commit 127d89f

Browse files
peter-seredafgreinacher
authored andcommitted
Make MockFile.Delete throw exception when file does not exist. (#387)
Fixes #284
1 parent 01a127c commit 127d89f

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

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

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace System.IO.Abstractions.TestingHelpers.Tests
22
{
3-
using NUnit.Framework;
3+
using System.Collections.Generic;
4+
using NUnit.Framework;
45

56
using XFS = MockUnixSupport;
67

@@ -10,7 +11,7 @@ public class MockFileDeleteTests
1011
public void MockFile_Delete_ShouldDeleteFile()
1112
{
1213
var fileSystem = new MockFileSystem();
13-
var path = XFS.Path("C:\\test");
14+
var path = XFS.Path("C:\\some_folder\\test");
1415
var directory = fileSystem.Path.GetDirectoryName(path);
1516
fileSystem.AddFile(path, new MockFileData("Bla"));
1617

@@ -35,5 +36,28 @@ public void MockFile_Delete_ShouldThrowArgumentExceptionIfPathContainsOnlyWhites
3536
// Assert
3637
Assert.Throws<ArgumentException>(action);
3738
}
39+
40+
[Test]
41+
public void MockFile_Delete_ShouldThrowDirectoryNotFoundExceptionIfParentFolderAbsent()
42+
{
43+
var fileSystem = new MockFileSystem();
44+
var path = XFS.Path("C:\\test\\somefile.txt");
45+
46+
Assert.Throws<DirectoryNotFoundException>(() => fileSystem.File.Delete(path));
47+
}
48+
49+
[Test]
50+
public void MockFile_Delete_ShouldSilentlyReturnIfNonExistingFileInExistingFolder()
51+
{
52+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()
53+
{
54+
{ XFS.Path("C:\\temp\\exist.txt"), new MockFileData("foobar") },
55+
});
56+
57+
string filePath = XFS.Path("C:\\temp\\somefile.txt");
58+
59+
// Delete() returns void, so there is nothing to check here beside absense of an exception
60+
Assert.DoesNotThrow(() => fileSystem.File.Delete(filePath));
61+
}
3862
}
3963
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ public void MockFileStream_Flush_WritesByteToFile()
2828
[Test]
2929
public void MockFileStream_Dispose_ShouldNotResurrectFile()
3030
{
31+
// path in this test case is a subject to Directory.GetParent(path) Linux issue
32+
// https://github.com/System-IO-Abstractions/System.IO.Abstractions/issues/395
3133
var fileSystem = new MockFileSystem();
32-
var path = XFS.Path("C:\\test");
34+
var path = XFS.Path("C:\\some_folder\\test");
3335
var directory = fileSystem.Path.GetDirectoryName(path);
3436
fileSystem.AddFile(path, new MockFileData("Bla"));
3537
var stream = fileSystem.File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Delete);

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,14 @@ public void MockFile_Delete_Should_RemoveFiles()
482482

483483
[Test]
484484
public void MockFile_Delete_No_File_Does_Nothing()
485-
{
486-
string filePath = XFS.Path(@"c:\something\demo.txt");
487-
var fileSystem = new MockFileSystem();
485+
{
486+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()
487+
{
488+
{ XFS.Path(@"c:\something\exist.txt"), new MockFileData("Demo text content") },
489+
});
490+
491+
string filePath = XFS.Path(@"c:\something\not_exist.txt");
492+
488493
fileSystem.File.Delete(filePath);
489494
}
490495

System.IO.Abstractions.TestingHelpers/MockFile.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ public override void Delete(string path)
191191
{
192192
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
193193

194+
// We mimic exact behavior of the standard File.Delete() method
195+
// which throws exception only if the folder does not exist,
196+
// but silently returns if deleting a non-existing file in an existing folder.
197+
VerifyDirectoryExists(path);
198+
194199
mockFileDataAccessor.RemoveFile(path);
195200
}
196201

@@ -991,7 +996,11 @@ private void VerifyDirectoryExists(string path)
991996
DirectoryInfoBase dir = mockFileDataAccessor.Directory.GetParent(path);
992997
if (!dir.Exists)
993998
{
994-
throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), dir));
999+
throw new DirectoryNotFoundException(
1000+
string.Format(
1001+
CultureInfo.InvariantCulture,
1002+
StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"),
1003+
dir));
9951004
}
9961005
}
9971006
}

0 commit comments

Comments
 (0)