From b3fa5c9a6b388205a70c6064d6f624eb8fe91a3b Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sun, 24 Sep 2023 17:03:54 -0700 Subject: [PATCH] Add very basic TUI test that also tests how the TUI uses escape sequences for colors --- client/client_test.go | 21 ++++++++++++--- client/lib/goldens/TestTui-ColoredOutput | 27 +++++++++++++++++++ .../goldens/TestTui-ColoredOutputWithSearch | 27 +++++++++++++++++++ client/testutils.go | 10 ++++++- 4 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 client/lib/goldens/TestTui-ColoredOutput create mode 100644 client/lib/goldens/TestTui-ColoredOutputWithSearch diff --git a/client/client_test.go b/client/client_test.go index 70e7285..dd02631 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -111,6 +111,7 @@ func TestParam(t *testing.T) { runTestsWithRetries(t, "testTui/scroll", testTui_scroll) runTestsWithRetries(t, "testTui/resize", testTui_resize) runTestsWithRetries(t, "testTui/delete", testTui_delete) + runTestsWithRetries(t, "testTui/color", testTui_color) // Assert there are no leaked connections assertNoLeakedConnections(t) @@ -1604,9 +1605,6 @@ func testTui_resize(t testing.TB) { }) out = strings.TrimSpace(strings.Split(out, "hishtory tquery")[1]) testutils.CompareGoldens(t, out, "TestTui-LongQuery") - - // Assert there are no leaked connections - // assertNoLeakedConnections(t) } func testTui_scroll(t testing.TB) { @@ -1646,6 +1644,23 @@ func testTui_scroll(t testing.TB) { } +func testTui_color(t testing.TB) { + // Setup + defer testutils.BackupAndRestore(t)() + tester, _, _ := setupTestTui(t) + + // Capture the TUI with full colored output, note that this golden will be harder to undersand + // from inspection and primarily servers to detect unintended changes in hishtory's output. + out := captureTerminalOutputComplex(t, tester, tester.ShellName(), 200, 50, []TmuxCommand{{Keys: "hishtory SPACE tquery ENTER"}}, true) + out = strings.TrimSpace(strings.Split(out, "hishtory tquery")[1]) + testutils.CompareGoldens(t, out, "TestTui-ColoredOutput") + + // And the same once a search query has been typed in + out = captureTerminalOutputComplex(t, tester, tester.ShellName(), 200, 50, []TmuxCommand{{Keys: "hishtory SPACE tquery ENTER"}, {Keys: "ech"}}, true) + out = strings.TrimSpace(strings.Split(out, "hishtory tquery")[1]) + testutils.CompareGoldens(t, out, "TestTui-ColoredOutputWithSearch") +} + func testTui_delete(t testing.TB) { // Setup defer testutils.BackupAndRestore(t)() diff --git a/client/lib/goldens/TestTui-ColoredOutput b/client/lib/goldens/TestTui-ColoredOutput new file mode 100644 index 0000000..b8ca946 --- /dev/null +++ b/client/lib/goldens/TestTui-ColoredOutput @@ -0,0 +1,27 @@ +Search Query: > ls + +┌────────────────────────────────────────────────────────────────────────────────────────────────────────┐ +│ Hostname CWD Timestamp Runtime Exit Code Command │ +│────────────────────────────────────────────────────────────────────────────────────────────────────────│ +│ localhost /tmp/ Oct 17 2022 21:43:21 PDT 3s 2 echo 'aaaaaa bbbb' │ +│ localhost /tmp/ Oct 17 2022 21:43:16 PDT 3s 2 ls ~/ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +└────────────────────────────────────────────────────────────────────────────────────────────────────────┘ +hiSHtory: Search your shell history  • ctrl+h help \ No newline at end of file diff --git a/client/lib/goldens/TestTui-ColoredOutputWithSearch b/client/lib/goldens/TestTui-ColoredOutputWithSearch new file mode 100644 index 0000000..e56e5b3 --- /dev/null +++ b/client/lib/goldens/TestTui-ColoredOutputWithSearch @@ -0,0 +1,27 @@ +Search Query: > ech  + +┌────────────────────────────────────────────────────────────────────────────────────────────────────────┐ +│ Hostname CWD Timestamp Runtime Exit Code Command │ +│────────────────────────────────────────────────────────────────────────────────────────────────────────│ +│ localhost /tmp/ Oct 17 2022 21:43:21 PDT 3s 2 echo 'aaaaaa bbbb' │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +└────────────────────────────────────────────────────────────────────────────────────────────────────────┘ +hiSHtory: Search your shell history  • ctrl+h help \ No newline at end of file diff --git a/client/testutils.go b/client/testutils.go index 60e7619..bda92b3 100644 --- a/client/testutils.go +++ b/client/testutils.go @@ -246,6 +246,10 @@ func captureTerminalOutputWithShellName(t testing.TB, tester shellTester, overri } func captureTerminalOutputWithShellNameAndDimensions(t testing.TB, tester shellTester, overriddenShellName string, width, height int, commands []TmuxCommand) string { + return captureTerminalOutputComplex(t, tester, overriddenShellName, width, height, commands, false) +} + +func captureTerminalOutputComplex(t testing.TB, tester shellTester, overriddenShellName string, width, height int, commands []TmuxCommand, includeEscapeSequences bool) string { sleepAmount := "0.1" if runtime.GOOS == "linux" { sleepAmount = "0.2" @@ -283,7 +287,11 @@ func captureTerminalOutputWithShellNameAndDimensions(t testing.TB, tester shellT if testutils.IsGithubAction() { fullCommand += " sleep 2.5\n" } - fullCommand += " tmux capture-pane -t foo -p\n" + fullCommand += " tmux capture-pane -t foo -p" + if includeEscapeSequences { + fullCommand += "e" + } + fullCommand += "\n" fullCommand += " tmux kill-session -t foo\n" testutils.TestLog(t, "Running tmux command: "+fullCommand) return strings.TrimSpace(tester.RunInteractiveShell(t, fullCommand))