core/completion prints bash, zsh, fish or PowerShell completion scripts
from a static command tree ("<program> completion <shell>"); core/doctor
is one report shape (resolved paths incl. the standard core/paths ones,
named checks), printed as a table or --json, exit 0/1/2. Together with
core/manual they implement the Yetano program standard
(docs/program-conventions.md in the meta-repo).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package completion
|
|
|
|
import (
|
|
"errors"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
var tree = Tree{
|
|
"serve": nil,
|
|
"config": Leaf("create", "validate"),
|
|
"completion": Leaf(Shells...),
|
|
}
|
|
|
|
func TestScripts(t *testing.T) {
|
|
for _, shell := range Shells {
|
|
s, err := Script("my-tool", shell, tree)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, want := range []string{"config", "create", "validate", "powershell"} {
|
|
if !strings.Contains(s, want) {
|
|
t.Errorf("%s script lacks %q", shell, want)
|
|
}
|
|
}
|
|
}
|
|
if _, err := Script("tool", "tcsh", tree); !errors.Is(err, ErrUsage) {
|
|
t.Errorf("unknown shell: %v", err)
|
|
}
|
|
if _, err := Script("tool", "bash", Tree{"a b": nil}); err == nil {
|
|
t.Error("a name with a space must be refused")
|
|
}
|
|
var b strings.Builder
|
|
if err := Run("tool", tree, nil, &b); !errors.Is(err, ErrUsage) {
|
|
t.Errorf("no shell: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestBashCompletes runs the generated bash function, when bash is there.
|
|
func TestBashCompletes(t *testing.T) {
|
|
if _, err := exec.LookPath("bash"); err != nil {
|
|
t.Skip("no bash")
|
|
}
|
|
s, _ := Script("tool", "bash", tree)
|
|
out, err := exec.Command("bash", "-c", s+"\nCOMP_WORDS=(tool config v); COMP_CWORD=2; _tool_complete; echo \"${COMPREPLY[@]}\"").CombinedOutput()
|
|
if err != nil || strings.TrimSpace(string(out)) != "validate" {
|
|
t.Fatalf("bash completion: %v %q", err, out)
|
|
}
|
|
}
|