Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ public override void Delete(string path, bool recursive)
throw new IOException("The directory name is invalid.");
}

foreach (var affectedPath in affectedPaths)
{
var file = mockFileDataAccessor.GetFile(affectedPath);
if (file != null && !file.IsDirectory && !file.AllowedFileShare.HasFlag(FileShare.Delete))
{
throw CommonExceptions.ProcessCannotAccessFileInUse(affectedPath);
}
}

foreach (var affectedPath in affectedPaths)
{
mockFileDataAccessor.RemoveFile(affectedPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,50 @@ public async Task MockFile_Lock_FileShareDeleteDoesNotThrowDelete()

await That(() => filesystem.File.Delete(filepath)).DoesNotThrow();
}
[Test]
public async Task MockFile_Lock_FileShareNoneThrowsRecursiveDirectoryDelete()
{
string directoryPath = XFS.Path(@"c:\something\does");
string lockedFilePath = XFS.Path(@"c:\something\does\exist.txt");
string otherFilePath = XFS.Path(@"c:\something\does\other.txt");
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ otherFilePath, new MockFileData("I'm here too") },
{ lockedFilePath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }}
});

var exception = await That(() => filesystem.Directory.Delete(directoryPath, true)).Throws<IOException>();
await That(exception.Message).IsEqualTo($"The process cannot access the file '{lockedFilePath}' because it is being used by another process.");
await That(filesystem.File.Exists(lockedFilePath)).IsTrue();
await That(filesystem.File.Exists(otherFilePath)).IsTrue();
}
[Test]
public async Task MockFile_Lock_FileShareNoneInSubdirectoryThrowsRecursiveDirectoryInfoDelete()
{
string directoryPath = XFS.Path(@"c:\something");
string lockedFilePath = XFS.Path(@"c:\something\does\exist\here.txt");
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ lockedFilePath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }}
});

var exception = await That(() => filesystem.DirectoryInfo.New(directoryPath).Delete(true)).Throws<IOException>();
await That(exception.Message).IsEqualTo($"The process cannot access the file '{lockedFilePath}' because it is being used by another process.");
await That(filesystem.File.Exists(lockedFilePath)).IsTrue();
}
[Test]
public async Task MockFile_Lock_FileShareDeleteDoesNotThrowRecursiveDirectoryDelete()
{
string directoryPath = XFS.Path(@"c:\something\does");
string filepath = XFS.Path(@"c:\something\does\exist.txt");
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.Delete }}
});

await That(() => filesystem.Directory.Delete(directoryPath, true)).DoesNotThrow();
await That(filesystem.Directory.Exists(directoryPath)).IsFalse();
}

private static IResolveConstraint IOException() => Is.TypeOf<IOException>().And.Property("HResult").EqualTo(unchecked((int)0x80070020));
}
Loading