Skip to content

Commit 7e7026e

Browse files
ElStefanStefan Lampert
and
Stefan Lampert
authored
fix: Delete directory recursive should delete files in subfolders (#422)
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 <[email protected]>
1 parent 1fbcf28 commit 7e7026e

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public bool DeleteContainer(IStorageLocation location, bool recursive = false)
124124
{
125125
foreach (IStorageLocation key in children)
126126
{
127-
DeleteContainer(key);
127+
DeleteContainer(key, recursive);
128128
}
129129
}
130130
else if (children.Any())

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,30 @@ public void Delete_Recursive_WithSubdirectory_ShouldDeleteDirectoryWithContent(
147147
FileSystem.Should().NotHaveDirectory(subdirectoryPath);
148148
}
149149

150+
[SkippableTheory]
151+
[AutoData]
152+
public void Delete_Recursive_WithFileInSubdirectory_ShouldDeleteDirectoryWithContent(
153+
string path, string subdirectory, string fileName, string fileContent)
154+
{
155+
FileSystem.Directory.CreateDirectory(path);
156+
string filePath = FileSystem.Path.Combine(path, fileName);
157+
FileSystem.File.WriteAllText(filePath, fileContent);
158+
159+
string subdirectoryPath = FileSystem.Path.Combine(path, subdirectory);
160+
FileSystem.Directory.CreateDirectory(subdirectoryPath);
161+
string subdirectoryFilePath = FileSystem.Path.Combine(path, subdirectory, fileName);
162+
FileSystem.File.WriteAllText(subdirectoryFilePath, fileContent);
163+
164+
FileSystem.Should().HaveDirectory(path);
165+
166+
FileSystem.Directory.Delete(path, true);
167+
168+
FileSystem.Should().NotHaveDirectory(path);
169+
FileSystem.Should().NotHaveFile(filePath);
170+
FileSystem.Should().NotHaveDirectory(subdirectoryPath);
171+
FileSystem.Should().NotHaveFile(subdirectoryFilePath);
172+
}
173+
150174
[SkippableTheory]
151175
[AutoData]
152176
public void Delete_ShouldAdjustTimes(string path, string subdirectoryName)

0 commit comments

Comments
 (0)