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>
This commit is contained in:
1 parent
83f4228e66
commit
131d82642c
6 files changed
+244
-13
No files matched your search
@@ -2,7 +2,10 @@ package completion
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -48,3 +51,66 @@ func TestBashCompletes(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user