diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs index b5376487d..272ba2ef4 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs @@ -158,6 +158,12 @@ public override void Delete(string path, bool recursive) " is not an empty directory."); } + bool isFile = !mockFileDataAccessor.GetFile(path).IsDirectory; + if (isFile) + { + throw new IOException("The directory name is invalid."); + } + foreach (var affectedPath in affectedPaths) { mockFileDataAccessor.RemoveFile(affectedPath); diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFile.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFile.cs index ad8a0f005..fd1a08af2 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFile.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFile.cs @@ -231,6 +231,11 @@ public override void Delete(string path) throw CommonExceptions.ProcessCannotAccessFileInUse(path); } + if (file != null && file.IsDirectory) + { + throw new UnauthorizedAccessException($"Access to the path '{path}' is denied."); + } + mockFileDataAccessor.RemoveFile(path); } diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs index fbd0fc41f..d36b434b4 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs @@ -1018,6 +1018,22 @@ public void MockDirectory_Delete_ShouldDeleteDirectoryRecursively() Assert.That(fileSystem.Directory.Exists(XFS.Path(@"c:\bar\bar2")), Is.False); } + [Test] + public void MockDirectory_Delete_ShouldThrowIOException_WhenPathIsAFile() + { + // Arrange + var fileSystem = new MockFileSystem(new Dictionary + { + { XFS.Path(@"c:\foo.txt"), new MockFileData("Demo text content") }, + }); + + // Act + TestDelegate action = () => fileSystem.Directory.Delete(XFS.Path(@"c:\foo.txt")); + + // Assert + Assert.Throws(action); + } + [Test] public void MockDirectory_GetFileSystemEntries_Returns_Files_And_Directories() { diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileTests.cs index dd91bfb21..7fb70a693 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileTests.cs @@ -495,6 +495,22 @@ public void MockFile_Delete_No_File_Does_Nothing() fileSystem.File.Delete(filePath); } + [Test] + public void MockFile_Delete_ShouldThrowUnauthorizedAccessException_WhenPathIsADirectory() + { + // Arrange + var fileSystem = new MockFileSystem(new Dictionary + { + { XFS.Path(@"c:\bar"), new MockDirectoryData() }, + }); + + // Act + TestDelegate action = () => fileSystem.File.Delete(XFS.Path(@"c:\bar")); + + // Assert + Assert.Throws(action); + } + [Test] public void MockFile_AppendText_AppendTextToAnExistingFile() {