Skip to content

Commit

Permalink
fix: make MockFileInfo.Delete honor FileAccess (#807)
Browse files Browse the repository at this point in the history
Fixes #794
  • Loading branch information
pianomanjh authored Feb 4, 2022
1 parent 49c3aaf commit c2acd1a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class MockFileInfo : FileInfoBase
private string path;
private readonly string originalPath;
private MockFileData cachedMockFileData;
private MockFile mockFile;
private bool refreshOnNextRead;

/// <inheritdoc />
Expand All @@ -20,13 +21,14 @@ public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path) : base(mo
this.mockFileSystem = mockFileSystem ?? throw new ArgumentNullException(nameof(mockFileSystem));
this.originalPath = path ?? throw new ArgumentNullException(nameof(path));
this.path = mockFileSystem.Path.GetFullPath(path);
this.mockFile = new MockFile(mockFileSystem);
Refresh();
}

/// <inheritdoc />
public override void Delete()
{
mockFileSystem.RemoveFile(path);
mockFile.Delete(path);
}

/// <inheritdoc />
Expand Down Expand Up @@ -212,20 +214,20 @@ public override IFileInfo CopyTo(string destFileName, bool overwrite)
{
return this;
}
mockFileSystem.File.Copy(FullName, destFileName, overwrite);
mockFile.Copy(FullName, destFileName, overwrite);
return mockFileSystem.FileInfo.FromFileName(destFileName);
}

/// <inheritdoc />
public override Stream Create()
{
return new MockFile(mockFileSystem).Create(FullName);
return mockFile.Create(FullName);
}

/// <inheritdoc />
public override StreamWriter CreateText()
{
return new MockFile(mockFileSystem).CreateText(FullName);
return mockFile.CreateText(FullName);
}

/// <inheritdoc />
Expand Down Expand Up @@ -259,45 +261,45 @@ public override FileSecurity GetAccessControl(AccessControlSections includeSecti
/// <inheritdoc />
public override void MoveTo(string destFileName)
{
mockFileSystem.File.Move(path, destFileName);
mockFile.Move(path, destFileName);
path = mockFileSystem.Path.GetFullPath(destFileName);
}

#if FEATURE_FILE_MOVE_WITH_OVERWRITE
/// <inheritdoc />
public override void MoveTo(string destFileName, bool overwrite)
{
mockFileSystem.File.Move(path, destFileName, overwrite);
mockFile.Move(path, destFileName, overwrite);
path = mockFileSystem.Path.GetFullPath(destFileName);
}
#endif

/// <inheritdoc />
public override Stream Open(FileMode mode)
{
return new MockFile(mockFileSystem).Open(FullName, mode);
return mockFile.Open(FullName, mode);
}

/// <inheritdoc />
public override Stream Open(FileMode mode, FileAccess access)
{
return new MockFile(mockFileSystem).Open(FullName, mode, access);
return mockFile.Open(FullName, mode, access);
}

/// <inheritdoc />
public override Stream Open(FileMode mode, FileAccess access, FileShare share)
{
return new MockFile(mockFileSystem).Open(FullName, mode, access, share);
return mockFile.Open(FullName, mode, access, share);
}

/// <inheritdoc />
public override Stream OpenRead() => mockFileSystem.File.OpenRead(path);
public override Stream OpenRead() => mockFile.OpenRead(path);

/// <inheritdoc />
public override StreamReader OpenText() => mockFileSystem.File.OpenText(path);
public override StreamReader OpenText() => mockFile.OpenText(path);

/// <inheritdoc />
public override Stream OpenWrite() => mockFileSystem.File.OpenWrite(path);
public override Stream OpenWrite() => mockFile.OpenWrite(path);

/// <inheritdoc />
public override IFileInfo Replace(string destinationFileName, string destinationBackupFileName)
Expand All @@ -308,15 +310,15 @@ public override IFileInfo Replace(string destinationFileName, string destination
/// <inheritdoc />
public override IFileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
{
mockFileSystem.File.Replace(path, destinationFileName, destinationBackupFileName, ignoreMetadataErrors);
mockFile.Replace(path, destinationFileName, destinationBackupFileName, ignoreMetadataErrors);
return mockFileSystem.FileInfo.FromFileName(destinationFileName);
}

/// <inheritdoc />
[SupportedOSPlatform("windows")]
public override void SetAccessControl(FileSecurity fileSecurity)
{
mockFileSystem.File.SetAccessControl(this.path, fileSecurity);
mockFile.SetAccessControl(this.path, fileSecurity);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,5 +749,18 @@ public void MockFileInfo_Exists_ShouldUpdateCachedDataOnRefresh()
// Assert
Assert.IsTrue(fileInfo.Exists);
}

[Test]
public void MockFileInfo_Delete_ShouldThrowIfFileAccessShareHasNoWriteOrDeleteAccess()
{
var fileSystem = new MockFileSystem();
fileSystem.AddFile(
@"c:\bar\foo.txt",
new MockFileData("text contents") { AllowedFileShare = FileShare.None });

var fi = fileSystem.FileInfo.FromFileName(@"c:\bar\foo.txt");

Assert.Throws(typeof(System.IO.IOException), () => fi.Delete());
}
}
}

0 comments on commit c2acd1a

Please sign in to comment.