System.IO.Abstractions icon indicating copy to clipboard operation
System.IO.Abstractions copied to clipboard

Move a readonly file results in inconsistent state

Open vbreuss opened this issue 3 years ago • 1 comments

Describe the bug Moving a readonly file throws (correctly) an UnauthorizedAccessException, but afterwards both the source file and also the target file exist.

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

    [Fact]
    public void MoveFile_WithReadOnlyAttribute_ShouldThrowExceptionAndNotMoveFile()
    {
      var sut = new MockFileSystem();
      sut.File.WriteAllText("foo.txt", "xyz");
      sut.File.SetAttributes("foo.txt", FileAttributes.ReadOnly);
      
      var exception = Record.Exception(() =>
      {
        sut.File.Move("foo.txt", "bar.txt");
      });

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

This test fails at the last line, as the file exists now twice, resulting in an inconsistent state of the MockFileSystem

Expected behavior The unit test should be successful.

Additional context This issue might have similar root cause as #849 ?

vbreuss avatar Aug 08 '22 14:08 vbreuss

I would be happy to provide a solution for this issue myself. Will create a pull request shortly...

vbreuss avatar Aug 09 '22 11:08 vbreuss

This is addressed in release v17.0.28.

github-actions[bot] avatar Aug 15 '22 13:08 github-actions[bot]