Skip to content
Myr

test: make fish_completions_test more robust (#1980)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Use temporary files instead of assuming the current directory is writable. Also, if creating a temporary file still returns an error, prevent the test from failing silently by replacing `log.Fatal` with `t.Fatal`.

authored by Branch Vincent · committed by GitHub · parent 2246fa8 · Browse files · 1 file+13 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+13 added−22 removed

  • other:+13 added−22 removed

1 file+13 added−22 removed

modified:fish_completions_test.go+13 added-22 removed
14 unmodified lines
15package cobra1617import (18 "bytes"19 "errors"20 "fmt"20 "log"21 "os"22 "path/filepath"23 "testing"24)2526func TestCompleteNoDesCmdInFishScript(t *testing.T) {
71 unmodified lines
98 check(t, output, fmt.Sprintf("%s=0", activeHelpVar))99}100101func TestGenFishCompletionFile(t *testing.T) {101 err := os.Mkdir("./tmp", 0755)102 tmpFile, err := os.CreateTemp("", "cobra-test")103 if err != nil {103 log.Fatal(err.Error())104 t.Fatal(err.Error())105 }106106 defer os.RemoveAll("./tmp")107 defer os.Remove(tmpFile.Name())108109 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}110 child := &Command{111 Use: "child",
1 unmodified line
113 Run: emptyRun,114 }115 rootCmd.AddCommand(child)116116 assertNoErr(t, rootCmd.GenFishCompletionFile("./tmp/test", false))117 assertNoErr(t, rootCmd.GenFishCompletionFile(tmpFile.Name(), false))118}119120func TestFailGenFishCompletionFile(t *testing.T) {120 err := os.Mkdir("./tmp", 0755)121 tmpDir, err := os.MkdirTemp("", "cobra-test")122 if err != nil {122 log.Fatal(err.Error())123 t.Fatal(err.Error())124 }125125 defer os.RemoveAll("./tmp")126 defer os.RemoveAll(tmpDir)127127 f, _ := os.OpenFile("./tmp/test", os.O_CREATE, 0400)128 f, _ := os.OpenFile(filepath.Join(tmpDir, "test"), os.O_CREATE, 0400)129 defer f.Close()130131 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}132 child := &Command{
2 unmodified lines
135 Run: emptyRun,136 }137 rootCmd.AddCommand(child)138138 got := rootCmd.GenFishCompletionFile("./tmp/test", false)139 if got == nil {140 t.Error("should raise permission denied error")139 got := rootCmd.GenFishCompletionFile(f.Name(), false)140 if !errors.Is(got, os.ErrPermission) {141 t.Errorf("got: %s, want: %s", got.Error(), os.ErrPermission.Error())142 }142143 if os.Getenv("MSYSTEM") == "MINGW64" {144 if got.Error() != "open ./tmp/test: Access is denied." {145 t.Errorf("got: %s, want: %s", got.Error(), "open ./tmp/test: Access is denied.")146 }147 } else {148 if got.Error() != "open ./tmp/test: permission denied" {149 t.Errorf("got: %s, want: %s", got.Error(), "open ./tmp/test: permission denied")150 }151 }143}