Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add special-characters tests using description
Browse files Browse the repository at this point in the history
Also, avoid changing COMP_TYPE.

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
marckhouzam committed Jan 18, 2025
1 parent 065c404 commit 473bdc0
Showing 2 changed files with 106 additions and 36 deletions.
23 changes: 19 additions & 4 deletions testprog/testprog.go
Original file line number Diff line number Diff line change
@@ -10,9 +10,20 @@ import (

var (
completions = []string{"bear\tan animal", "bearpaw\ta dessert", "dog", "unicorn\tmythical"}
completionsWithSpecialCharacters = []string{`bash1 space`, `bash2\escape`, `bash3\ escaped\ space`, `bash4>redirect`, `bash5#comment`, `bash6$var`, `bash7|pipe`, `bash8;semicolon`, `bash9=equals`, `bashA:colon`}
specialCharComps = []string{"at@", "equal=", "slash/", "colon:", "period.", "comma,", "letter"}
specialFlag string
completionsWithSpecialCharacters = []string{
"bash1 space\twith space",
`bash2\escape` + "\twith escape",
`bash3\ escaped\ space` + "\twith escape and space",
"bash4>redirect\twith redirect",
"bash5#comment\twith comment",
"bash6$var\twith var",
"bash7|pipe\twith pipe",
"bash8;semicolon\twith semicolon",
"bash9=equals\twith equal",
"bashA:colon\twith colon",
}
specialCharComps = []string{"at@", "equal=", "slash/", "colon:", "period.", "comma,", "letter"}
specialFlag string
)

func getCompsFilteredByPrefix(prefix string) []string {
@@ -53,13 +64,17 @@ var specialCharsCmdPrefix = &cobra.Command{
Use: "special-chars",
Short: "Directive: special chars",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

var finalComps []string
for _, comp := range completionsWithSpecialCharacters {
if strings.HasPrefix(comp, toComplete) {
finalComps = append(finalComps, comp)
}
}
return finalComps, cobra.ShellCompDirectiveDefault
return finalComps, cobra.ShellCompDirectiveNoFileComp
},
Run: func(cmd *cobra.Command, args []string) {
if specialFlag != "" {
119 changes: 87 additions & 32 deletions tests/bash/comp-tests.bash
Original file line number Diff line number Diff line change
@@ -59,7 +59,6 @@ source /dev/stdin <<- EOF
EOF

cd testingdir
COMP_TYPE=63

# Basic first level commands (static completion)
if [ "$BASHCOMP_VERSION" = bash2 ]; then
@@ -159,36 +158,46 @@ _completionTests_verifyCompletion "testprog --customComp=f" "firstComp forthComp
# Special characters
#################################################
if [ "$BASHCOMP_VERSION" = bash2 ]; then
BASH_COMP_NO_SORT=1
_completionTests_verifyCompletion "testprog prefix special-chars bash" "bash1 space bash2\\escape bash3\\ escaped\\ space bash4>redirect bash5#comment bash6\$var bash7|pipe bash8;semicolon bash9=equals bashA:colon"

COMP_TYPE=9
_completionTests_verifyCompletion "testprog prefix special-chars bash1" "bash1\ space"
_completionTests_verifyCompletion "testprog prefix special-chars bash2\\e" ""
_completionTests_verifyCompletion "testprog prefix special-chars bash2\\\\e" "bash2\\\\escape"
# TODO: completionTests_verifyCompletion doesn't support testing something
# like this.
#_completionTests_verifyCompletion "testprog prefix special-chars bash3\\ e" "bash\\ escaped\\ space"
_completionTests_verifyCompletion "testprog prefix special-chars bash4" "bash4\\>redirect"
_completionTests_verifyCompletion "testprog prefix special-chars bash4>" ""
_completionTests_verifyCompletion "testprog prefix special-chars bash4\\>" "bash4\\>redirect"
_completionTests_verifyCompletion "testprog prefix special-chars bash5#c" "bash5\\#comment"
_completionTests_verifyCompletion "testprog prefix special-chars bash5\\#c" "bash5\\#comment"
_completionTests_verifyCompletion "testprog prefix special-chars bash6\$v" "bash6\\\$var"
_completionTests_verifyCompletion "testprog prefix special-chars bash6\\\$v" "bash6\\\$var"
_completionTests_verifyCompletion "testprog prefix special-chars bash7|p" ""
_completionTests_verifyCompletion "testprog prefix special-chars bash7\\|p" "bash7\\|pipe"
# In practice, _init_completion would short circuit cobra completion so
# custom completion would never see an invocation like this.
#_completionTests_verifyCompletion "testprog prefix special-chars bash8;s" ""
_completionTests_verifyCompletion "testprog prefix special-chars bash8\\;s" "bash8\\;semicolon"
_completionTests_verifyCompletion "testprog prefix special-chars bash9=e" "equals"
_completionTests_verifyCompletion "testprog prefix special-chars bashA:c" "colon"
COMP_TYPE=63

unset BASH_COMP_NO_SORT
fi
_completionTests_verifyCompletion 'testprog prefix special-chars bash' 'bash1\ space bash2\\escape bash3\\\ escaped\\\ space bash4\>redirect bash5#comment bash6\$var bash7\|pipe bash8\;semicolon bash9=equals bashA:colon' nofile

_completionTests_verifyCompletion 'testprog prefix special-chars bash1' 'bash1\ space' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash1 ' '' nofile

_completionTests_verifyCompletion 'testprog prefix special-chars bash2' 'bash2\\escape' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash2\\e' 'bash2\\escape' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash2e' '' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash2\e' '' nofile

_completionTests_verifyCompletion 'testprog prefix special-chars bash3' 'bash3\\\ escaped\\\ space' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash3\\' 'bash3\\\ escaped\\\ space' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash3\ ' '' nofile

_completionTests_verifyCompletion 'testprog prefix special-chars bash4' 'bash4\>redirect' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash4\>' 'bash4\>redirect' nofile
# Surprisingly, bash still calls the completion function with an unescaped redirect, but it does not
# pass the directive appropriately. This looks like a bug in bash. Either way, we want our
# script to return no completion so as to let bash do file completion.
_completionTests_verifyCompletion 'testprog prefix special-chars bash4>' ''

_completionTests_verifyCompletion 'testprog prefix special-chars bash5#c' 'bash5#comment' nofile

_completionTests_verifyCompletion 'testprog prefix special-chars bash6\$v' 'bash6\$var' nofile
# Bash still calls the completion function with an unescaped variable
_completionTests_verifyCompletion 'testprog prefix special-chars bash6$v' '' nofile

_completionTests_verifyCompletion 'testprog prefix special-chars bash7\|p' 'bash7\|pipe' nofile
# In practice, bash justifiably does not call our completion script in the below case
# because after the pipe (|), it expects another command. So, we don't need to test this.
# _completionTests_verifyCompletion 'testprog prefix special-chars bash7|p' ''

_completionTests_verifyCompletion 'testprog prefix special-chars bash8\;s' 'bash8\;semicolon' nofile
# In practice, bash justifiably does not call our completion script in the below case
# because after the semicolon (;), it expects another command. So, we don't need to test this.
# _completionTests_verifyCompletion 'testprog prefix special-chars bash8;s' '''

_completionTests_verifyCompletion 'testprog prefix special-chars bash9=e' 'equals' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bashA:c' 'colon' nofile
fi

#################################################
# Special cases
@@ -246,7 +255,7 @@ _completionTests_verifyCompletion "testprog prefix nofile b" "bear bearpaw" nofi
# Measure speed of execution with insert-completions without descriptions (for both v1 and v2)
_completionTests_timing "testprog manycomps " 0.2 "insert-completions no descs"

COMP_TYPE=63
unset COMP_TYPE

# Test descriptions of bash v2
if [ "$BASHCOMP_VERSION" = bash2 ]; then
@@ -292,6 +301,52 @@ EOF
# Measure speed of execution with descriptions
_completionTests_timing "testprog manycomps " 0.5 "with descriptions"

############################
# Special character handling
############################
_completionTests_verifyCompletion 'testprog prefix special-chars bash' 'bash1\ space (with space) bash2\\escape (with escape) bash3\\\ escaped\\\ space (with escape and space) bash4\>redirect (with redirect) bash5#comment (with comment) bash6\$var (with var) bash7\|pipe (with pipe) bash8\;semicolon (with semicolon) bash9=equals (with equal) bashA:colon (with colon)' nofile

_completionTests_verifyCompletion 'testprog prefix special-chars bash1' 'bash1\ space' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash1 ' '' nofile

_completionTests_verifyCompletion 'testprog prefix special-chars bash2' 'bash2\\escape' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash2\\e' 'bash2\\escape' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash2e' '' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash2\e' '' nofile

_completionTests_verifyCompletion 'testprog prefix special-chars bash3' 'bash3\\\ escaped\\\ space' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash3\\' 'bash3\\\ escaped\\\ space' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash3\ ' '' nofile

_completionTests_verifyCompletion 'testprog prefix special-chars bash4' 'bash4\>redirect' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bash4\>' 'bash4\>redirect' nofile
# Surprisingly, bash still calls the completion function with an unescaped redirect, but it does not
# pass the directive appropriately. This looks like a bug in bash. Either way, we want our
# script to return no completion so as to let bash do file completion.
_completionTests_verifyCompletion 'testprog prefix special-chars bash4>' ''

_completionTests_verifyCompletion 'testprog prefix special-chars bash5#c' 'bash5#comment' nofile

_completionTests_verifyCompletion 'testprog prefix special-chars bash6\$v' 'bash6\$var' nofile
# Bash still calls the completion function with an unescaped variable
_completionTests_verifyCompletion 'testprog prefix special-chars bash6$v' '' nofile

_completionTests_verifyCompletion 'testprog prefix special-chars bash7\|p' 'bash7\|pipe' nofile
# In practice, bash justifiably does not call our completion script in the below case
# because after the pipe (|), it expects another command. So, we don't need to test this.
# _completionTests_verifyCompletion 'testprog prefix special-chars bash7|p' ''

_completionTests_verifyCompletion 'testprog prefix special-chars bash8\;s' 'bash8\;semicolon' nofile
# In practice, bash justifiably does not call our completion script in the below case
# because after the semicolon (;), it expects another command. So, we don't need to test this.
# _completionTests_verifyCompletion 'testprog prefix special-chars bash8;s' '''

_completionTests_verifyCompletion 'testprog prefix special-chars bash9=e' 'equals' nofile
_completionTests_verifyCompletion 'testprog prefix special-chars bashA:c' 'colon' nofile
##################################
# end of pecial character handling
##################################

# Test descriptions are properly removed when using other bash completion types
# The types are: menu-complete/menu-complete-backward (COMP_TYPE == 37)
# and insert-completions (COMP_TYPE == 42)
@@ -309,7 +364,7 @@ EOF
# Measure speed of execution with insert-completions with descriptions
_completionTests_timing "testprog manycomps " 0.2 "insert-completions no descs"

COMP_TYPE=63
unset COMP_TYPE
fi

# This must be the last call. It allows to exit with an exit code

0 comments on commit 473bdc0

Please sign in to comment.