2020-07-10 13:12:46 -07:00
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-06-15 17:08:16 -07:00
|
|
|
"fmt"
|
2022-06-19 19:38:49 -07:00
|
|
|
"log"
|
|
|
|
"os"
|
2020-07-10 13:12:46 -07:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCompleteNoDesCmdInFishScript(t *testing.T) {
|
|
|
|
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
|
|
|
|
child := &Command{
|
|
|
|
Use: "child",
|
|
|
|
ValidArgsFunction: validArgsFunc,
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
rootCmd.AddCommand(child)
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2021-02-07 17:08:50 -07:00
|
|
|
assertNoErr(t, rootCmd.GenFishCompletion(buf, false))
|
2020-07-10 13:12:46 -07:00
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
check(t, output, ShellCompNoDescRequestCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompleteCmdInFishScript(t *testing.T) {
|
|
|
|
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
|
|
|
|
child := &Command{
|
|
|
|
Use: "child",
|
|
|
|
ValidArgsFunction: validArgsFunc,
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
rootCmd.AddCommand(child)
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2021-02-07 17:08:50 -07:00
|
|
|
assertNoErr(t, rootCmd.GenFishCompletion(buf, true))
|
2020-07-10 13:12:46 -07:00
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
check(t, output, ShellCompRequestCmd)
|
|
|
|
checkOmit(t, output, ShellCompNoDescRequestCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProgWithDash(t *testing.T) {
|
|
|
|
rootCmd := &Command{Use: "root-dash", Args: NoArgs, Run: emptyRun}
|
|
|
|
buf := new(bytes.Buffer)
|
2021-02-07 17:08:50 -07:00
|
|
|
assertNoErr(t, rootCmd.GenFishCompletion(buf, false))
|
2020-07-10 13:12:46 -07:00
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
// Functions name should have replace the '-'
|
|
|
|
check(t, output, "__root_dash_perform_completion")
|
|
|
|
checkOmit(t, output, "__root-dash_perform_completion")
|
|
|
|
|
|
|
|
// The command name should not have replaced the '-'
|
|
|
|
check(t, output, "-c root-dash")
|
|
|
|
checkOmit(t, output, "-c root_dash")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProgWithColon(t *testing.T) {
|
|
|
|
rootCmd := &Command{Use: "root:colon", Args: NoArgs, Run: emptyRun}
|
|
|
|
buf := new(bytes.Buffer)
|
2021-02-07 17:08:50 -07:00
|
|
|
assertNoErr(t, rootCmd.GenFishCompletion(buf, false))
|
2020-07-10 13:12:46 -07:00
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
// Functions name should have replace the ':'
|
|
|
|
check(t, output, "__root_colon_perform_completion")
|
|
|
|
checkOmit(t, output, "__root:colon_perform_completion")
|
|
|
|
|
|
|
|
// The command name should not have replaced the ':'
|
|
|
|
check(t, output, "-c root:colon")
|
|
|
|
checkOmit(t, output, "-c root_colon")
|
|
|
|
}
|
2022-06-15 17:08:16 -07:00
|
|
|
|
|
|
|
func TestFishCompletionNoActiveHelp(t *testing.T) {
|
|
|
|
c := &Command{Use: "c", Run: emptyRun}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
assertNoErr(t, c.GenFishCompletion(buf, true))
|
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
// check that active help is being disabled
|
|
|
|
activeHelpVar := activeHelpEnvVar(c.Name())
|
|
|
|
check(t, output, fmt.Sprintf("%s=0", activeHelpVar))
|
|
|
|
}
|
2022-06-19 19:38:49 -07:00
|
|
|
|
|
|
|
func TestGenFishCompletionFile(t *testing.T) {
|
|
|
|
err := os.Mkdir("./tmp", 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll("./tmp")
|
|
|
|
|
|
|
|
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
|
|
|
|
child := &Command{
|
|
|
|
Use: "child",
|
|
|
|
ValidArgsFunction: validArgsFunc,
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
rootCmd.AddCommand(child)
|
|
|
|
|
|
|
|
assertNoErr(t, rootCmd.GenFishCompletionFile("./tmp/test", false))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailGenFishCompletionFile(t *testing.T) {
|
|
|
|
err := os.Mkdir("./tmp", 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll("./tmp")
|
|
|
|
|
|
|
|
f, _ := os.OpenFile("./tmp/test", os.O_CREATE, 0400)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
|
|
|
|
child := &Command{
|
|
|
|
Use: "child",
|
|
|
|
ValidArgsFunction: validArgsFunc,
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
rootCmd.AddCommand(child)
|
|
|
|
|
|
|
|
got := rootCmd.GenFishCompletionFile("./tmp/test", false)
|
|
|
|
if got == nil {
|
|
|
|
t.Error("should raise permission denied error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("MSYSTEM") == "MINGW64" {
|
|
|
|
if got.Error() != "open ./tmp/test: Access is denied." {
|
|
|
|
t.Errorf("got: %s, want: %s", got.Error(), "open ./tmp/test: Access is denied.")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if got.Error() != "open ./tmp/test: permission denied" {
|
|
|
|
t.Errorf("got: %s, want: %s", got.Error(), "open ./tmp/test: permission denied")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|