Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
64 changes: 64 additions & 0 deletions PowerKit.Tests/DirectoryExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.IO;
using FluentAssertions;
using PowerKit;
using PowerKit.Extensions;
using Xunit;

namespace PowerKit.Tests;

public class DirectoryExtensionsTests
{
[Fact]
public void TryDelete_Test()
{
// Arrange
using var tempDir = TempDirectory.Create();

// Act
var result = Directory.TryDelete(tempDir.Path);

// Assert
result.Should().BeTrue();
Directory.Exists(tempDir.Path).Should().BeFalse();
}

[Fact]
public void TryDelete_NonExisting_Test()
{
// Act
var result = Directory.TryDelete(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));

// Assert
result.Should().BeFalse();
}

[Fact]
public void TryDelete_NonEmpty_Test()
{
// Arrange
using var tempDir = TempDirectory.Create();
File.WriteAllText(Path.Combine(tempDir.Path, "file.txt"), "hello");

// Act
var result = Directory.TryDelete(tempDir.Path, recursive: false);

// Assert
result.Should().BeFalse();
Directory.Exists(tempDir.Path).Should().BeTrue();
}

[Fact]
public void TryDelete_NonEmpt_Recursive_Test()
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
{
// Arrange
using var tempDir = TempDirectory.Create();
File.WriteAllText(Path.Combine(tempDir.Path, "file.txt"), "hello");

// Act
var result = Directory.TryDelete(tempDir.Path, recursive: true);

// Assert
result.Should().BeTrue();
Directory.Exists(tempDir.Path).Should().BeFalse();
}
}
24 changes: 24 additions & 0 deletions PowerKit.Tests/FileExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ namespace PowerKit.Tests;

public class FileExtensionsTests
{
[Fact]
public void TryDelete_Test()
{
// Arrange
using var tempFile = TempFile.Create();

// Act
var result = File.TryDelete(tempFile.Path);

// Assert
result.Should().BeTrue();
File.Exists(tempFile.Path).Should().BeFalse();
}

[Fact]
public void TryDelete_NonExisting_Test()
{
// Act
var result = File.TryDelete(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));

// Assert
result.Should().BeTrue();
}

[Fact]
public async Task WriteAllZeroes_Test()
{
Expand Down
26 changes: 26 additions & 0 deletions PowerKit/Extensions/DirectoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.IO;

namespace PowerKit.Extensions;

internal static class DirectoryExtensions
{
extension(Directory)
{
/// <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.
/// </summary>
public static bool TryDelete(string path, bool recursive = false)
{
try
{
Directory.Delete(path, recursive);
return true;
}
catch
{
return false;
}
Comment thread
Tyrrrz marked this conversation as resolved.
}
}
}
17 changes: 17 additions & 0 deletions PowerKit/Extensions/FileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ internal static class FileExtensions
{
extension(File)
{
/// <summary>
/// Attempts to delete the file at the specified path.
/// Returns <see langword="true" /> if the file was successfully deleted, or <see langword="false" /> if an error occurred.
/// </summary>
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
public static bool TryDelete(string path)
{
try
{
File.Delete(path);
return true;
}
catch
{
return false;
}
Comment thread
Tyrrrz marked this conversation as resolved.
}

/// <summary>
/// Creates a file at the specified path and fills it with zeroes.
/// </summary>
Expand Down