Add shell completion

This commit is contained in:
2026-07-28 20:31:44 +02:00
parent ef32aa473e
commit e89c524438
5 changed files with 371 additions and 16 deletions

77
cli_test.go Normal file
View File

@@ -0,0 +1,77 @@
package main
import (
"bytes"
"strings"
"testing"
)
func TestCompletionCommandGeneratesSupportedShells(t *testing.T) {
for _, shell := range completionShells {
t.Run(shell, func(t *testing.T) {
var output bytes.Buffer
handled, err := handleCompletionCommand([]string{"completion", shell}, &output)
if err != nil {
t.Fatal(err)
}
if !handled || output.Len() == 0 ||
!strings.Contains(output.String(), "diple") {
t.Fatalf("completion output = %q, handled=%t", output.String(), handled)
}
})
}
}
func TestZshCompletionRegistersWithoutCallingCompletionFunction(t *testing.T) {
var output bytes.Buffer
handled, err := handleCompletionCommand([]string{"completion", "zsh"}, &output)
if err != nil {
t.Fatal(err)
}
script := output.String()
if !handled || !strings.Contains(script, "compdef _diple diple") {
t.Fatalf("Zsh completion does not register _diple:\n%s", script)
}
if strings.Contains(script, `_diple "$@"`) {
t.Fatalf("Zsh completion invokes _diple while being sourced:\n%s", script)
}
}
func TestCompletionCommandRejectsUnknownShell(t *testing.T) {
var output bytes.Buffer
handled, err := handleCompletionCommand(
[]string{"completion", "powershell"}, &output,
)
if !handled || err == nil || !strings.Contains(err.Error(), "unsupported shell") {
t.Fatalf("handled=%t error=%v", handled, err)
}
}
func TestCompletionCommandDoesNotClaimNormalInvocation(t *testing.T) {
handled, err := handleCompletionCommand([]string{"--repo", "owner/repo"}, &bytes.Buffer{})
if handled || err != nil {
t.Fatalf("handled=%t error=%v", handled, err)
}
}
func TestCLIHelpIsGroupedAndActionable(t *testing.T) {
var output bytes.Buffer
writeCLIHelp(&output, defaultConfig(), "/tmp/diple/config.toml")
help := output.String()
for _, expected := range []string{
"Usage:",
"Pull-request selection:",
"GitHub and refresh:",
"Appearance and navigation:",
"Local state:",
"diple completion <bash|zsh|fish>",
"--repo OWNER/REPOSITORY",
"--cache=false",
"gh auth login",
"/tmp/diple/config.toml",
} {
if !strings.Contains(help, expected) {
t.Fatalf("help does not contain %q:\n%s", expected, help)
}
}
}