using System;
using System.IO;
namespace Server.Tests;
///
/// A uniquely named directory under the system temp path, deleted with its contents on dispose so a
/// failing assertion cannot leave files behind.
///
internal sealed class TempDirectory : IDisposable
{
public TempDirectory(string prefix = "modernuo-test")
{
Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"{prefix}-{Guid.NewGuid():N}");
Directory.CreateDirectory(Path);
}
public string Path { get; }
public string Combine(string relativePath) => System.IO.Path.Combine(Path, relativePath);
/// Writes to and returns its full path.
public string WriteFile(string relativePath, string content)
{
var path = Combine(relativePath);
File.WriteAllText(path, content);
return path;
}
public void Dispose()
{
try
{
if (Directory.Exists(Path))
{
Directory.Delete(Path, true);
}
}
catch (IOException)
{
// A handle still open on Windows; the OS temp cleaner reclaims it.
}
}
}