Skip to content

Commit

Permalink
fix: Align MockDirectory and MockFile behavior with .NET's System.IO …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
WestRyanK authored Jan 25, 2025
1 parent 233b809 commit fab80f9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ public override void Delete(string path, bool recursive)
" is not an empty directory.");
}

bool isFile = !mockFileDataAccessor.GetFile(path).IsDirectory;
if (isFile)
{
throw new IOException("The directory name is invalid.");
}

foreach (var affectedPath in affectedPaths)
{
mockFileDataAccessor.RemoveFile(affectedPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ public override void Delete(string path)
throw CommonExceptions.ProcessCannotAccessFileInUse(path);
}

if (file != null && file.IsDirectory)
{
throw new UnauthorizedAccessException($"Access to the path '{path}' is denied.");
}

mockFileDataAccessor.RemoveFile(path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,22 @@ public void MockDirectory_Delete_ShouldDeleteDirectoryRecursively()
Assert.That(fileSystem.Directory.Exists(XFS.Path(@"c:\bar\bar2")), Is.False);
}

[Test]
public void MockDirectory_Delete_ShouldThrowIOException_WhenPathIsAFile()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"c:\foo.txt"), new MockFileData("Demo text content") },
});

// Act
TestDelegate action = () => fileSystem.Directory.Delete(XFS.Path(@"c:\foo.txt"));

// Assert
Assert.Throws<IOException>(action);
}

[Test]
public void MockDirectory_GetFileSystemEntries_Returns_Files_And_Directories()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,22 @@ public void MockFile_Delete_No_File_Does_Nothing()
fileSystem.File.Delete(filePath);
}

[Test]
public void MockFile_Delete_ShouldThrowUnauthorizedAccessException_WhenPathIsADirectory()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"c:\bar"), new MockDirectoryData() },
});

// Act
TestDelegate action = () => fileSystem.File.Delete(XFS.Path(@"c:\bar"));

// Assert
Assert.Throws<UnauthorizedAccessException>(action);
}

[Test]
public void MockFile_AppendText_AppendTextToAnExistingFile()
{
Expand Down

0 comments on commit fab80f9

Please sign in to comment.