Skip to content

Commit

Permalink
Add unit test for fish completions.
Browse files Browse the repository at this point in the history
  • Loading branch information
t29kida authored and hoshsadiq committed Feb 8, 2022
1 parent 73e1b0f commit 5c10ffa
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions fish_completions_test.go
Expand Up @@ -2,6 +2,8 @@ package cobra

import (
"bytes"
"log"
"os"
"testing"
)

Expand Down Expand Up @@ -67,3 +69,51 @@ func TestProgWithColon(t *testing.T) {
check(t, output, "-c root:colon")
checkOmit(t, output, "-c root_colon")
}

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 got.Error() != "open ./tmp/test: permission denied" {
t.Errorf("got: %s, want: %s", got.Error(), "open ./tmp/test: permission denied")
}
}

0 comments on commit 5c10ffa

Please sign in to comment.