diff --git a/nushell_completions_test.go b/nushell_completions_test.go index 3958a13..d86e8ed 100644 --- a/nushell_completions_test.go +++ b/nushell_completions_test.go @@ -16,9 +16,11 @@ package cobra import ( "bytes" + "errors" "fmt" "log" "os" + "path/filepath" "testing" ) @@ -40,12 +42,12 @@ func TestGenNushellCompletion(t *testing.T) { } func TestGenNushellCompletionFile(t *testing.T) { - err := os.Mkdir("./tmp", 0o755) + tmpFile, err := os.CreateTemp("", "cobra-test") if err != nil { log.Fatal(err.Error()) } - defer os.RemoveAll("./tmp") + defer os.RemoveAll(tmpFile.Name()) rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} child := &Command{ @@ -55,18 +57,18 @@ func TestGenNushellCompletionFile(t *testing.T) { } rootCmd.AddCommand(child) - assertNoErr(t, rootCmd.GenNushellCompletionFile("./tmp/test", true)) + assertNoErr(t, rootCmd.GenNushellCompletionFile(tmpFile.Name(), true)) } func TestFailGenNushellCompletionFile(t *testing.T) { - err := os.Mkdir("./tmp", 0o755) + tmpDir, err := os.MkdirTemp("", "cobra-test") if err != nil { - log.Fatal(err.Error()) + t.Fatal(err.Error()) } - defer os.RemoveAll("./tmp") + defer os.RemoveAll(tmpDir) - f, _ := os.OpenFile("./tmp/test", os.O_CREATE, 0o400) + f, _ := os.OpenFile(filepath.Join(tmpDir, "test"), os.O_CREATE, 0400) defer f.Close() rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} @@ -77,19 +79,9 @@ func TestFailGenNushellCompletionFile(t *testing.T) { } rootCmd.AddCommand(child) - got := rootCmd.GenNushellCompletionFile("./tmp/test", true) - 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") - } + got := rootCmd.GenFishCompletionFile(f.Name(), false) + if !errors.Is(got, os.ErrPermission) { + t.Errorf("got: %s, want: %s", got.Error(), os.ErrPermission.Error()) } }