Add unit test for fish completion (#1515)
This commit is contained in:
		@ -3,6 +3,8 @@ package cobra
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@ -80,3 +82,57 @@ func TestFishCompletionNoActiveHelp(t *testing.T) {
 | 
			
		||||
	activeHelpVar := activeHelpEnvVar(c.Name())
 | 
			
		||||
	check(t, output, fmt.Sprintf("%s=0", activeHelpVar))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user