Skip to content

Commit

Permalink
fix: Delete directory recursive should delete files in subfolders (#422)
Browse files Browse the repository at this point in the history
Calling `MockFileSystem.Directory.Delete` with `recursive: true` throws
a `DirectoryNotEmptyException` when trying to delete a directory with
files in it.

As the documentation of [`Directory.Delete(String,
Boolean)`](https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.delete?view=net-8.0#system-io-directory-delete(system-string-system-boolean))
indicates, the method should actually try to delete all files and
subfolders instead.

---------

Co-authored-by: Stefan Lampert <s.lampert@tig.at>
  • Loading branch information
ElStefan and Stefan Lampert authored Nov 16, 2023
1 parent 1fbcf28 commit 7e7026e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public bool DeleteContainer(IStorageLocation location, bool recursive = false)
{
foreach (IStorageLocation key in children)
{
DeleteContainer(key);
DeleteContainer(key, recursive);
}
}
else if (children.Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,30 @@ public void Delete_Recursive_WithSubdirectory_ShouldDeleteDirectoryWithContent(
FileSystem.Should().NotHaveDirectory(subdirectoryPath);
}

[SkippableTheory]
[AutoData]
public void Delete_Recursive_WithFileInSubdirectory_ShouldDeleteDirectoryWithContent(
string path, string subdirectory, string fileName, string fileContent)
{
FileSystem.Directory.CreateDirectory(path);
string filePath = FileSystem.Path.Combine(path, fileName);
FileSystem.File.WriteAllText(filePath, fileContent);

string subdirectoryPath = FileSystem.Path.Combine(path, subdirectory);
FileSystem.Directory.CreateDirectory(subdirectoryPath);
string subdirectoryFilePath = FileSystem.Path.Combine(path, subdirectory, fileName);
FileSystem.File.WriteAllText(subdirectoryFilePath, fileContent);

FileSystem.Should().HaveDirectory(path);

FileSystem.Directory.Delete(path, true);

FileSystem.Should().NotHaveDirectory(path);
FileSystem.Should().NotHaveFile(filePath);
FileSystem.Should().NotHaveDirectory(subdirectoryPath);
FileSystem.Should().NotHaveFile(subdirectoryFilePath);
}

[SkippableTheory]
[AutoData]
public void Delete_ShouldAdjustTimes(string path, string subdirectoryName)
Expand Down

0 comments on commit 7e7026e

Please sign in to comment.