diff --git a/client/client_test.go b/client/client_test.go index a0117a8..b2e9410 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -98,6 +98,7 @@ func TestParam(t *testing.T) { t.Run("testPresavingDisabled/"+tester.ShellName(), func(t *testing.T) { testPresavingDisabled(t, tester) }) t.Run("testControlR/online/"+tester.ShellName(), func(t *testing.T) { testControlR(t, tester, tester.ShellName(), Online) }) t.Run("testControlR/offline/"+tester.ShellName(), func(t *testing.T) { testControlR(t, tester, tester.ShellName(), Offline) }) + t.Run("testTabCompletion/"+tester.ShellName(), func(t *testing.T) { testTabCompletion(t, tester) }) } t.Run("testPresaving/fish", func(t *testing.T) { testPresaving(t, zshTester{}, "fish") }) t.Run("testControlR/fish", func(t *testing.T) { testControlR(t, bashTester{}, "fish", Online) }) @@ -2370,6 +2371,22 @@ func testPresaving(t *testing.T, tester shellTester, shellName string) { } } +func testTabCompletion(t *testing.T, tester shellTester) { + // Setup + defer testutils.BackupAndRestore(t)() + installHishtory(t, tester, "") + + // Check that tab completions work to complete a command + out := captureTerminalOutput(t, tester, []string{"hishtory SPACE config-g Tab"}) + expected := "hishtory config-get" + require.True(t, strings.HasSuffix(out, expected), fmt.Sprintf("Expected out=%#v to end with %#v", out, expected)) + + // Check that tab completions work to view suggestions + out = captureTerminalOutput(t, tester, []string{"hishtory SPACE config- Tab"}) + out = strings.Join(strings.Split(out, "\n")[1:], "\n") + testutils.CompareGoldens(t, out, "testTabCompletion-suggestions") +} + func testUninstall(t *testing.T, tester shellTester) { // Setup defer testutils.BackupAndRestore(t)() diff --git a/client/lib/config.zsh b/client/lib/config.zsh index f444106..168d357 100644 --- a/client/lib/config.zsh +++ b/client/lib/config.zsh @@ -41,4 +41,11 @@ _hishtory_bind_control_r() { [ "$(hishtory config-get enable-control-r)" = true ] && _hishtory_bind_control_r -source <(hishtory completion zsh); compdef _hishtory hishtory \ No newline at end of file +# If running in a test environment, force loading of compinit so that shell completions work. +# Otherwise, we respect the user's choice and only run compdef if the user has loaded compinit. +if [ -n "${HISHTORY_TEST:-}" ]; then + autoload -Uz compinit + compinit +fi + +source <(hishtory completion zsh); which compdef 2>&1 >/dev/null && compdef _hishtory hishtory \ No newline at end of file diff --git a/client/lib/goldens/testTabCompletion-suggestions b/client/lib/goldens/testTabCompletion-suggestions new file mode 100644 index 0000000..e1b953a --- /dev/null +++ b/client/lib/goldens/testTabCompletion-suggestions @@ -0,0 +1,4 @@ +config-add -- Add a config option +config-delete -- Delete a config option +config-get -- Get the value of a config option +config-set -- Set the value of a config option \ No newline at end of file