From 2e5f3454114cdf5d0236b060ae16a8704b6df1aa Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sun, 24 Sep 2023 18:28:06 -0700 Subject: [PATCH] Refactor tmux capturing to take in a struct to avoid having to create so many overloaded functions --- client/client_test.go | 6 +++--- client/testutils.go | 49 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index 4485cd1..0fee415 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1651,19 +1651,19 @@ func testTui_color(t testing.TB) { // 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 := captureTerminalOutputComplex(t, TmuxCaptureConfig{tester: tester, complexCommands: []TmuxCommand{{Keys: "hishtory SPACE tquery ENTER"}}, includeEscapeSequences: 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 = captureTerminalOutputComplex(t, TmuxCaptureConfig{tester: tester, complexCommands: []TmuxCommand{{Keys: "hishtory SPACE tquery ENTER"}, {Keys: "ech"}}, includeEscapeSequences: true}) out = strings.TrimSpace(strings.Split(out, "hishtory tquery")[1]) testutils.CompareGoldens(t, out, "TestTui-ColoredOutputWithSearch") // And one more time with beta-mode for highlighting matches tester.RunInteractiveShell(t, ` hishtory config-set beta-mode true`) require.Equal(t, "true", strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get beta-mode`))) - out = captureTerminalOutputComplex(t, tester, tester.ShellName(), 200, 50, []TmuxCommand{{Keys: "hishtory SPACE tquery ENTER"}, {Keys: "ech"}}, true) + out = captureTerminalOutputComplex(t, TmuxCaptureConfig{tester: tester, complexCommands: []TmuxCommand{{Keys: "hishtory SPACE tquery ENTER"}, {Keys: "ech"}}, includeEscapeSequences: true}) out = strings.TrimSpace(strings.Split(out, "hishtory tquery")[1]) testutils.CompareGoldens(t, out, "TestTui-ColoredOutputWithSearch-BetaMode") } diff --git a/client/testutils.go b/client/testutils.go index bda92b3..89517f7 100644 --- a/client/testutils.go +++ b/client/testutils.go @@ -246,15 +246,41 @@ 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) + return captureTerminalOutputComplex(t, + TmuxCaptureConfig{ + tester: tester, + overriddenShellName: overriddenShellName, + width: width, + height: height, + complexCommands: commands, + }) } -func captureTerminalOutputComplex(t testing.TB, tester shellTester, overriddenShellName string, width, height int, commands []TmuxCommand, includeEscapeSequences bool) string { +type TmuxCaptureConfig struct { + tester shellTester + overriddenShellName string + commands []string + complexCommands []TmuxCommand + width, height int + includeEscapeSequences bool +} + +func captureTerminalOutputComplex(t testing.TB, captureConfig TmuxCaptureConfig) string { + require.NotNil(t, captureConfig.tester) + if captureConfig.overriddenShellName == "" { + captureConfig.overriddenShellName = captureConfig.tester.ShellName() + } + if captureConfig.width == 0 { + captureConfig.width = 200 + } + if captureConfig.height == 0 { + captureConfig.height = 50 + } sleepAmount := "0.1" if runtime.GOOS == "linux" { sleepAmount = "0.2" } - if overriddenShellName == "fish" { + if captureConfig.overriddenShellName == "fish" { // Fish is considerably slower so this is sadly necessary sleepAmount = "0.5" } @@ -263,13 +289,20 @@ func captureTerminalOutputComplex(t testing.TB, tester shellTester, overriddenSh } fullCommand := "" fullCommand += " tmux kill-session -t foo || true\n" - fullCommand += fmt.Sprintf(" tmux -u new-session -d -x %d -y %d -s foo %s\n", width, height, overriddenShellName) + fullCommand += fmt.Sprintf(" tmux -u new-session -d -x %d -y %d -s foo %s\n", captureConfig.width, captureConfig.height, captureConfig.overriddenShellName) fullCommand += " sleep 1\n" - if overriddenShellName == "bash" { + if captureConfig.overriddenShellName == "bash" { fullCommand += " tmux send -t foo SPACE source SPACE ~/.bashrc ENTER\n" } fullCommand += " sleep " + sleepAmount + "\n" - for _, cmd := range commands { + if len(captureConfig.commands) > 0 { + require.Empty(t, captureConfig.complexCommands) + for _, command := range captureConfig.commands { + captureConfig.complexCommands = append(captureConfig.complexCommands, TmuxCommand{Keys: command}) + } + } + require.NotEmpty(t, captureConfig.complexCommands) + for _, cmd := range captureConfig.complexCommands { if cmd.Keys != "" { fullCommand += " tmux send -t foo -- " fullCommand += cmd.Keys @@ -288,13 +321,13 @@ func captureTerminalOutputComplex(t testing.TB, tester shellTester, overriddenSh fullCommand += " sleep 2.5\n" } fullCommand += " tmux capture-pane -t foo -p" - if includeEscapeSequences { + if captureConfig.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)) + return strings.TrimSpace(captureConfig.tester.RunInteractiveShell(t, fullCommand)) } func assertNoLeakedConnections(t testing.TB) {