Il progetto adotta il nome «Rail Enterprise» ovunque (codice RailEnterprise.*, variabili RE_*, intestazioni RESV/RECL, immagine rail-enterprise-server) per allinearsi al repository Gitea. Aggiunge Gitea Actions sul modello di Chronos: CI su PR e main, rilascio da tag con gioco Windows (client Godot + server in server/), server Windows/Linux e immagine Docker nel registro Gitea, nightly. Aggiunge i riferimenti visivi 06–09, i modelli di PR e issue, l'ADR 0014 e aggiorna README (comandi locali), STATE e CLAUDE.md. 120 test verdi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
4.7 KiB
C#
121 lines
4.7 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
|
|
namespace RailEnterprise.Tests.Harness;
|
|
|
|
/// <summary>
|
|
/// Scopre i metodi [Test] per reflection e li esegue. Opzioni:
|
|
/// filtri liberi (sottostringa del nome), --list, --serial, --skip-slow.
|
|
/// Il codice di uscita è 0 solo se tutti i test selezionati passano.
|
|
/// </summary>
|
|
public static class TestRunner
|
|
{
|
|
private sealed record TestCase(string Name, MethodInfo Method, TestAttribute Attribute);
|
|
|
|
private sealed record TestOutcome(string Name, bool Passed, long Millis, string? Error);
|
|
|
|
public static int Run(string[] args, Assembly assembly)
|
|
{
|
|
bool list = args.Contains("--list");
|
|
bool serial = args.Contains("--serial");
|
|
bool skipSlow = args.Contains("--skip-slow");
|
|
var filters = args.Where(a => !a.StartsWith("--", StringComparison.Ordinal)).ToArray();
|
|
|
|
var tests = Discover(assembly)
|
|
.Where(t => filters.Length == 0 || filters.Any(f => t.Name.Contains(f, StringComparison.OrdinalIgnoreCase)))
|
|
.Where(t => !(skipSlow && t.Attribute.Slow))
|
|
.OrderBy(t => t.Name, StringComparer.Ordinal)
|
|
.ToList();
|
|
|
|
if (list)
|
|
{
|
|
foreach (var t in tests)
|
|
Console.WriteLine($"{t.Name}{(t.Attribute.Slow ? " [lento]" : "")}{(t.Attribute.Description is { } d ? " — " + d : "")}");
|
|
return 0;
|
|
}
|
|
|
|
Console.WriteLine($"Rail Enterprise — {tests.Count} test selezionati");
|
|
var total = Stopwatch.StartNew();
|
|
var outcomes = new ConcurrentBag<TestOutcome>();
|
|
|
|
var parallel = tests.Where(t => !serial && !t.Attribute.Isolated).ToList();
|
|
var sequential = tests.Where(t => serial || t.Attribute.Isolated).ToList();
|
|
|
|
Parallel.ForEach(parallel, new ParallelOptions { MaxDegreeOfParallelism = Math.Max(1, Environment.ProcessorCount) },
|
|
t =>
|
|
{
|
|
var o = Execute(t);
|
|
Report(o);
|
|
outcomes.Add(o);
|
|
});
|
|
foreach (var t in sequential)
|
|
{
|
|
var o = Execute(t);
|
|
Report(o);
|
|
outcomes.Add(o);
|
|
}
|
|
|
|
var failed = outcomes.Where(o => !o.Passed).OrderBy(o => o.Name, StringComparer.Ordinal).ToList();
|
|
Console.WriteLine();
|
|
Console.WriteLine($"Eseguiti {outcomes.Count}, superati {outcomes.Count - failed.Count}, falliti {failed.Count} in {total.Elapsed.TotalSeconds:F1} s");
|
|
foreach (var f in failed)
|
|
Console.WriteLine($" FALLITO {f.Name}: {f.Error}");
|
|
return failed.Count == 0 && tests.Count > 0 ? 0 : 1;
|
|
}
|
|
|
|
private static readonly Lock ConsoleLock = new();
|
|
|
|
private static void Report(TestOutcome o)
|
|
{
|
|
lock (ConsoleLock)
|
|
{
|
|
if (o.Passed)
|
|
Console.WriteLine($" ok {o.Name} ({o.Millis} ms)");
|
|
else
|
|
Console.WriteLine($" FALLITO {o.Name} ({o.Millis} ms)\n {o.Error}");
|
|
}
|
|
}
|
|
|
|
private static IEnumerable<TestCase> Discover(Assembly assembly)
|
|
{
|
|
const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
|
|
foreach (var type in assembly.GetTypes())
|
|
{
|
|
foreach (var method in type.GetMethods(flags))
|
|
{
|
|
var attr = method.GetCustomAttribute<TestAttribute>();
|
|
if (attr is null) continue;
|
|
if (method.GetParameters().Length != 0)
|
|
throw new InvalidOperationException($"Il test {type.Name}.{method.Name} non deve avere parametri");
|
|
yield return new TestCase($"{type.Name}.{method.Name}", method, attr);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static TestOutcome Execute(TestCase t)
|
|
{
|
|
var sw = Stopwatch.StartNew();
|
|
try
|
|
{
|
|
object? instance = t.Method.IsStatic ? null : Activator.CreateInstance(t.Method.DeclaringType!);
|
|
object? result = t.Method.Invoke(instance, null);
|
|
if (result is Task task) task.GetAwaiter().GetResult();
|
|
(instance as IDisposable)?.Dispose();
|
|
return new TestOutcome(t.Name, true, sw.ElapsedMilliseconds, null);
|
|
}
|
|
catch (TargetInvocationException ex) when (ex.InnerException is not null)
|
|
{
|
|
return new TestOutcome(t.Name, false, sw.ElapsedMilliseconds, Describe(ex.InnerException));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new TestOutcome(t.Name, false, sw.ElapsedMilliseconds, Describe(ex));
|
|
}
|
|
}
|
|
|
|
private static string Describe(Exception ex) => ex is AssertionException
|
|
? ex.Message
|
|
: $"{ex.GetType().Name}: {ex.Message}\n{ex.StackTrace}";
|
|
}
|