Solution layout, build rules with zero-package guard, in-house test framework, localization runtime, brand mark rasteriser with ICO/PNG generation, Core contracts (imaging, video, domain model, TIFF reader), Titano.Tools (make-icon, i18n, xaml-lint, package-guard) and docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
97 lines
3.1 KiB
C#
97 lines
3.1 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Text;
|
|
|
|
namespace Titano.Testing;
|
|
|
|
/// <summary>
|
|
/// Per-test services. A test class may take a <see cref="TestContext"/> in its constructor;
|
|
/// one context is created for every test execution and disposed after it.
|
|
/// </summary>
|
|
public sealed class TestContext : IDisposable
|
|
{
|
|
private readonly ConcurrentQueue<string> _log = new();
|
|
private readonly List<string> _tempDirectories = [];
|
|
private readonly Lock _gate = new();
|
|
|
|
internal TestContext(string testName, CancellationToken cancellation)
|
|
{
|
|
TestName = testName;
|
|
Cancellation = cancellation;
|
|
}
|
|
|
|
public string TestName { get; }
|
|
|
|
/// <summary>Signalled when the test times out or the run is aborted.</summary>
|
|
public CancellationToken Cancellation { get; }
|
|
|
|
/// <summary>Repository root (the directory that holds <c>Titano.slnx</c>), or the working directory.</summary>
|
|
public static string RepositoryRoot { get; } = FindRepositoryRoot();
|
|
|
|
/// <summary><c>tests/fixtures</c> under the repository root.</summary>
|
|
public static string FixturesDirectory => Path.Combine(RepositoryRoot, "tests", "fixtures");
|
|
|
|
/// <summary>Creates an empty directory that is deleted when the test ends.</summary>
|
|
public string CreateTempDirectory(string? hint = null)
|
|
{
|
|
var path = Path.Combine(Path.GetTempPath(), "titano-tests", $"{Sanitize(hint ?? TestName)}-{Guid.NewGuid():N}");
|
|
Directory.CreateDirectory(path);
|
|
lock (_gate) _tempDirectories.Add(path);
|
|
return path;
|
|
}
|
|
|
|
/// <summary>Lines written here are printed only when the test fails or in verbose mode.</summary>
|
|
public void Log(string message) => _log.Enqueue(message);
|
|
|
|
public string LogText
|
|
{
|
|
get
|
|
{
|
|
var sb = new StringBuilder();
|
|
foreach (var line in _log) sb.AppendLine(line);
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
|
|
[System.Diagnostics.CodeAnalysis.DoesNotReturn]
|
|
public static void Skip(string reason) => throw new SkipException(reason);
|
|
|
|
public void Dispose()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
foreach (var dir in _tempDirectories)
|
|
{
|
|
try
|
|
{
|
|
if (Directory.Exists(dir)) Directory.Delete(dir, recursive: true);
|
|
}
|
|
catch (IOException)
|
|
{
|
|
// Best effort: a file still open by the OS indexer must not fail the test.
|
|
}
|
|
catch (UnauthorizedAccessException)
|
|
{
|
|
}
|
|
}
|
|
_tempDirectories.Clear();
|
|
}
|
|
}
|
|
|
|
private static string Sanitize(string s)
|
|
{
|
|
var chars = s.Select(c => char.IsLetterOrDigit(c) ? c : '-').Take(48).ToArray();
|
|
return new string(chars);
|
|
}
|
|
|
|
private static string FindRepositoryRoot()
|
|
{
|
|
var dir = new DirectoryInfo(AppContext.BaseDirectory);
|
|
while (dir is not null)
|
|
{
|
|
if (File.Exists(Path.Combine(dir.FullName, "Titano.slnx"))) return dir.FullName;
|
|
dir = dir.Parent;
|
|
}
|
|
return Directory.GetCurrentDirectory();
|
|
}
|
|
}
|