-
Notifications
You must be signed in to change notification settings - Fork 1
Add TempFile and TempDirectory utilities #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
ea8bdbe
73c470c
6c925e5
20d7b62
e436b2c
d328eb2
fb1b146
9bfc1e5
f479ea3
a9c01eb
fcf6399
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||
| } | ||
| } |
| 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 | ||
| 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(); | ||
| } | ||
|
Tyrrrz marked this conversation as resolved.
|
||
| } | ||
| 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() | ||
| ); | ||
|
Tyrrrz marked this conversation as resolved.
|
||
|
|
||
| Directory.CreateDirectory(dirPath); | ||
|
|
||
| return new TempDirectory(dirPath); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| 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 = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".tmp"); | ||
|
Check failure on line 28 in PowerKit/TempFile.cs
|
||
| File.Create(filePath).Dispose(); | ||
| return new TempFile(filePath); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.