Four App tests switched the process-wide UI language inside parallel classes, so another class's English assertion, or its constructor's reset to English, could land in the middle of them; they now live in the serial ArchiveLanguageTests and ImportLanguageTests. The App and Core test programs fail a test that leaves English outside a serial class at the switch, through the new TestContext.Current and TestContext.IsSerial. The FrameWindow cancellation test counted unobserved task exceptions while other classes ran beside it; it moves to the serial FrameWindowIsolationTests, and both unobserved-exception tests drain the finalizer queue before they start counting. The runner unwrapped TargetInvocationException only around the test method: a skip or an assertion thrown by a test class constructor or field initializer was reported as a failure reading "Exception has been thrown by the target of an invocation". Constructors are unwrapped the same way now. TestRunner.Discover(Type) and ExecuteAsync are public, so Tools.Tests checks the runner with nested fixtures. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
112 lines
3.8 KiB
C#
112 lines
3.8 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 static readonly AsyncLocal<TestContext?> CurrentTest = new();
|
|
private readonly ConcurrentQueue<string> _log = new();
|
|
private readonly List<string> _tempDirectories = [];
|
|
private readonly Lock _gate = new();
|
|
|
|
internal TestContext(string testName, bool isSerial, CancellationToken cancellation)
|
|
{
|
|
TestName = testName;
|
|
IsSerial = isSerial;
|
|
Cancellation = cancellation;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The test running on this flow of execution (its constructor, body and the tasks it starts); null outside tests.
|
|
/// Lets a test program guard process-wide state, which only a serial test may change.
|
|
/// </summary>
|
|
public static TestContext? Current
|
|
{
|
|
get => CurrentTest.Value;
|
|
internal set => CurrentTest.Value = value;
|
|
}
|
|
|
|
public string TestName { get; }
|
|
|
|
/// <summary>True when the test's class is <see cref="SerialAttribute"/>: no other test runs beside it.</summary>
|
|
public bool IsSerial { 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();
|
|
}
|
|
}
|