mirror of
https://github.com/spf13/cobra
synced 2024-12-29 15:57:10 +00:00
tmpfile handling changes
This commit is contained in:
parent
470ca0a628
commit
93a41331ec
1 changed files with 12 additions and 20 deletions
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue