diff --git a/PowerKit.Tests/DirectoryExtensionsTests.cs b/PowerKit.Tests/DirectoryExtensionsTests.cs index 7356e8a..f4224d1 100644 --- a/PowerKit.Tests/DirectoryExtensionsTests.cs +++ b/PowerKit.Tests/DirectoryExtensionsTests.cs @@ -8,6 +8,36 @@ namespace PowerKit.Tests; public class DirectoryExtensionsTests { + [Fact] + public void Reset_Test() + { + // Arrange + using var tempDir = TempDirectory.Create(); + File.WriteAllText(Path.Combine(tempDir.Path, "test.txt"), "test"); + + // Act + Directory.Reset(tempDir.Path); + + // Assert + Directory.Exists(tempDir.Path).Should().BeTrue(); + Directory.GetFileSystemEntries(tempDir.Path).Should().BeEmpty(); + } + + [Fact] + public void Reset_NonExistent_Test() + { + // Arrange + using var tempDir = TempDirectory.Create(); + var dirPath = Path.Combine(tempDir.Path, "nonexistent"); + + // Act + Directory.Reset(dirPath); + + // Assert + Directory.Exists(dirPath).Should().BeTrue(); + Directory.GetFileSystemEntries(dirPath).Should().BeEmpty(); + } + [Fact] public void TryDelete_Test() { diff --git a/PowerKit/Extensions/DirectoryExtensions.cs b/PowerKit/Extensions/DirectoryExtensions.cs index 33a618d..aa8065e 100644 --- a/PowerKit/Extensions/DirectoryExtensions.cs +++ b/PowerKit/Extensions/DirectoryExtensions.cs @@ -6,6 +6,20 @@ internal static class DirectoryExtensions { extension(Directory) { + /// + /// Deletes the directory and all its contents, then recreates it as an empty directory. + /// + public static void Reset(string path) + { + try + { + Directory.Delete(path, true); + } + catch (DirectoryNotFoundException) { } + + Directory.CreateDirectory(path); + } + /// /// Attempts to delete the directory at the specified path. /// Returns if the directory was successfully deleted, or if an error occurred.