You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MockDirectory.Delete recursive does not consider nested files MockFileData.AllowedFileShare property #747
Describe the bug
MockDirectory.Delete recursive does not consider nested files which MockFileData.AllowedFileShare property does not allow delete operation.
To Reproduce
The following test reproduces the error:
[Test]
public void MockDirectory_Delete_ShouldThrowIfFileAccessShareHasNoWriteOrDeleteAccess()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"c:\bar\foo.txt", new MockFileData("some demo content") },
{ @"c:\bar\baz.txt", new MockFileData("some demo content"){ AllowedFileShare = FileShare.None } }
});
// Throws exception
Assert.Throws(typeof(IOException), () => fileSystem.File.Delete(@"c:\bar\baz.txt"));
// Does not throw exception
Assert.Throws(typeof(IOException), () => fileSystem.Directory.Delete(@"c:\bar", true));
}
Expected behavior
I'm not sure if this is indeed a bug or I didn't understand the semantics of the AllowedFileShare property, but the behaviour seems inconsistent. In case it's a bug, I'd be happy to open a PR for it.
Hi, this still happens on main (7f444c9), on Linux and macOS: with the setup from the report, File.Delete and FileInfo.Delete on baz.txt throw IOException: The process cannot access the file '/bar/baz.txt' because it is being used by another process., but Directory.Delete("/bar", true) returns normally and removes baz.txt with the rest of the folder. DirectoryInfo.Delete(true) does the same with a locked file a few levels down. The cause is that MockDirectory.Delete(string, bool) removes every affected path without the AllowedFileShare.HasFlag(FileShare.Delete) check that MockFile.Delete makes. The report offered a PR back in 2021 and none followed, so I'd like to take this if that's okay: before removing anything, check every affected file and throw the same exception as File.Delete, so the tree is left as it was. Tests would go next to MockFile_Lock_FileShareNoneThrowsDelete in MockFileLockTests, for a locked file and for FileShare.Delete still deleting. Should I open a PR?
Activity
fgreinacher commentedon Aug 9, 2021
Thanks for reporting. You got the idea totally right and I agree it's a gap in our implementation.
mohsinaliwork686-rgb commentedon Nov 23, 2026
Hi, this still happens on
main(7f444c9), on Linux and macOS: with the setup from the report,File.DeleteandFileInfo.Deleteonbaz.txtthrowIOException: The process cannot access the file '/bar/baz.txt' because it is being used by another process., butDirectory.Delete("/bar", true)returns normally and removesbaz.txtwith the rest of the folder.DirectoryInfo.Delete(true)does the same with a locked file a few levels down. The cause is thatMockDirectory.Delete(string, bool)removes every affected path without theAllowedFileShare.HasFlag(FileShare.Delete)check thatMockFile.Deletemakes. The report offered a PR back in 2021 and none followed, so I'd like to take this if that's okay: before removing anything, check every affected file and throw the same exception asFile.Delete, so the tree is left as it was. Tests would go next toMockFile_Lock_FileShareNoneThrowsDeleteinMockFileLockTests, for a locked file and forFileShare.Deletestill deleting. Should I open a PR?vbreuss commentedon Nov 24, 2026
I assigned the issue to you, @mohsinaliwork686-rgb
fix: respect `AllowedFileShare` in recursive `Directory.Delete` (Test…