Skip to content

Commit

Permalink
tmpfile handling changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ayax79 committed Dec 2, 2024
1 parent 470ca0a commit 93a4133
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions nushell_completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package cobra

import (
"bytes"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"testing"
)

Expand All @@ -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{
Expand All @@ -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}
Expand All @@ -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())
}
}

Expand Down

0 comments on commit 93a4133

Please sign in to comment.