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
@@ -5,7 +5,10 @@ using static Titano.Tools.Commands.ScreenshotsCommand;
|
||||
|
||||
namespace Titano.Tools.Tests;
|
||||
|
||||
/// <summary>The screenshot diff fails on changed screens, tolerates noise and only warns about screens without a baseline (BRIEF §20.2).</summary>
|
||||
/// <summary>
|
||||
/// The screenshot diff fails on changed screens and tolerates noise (BRIEF §20.2); a screen without a baseline fails
|
||||
/// once any baseline exists, and a run before the first baselines says that it compared nothing.
|
||||
/// </summary>
|
||||
public sealed class ScreenshotsCommandTests(TestContext context)
|
||||
{
|
||||
private const int Size = 100;
|
||||
@@ -90,17 +93,47 @@ public sealed class ScreenshotsCommandTests(TestContext context)
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AScreenWithoutABaselineOnlyWarns()
|
||||
public void BeforeTheFirstBaselinesTheRunWarnsThatItComparedNothing()
|
||||
{
|
||||
var (captured, baseline, output) = Folders();
|
||||
WriteScreen(captured, "welcome-dark.png");
|
||||
using var log = new StringWriter();
|
||||
var report = Run(captured, Path.Combine(baseline, "not-there"), output, log);
|
||||
WriteScreen(captured, "about-dark.png");
|
||||
// A README is not a baseline: the folder as committed before the first screens are accepted.
|
||||
File.WriteAllText(Path.Combine(baseline, "README.md"), "# Screenshot baselines");
|
||||
foreach (var folder in new[] { baseline, Path.Combine(baseline, "not-there") })
|
||||
{
|
||||
using var log = new StringWriter();
|
||||
var report = Run(captured, folder, output, log);
|
||||
|
||||
Assert.False(report.Fails);
|
||||
Assert.Equal(Verdict.New, report.Screens.Single().Verdict);
|
||||
Assert.Contains("::warning title=Screenshots without baseline::1 screenshot(s) have no baseline (welcome-dark.png).", log.ToString());
|
||||
Assert.Contains("0 compared", log.ToString());
|
||||
Assert.False(report.Fails);
|
||||
Assert.False(report.Armed);
|
||||
Assert.True(report.Screens.All(s => s.Verdict == Verdict.New));
|
||||
Assert.Contains("::warning title=No screenshot baselines::The baseline folder holds no PNG: nothing was compared, and no visual change can fail " +
|
||||
"this step until baselines are committed. 2 screenshot(s) without a baseline (about-dark.png, welcome-dark.png);", log.ToString());
|
||||
Assert.False(log.ToString().Contains("::error", StringComparison.Ordinal));
|
||||
Assert.Contains("0 compared", log.ToString());
|
||||
Assert.Contains("**The baseline folder holds no PNG: nothing was compared", File.ReadAllText(Path.Combine(output, "summary.md")));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OnceAnyBaselineExistsAScreenWithoutOneFails()
|
||||
{
|
||||
var (captured, baseline, output) = Folders();
|
||||
WriteScreen(baseline, "library-dark.png");
|
||||
WriteScreen(captured, "library-dark.png");
|
||||
// A screen added after the baselines were committed, whose baseline nobody accepted.
|
||||
WriteScreen(captured, "library-new-dark.png");
|
||||
using var log = new StringWriter();
|
||||
var report = Run(captured, baseline, output, log);
|
||||
|
||||
Assert.True(report.Armed);
|
||||
Assert.True(report.Fails);
|
||||
Assert.SequenceEqual(new[] { Verdict.Same, Verdict.Unbased }, report.Screens.Select(s => s.Verdict));
|
||||
Assert.Contains("::error title=Screenshot without baseline::library-new-dark.png: no baseline, while other screens have one (screenshots accept adds it)", log.ToString());
|
||||
Assert.False(log.ToString().Contains("::warning", StringComparison.Ordinal), "the not-compared warning belongs to a run before the first baselines");
|
||||
Assert.Contains("1 compared: 1 within tolerance, 0 changed; 1 without a baseline; 0 missing.", log.ToString());
|
||||
Assert.False(File.ReadAllText(Path.Combine(output, "summary.md")).Contains("holds no PNG", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -138,8 +171,11 @@ public sealed class ScreenshotsCommandTests(TestContext context)
|
||||
int Run(params string[] args) => ScreenshotsCommand.Run(args, _ => null, log);
|
||||
|
||||
Assert.Equal(1, Run("diff", captured, "--baseline", baseline, "--out", output, "--tolerance", "8", "--max-diff", "0.001"));
|
||||
Assert.Equal(0, Run("diff", captured, "--baseline", baseline, "--out", output, "--max-diff", "0.05"));
|
||||
// a.png is now within the limit, but b.png still has no baseline beside a.png's.
|
||||
Assert.Equal(1, Run("diff", captured, "--baseline", baseline, "--out", output, "--max-diff", "0.05"));
|
||||
Assert.FileExists(Path.Combine(output, "a-diff.png"));
|
||||
File.Copy(Path.Combine(captured, "b.png"), Path.Combine(baseline, "b.png"));
|
||||
Assert.Equal(0, Run("diff", captured, "--baseline", baseline, "--out", output, "--max-diff", "0.05"));
|
||||
Assert.Equal(0, Run("accept", captured, "--baseline", baseline));
|
||||
Assert.FileExists(Path.Combine(baseline, "b.png"));
|
||||
Assert.Equal(0, Run("diff", captured, "--baseline", baseline, "--out", output));
|
||||
|
||||
Reference in new issue
Block a user