Skip to content

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

Closed

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.

Activity

  1. added
    state: needs discussion
    type: bug
    on May 18, 2022
  2. fgreinacher commented on May 18, 2022

    @fgreinacher
    Contributor
  3. added
    state: ready to pick
    area: testinghelpers
    and removed
    state: needs discussion
    on May 18, 2022
  4. VictorLlanir commented on Jul 2, 2022

    @VictorLlanir

    Hello, @fgreinacher, I would like to work on this issue.

  5. fgreinacher commented on Jul 3, 2022

    @fgreinacher
    Contributor

    Hello, @fgreinacher, I would like to work on this issue.

    Thanks!

  6. VictorLlanir commented on Jul 3, 2022

    @VictorLlanir

    You're welcome, @fgreinacher! I've already opened a PR (#863) for this issue.

  7. github-actions commented on Jul 19, 2022

    @github-actions

    This is addressed in release v17.0.23.

  8. added
    state: released
    on Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testinghelpersstate: ready to pickstate: releasedtype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions