Skip to content

Commit

Permalink
fix: #870 Moving a readonly file duplicates the file
Browse files Browse the repository at this point in the history
  • Loading branch information
vbtig committed Aug 9, 2022
1 parent e8ed38b commit c757dbb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/System.IO.Abstractions.TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ public override void Move(string sourceFileName, string destFileName)
}
VerifyDirectoryExists(destFileName);

mockFileDataAccessor.AddFile(destFileName, new MockFileData(sourceFile));
mockFileDataAccessor.RemoveFile(sourceFileName);
mockFileDataAccessor.AddFile(destFileName, new MockFileData(sourceFile));
}

#if FEATURE_FILE_MOVE_WITH_OVERWRITE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ public void MockFile_Move_ShouldMoveFileWithinMemoryFileSystem()
Assert.That(fileSystem.FileExists(sourceFilePath), Is.False);
}

[Test]
public void MockFile_Move_WithReadOnlyAttribute_ShouldThrowUnauthorizedAccessExceptionAndNotMoveFile()
{
var sourceFilePath = @"c:\foo.txt";
var destFilePath = @"c:\bar.txt";
var fileSystem = new MockFileSystem();
fileSystem.File.WriteAllText(sourceFilePath, "this is some content");
fileSystem.File.SetAttributes(sourceFilePath, FileAttributes.ReadOnly);

Assert.Throws<UnauthorizedAccessException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));

Assert.That(fileSystem.File.Exists(sourceFilePath), Is.EqualTo(true));
Assert.That(fileSystem.File.Exists(destFilePath), Is.EqualTo(false));
}

[Test]
public void MockFile_Move_SameSourceAndTargetIsANoOp()
{
Expand Down

0 comments on commit c757dbb

Please sign in to comment.