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) } } }