Files

98 lines
2.8 KiB
C#
Raw Permalink Normal View History

2024-01-04 16:27:14 +00:00
using Xunit.Sdk;
2022-09-18 01:34:36 -07:00
2023-12-31 11:09:44 +00:00
namespace Velopack.CommandLine.Tests;
2023-12-15 17:27:33 +00:00
public abstract class TempFileTestBase : IDisposable
2022-09-18 01:34:36 -07:00
{
2023-12-15 17:27:33 +00:00
private readonly Lazy<DirectoryInfo> _WorkingDirectory = new(() => {
DirectoryInfo working = new(
Path.Combine(Path.GetTempPath(),
typeof(TempFileTestBase).Assembly.GetName().Name!,
Path.GetRandomFileName()));
2022-09-23 22:44:34 -07:00
2023-12-15 17:27:33 +00:00
if (working.Exists) {
working.Delete(recursive: true);
}
working.Create();
return working;
}, LazyThreadSafetyMode.ExecutionAndPublication);
private readonly List<FileInfo> _TempFiles = new();
private readonly List<DirectoryInfo> _TempDirectories = new();
protected DirectoryInfo TempDirectory => _WorkingDirectory.Value;
private bool _Disposed;
public FileInfo CreateTempFile(DirectoryInfo? directory = null, string? name = null)
2022-09-18 01:34:36 -07:00
{
2023-12-15 17:27:33 +00:00
var tempFile = new FileInfo(GetPath(directory, name));
tempFile.Create().Close();
_TempFiles.Add(tempFile);
return tempFile;
}
2022-09-18 01:34:36 -07:00
2023-12-15 17:27:33 +00:00
public DirectoryInfo CreateTempDirectory(DirectoryInfo? parent = null, string? name = null)
{
var tempDir = new DirectoryInfo(GetPath(parent, name));
tempDir.Create();
_TempDirectories.Add(tempDir);
return tempDir;
}
2022-09-18 01:34:36 -07:00
2023-12-15 17:27:33 +00:00
private string GetPath(DirectoryInfo? parentDirectory, string? name)
{
var directory = parentDirectory ?? _WorkingDirectory.Value;
var fileName = name ?? Path.GetRandomFileName();
return Path.Combine(directory.FullName, fileName);
}
2022-09-18 01:34:36 -07:00
2023-12-15 17:27:33 +00:00
protected virtual void Dispose(bool disposing)
{
if (_Disposed || !disposing) {
return;
2022-09-18 01:34:36 -07:00
}
2023-12-15 17:27:33 +00:00
_Disposed = true;
2022-09-18 01:34:36 -07:00
2023-12-15 17:27:33 +00:00
ExceptionAggregator aggregator = new();
2022-09-18 01:34:36 -07:00
2023-12-15 17:27:33 +00:00
var items = _TempFiles
.Cast<FileSystemInfo>()
.Concat(_TempDirectories)
.Concat(_WorkingDirectory.IsValueCreated ? new[] { _WorkingDirectory.Value } : Enumerable.Empty<DirectoryInfo>());
2022-09-18 01:34:36 -07:00
2023-12-15 17:27:33 +00:00
foreach (var fsi in items) {
fsi.Refresh();
if (!fsi.Exists) return;
2022-09-18 01:34:36 -07:00
2023-12-15 17:27:33 +00:00
Action? action = fsi switch {
FileInfo file => () => file.Delete(),
DirectoryInfo dir => () => dir.Delete(recursive: true),
_ => null,
};
2022-09-18 01:34:36 -07:00
2023-12-15 17:27:33 +00:00
if (action is null) return;
2022-09-18 01:34:36 -07:00
2023-12-15 17:27:33 +00:00
aggregator.Run(() => {
for (int i = 0; i < 100; i++) {
try {
action();
break;
} catch {
Thread.Sleep(TimeSpan.FromMilliseconds(10));
2022-09-18 01:34:36 -07:00
}
2023-12-15 17:27:33 +00:00
}
});
2022-09-18 01:34:36 -07:00
}
2023-12-15 17:27:33 +00:00
if (aggregator.HasExceptions) {
throw aggregator.ToException();
2022-09-18 01:34:36 -07:00
}
}
2023-12-15 17:27:33 +00:00
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
2022-09-18 01:34:36 -07:00
}