TestableIO / System.IO.Abstractions.Extensions

Convenience functionality on top of System.IO.Abstractions
MIT License
21 stars 6 forks source link

Feature request: Temporary files / folders using the using statement to simplify creation and deletion #40

Closed MattKotsenas closed 1 year ago

MattKotsenas commented 1 year ago

Is your feature request related to a problem? Please describe.

Console apps often need to create temp files / folders to do work in, but then want to ensure that the temp files are cleaned up before the program exits. Today this is usually accomplished with code like this:

var fileSystem = new FileSystem();
var temp = fileSystem.Path.Combine(_fileSystem.Path.GetTempPath(), _fileSystem.Path.GetRandomFileName());

try
{
    _fileSystem.Directory.CreateDirectory(temp);

    // ... Do work ...
}
finally
{
    _fileSystem.Directory.Delete(temp, recursive: true);
}

while this code works fine, it's a bit clunky (in my opinion) as the temp item needs to be manually created and destroyed, and the code can possibly be separated by many lines, making it more difficult to recognize it as the "temp file pattern".

Describe the solution you'd like

Ideally a TempFile / TempDirectory class that implements IDisposable so that a using statement can create and destroy my temp file for me when it goes out-of-scope. This is often referred to as the RAII pattern.

The interface for a directory could be as simple as this:

public interface ITemporaryDirectory : IDisposable
{
    IDirectoryInfo Directory { get; }
}

with a sample usage like this:

var fileSystem = new FileSystem();

// Create supplying a name
using var temp1 = fileSystem.Directory.CreateTemporaryDirectory("my-temp-name");

// Create without supplying a name (gets a random name)
using var temp2 = fileSystem.Directory.CreateTemporaryDirectory();

Describe alternatives you've considered

I've implemented this pattern in projects in the past, the intent here is to create a generic and reusable one.

Additional context

None

MattKotsenas commented 1 year ago

I'm happy to open a PR here if this feature request is approved, but I wanted to check first before spending time on it. If you have any questions or concerns, please let me know. Thanks!

gigi81 commented 1 year ago

Hi @MattKotsenas. I like the idea but I would change the naming. I would call it "DisposableDirectory" instead of "TemporaryDirectory" as I feel it could be confusing as "temporary" is already used. Also, I would return the IDirectoryInfo as an out parameter of the CreateDisposableDirectory in order to reduce typing. That would also make the new interface redundant. Hope the above notes makes sense. Let me know what you think. I would be happy to merge a PR on this but please make sure there is coverage.

MattKotsenas commented 1 year ago

@gigi81 Thanks! I'm open to anything that improves developer experience. I'll get started on this and post a PR; also happy to iterate on naming and API shape there as well.

github-actions[bot] commented 1 year ago

This is addressed in release v1.0.43.