-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ea8bdbe
feat: implement TempFile and TempDirectory
Copilot 73c470c
fix: use explicit ToString() for GUID in TempFile.Create
Copilot 6c925e5
Update PowerKit.Tests/TempFileTests.cs
Tyrrrz 20d7b62
Update PowerKit/TempFile.cs
Tyrrrz e436b2c
fix: create file in TempFile.Create, remove redundant try/catch, fix …
Copilot d328eb2
Merge remote changes and apply reviewer feedback
Copilot fb1b146
Add `using` to all TempFile/TempDirectory.Create() calls in tests
Copilot 9bfc1e5
Remove leaky `.EndWith(".tmp")` assertion from Create_Test
Copilot f479ea3
Simplify TempFile: arrow-body Dispose with not-found comment, drop re…
Copilot a9c01eb
Fix compile error: use FQN for System.IO.Path in TempFile.Create() to…
Copilot fcf6399
Update TempFile.cs
Tyrrrz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.