Skip to content
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

Add methods to get a system temp dir, create temp file and folder #79

Merged
merged 3 commits into from
Aug 11, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- `AbsolutePath::ReadKind` to check the file system object kind (file, directory, or something else) and whether it exists at all.

Thanks to @Kataane.
- [#76](https://github.com/ForNeVeR/TruePath/issues/76): a new `Temporary` class for creating a temp file or folder.

Thanks to @Illusion4.

## [1.3.0] - 2024-06-21
### Added
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ Aside from the strict types, the following features are supported for the paths:

(Note how `GetFileNameWithoutExtension()` works nicely together with `GetExtensionWithDot()` to reconstruct the resulting path from their concatenation, however weird the initial name was — no extension, trailing dot, no base name.)

### `Temporary`

`TruePath.Temporary` class contains a set of utility methods to work with the system temp directory (most widely known as `TEMP` or `TMP` environment variable):
- `Temporary::SystemTempDirectory()` will return it as an absolute path;
- `Temporary::CreateTempFile()` will create a randomly-named file in the system temp directory and return an absolute path to it;
- `Temporary::CreateTempFolder()` will create a randomly-named folder in the system temp directory and return an absolute path to it.

Documentation
-------------
- [Contributor Guide][docs.contributing]
Expand Down
51 changes: 51 additions & 0 deletions TruePath.Tests/TemporaryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-FileCopyrightText: 2024 TruePath contributors <https://github.com/ForNeVeR/TruePath>
//
// SPDX-License-Identifier: MIT

namespace TruePath.Tests;

public class TemporaryTests
{
[Fact]
public void SystemTempDirectory_ReturnsValidDirectory()
{
// Act
var tempDir = Temporary.SystemTempDirectory();

// Assert
Assert.True(Directory.Exists(tempDir.Value));
}

[Fact]
public void CreateTempFile_CreatesNewFile()
{
// Act
var tempFile = Temporary.CreateTempFile();

// Assert
Assert.True(File.Exists(tempFile.Value));

// Cleanup
File.Delete(tempFile.Value);
}

[Theory]
[InlineData(null)]
[InlineData("test_")]
public void CreateTempFolder_CreatesNewFolder(string? prefix)
{
// Act
var tempFolder = Temporary.CreateTempFolder(prefix);

// Assert
Assert.True(Directory.Exists(tempFolder.Value));
if (prefix != null)
{
Assert.StartsWith(prefix, tempFolder.FileName);
}

// Cleanup
Directory.Delete(tempFolder.Value);
}
}

43 changes: 43 additions & 0 deletions TruePath/Temporary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-FileCopyrightText: 2024 TruePath contributors <https://github.com/ForNeVeR/TruePath>
//
// SPDX-License-Identifier: MIT

namespace TruePath;

/// <summary>
/// Provides methods for working with temporary files, directories and folders.
/// </summary>
public static class Temporary
{

/// <summary>
/// Gets the system's temporary directory.
/// </summary>
/// <returns>An AbsolutePath representing the system's temporary directory.</returns>
public static AbsolutePath SystemTempDirectory()
{
var tempPath = Path.GetTempPath();
return AbsolutePath.CurrentWorkingDirectory / tempPath;
}

/// <summary>
/// Creates a temporary file.
/// </summary>
/// <returns>An AbsolutePath representing the newly created temporary file.</returns>
public static AbsolutePath CreateTempFile()
{
var tempPath = Path.GetTempFileName();
return AbsolutePath.CurrentWorkingDirectory / tempPath;
}

/// <summary>
/// Creates a temporary folder with the specified prefix.
/// </summary>
/// <param name="prefix">An optional string to add to the beginning of the subdirectory name.</param>
/// <returns>An AbsolutePath representing newly created temporary folder</returns>
public static AbsolutePath CreateTempFolder(string? prefix = null)
{
var tempDirectoryInfo = Directory.CreateTempSubdirectory(prefix);
return AbsolutePath.CurrentWorkingDirectory / tempDirectoryInfo.FullName;
}
}