Skip to content

Cannot Move a readonly file #1207

Closed

Description

@patiqs

Describe the bug
File.Move behavior is different in MockFileSystem and physical FileSystem.

To Reproduce
The test below is green with real FileSystem, but MockFileSystem throws System.UnauthorizedAccessException : Access to the path 'C:\any.txt' is denied.

  [Test]
  public void Move_a_readonly_file()
  {
    //arrange
    //IFileSystem fileSystem = new FileSystem();
    IFileSystem fileSystem = new MockFileSystem();
    var file = "any.txt";
    fileSystem.File.WriteAllText(file, "foo");

    //act
    fileSystem.File.SetAttributes(file, FileAttributes.ReadOnly);   
    fileSystem.File.Move(file, file+".moved");

    //assert
    Assert.That(fileSystem.File.Exists(file+".moved"));
    Assert.That(fileSystem.File.Exists(file), Is.False);
  }

Additional context
Tested with
Dotnet SDK: 8.0.404
Package I use:
TargetFramework: net48
Windows I use: Windows11 Ver:10.0.22631 Build 22631

Activity

  1. added
    type: bugIssues that describe misbehaving functionality
    on Feb 9, 2025
  2. vbreuss commented on Feb 11, 2025

    @vbreuss
    Member

    Yes, you are right, this is indeed a bug!
    If you want, I would be happy to accept a PR.

    Alternatively you could use the alternative testing library Testably.Abstractions, which uses the same IFileSystem interface and has implemented the behaviour with moving a readonly file correctly.

  3. added
    state: ready to pickIssues that are ready for being worked on
    and removed on Feb 11, 2025
  4. linked a pull request that will close this issuefix: move a read-only file #1239on Mar 1, 2025
  5. github-actions commented on Mar 1, 2025

    @github-actions

    This is addressed in release v22.0.11.

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