-
Notifications
You must be signed in to change notification settings - Fork 778
AppTestHelper.AssertAnsiSnapshot + make ToAnsi platform-independent #5343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
941d0dc
AppTestHelper: add AssertAnsiSnapshot golden-file assertion
tig 1483425
Fix ANSI snapshot line endings
Copilot 0d09dc2
Make ToAnsi platform-independent (fixed '\n', not Environment.NewLine)
tig fc8e271
Stop helper before ANSI snapshot mismatch throw
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| namespace AppTestHelpers; | ||
|
|
||
| /// <summary> | ||
| /// Thrown by <see cref="AppTestHelper.AssertAnsiSnapshot" /> when the rendered screen does | ||
| /// not match the recorded golden. Deliberately framework-agnostic (no xunit/nunit | ||
| /// dependency) — any test runner reports a thrown exception as a failure. | ||
| /// </summary> | ||
| public sealed class AnsiSnapshotException : Exception | ||
| { | ||
| /// <inheritdoc cref="AnsiSnapshotException" /> | ||
| public AnsiSnapshotException (string message) : base (message) { } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| using System.Runtime.CompilerServices; | ||
| using System.Text; | ||
|
|
||
| namespace AppTestHelpers; | ||
|
|
||
| public partial class AppTestHelper | ||
| { | ||
| /// <summary> | ||
| /// Asserts the current screen against a golden <b>ANSI</b> snapshot file. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Captures the screen via <c>IDriver.ToAnsi ()</c> — the exact escape-sequence stream | ||
| /// the driver would write to recreate it (truecolor, bold, reverse, blink, layout), | ||
| /// excluding the terminal cursor (a separate, non-deterministic <c>SetCursor</c>, so | ||
| /// snapshots stay stable). Row separators are normalized to <c>\n</c> so the same | ||
| /// golden compares on every platform. The recorded <c>.ans</c> file <i>is</i> the look: | ||
| /// <c>cat <file>.ans</c> in a truecolor terminal reproduces the screen exactly. | ||
| /// </para> | ||
| /// <para> | ||
| /// Complements <see cref="AnsiScreenShot" /> (which only dumps to a writer): this | ||
| /// records on first run (or when the <c>UPDATE_SNAPSHOTS</c> environment variable is | ||
| /// <c>1</c>/<c>true</c>) and otherwise compares byte-for-byte. On mismatch it writes a | ||
| /// sibling <c>.ans.actual</c> and throws with the plain-text render inline plus the | ||
| /// <c>cat</c> commands — enough to verify the look without an interactive run. Set | ||
| /// <c>SNAPSHOT_DIR</c> to override the golden root (default: <c>__snapshots__/</c> | ||
| /// beside the calling test source). | ||
| /// </para> | ||
| /// </remarks> | ||
| /// <param name="name">Snapshot name, unique within the test (becomes <c><name>.ans</c>).</param> | ||
| /// <param name="callerFile">Compiler-supplied; locates <c>__snapshots__/</c> beside the test.</param> | ||
| /// <returns>This <see cref="AppTestHelper" /> (fluent).</returns> | ||
| public AppTestHelper AssertAnsiSnapshot (string name, [CallerFilePath] string callerFile = "") | ||
| { | ||
| ArgumentException.ThrowIfNullOrWhiteSpace (name); | ||
|
|
||
| string? ansi = null; | ||
| string? plain = null; | ||
|
|
||
| WaitIteration (app => | ||
| { | ||
| ansi = app.Driver?.ToAnsi (); | ||
| plain = app.Driver?.ToString (); | ||
| }); | ||
|
|
||
| ansi = NormalizeAnsiLineEndings (ansi ?? string.Empty); | ||
|
|
||
| string dir = SnapshotDirectory (callerFile); | ||
| Directory.CreateDirectory (dir); | ||
| string path = Path.Combine (dir, name + ".ans"); | ||
|
|
||
| bool update = Environment.GetEnvironmentVariable ("UPDATE_SNAPSHOTS") is "1" or "true"; | ||
|
|
||
| if (update || !File.Exists (path)) | ||
| { | ||
| // Byte-exact, UTF-8 without BOM, no newline translation: the file must remain a | ||
| // faithful `cat`-able reproduction of the terminal stream. Mark *.ans `binary` in | ||
| // .gitattributes so core.autocrlf cannot corrupt it. | ||
| File.WriteAllText (path, ansi, new UTF8Encoding (false)); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| string expected = NormalizeAnsiLineEndings (File.ReadAllText (path)); | ||
|
|
||
| if (string.Equals (expected, ansi, StringComparison.Ordinal)) | ||
| { | ||
| return this; | ||
| } | ||
|
|
||
| string actualPath = path + ".actual"; | ||
| File.WriteAllText (actualPath, ansi, new UTF8Encoding (false)); | ||
|
|
||
| throw new AnsiSnapshotException ( | ||
| $""" | ||
| ANSI snapshot '{name}' did not match {path}. | ||
|
|
||
| Plain-text render of the actual screen (glyphs only — colors/styles omitted): | ||
| ---------------------------------------------------------------------- | ||
| {plain} | ||
| ---------------------------------------------------------------------- | ||
|
|
||
| Exact look (with colors/styles): cat '{actualPath}' | ||
| Expected look: cat '{path}' | ||
|
|
||
| If this change is intended, accept it by re-running with UPDATE_SNAPSHOTS=1 | ||
| (or copy the .actual over the .ans). | ||
| """); | ||
| } | ||
|
|
||
| private static string SnapshotDirectory (string callerFile) | ||
| { | ||
| string? overrideDir = Environment.GetEnvironmentVariable ("SNAPSHOT_DIR"); | ||
|
|
||
| if (!string.IsNullOrWhiteSpace (overrideDir)) | ||
| { | ||
| return overrideDir; | ||
| } | ||
|
|
||
| string? sourceDir = Path.GetDirectoryName (callerFile); | ||
|
|
||
| if (string.IsNullOrEmpty (sourceDir)) | ||
| { | ||
| throw new InvalidOperationException ( | ||
| "Could not resolve the snapshot directory from the caller path. Set SNAPSHOT_DIR."); | ||
| } | ||
|
|
||
| return Path.Combine (sourceDir, "__snapshots__"); | ||
| } | ||
|
|
||
| private static string NormalizeAnsiLineEndings (string ansi) => ansi.Replace ("\r\n", "\n").Replace ("\r", "\n"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using AppTestHelpers; | ||
| using Terminal.Gui.Drivers; | ||
|
|
||
| namespace IntegrationTests; | ||
|
|
||
| /// <summary> | ||
| /// Demonstrates <see cref="AppTestHelper.AssertAnsiSnapshot" />: render a screen, capture it | ||
| /// as pure ANSI into a golden, compare byte-for-byte thereafter. The recorded | ||
| /// <c>__snapshots__/*.ans</c> can be <c>cat</c>'d in a truecolor terminal to see the exact | ||
| /// look without an interactive run. | ||
| /// </summary> | ||
| public class AnsiSnapshotTests (ITestOutputHelper outputHelper) | ||
| { | ||
| private readonly TextWriter _out = new TestOutputWriter (outputHelper); | ||
|
|
||
| [Fact] | ||
| public void AssertAnsiSnapshot_Records_Then_Compares () | ||
| { | ||
| using AppTestHelper c = With.A<Window> (20, 4, DriverRegistry.Names.ANSI, _out) | ||
| .Add ( | ||
| new Label | ||
| { | ||
| X = 1, | ||
| Y = 1, | ||
| Text = "Hello, snapshot!" | ||
| }) | ||
| .WaitIteration () | ||
| .AssertAnsiSnapshot (nameof (AssertAnsiSnapshot_Records_Then_Compares)) | ||
| .Stop (); | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
...s/IntegrationTests/FluentTests/__snapshots__/AssertAnsiSnapshot_Records_Then_Compares.ans
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [39m[49m┌──────────────────┐ | ||
| │ │ | ||
| │ Hello, snapshot! │ | ||
| └──────────────────┘ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.