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. fgreinacher commented on May 18, 2022

    @fgreinacher
    Contributor
  2. added
    state: ready to pickIssues that are ready for being worked on
    and removed on May 18, 2022
  3. VictorLlanir commented on Jul 2, 2022

    @VictorLlanir

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

  4. fgreinacher commented on Jul 3, 2022

    @fgreinacher
    Contributor

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

    Thanks!

  5. VictorLlanir commented on Jul 3, 2022

    @VictorLlanir

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

  6. github-actions commented on Jul 19, 2022

    @github-actions

    This is addressed in release v17.0.23.

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: 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