From 2246fa82e91dda4486b9466fab691b41b516f747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Taavi=20V=C3=A4=C3=A4n=C3=A4nen?= Date: Tue, 13 Jun 2023 18:12:49 +0300 Subject: [PATCH 1/3] Fix grammar: 'allows to' (#1978) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The use in generated bash completion files is getting flagged by Lintian (the Debian package linting tool). Signed-off-by: Taavi Väänänen --- bash_completions.go | 2 +- bash_completionsV2.go | 2 +- cobra.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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 From 988bd76139210bc1d686c7112b4eb5e784365f1f Mon Sep 17 00:00:00 2001 From: Branch Vincent Date: Fri, 16 Jun 2023 07:25:30 -0700 Subject: [PATCH 2/3] test: make fish_completions_test more robust (#1980) 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`. --- fish_completions_test.go | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) 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()) } } From fdee73b4a0d0be3e233b968ab038866b7cd99772 Mon Sep 17 00:00:00 2001 From: Paul Holzinger <45212748+Luap99@users.noreply.github.com> Date: Mon, 19 Jun 2023 18:16:18 +0200 Subject: [PATCH 3/3] powershell: escape variable with curly brackets (#1960) This fixes an issue with program names that include a dot, in our case `podman.exe`. This was caused by the change in commit 6ba7ebbc. Fixes #1853 Signed-off-by: Paul Holzinger --- powershell_completions.go | 6 +++--- powershell_completions_test.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/powershell_completions.go b/powershell_completions.go index 177d2755..55195193 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 @@ -279,7 +279,7 @@ filter __%[1]s_escapeStringWithSpecialChars { } } -Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock $__%[2]sCompleterBlock +Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock ${__%[2]sCompleterBlock} `, name, nameForVar, compCmd, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder, activeHelpEnvVar(name))) 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)) }