diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileDeleteTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileDeleteTests.cs index c5a1e9c01..6f9fd93fc 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileDeleteTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileDeleteTests.cs @@ -1,6 +1,7 @@ namespace System.IO.Abstractions.TestingHelpers.Tests { - using NUnit.Framework; + using System.Collections.Generic; + using NUnit.Framework; using XFS = MockUnixSupport; @@ -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")); @@ -35,5 +36,28 @@ public void MockFile_Delete_ShouldThrowArgumentExceptionIfPathContainsOnlyWhites // Assert Assert.Throws(action); } + + [Test] + public void MockFile_Delete_ShouldThrowDirectoryNotFoundExceptionIfParentFolderAbsent() + { + var fileSystem = new MockFileSystem(); + var path = XFS.Path("C:\\test\\somefile.txt"); + + Assert.Throws(() => fileSystem.File.Delete(path)); + } + + [Test] + public void MockFile_Delete_ShouldSilentlyReturnIfNonExistingFileInExistingFolder() + { + var fileSystem = new MockFileSystem(new Dictionary() + { + { 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)); + } } } \ No newline at end of file diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamTests.cs index b2a14abfb..ec3781b4c 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamTests.cs @@ -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); diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileTests.cs index 2468c9824..d26a382bd 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileTests.cs @@ -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() + { + { XFS.Path(@"c:\something\exist.txt"), new MockFileData("Demo text content") }, + }); + + string filePath = XFS.Path(@"c:\something\not_exist.txt"); + fileSystem.File.Delete(filePath); } diff --git a/System.IO.Abstractions.TestingHelpers/MockFile.cs b/System.IO.Abstractions.TestingHelpers/MockFile.cs index 1322e2e29..b77d644e7 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFile.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFile.cs @@ -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); } @@ -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)); } } }