diff --git a/bash_completions.go b/bash_completions.go index 10c78847..8a531518 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -85,7 +85,7 @@ __%[1]s_handle_go_custom_completion() local out requestComp lastParam lastChar comp directive args # Prepare the command to request completions for the program. - # Calling ${words[0]} instead of directly %[1]s allows to handle aliases + # Calling ${words[0]} instead of directly %[1]s allows handling aliases args=("${words[@]:1}") # Disable ActiveHelp which is not supported for bash completion v1 requestComp="%[8]s=0 ${words[0]} %[2]s ${args[*]}" diff --git a/bash_completionsV2.go b/bash_completionsV2.go index 19b09560..1cce5c32 100644 --- a/bash_completionsV2.go +++ b/bash_completionsV2.go @@ -57,7 +57,7 @@ __%[1]s_get_completion_results() { local requestComp lastParam lastChar args # Prepare the command to request completions for the program. - # Calling ${words[0]} instead of directly %[1]s allows to handle aliases + # Calling ${words[0]} instead of directly %[1]s allows handling aliases args=("${words[@]:1}") requestComp="${words[0]} %[2]s ${args[*]}" diff --git a/cobra.go b/cobra.go index b07b44a0..f23f5092 100644 --- a/cobra.go +++ b/cobra.go @@ -48,7 +48,7 @@ const ( defaultCaseInsensitive = false ) -// EnablePrefixMatching allows to set automatic prefix matching. Automatic prefix matching can be a dangerous thing +// EnablePrefixMatching allows setting automatic prefix matching. Automatic prefix matching can be a dangerous thing // to automatically enable in CLI tools. // Set this to true to enable it. var EnablePrefixMatching = defaultPrefixMatching diff --git a/fish_completions_test.go b/fish_completions_test.go index 10d97d85..ce2a531d 100644 --- a/fish_completions_test.go +++ b/fish_completions_test.go @@ -16,9 +16,10 @@ package cobra import ( "bytes" + "errors" "fmt" - "log" "os" + "path/filepath" "testing" ) @@ -98,12 +99,12 @@ func TestFishCompletionNoActiveHelp(t *testing.T) { } func TestGenFishCompletionFile(t *testing.T) { - err := os.Mkdir("./tmp", 0755) + tmpFile, err := os.CreateTemp("", "cobra-test") if err != nil { - log.Fatal(err.Error()) + t.Fatal(err.Error()) } - defer os.RemoveAll("./tmp") + defer os.Remove(tmpFile.Name()) rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} child := &Command{ @@ -113,18 +114,18 @@ func TestGenFishCompletionFile(t *testing.T) { } rootCmd.AddCommand(child) - assertNoErr(t, rootCmd.GenFishCompletionFile("./tmp/test", false)) + assertNoErr(t, rootCmd.GenFishCompletionFile(tmpFile.Name(), false)) } func TestFailGenFishCompletionFile(t *testing.T) { - err := os.Mkdir("./tmp", 0755) + 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, 0400) + f, _ := os.OpenFile(filepath.Join(tmpDir, "test"), os.O_CREATE, 0400) defer f.Close() rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} @@ -135,18 +136,8 @@ func TestFailGenFishCompletionFile(t *testing.T) { } rootCmd.AddCommand(child) - got := rootCmd.GenFishCompletionFile("./tmp/test", false) - 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()) } } diff --git a/powershell_completions.go b/powershell_completions.go index 8585d845..6929dbaa 100644 --- a/powershell_completions.go +++ b/powershell_completions.go @@ -47,7 +47,7 @@ filter __%[1]s_escapeStringWithSpecialChars { `+" $_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|\"|`|\\||<|>|&','`$&'"+` } -[scriptblock]$__%[2]sCompleterBlock = { +[scriptblock]${__%[2]sCompleterBlock} = { param( $WordToComplete, $CommandAst, @@ -122,7 +122,7 @@ filter __%[1]s_escapeStringWithSpecialChars { __%[1]s_debug "Calling $RequestComp" # First disable ActiveHelp which is not supported for Powershell - $env:%[10]s=0 + ${env:%[10]s}=0 #call the command store the output in $out and redirect stderr and stdout to null # $Out is an array contains each line per element diff --git a/powershell_completions_test.go b/powershell_completions_test.go index b4092134..603b50c9 100644 --- a/powershell_completions_test.go +++ b/powershell_completions_test.go @@ -29,5 +29,5 @@ func TestPwshCompletionNoActiveHelp(t *testing.T) { // check that active help is being disabled activeHelpVar := activeHelpEnvVar(c.Name()) - check(t, output, fmt.Sprintf("%s=0", activeHelpVar)) + check(t, output, fmt.Sprintf("${env:%s}=0", activeHelpVar)) }