elsa-core/test/shared/Elsa.Testing.Shared/Helpers/TemporaryFolder.cs
Craig Fowler d285b8b505 WIP composing customize attributes
This isn't really related to #751 - it's a refactor of some of
our test logic, to avoid bloat of semi-repeated autofixture
customization attributes.

By switching to a behaviour-driven model, it's possible to
compose them, which means that there's no need to part-duplicate
between them.  It should also reduce the overall number of them,
as combinations can be put together on-demand, rather than needing
a new attribute of their own.
2021-04-02 11:12:21 +01:00

34 lines
1.1 KiB
C#

using System;
using System.IO;
namespace Elsa.Testing.Shared.Helpers
{
public class TemporaryFolder : IDisposable
{
private readonly bool _deleteOnDispose;
public TemporaryFolder(bool deleteOnDispose = true)
{
Folder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
_deleteOnDispose = deleteOnDispose;
}
public string Folder { get; }
/// <summary>
/// Gets a path string for a file or folder which is contained within the <see cref="Folder"/>.
/// </summary>
/// <param name="relativePath">The relative path, within the temporary folder.</param>
/// <returns>A path for content within the temporary folder.</returns>
public string GetContainedPath(string relativePath) => Path.Combine(Folder, relativePath);
public void Dispose()
{
if (_deleteOnDispose)
if (Directory.Exists(Folder))
Directory.Delete(Folder, true);
}
public override string ToString() => Folder;
}
}