From b6b4dbe37a457f7a30a34a1758612704b9072109 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Wed, 13 Sep 2023 20:29:06 -0700 Subject: [PATCH] Add tests for presaving history entries --- client/client_test.go | 41 +++++++++++++++++++++++++ client/lib/goldens/testPresaving-query | 2 ++ client/lib/goldens/testPresaving-tquery | 27 ++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 client/lib/goldens/testPresaving-query create mode 100644 client/lib/goldens/testPresaving-tquery diff --git a/client/client_test.go b/client/client_test.go index c06d5dc..48570b9 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -182,6 +182,7 @@ func TestParam(t *testing.T) { t.Run("testHandleUpgradedFeatures/"+tester.ShellName(), func(t *testing.T) { testHandleUpgradedFeatures(t, tester) }) t.Run("testCustomColumns/"+tester.ShellName(), func(t *testing.T) { testCustomColumns(t, tester) }) t.Run("testUninstall/"+tester.ShellName(), func(t *testing.T) { testUninstall(t, tester) }) + t.Run("testPresaving/"+tester.ShellName(), func(t *testing.T) { testPresaving(t, tester) }) runTestsWithRetries(t, "testControlR/"+tester.ShellName(), func(t testing.TB) { testControlR(t, tester, tester.ShellName(), Online) }) } runTestsWithRetries(t, "testControlR/offline/bash", func(t testing.TB) { testControlR(t, bashTester{}, "bash", Offline) }) @@ -2310,6 +2311,46 @@ echo bar`) testutils.CompareGoldens(t, out, testName) } +func testPresaving(t *testing.T, tester shellTester) { + // Setup + defer testutils.BackupAndRestore(t)() + userSecret := installHishtory(t, tester, "") + manuallySubmitHistoryEntry(t, userSecret, testutils.MakeFakeHistoryEntry("table_sizing aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) + + // Enable beta-mode since presaving is behind that feature flag + out := strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get beta-mode`)) + require.Equal(t, out, "false") + tester.RunInteractiveShell(t, `hishtory config-set beta-mode true`) + out = strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get beta-mode`)) + require.Equal(t, out, "true") + + // Start a command that will take a long time to execute in the background, so + // we can check that it was recorded even though it never finished. + require.NoError(t, os.Chdir("/")) + go tester.RunInteractiveShell(t, `sleep 13371337`) + time.Sleep(time.Millisecond * 500) + + // Test that it shows up in hishtory export + out = tester.RunInteractiveShell(t, ` hishtory export sleep -export`) + expectedOutput := "sleep 13371337\n" + if diff := cmp.Diff(expectedOutput, out); diff != "" { + t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out) + } + + // Test that it shows up in hishtory query and that the runtime is displayed correctly + tester.RunInteractiveShell(t, ` hishtory config-set displayed-columns CWD Runtime Command`) + out = tester.RunInteractiveShell(t, ` hishtory query sleep 13371337 -export -tquery`) + testutils.CompareGoldens(t, out, "testPresaving-query") + + // And the same for tquery + out = captureTerminalOutputWithComplexCommands(t, tester, + []TmuxCommand{ + {Keys: "hishtory SPACE tquery ENTER", ExtraDelay: 2.0}, + {Keys: "sleep SPACE 13371337 SPACE -export SPACE -tquery"}}) + out = strings.TrimSpace(strings.Split(out, "hishtory tquery")[1]) + testutils.CompareGoldens(t, out, "testPresaving-tquery") +} + func testUninstall(t *testing.T, tester shellTester) { // Setup defer testutils.BackupAndRestore(t)() diff --git a/client/lib/goldens/testPresaving-query b/client/lib/goldens/testPresaving-query new file mode 100644 index 0000000..1bf276c --- /dev/null +++ b/client/lib/goldens/testPresaving-query @@ -0,0 +1,2 @@ +CWD Runtime Command +/ N/A sleep 13371337 diff --git a/client/lib/goldens/testPresaving-tquery b/client/lib/goldens/testPresaving-tquery new file mode 100644 index 0000000..b2b645a --- /dev/null +++ b/client/lib/goldens/testPresaving-tquery @@ -0,0 +1,27 @@ +Search Query: > sleep 13371337 -export -tquery + +┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ +│ CWD Runtime Command │ +│─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────│ +│ / N/A sleep 13371337 │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ +hiSHtory: Search your shell history • ctrl+h help \ No newline at end of file