Skip to content

Commit 4da05d4

Browse files
committed
fix: correctly handle readonly directories during delete
1 parent f764c5c commit 4da05d4

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

Source/Testably.Abstractions.Testing/Storage/InMemoryContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public IStorageAccessHandle RequestAccess(FileAccess access, FileShare share,
190190
!ignoreMetadataErrors &&
191191
Attributes.HasFlag(FileAttributes.ReadOnly))
192192
{
193-
throw ExceptionFactory.AccessToPathDenied();
193+
throw ExceptionFactory.AccessToPathDenied(_location.FullPath);
194194
}
195195
#if FEATURE_FILESYSTEM_UNIXFILEMODE
196196
if (!deleteAccess && !_fileSystem.UnixFileModeStrategy

Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public bool DeleteContainer(
170170
location);
171171

172172
using (container.RequestAccess(FileAccess.Write, FileShare.ReadWrite,
173+
ignoreMetadataErrors: false,
173174
deleteAccess: true))
174175
{
175176
if (_containers.TryRemove(location, out IStorageContainer? removed))

Tests/Testably.Abstractions.Tests/FileSystem/Directory/DeleteTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ await That(Act).Throws<DirectoryNotFoundException>()
6262
.WithHResult(-2147024893);
6363
}
6464

65+
[Theory]
66+
[AutoData]
67+
public async Task Delete_ReadonlyDirectory_ShouldThrowIOExceptionOnWindows(string path)
68+
{
69+
IDirectoryInfo sut = FileSystem.Directory.CreateDirectory(path);
70+
sut.Attributes = FileAttributes.ReadOnly;
71+
sut.Refresh();
72+
73+
void Act()
74+
{
75+
FileSystem.Directory.Delete(path);
76+
}
77+
78+
await That(Act).Throws<IOException>()
79+
.OnlyIf(Test.RunsOnWindows)
80+
.WithMessage($"Access to the path '{sut.FullName}' is denied.").And
81+
.WithHResult(-2146232800);
82+
}
83+
6584
[Theory]
6685
[AutoData]
6786
public async Task Delete_Recursive_MissingDirectory_ShouldThrowDirectoryNotFoundException(

Tests/Testably.Abstractions.Tests/FileSystem/DirectoryInfo/DeleteTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ await That(Act).Throws<DirectoryNotFoundException>()
2222
.WithHResult(-2147024893);
2323
}
2424

25+
[Theory]
26+
[AutoData]
27+
public async Task Delete_ReadonlyDirectory_ShouldThrowIOExceptionOnWindows(string path)
28+
{
29+
IDirectoryInfo sut = FileSystem.Directory.CreateDirectory(path);
30+
sut.Attributes = FileAttributes.ReadOnly;
31+
sut.Refresh();
32+
33+
void Act()
34+
{
35+
sut.Delete();
36+
}
37+
38+
await That(Act).Throws<IOException>()
39+
.OnlyIf(Test.RunsOnWindows)
40+
.WithMessage($"Access to the path '{sut.FullName}' is denied.").And
41+
.WithHResult(-2146232800);
42+
}
43+
2544
[Theory]
2645
[AutoData]
2746
public async Task Delete_Recursive_WithOpenFile_ShouldThrowIOException_OnWindows(

0 commit comments

Comments
 (0)