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