Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot Move a readonly file #1207

Open
patiqs opened this issue Feb 9, 2025 · 1 comment
Open

Cannot Move a readonly file #1207

patiqs opened this issue Feb 9, 2025 · 1 comment
Labels
area: testinghelpers Issues that address the testing helpers state: ready to pick Issues that are ready for being worked on type: bug Issues that describe misbehaving functionality

Comments

@patiqs
Copy link

patiqs commented Feb 9, 2025

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

@patiqs patiqs added state: needs discussion Issues that need further discussion type: bug Issues that describe misbehaving functionality labels Feb 9, 2025
@vbreuss
Copy link
Member

vbreuss commented Feb 11, 2025

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.

@vbreuss vbreuss added state: ready to pick Issues that are ready for being worked on area: testinghelpers Issues that address the testing helpers and removed state: needs discussion Issues that need further discussion labels Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: testinghelpers Issues that address the testing helpers state: ready to pick Issues that are ready for being worked on type: bug Issues that describe misbehaving functionality
Projects
None yet
Development

No branches or pull requests

2 participants