Merge branch 'main' into patch-1

This commit is contained in:
Mavaddat Javid 2023-06-19 16:46:04 -04:00 committed by GitHub
commit d17f539906
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 28 deletions

View file

@ -85,7 +85,7 @@ __%[1]s_handle_go_custom_completion()
local out requestComp lastParam lastChar comp directive args local out requestComp lastParam lastChar comp directive args
# Prepare the command to request completions for the program. # 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}") args=("${words[@]:1}")
# Disable ActiveHelp which is not supported for bash completion v1 # Disable ActiveHelp which is not supported for bash completion v1
requestComp="%[8]s=0 ${words[0]} %[2]s ${args[*]}" requestComp="%[8]s=0 ${words[0]} %[2]s ${args[*]}"

View file

@ -57,7 +57,7 @@ __%[1]s_get_completion_results() {
local requestComp lastParam lastChar args local requestComp lastParam lastChar args
# Prepare the command to request completions for the program. # 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}") args=("${words[@]:1}")
requestComp="${words[0]} %[2]s ${args[*]}" requestComp="${words[0]} %[2]s ${args[*]}"

View file

@ -48,7 +48,7 @@ const (
defaultCaseInsensitive = false 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. // to automatically enable in CLI tools.
// Set this to true to enable it. // Set this to true to enable it.
var EnablePrefixMatching = defaultPrefixMatching var EnablePrefixMatching = defaultPrefixMatching

View file

@ -16,9 +16,10 @@ package cobra
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"log"
"os" "os"
"path/filepath"
"testing" "testing"
) )
@ -98,12 +99,12 @@ func TestFishCompletionNoActiveHelp(t *testing.T) {
} }
func TestGenFishCompletionFile(t *testing.T) { func TestGenFishCompletionFile(t *testing.T) {
err := os.Mkdir("./tmp", 0755) tmpFile, err := os.CreateTemp("", "cobra-test")
if err != nil { 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} rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
child := &Command{ child := &Command{
@ -113,18 +114,18 @@ func TestGenFishCompletionFile(t *testing.T) {
} }
rootCmd.AddCommand(child) rootCmd.AddCommand(child)
assertNoErr(t, rootCmd.GenFishCompletionFile("./tmp/test", false)) assertNoErr(t, rootCmd.GenFishCompletionFile(tmpFile.Name(), false))
} }
func TestFailGenFishCompletionFile(t *testing.T) { func TestFailGenFishCompletionFile(t *testing.T) {
err := os.Mkdir("./tmp", 0755) tmpDir, err := os.MkdirTemp("", "cobra-test")
if err != nil { 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() defer f.Close()
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
@ -135,18 +136,8 @@ func TestFailGenFishCompletionFile(t *testing.T) {
} }
rootCmd.AddCommand(child) rootCmd.AddCommand(child)
got := rootCmd.GenFishCompletionFile("./tmp/test", false) got := rootCmd.GenFishCompletionFile(f.Name(), false)
if got == nil { if !errors.Is(got, os.ErrPermission) {
t.Error("should raise permission denied error") t.Errorf("got: %s, want: %s", got.Error(), os.ErrPermission.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")
}
} }
} }

View file

@ -47,7 +47,7 @@ filter __%[1]s_escapeStringWithSpecialChars {
`+" $_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|\"|`|\\||<|>|&','`$&'"+` `+" $_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|\"|`|\\||<|>|&','`$&'"+`
} }
[scriptblock]$__%[2]sCompleterBlock = { [scriptblock]${__%[2]sCompleterBlock} = {
param( param(
$WordToComplete, $WordToComplete,
$CommandAst, $CommandAst,
@ -122,7 +122,7 @@ filter __%[1]s_escapeStringWithSpecialChars {
__%[1]s_debug "Calling $RequestComp" __%[1]s_debug "Calling $RequestComp"
# First disable ActiveHelp which is not supported for Powershell # 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 #call the command store the output in $out and redirect stderr and stdout to null
# $Out is an array contains each line per element # $Out is an array contains each line per element

View file

@ -29,5 +29,5 @@ func TestPwshCompletionNoActiveHelp(t *testing.T) {
// check that active help is being disabled // check that active help is being disabled
activeHelpVar := activeHelpEnvVar(c.Name()) activeHelpVar := activeHelpEnvVar(c.Name())
check(t, output, fmt.Sprintf("%s=0", activeHelpVar)) check(t, output, fmt.Sprintf("${env:%s}=0", activeHelpVar))
} }