fix(tools): screenshot diff fails a screen without a baseline once any is committed
A screenshot without a baseline only warned, so with no baseline committed every screen was "new" and the CI step could not fail, and a screen added after the first baselines could stay ungated for good. Now the baseline folder is armed as soon as it holds a PNG: from then on a screen without its own baseline fails (Verdict.Unbased, an error annotation). Before the first baselines the run says plainly, in its warning and in bold in the job summary, that it compared nothing and that no visual change can fail it. The baselines README explains why the existing ci-screenshots pictures cannot serve (captured before the clock was fixed: times and dates follow the capture moment) and how the first set is accepted from the first green windows-latest run of the reproducible capture. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
4 files changed
+86
-23
No files matched your search
@@ -9,7 +9,9 @@ namespace Titano.Tools.Commands;
|
||||
/// PNG with the baseline of the same name (<c>tests/visual/baselines</c> unless <c>--baseline</c>): a pixel differs when
|
||||
/// one of its channels moves by more than <c>--tolerance</c> (of 255), and a screen fails when more than <c>--max-diff</c>
|
||||
/// of its pixels differ (a fraction), when its size changed, or when a baseline has no screenshot any more. A screen
|
||||
/// without a baseline only warns. Every screen with differing pixels gets <c><name>-diff.png</c> in <c>--out</c> (the
|
||||
/// without a baseline fails once the baseline folder holds any PNG, so a screen added later cannot stay ungated; before
|
||||
/// the first baselines are committed nothing is compared, and the run says so in a warning and in its summary instead
|
||||
/// of passing for a clean diff. Every screen with differing pixels gets <c><name>-diff.png</c> in <c>--out</c> (the
|
||||
/// baseline faded to grey, the differing pixels magenta) and the run a <c>summary.md</c> there, copied to the job summary
|
||||
/// in GitHub Actions. <c>screenshots accept <dir></c> copies the screenshots over the baselines.
|
||||
/// </summary>
|
||||
@@ -77,9 +79,12 @@ internal sealed class ScreenshotsCommand : ICommand
|
||||
Resized,
|
||||
Unreadable,
|
||||
|
||||
/// <summary>A screenshot without a baseline: warns only.</summary>
|
||||
/// <summary>A screenshot without a baseline while the baseline folder holds none at all: warns only.</summary>
|
||||
New,
|
||||
|
||||
/// <summary>A screenshot without a baseline while other screens have one.</summary>
|
||||
Unbased,
|
||||
|
||||
/// <summary>A baseline whose screen was not captured.</summary>
|
||||
Missing,
|
||||
}
|
||||
@@ -87,10 +92,11 @@ internal sealed class ScreenshotsCommand : ICommand
|
||||
internal sealed record ScreenResult(string Name, Verdict Verdict, long Differing, long Pixels, string? Detail = null)
|
||||
{
|
||||
public double Fraction => Pixels == 0 ? 0 : (double)Differing / Pixels;
|
||||
public bool Fails => Verdict is Verdict.Changed or Verdict.Resized or Verdict.Unreadable or Verdict.Missing;
|
||||
public bool Fails => Verdict is Verdict.Changed or Verdict.Resized or Verdict.Unreadable or Verdict.Unbased or Verdict.Missing;
|
||||
}
|
||||
|
||||
internal sealed record DiffReport(IReadOnlyList<ScreenResult> Screens)
|
||||
/// <param name="Armed">The baseline folder holds at least one PNG: from then on every screen needs its own.</param>
|
||||
internal sealed record DiffReport(IReadOnlyList<ScreenResult> Screens, bool Armed)
|
||||
{
|
||||
public bool Fails => Screens.Any(s => s.Fails);
|
||||
}
|
||||
@@ -103,6 +109,7 @@ internal sealed class ScreenshotsCommand : ICommand
|
||||
{
|
||||
var captured = PngNames(options.Directory);
|
||||
var baselines = Directory.Exists(options.Baseline) ? PngNames(options.Baseline) : new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
var armed = baselines.Count > 0;
|
||||
// Diffs of an earlier run must not pass for this run's.
|
||||
if (Directory.Exists(options.Output))
|
||||
{
|
||||
@@ -115,7 +122,7 @@ internal sealed class ScreenshotsCommand : ICommand
|
||||
{
|
||||
if (!baselines.Contains(name))
|
||||
{
|
||||
results.Add(new ScreenResult(name, Verdict.New, 0, 0));
|
||||
results.Add(new ScreenResult(name, armed ? Verdict.Unbased : Verdict.New, 0, 0));
|
||||
continue;
|
||||
}
|
||||
results.Add(CompareFiles(name, Path.Combine(options.Baseline, name), Path.Combine(options.Directory, name), options));
|
||||
@@ -124,7 +131,7 @@ internal sealed class ScreenshotsCommand : ICommand
|
||||
results.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.Name, y.Name));
|
||||
|
||||
foreach (var r in results) output.WriteLine($"{Label(r.Verdict),-11} {r.Name}{Describe(r, options)}");
|
||||
var report = new DiffReport(results);
|
||||
var report = new DiffReport(results, armed);
|
||||
var markdown = Markdown(report, options);
|
||||
File.WriteAllText(Path.Combine(options.Output, "summary.md"), markdown);
|
||||
if (stepSummary is not null) File.AppendAllText(stepSummary, markdown);
|
||||
@@ -132,14 +139,17 @@ internal sealed class ScreenshotsCommand : ICommand
|
||||
if (annotate)
|
||||
{
|
||||
foreach (var r in results.Where(r => r.Fails))
|
||||
output.WriteLine($"::error title={Escape("Screenshot " + Label(r.Verdict), property: true)}::{Escape(r.Name + Describe(r, options), property: false)}");
|
||||
// One warning for them all: before the first baselines every screen is new, and GitHub shows only ten.
|
||||
{
|
||||
var title = r.Verdict == Verdict.Unbased ? "Screenshot without baseline" : "Screenshot " + Label(r.Verdict);
|
||||
output.WriteLine($"::error title={Escape(title, property: true)}::{Escape(r.Name + Describe(r, options), property: false)}");
|
||||
}
|
||||
// One warning for them all (GitHub shows only ten): before the first baselines every screen is new.
|
||||
var unbased = results.Where(r => r.Verdict == Verdict.New).Select(r => r.Name).ToList();
|
||||
if (unbased.Count > 0)
|
||||
{
|
||||
var names = string.Join(", ", unbased.Take(5)) + (unbased.Count > 5 ? ", …" : string.Empty);
|
||||
output.WriteLine($"::warning title={Escape("Screenshots without baseline", property: true)}::" +
|
||||
Escape($"{unbased.Count} screenshot(s) have no baseline ({names}). tests/visual/baselines/README.md says how to add them.", property: false));
|
||||
output.WriteLine($"::warning title={Escape("No screenshot baselines", property: true)}::" +
|
||||
Escape($"{NotArmed} {unbased.Count} screenshot(s) without a baseline ({names}); tests/visual/baselines/README.md says how to add them.", property: false));
|
||||
}
|
||||
}
|
||||
return report;
|
||||
@@ -238,6 +248,7 @@ internal sealed class ScreenshotsCommand : ICommand
|
||||
Verdict.Resized => "resized",
|
||||
Verdict.Unreadable => "unreadable",
|
||||
Verdict.New => "new",
|
||||
Verdict.Unbased => "no baseline",
|
||||
_ => "missing",
|
||||
};
|
||||
|
||||
@@ -247,16 +258,20 @@ internal sealed class ScreenshotsCommand : ICommand
|
||||
Verdict.Within or Verdict.Changed => $": {Percent(r.Fraction)} of the pixels differ ({r.Differing.ToString("N0", CultureInfo.InvariantCulture)}; limit {Percent(options.MaxDiff)})",
|
||||
Verdict.Resized or Verdict.Unreadable => $": {r.Detail}",
|
||||
Verdict.New => ": no baseline",
|
||||
Verdict.Unbased => ": no baseline, while other screens have one (screenshots accept adds it)",
|
||||
_ => ": a baseline without a screenshot",
|
||||
};
|
||||
|
||||
/// <summary>What a run before the first baselines must not hide: it compared nothing, so it could not have failed.</summary>
|
||||
private const string NotArmed = "The baseline folder holds no PNG: nothing was compared, and no visual change can fail this step until baselines are committed.";
|
||||
|
||||
private static string Percent(double fraction) => (fraction * 100).ToString("0.###", CultureInfo.InvariantCulture) + " %";
|
||||
|
||||
private static string Totals(DiffReport report)
|
||||
{
|
||||
int Count(params Verdict[] verdicts) => report.Screens.Count(s => verdicts.Contains(s.Verdict));
|
||||
return $"{Count(Verdict.Same, Verdict.Within, Verdict.Changed, Verdict.Resized, Verdict.Unreadable)} compared: {Count(Verdict.Same, Verdict.Within)} within tolerance, " +
|
||||
$"{Count(Verdict.Changed, Verdict.Resized, Verdict.Unreadable)} changed; {Count(Verdict.New)} without a baseline; {Count(Verdict.Missing)} missing.";
|
||||
$"{Count(Verdict.Changed, Verdict.Resized, Verdict.Unreadable)} changed; {Count(Verdict.New, Verdict.Unbased)} without a baseline; {Count(Verdict.Missing)} missing.";
|
||||
}
|
||||
|
||||
private static string Markdown(DiffReport report, DiffOptions options)
|
||||
@@ -264,6 +279,7 @@ internal sealed class ScreenshotsCommand : ICommand
|
||||
var md = new StringBuilder();
|
||||
md.AppendLine("### Screenshot diff").AppendLine();
|
||||
md.AppendLine(CultureInfo.InvariantCulture, $"Tolerance {options.Tolerance} of 255 per channel, at most {Percent(options.MaxDiff)} of the pixels.").AppendLine();
|
||||
if (!report.Armed) md.AppendLine(CultureInfo.InvariantCulture, $"**{NotArmed}**").AppendLine();
|
||||
var listed = report.Screens.Where(s => s.Verdict != Verdict.Same).ToList();
|
||||
if (listed.Count > 0)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user