namespace SongTidy.Tests.TestUtilities;
///
/// Creates a unique temporary directory per test instance and removes it on disposal.
///
public sealed class TempDirectory : IDisposable {
///
/// Gets the unique directory path managed by this instance.
///
public string FullPath { get; }
///
/// Creates a unique temporary directory.
///
public TempDirectory() {
FullPath = Path.Combine(Path.GetTempPath(), "songtidy-tests-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(FullPath);
}
///
/// Deletes the directory tree, retrying once after a short delay.
///
public void Dispose() {
try {
// SAFETY: Recursive deletion is confined to the GUID-named directory
// created by this instance under the OS temp folder.
Directory.Delete(FullPath, recursive: true);
} catch (IOException) {
// A file handle may still be closing; retry once before giving up.
Thread.Sleep(100);
try {
// SAFETY: The retry targets the same instance-owned temp directory.
Directory.Delete(FullPath, recursive: true);
} catch (IOException) {
// Leftovers in the OS temp folder are harmless; never fail a test over cleanup.
}
}
}
}