Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions PowerKit.Tests/DirectoryExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
14 changes: 14 additions & 0 deletions PowerKit/Extensions/DirectoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ internal static class DirectoryExtensions
{
extension(Directory)
{
/// <summary>
/// Deletes the directory and all its contents, then recreates it as an empty directory.
/// </summary>
public static void Reset(string path)
{
try
{
Directory.Delete(path, true);
}
catch (DirectoryNotFoundException) { }

Directory.CreateDirectory(path);
}

/// <summary>
/// Attempts to delete the directory at the specified path.
/// Returns <see langword="true" /> if the directory was successfully deleted, or <see langword="false" /> if an error occurred.
Expand Down