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

fix: correctly handle access when moving a file #873

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/System.IO.Abstractions.TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Runtime.Versioning;
using System.Security.AccessControl;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace System.IO.Abstractions.TestingHelpers
{
Expand Down Expand Up @@ -397,8 +395,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));
fgreinacher marked this conversation as resolved.
Show resolved Hide resolved
}

#if FEATURE_FILE_MOVE_WITH_OVERWRITE
Expand Down Expand Up @@ -443,8 +441,8 @@ public override void Move(string sourceFileName, string destFileName, bool overw
}
VerifyDirectoryExists(destFileName);

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

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