diff --git a/PowerKit.Tests/TempDirectoryTests.cs b/PowerKit.Tests/TempDirectoryTests.cs new file mode 100644 index 0000000..e870963 --- /dev/null +++ b/PowerKit.Tests/TempDirectoryTests.cs @@ -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(); + } +} diff --git a/PowerKit.Tests/TempFileTests.cs b/PowerKit.Tests/TempFileTests.cs new file mode 100644 index 0000000..2610b37 --- /dev/null +++ b/PowerKit.Tests/TempFileTests.cs @@ -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 + 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(); + } +} diff --git a/PowerKit/TempDirectory.cs b/PowerKit/TempDirectory.cs new file mode 100644 index 0000000..4ab9f1e --- /dev/null +++ b/PowerKit/TempDirectory.cs @@ -0,0 +1,43 @@ +using System; +using System.IO; + +namespace PowerKit; + +/// +/// Represents a temporary directory that is automatically deleted when disposed. +/// +internal partial class TempDirectory(string path) : IDisposable +{ + /// + /// Gets the path of the temporary directory. + /// + public string Path { get; } = path; + + /// + public void Dispose() + { + try + { + Directory.Delete(Path, true); + } + catch (DirectoryNotFoundException) { } + } +} + +internal partial class TempDirectory +{ + /// + /// Creates a new temporary directory. + /// + public static TempDirectory Create() + { + var dirPath = System.IO.Path.Combine( + System.IO.Path.GetTempPath(), + Guid.NewGuid().ToString() + ); + + Directory.CreateDirectory(dirPath); + + return new TempDirectory(dirPath); + } +} diff --git a/PowerKit/TempFile.cs b/PowerKit/TempFile.cs new file mode 100644 index 0000000..97d8621 --- /dev/null +++ b/PowerKit/TempFile.cs @@ -0,0 +1,33 @@ +using System; +using System.IO; + +namespace PowerKit; + +/// +/// Represents a temporary file that is automatically deleted when disposed. +/// +internal partial class TempFile(string path) : IDisposable +{ + /// + /// Gets the path of the temporary file. + /// + public string Path { get; } = path; + + /// + // File.Delete does not throw if the file does not exist + public void Dispose() => File.Delete(Path); +} + +internal partial class TempFile +{ + /// + /// Creates a new temporary file. + /// + 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); + } +}