Skip to content

Commit fab80f9

Browse files
authored
fix: Align MockDirectory and MockFile behavior with .NET's System.IO classes (#1190)
* fix: Align MockDirectory behavior with System.IO.Directory when deleting files The MockDirectory class previously did not replicate the behavior of System.IO.Directory when calling the Delete method on a file path. In System.IO.Directory, an IOException is thrown if the specified path points to a file instead of a directory. This commit updates MockDirectory to ensure it throws an IOException in this scenario, maintaining consistency with the expected behavior of System.IO.Directory. * fix: Align MockFile behavior with System.IO.File when deleting directories The MockFile class previously did not replicate the behavior of System.IO.File when calling the Delete method on a directory path. In System.IO.File, an UnauthorizedAccessException is thrown if the specified path points to a directory instead of a file. This commit updates MockFile to ensure it throws an UnauthorizedAccessException in this scenario, maintaining consistency with the expected behavior of System.IO.File.
1 parent 233b809 commit fab80f9

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ public override void Delete(string path, bool recursive)
158158
" is not an empty directory.");
159159
}
160160

161+
bool isFile = !mockFileDataAccessor.GetFile(path).IsDirectory;
162+
if (isFile)
163+
{
164+
throw new IOException("The directory name is invalid.");
165+
}
166+
161167
foreach (var affectedPath in affectedPaths)
162168
{
163169
mockFileDataAccessor.RemoveFile(affectedPath);

src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFile.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ public override void Delete(string path)
231231
throw CommonExceptions.ProcessCannotAccessFileInUse(path);
232232
}
233233

234+
if (file != null && file.IsDirectory)
235+
{
236+
throw new UnauthorizedAccessException($"Access to the path '{path}' is denied.");
237+
}
238+
234239
mockFileDataAccessor.RemoveFile(path);
235240
}
236241

tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,22 @@ public void MockDirectory_Delete_ShouldDeleteDirectoryRecursively()
10181018
Assert.That(fileSystem.Directory.Exists(XFS.Path(@"c:\bar\bar2")), Is.False);
10191019
}
10201020

1021+
[Test]
1022+
public void MockDirectory_Delete_ShouldThrowIOException_WhenPathIsAFile()
1023+
{
1024+
// Arrange
1025+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
1026+
{
1027+
{ XFS.Path(@"c:\foo.txt"), new MockFileData("Demo text content") },
1028+
});
1029+
1030+
// Act
1031+
TestDelegate action = () => fileSystem.Directory.Delete(XFS.Path(@"c:\foo.txt"));
1032+
1033+
// Assert
1034+
Assert.Throws<IOException>(action);
1035+
}
1036+
10211037
[Test]
10221038
public void MockDirectory_GetFileSystemEntries_Returns_Files_And_Directories()
10231039
{

tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,22 @@ public void MockFile_Delete_No_File_Does_Nothing()
495495
fileSystem.File.Delete(filePath);
496496
}
497497

498+
[Test]
499+
public void MockFile_Delete_ShouldThrowUnauthorizedAccessException_WhenPathIsADirectory()
500+
{
501+
// Arrange
502+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
503+
{
504+
{ XFS.Path(@"c:\bar"), new MockDirectoryData() },
505+
});
506+
507+
// Act
508+
TestDelegate action = () => fileSystem.File.Delete(XFS.Path(@"c:\bar"));
509+
510+
// Assert
511+
Assert.Throws<UnauthorizedAccessException>(action);
512+
}
513+
498514
[Test]
499515
public void MockFile_AppendText_AppendTextToAnExistingFile()
500516
{

0 commit comments

Comments
 (0)