|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using System.Text; |
| 3 | + |
| 4 | +namespace AppTestHelpers; |
| 5 | + |
| 6 | +public partial class AppTestHelper |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Asserts the current screen against a golden <b>ANSI</b> snapshot file. |
| 10 | + /// </summary> |
| 11 | + /// <remarks> |
| 12 | + /// <para> |
| 13 | + /// Captures the screen via <c>IDriver.ToAnsi ()</c> — the exact escape-sequence stream |
| 14 | + /// the driver would write to recreate it (truecolor, bold, reverse, blink, layout), |
| 15 | + /// excluding the terminal cursor (a separate, non-deterministic <c>SetCursor</c>, so |
| 16 | + /// snapshots stay stable). Row separators are normalized to <c>\n</c> so the same |
| 17 | + /// golden compares on every platform. The recorded <c>.ans</c> file <i>is</i> the look: |
| 18 | + /// <c>cat <file>.ans</c> in a truecolor terminal reproduces the screen exactly. |
| 19 | + /// </para> |
| 20 | + /// <para> |
| 21 | + /// Complements <see cref="AnsiScreenShot" /> (which only dumps to a writer): this |
| 22 | + /// records on first run (or when the <c>UPDATE_SNAPSHOTS</c> environment variable is |
| 23 | + /// <c>1</c>/<c>true</c>) and otherwise compares byte-for-byte. On mismatch it writes a |
| 24 | + /// sibling <c>.ans.actual</c> and throws with the plain-text render inline plus the |
| 25 | + /// <c>cat</c> commands — enough to verify the look without an interactive run. Set |
| 26 | + /// <c>SNAPSHOT_DIR</c> to override the golden root (default: <c>__snapshots__/</c> |
| 27 | + /// beside the calling test source). |
| 28 | + /// </para> |
| 29 | + /// </remarks> |
| 30 | + /// <param name="name">Snapshot name, unique within the test (becomes <c><name>.ans</c>).</param> |
| 31 | + /// <param name="callerFile">Compiler-supplied; locates <c>__snapshots__/</c> beside the test.</param> |
| 32 | + /// <returns>This <see cref="AppTestHelper" /> (fluent).</returns> |
| 33 | + public AppTestHelper AssertAnsiSnapshot (string name, [CallerFilePath] string callerFile = "") |
| 34 | + { |
| 35 | + ArgumentException.ThrowIfNullOrWhiteSpace (name); |
| 36 | + |
| 37 | + string? ansi = null; |
| 38 | + string? plain = null; |
| 39 | + |
| 40 | + WaitIteration (app => |
| 41 | + { |
| 42 | + ansi = app.Driver?.ToAnsi (); |
| 43 | + plain = app.Driver?.ToString (); |
| 44 | + }); |
| 45 | + |
| 46 | + ansi = NormalizeAnsiLineEndings (ansi ?? string.Empty); |
| 47 | + |
| 48 | + string dir = SnapshotDirectory (callerFile); |
| 49 | + Directory.CreateDirectory (dir); |
| 50 | + string path = Path.Combine (dir, name + ".ans"); |
| 51 | + |
| 52 | + bool update = Environment.GetEnvironmentVariable ("UPDATE_SNAPSHOTS") is "1" or "true"; |
| 53 | + |
| 54 | + if (update || !File.Exists (path)) |
| 55 | + { |
| 56 | + // Byte-exact, UTF-8 without BOM, no newline translation: the file must remain a |
| 57 | + // faithful `cat`-able reproduction of the terminal stream. Mark *.ans `binary` in |
| 58 | + // .gitattributes so core.autocrlf cannot corrupt it. |
| 59 | + File.WriteAllText (path, ansi, new UTF8Encoding (false)); |
| 60 | + |
| 61 | + return this; |
| 62 | + } |
| 63 | + |
| 64 | + string expected = NormalizeAnsiLineEndings (File.ReadAllText (path)); |
| 65 | + |
| 66 | + if (string.Equals (expected, ansi, StringComparison.Ordinal)) |
| 67 | + { |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + string actualPath = path + ".actual"; |
| 72 | + File.WriteAllText (actualPath, ansi, new UTF8Encoding (false)); |
| 73 | + |
| 74 | + AnsiSnapshotException exception = new ( |
| 75 | + $""" |
| 76 | + ANSI snapshot '{name}' did not match {path}. |
| 77 | +
|
| 78 | + Plain-text render of the actual screen (glyphs only — colors/styles omitted): |
| 79 | + ---------------------------------------------------------------------- |
| 80 | + {plain} |
| 81 | + ---------------------------------------------------------------------- |
| 82 | +
|
| 83 | + Exact look (with colors/styles): cat '{actualPath}' |
| 84 | + Expected look: cat '{path}' |
| 85 | +
|
| 86 | + If this change is intended, accept it by re-running with UPDATE_SNAPSHOTS=1 |
| 87 | + (or copy the .actual over the .ans). |
| 88 | + """); |
| 89 | + |
| 90 | + Stop (); |
| 91 | + |
| 92 | + throw exception; |
| 93 | + } |
| 94 | + |
| 95 | + private static string SnapshotDirectory (string callerFile) |
| 96 | + { |
| 97 | + string? overrideDir = Environment.GetEnvironmentVariable ("SNAPSHOT_DIR"); |
| 98 | + |
| 99 | + if (!string.IsNullOrWhiteSpace (overrideDir)) |
| 100 | + { |
| 101 | + return overrideDir; |
| 102 | + } |
| 103 | + |
| 104 | + string? sourceDir = Path.GetDirectoryName (callerFile); |
| 105 | + |
| 106 | + if (string.IsNullOrEmpty (sourceDir)) |
| 107 | + { |
| 108 | + throw new InvalidOperationException ( |
| 109 | + "Could not resolve the snapshot directory from the caller path. Set SNAPSHOT_DIR."); |
| 110 | + } |
| 111 | + |
| 112 | + return Path.Combine (sourceDir, "__snapshots__"); |
| 113 | + } |
| 114 | + |
| 115 | + private static string NormalizeAnsiLineEndings (string ansi) => ansi.Replace ("\r\n", "\n").Replace ("\r", "\n"); |
| 116 | +} |
0 commit comments