Skip to content

Recursively delete a directory with readonly file results in inconsistent state #849

@vbreuss

Description

@vbreuss

Describe the bug
Recursively deleting a directory with read only file results in an inconsistent state of the MockFileSystem:

  • The deletion results (as expected) in an UnauthorizedAccessException
  • The file is still present (File.Exists returns true)
  • The directory however is removed (Directory.Exists returns false)

To Reproduce
Consider the following unit test (using Xunit and FluentAssertions):

    [Fact]
    public void DeleteDirectoryRecursive_WithReadOnlyFile_ShouldThrowExceptionAndNotDeleteDirectory()
    {
      var sut = new MockFileSystem();
      sut.Directory.CreateDirectory("foo");
      sut.File.WriteAllText("foo/bar.txt", "xyz");
      sut.File.SetAttributes("foo/bar.txt", FileAttributes.ReadOnly);
      
      var exception = Record.Exception(() =>
      {
        sut.Directory.Delete("foo", true);
      });

      exception.Should().BeAssignableTo<UnauthorizedAccessException>();
      sut.File.Exists("foo/bar.txt").Should().BeTrue();
      sut.Directory.Exists("foo").Should().BeTrue();
    }

This test fails at the last line, as the directory is deleted, although the file still exists, resulting in an inconsistent state of the MockFileSystem

Expected behavior
The unit test should be successful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testinghelpersIssues that address the testing helpersstate: ready to pickIssues that are ready for being worked onstate: releasedIssues that are releasedtype: bugIssues that describe misbehaving functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions