From 6086cca6e1014aff1c883d2f4de44dad1947b94e Mon Sep 17 00:00:00 2001 From: mohsinaliwork686-rgb <229973101+mohsinaliwork686-rgb@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:50:48 +0500 Subject: [PATCH] fix: respect `AllowedFileShare` in recursive `Directory.Delete` --- .../MockDirectory.cs | 9 ++++ .../MockFileLockTests.cs | 44 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs index 242bb0d5e..6d8bc2718 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs @@ -164,6 +164,15 @@ public override void Delete(string path, bool recursive) throw new IOException("The directory name is invalid."); } + foreach (var affectedPath in affectedPaths) + { + var file = mockFileDataAccessor.GetFile(affectedPath); + if (file != null && !file.IsDirectory && !file.AllowedFileShare.HasFlag(FileShare.Delete)) + { + throw CommonExceptions.ProcessCannotAccessFileInUse(affectedPath); + } + } + foreach (var affectedPath in affectedPaths) { mockFileDataAccessor.RemoveFile(affectedPath); diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileLockTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileLockTests.cs index f54d74274..f0f9a6bbc 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileLockTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileLockTests.cs @@ -185,6 +185,50 @@ public async Task MockFile_Lock_FileShareDeleteDoesNotThrowDelete() await That(() => filesystem.File.Delete(filepath)).DoesNotThrow(); } + [Test] + public async Task MockFile_Lock_FileShareNoneThrowsRecursiveDirectoryDelete() + { + string directoryPath = XFS.Path(@"c:\something\does"); + string lockedFilePath = XFS.Path(@"c:\something\does\exist.txt"); + string otherFilePath = XFS.Path(@"c:\something\does\other.txt"); + var filesystem = new MockFileSystem(new Dictionary + { + { otherFilePath, new MockFileData("I'm here too") }, + { lockedFilePath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} + }); + + var exception = await That(() => filesystem.Directory.Delete(directoryPath, true)).Throws(); + await That(exception.Message).IsEqualTo($"The process cannot access the file '{lockedFilePath}' because it is being used by another process."); + await That(filesystem.File.Exists(lockedFilePath)).IsTrue(); + await That(filesystem.File.Exists(otherFilePath)).IsTrue(); + } + [Test] + public async Task MockFile_Lock_FileShareNoneInSubdirectoryThrowsRecursiveDirectoryInfoDelete() + { + string directoryPath = XFS.Path(@"c:\something"); + string lockedFilePath = XFS.Path(@"c:\something\does\exist\here.txt"); + var filesystem = new MockFileSystem(new Dictionary + { + { lockedFilePath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} + }); + + var exception = await That(() => filesystem.DirectoryInfo.New(directoryPath).Delete(true)).Throws(); + await That(exception.Message).IsEqualTo($"The process cannot access the file '{lockedFilePath}' because it is being used by another process."); + await That(filesystem.File.Exists(lockedFilePath)).IsTrue(); + } + [Test] + public async Task MockFile_Lock_FileShareDeleteDoesNotThrowRecursiveDirectoryDelete() + { + string directoryPath = XFS.Path(@"c:\something\does"); + string filepath = XFS.Path(@"c:\something\does\exist.txt"); + var filesystem = new MockFileSystem(new Dictionary + { + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.Delete }} + }); + + await That(() => filesystem.Directory.Delete(directoryPath, true)).DoesNotThrow(); + await That(filesystem.Directory.Exists(directoryPath)).IsFalse(); + } private static IResolveConstraint IOException() => Is.TypeOf().And.Property("HResult").EqualTo(unchecked((int)0x80070020)); } \ No newline at end of file