From c2acd1aa7363e83a184b61778459fa5032d0c76a Mon Sep 17 00:00:00 2001 From: Josh Date: Fri, 4 Feb 2022 04:28:48 -0800 Subject: [PATCH] fix: make MockFileInfo.Delete honor FileAccess (#807) Fixes #794 --- .../MockFileInfo.cs | 30 ++++++++++--------- .../MockFileInfoTests.cs | 13 ++++++++ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs b/src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs index 175bd8a21..840accb1e 100644 --- a/src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs +++ b/src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs @@ -12,6 +12,7 @@ public class MockFileInfo : FileInfoBase private string path; private readonly string originalPath; private MockFileData cachedMockFileData; + private MockFile mockFile; private bool refreshOnNextRead; /// @@ -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(); } /// public override void Delete() { - mockFileSystem.RemoveFile(path); + mockFile.Delete(path); } /// @@ -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); } /// public override Stream Create() { - return new MockFile(mockFileSystem).Create(FullName); + return mockFile.Create(FullName); } /// public override StreamWriter CreateText() { - return new MockFile(mockFileSystem).CreateText(FullName); + return mockFile.CreateText(FullName); } /// @@ -259,7 +261,7 @@ public override FileSecurity GetAccessControl(AccessControlSections includeSecti /// public override void MoveTo(string destFileName) { - mockFileSystem.File.Move(path, destFileName); + mockFile.Move(path, destFileName); path = mockFileSystem.Path.GetFullPath(destFileName); } @@ -267,7 +269,7 @@ public override void MoveTo(string destFileName) /// 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 @@ -275,29 +277,29 @@ public override void MoveTo(string destFileName, bool overwrite) /// public override Stream Open(FileMode mode) { - return new MockFile(mockFileSystem).Open(FullName, mode); + return mockFile.Open(FullName, mode); } /// public override Stream Open(FileMode mode, FileAccess access) { - return new MockFile(mockFileSystem).Open(FullName, mode, access); + return mockFile.Open(FullName, mode, access); } /// 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); } /// - public override Stream OpenRead() => mockFileSystem.File.OpenRead(path); + public override Stream OpenRead() => mockFile.OpenRead(path); /// - public override StreamReader OpenText() => mockFileSystem.File.OpenText(path); + public override StreamReader OpenText() => mockFile.OpenText(path); /// - public override Stream OpenWrite() => mockFileSystem.File.OpenWrite(path); + public override Stream OpenWrite() => mockFile.OpenWrite(path); /// public override IFileInfo Replace(string destinationFileName, string destinationBackupFileName) @@ -308,7 +310,7 @@ public override IFileInfo Replace(string destinationFileName, string destination /// 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); } @@ -316,7 +318,7 @@ public override IFileInfo Replace(string destinationFileName, string destination [SupportedOSPlatform("windows")] public override void SetAccessControl(FileSecurity fileSecurity) { - mockFileSystem.File.SetAccessControl(this.path, fileSecurity); + mockFile.SetAccessControl(this.path, fileSecurity); } /// diff --git a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs index 440d2b7a7..860615eeb 100644 --- a/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs +++ b/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs @@ -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()); + } } }