Skip to content

Commit

Permalink
add UsingDirectoryAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
smdn committed Apr 15, 2022
1 parent ec32847 commit c1bca51
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Smdn.Test.NUnit.Utils/Smdn.Test.NUnit/IOUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ void DeleteDirectory()
}
}

public static Task UsingDirectoryAsync(string path, Func<string, Task> action)
=> UsingDirectoryAsync(path, ensureDirectoryCreated: false, action);

public static async Task UsingDirectoryAsync(string path, bool ensureDirectoryCreated, Func<string, Task> action)
{
try {
TryIO(DeleteDirectory);

if (ensureDirectoryCreated)
TryIO(() => Directory.CreateDirectory(path));

await action(path).ConfigureAwait(false);
}
finally {
TryIO(DeleteDirectory);
}

void DeleteDirectory()
{
if (Directory.Exists(path))
Directory.Delete(path, true);
}
}

public static void UsingFile(string path, Action action)
{
try {
Expand Down

0 comments on commit c1bca51

Please sign in to comment.