F0: fondamenta della soluzione, script, documenti e harness di test
- ViaFerrea.sln con Core e Protocol (net10.0 + net8.0), Server, Tools, Tests - Directory.Build.props con versione unica e warning come errori - Script build/test/check-nuget/publish-image (+ build.ps1) - Documenti: GUIDA, GDD, ARCHITECTURE, PROTOCOL (bozza), STATE, ADR 0001 - Harness di test interno con i primi test su Rng (xoshiro256**) - Immagini di riferimento 01-05 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0191R7vXgtbAvSV4V8tj8uzL
This commit is contained in:
36 files changed
+1647
-2
No files matched your search
@@ -0,0 +1,120 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ViaFerrea.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($"Via Ferrea — {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}";
|
||||
}
|
||||
Reference in new issue
Block a user