Files
core/completion/completion_test.go
T
terminarandClaude Opus 5.5 131d82642c configcmd.OneOf for alternative fields; completion.Files for paths
UndocumentedKeys reported the unused alternatives of a struct like
SecretRef (env/file/value) as undocumented; a struct implementing the
marker configcmd.OneOf now counts as documented with one alternative,
and upgrade offers none of the others to a file that chose one.

completion.Tree gets the key completion.Files: that level completes
file and directory paths besides command names (bash, zsh, fish,
PowerShell), for programs started as "<program> [path]" like yamc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 23:40:45 +01:00

117 lines
3.4 KiB
Go

package completion
import (
"errors"
"os"
"os/exec"
"path/filepath"
"strconv"
"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)
}
}
// A program without subcommands takes a path as its first argument.
var pathTree = Tree{
Files: nil,
"manual": nil,
"config": Leaf("path"),
"completion": Leaf(Shells...),
}
func TestScriptsWithFiles(t *testing.T) {
want := map[string]string{
"bash": "compgen -f",
"zsh": "_files",
"fish": "__fish_complete_path",
"powershell": "CompleteFilename",
}
for _, shell := range Shells {
s, err := Script("tool", shell, pathTree)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(s, want[shell]) {
t.Errorf("%s script lacks path completion %q", shell, want[shell])
}
if strings.Contains(s, Files) {
t.Errorf("%s script offers the marker %q as a word", shell, Files)
}
// Without the marker no level completes paths.
s, _ = Script("tool", shell, tree)
if strings.Contains(s, want[shell]) {
t.Errorf("%s script completes paths without Files", shell)
}
}
}
// TestBashCompletesPaths runs the generated bash function in a directory
// with a file whose name has a space, when bash is there.
func TestBashCompletesPaths(t *testing.T) {
if _, err := exec.LookPath("bash"); err != nil {
t.Skip("no bash")
}
dir := t.TempDir()
for _, name := range []string{"my notes.txt", "manuscript"} {
if err := os.WriteFile(filepath.Join(dir, name), nil, 0o600); err != nil {
t.Fatal(err)
}
}
s, _ := Script("tool", "bash", pathTree)
for cword, want := range map[string]string{
"m": "manual|manuscript|my notes.txt", // command names and paths
"config p": "path", // a level without Files: no paths
} {
words := strings.Fields("tool " + cword)
script := s + "\nCOMP_WORDS=(" + strings.Join(words, " ") + "); COMP_CWORD=" +
strconv.Itoa(len(words)-1) + "; _tool_complete; printf '%s\\n' \"${COMPREPLY[@]}\" | sort | paste -sd '|' -"
cmd := exec.Command("bash", "-c", script)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil || strings.TrimSpace(string(out)) != want {
t.Errorf("bash completion of %q: %v %q, want %q", cword, err, out, want)
}
}
}