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
45 changes: 45 additions & 0 deletions PowerKit.Tests/TempDirectoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.IO;
using FluentAssertions;
using PowerKit;
using Xunit;

namespace PowerKit.Tests;

public class TempDirectoryTests
{
[Fact]
public void Create_Test()
{
// Act
using var tempDir = TempDirectory.Create();

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

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

// Act
tempDir.Dispose();

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

[Fact]
public void Dispose_AlreadyDeleted_Test()
{
// Arrange
using var tempDir = TempDirectory.Create();
Directory.Delete(tempDir.Path);

// Act & assert
var act = tempDir.Dispose;
act.Should().NotThrow();
}
}
44 changes: 44 additions & 0 deletions PowerKit.Tests/TempFileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.IO;
using FluentAssertions;
using PowerKit;
using Xunit;

namespace PowerKit.Tests;

public class TempFileTests
{
[Fact]
public void Create_Test()
{
// Act
using var tempFile = TempFile.Create();

// Assert
Comment thread
Tyrrrz marked this conversation as resolved.
File.Exists(tempFile.Path).Should().BeTrue();
}

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

// Act
tempFile.Dispose();

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

[Fact]
public void Dispose_AlreadyDeleted_Test()
{
// Arrange
using var tempFile = TempFile.Create();
File.Delete(tempFile.Path);

// Act & assert
var act = tempFile.Dispose;
act.Should().NotThrow();
}
Comment thread
Tyrrrz marked this conversation as resolved.
}
43 changes: 43 additions & 0 deletions PowerKit/TempDirectory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.IO;

namespace PowerKit;

/// <summary>
/// Represents a temporary directory that is automatically deleted when disposed.
/// </summary>
internal partial class TempDirectory(string path) : IDisposable
{
/// <summary>
/// Gets the path of the temporary directory.
/// </summary>
public string Path { get; } = path;

/// <inheritdoc />
public void Dispose()
{
try
{
Directory.Delete(Path, true);
}
catch (DirectoryNotFoundException) { }
}
}

internal partial class TempDirectory
{
/// <summary>
/// Creates a new temporary directory.
/// </summary>
public static TempDirectory Create()
{
var dirPath = System.IO.Path.Combine(
System.IO.Path.GetTempPath(),
Guid.NewGuid().ToString()
);
Comment thread
Tyrrrz marked this conversation as resolved.

Directory.CreateDirectory(dirPath);

return new TempDirectory(dirPath);
}
}
33 changes: 33 additions & 0 deletions PowerKit/TempFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.IO;

namespace PowerKit;

/// <summary>
/// Represents a temporary file that is automatically deleted when disposed.
/// </summary>
internal partial class TempFile(string path) : IDisposable
{
/// <summary>
/// Gets the path of the temporary file.
/// </summary>
public string Path { get; } = path;

/// <inheritdoc />
// File.Delete does not throw if the file does not exist
public void Dispose() => File.Delete(Path);
}

internal partial class TempFile
{
/// <summary>
/// Creates a new temporary file.
/// </summary>
public static TempFile Create()
{
var filePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid() + ".tmp");
File.Create(filePath).Dispose();

return new TempFile(filePath);
}
}