dennisdoomen / dennisdoomen/pathy

[API Proposal]: CreateTempDirectory and a self-cleaning temporary directory scope

Open
#140 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
C#
Stars
27
Forks
5
Avg merge
21h 29m
Merged PRs (30d)
10

Description

### Background and motivation

`ChainablePath.Temp` gives you the user temporary folder, but every test and script that needs its own scratch directory still has to invent a unique name, create it, and remember to delete it in a `finally` block. That boilerplate is repeated in nearly every test suite that touches the file system, and when the cleanup is forgotten it leaves rubbish behind on developer machines and build agents.

### API Proposal

```C#
namespace Pathy
{
public readonly struct ChainablePath
{
public static ChainablePath CreateTempDirectory(string prefix = null);
}

public sealed class TemporaryDirectory : IDisposable
{
public TemporaryDirectory(string prefix = null);

public ChainablePath Path { get; }

public static implicit operator ChainablePath(TemporaryDirectory directory);

public void Dispose();
}
}
```

`CreateTempDirectory` creates and returns a uniquely named directory. `TemporaryDirectory` does the same but deletes the directory recursively on disposal.

### API Usage

```C#
using var temp = new TemporaryDirectory("pathy-specs");

var file = temp.Path / "input.txt";
file.WriteAllText("hello");

// everything under temp.Path is removed when the scope ends
```

Without the scope:

```C#
var workingDirectory = ChainablePath.CreateTempDirectory();
```

### Alternative Designs

* Only ship `CreateTempDirectory` and let callers handle cleanup. Simpler, but leaves the most error-prone part unsolved.
* Ship the disposable type in a separate testing package. Reasonable, though it needs no extra dependency and is just as useful in scripts.

### Risks

`Dispose` performs a recursive delete, which is destructive and must be extremely careful about what it is deleting; it should refuse to act on anything it did not create. Swallowing versus rethrowing cleanup failures needs a decision, since throwing from `Dispose` can mask the original test failure. Adding a class to a package that is currently a single struct plus extension methods is also a notable change in shape.

Contributor guide

Open the contributing guide

Research direction

Start by locating ChainablePath.Temp and the existing single-struct and extension-method implementation. Review how temporary paths are currently created, then assess the proposed ChainablePath.CreateTempDirectory and TemporaryDirectory APIs, including recursive cleanup and protection against deleting paths they did not create. Done requires resolving the cleanup-failure behavior and validating the API shape described in the proposal.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.