Refactor tmux capturing to take in a struct to avoid having to create so many overloaded functions

This commit is contained in:
David Dworken 2023-09-24 18:28:06 -07:00
parent 7e33e942e6
commit 2e5f345411
No known key found for this signature in database
2 changed files with 44 additions and 11 deletions

View File

@ -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")
}

View File

@ -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) {