Skip to content

Commit

Permalink
test(bash): Add a bunch of tests for bash special characters.
Browse files Browse the repository at this point in the history
Signed-off-by: Jeffrey Faer <[email protected]>
  • Loading branch information
JeffFaer committed Oct 30, 2024
1 parent 2d3e3dc commit dd845de
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
21 changes: 19 additions & 2 deletions testprog/testprog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

var (
completions = []string{"bear\tan animal", "bearpaw\ta dessert", "dog", "unicorn\tmythical"}
specialCharComps = []string{"at@", "equal=", "slash/", "colon:", "period.", "comma,", "letter"}
completions = []string{"bear\tan animal", "bearpaw\ta dessert", "dog", "unicorn\tmythical"}
completionsWithSpecialCharacters = []string{"bash space", "bash\\escape", "bash\\ escaped\\ space", "bash>redirect", "bash#comment", "bash$var", "bash|pipe", "bash;semicolon"}
specialCharComps = []string{"at@", "equal=", "slash/", "colon:", "period.", "comma,", "letter"}
)

func getCompsFilteredByPrefix(prefix string) []string {
Expand Down Expand Up @@ -47,6 +48,21 @@ var defaultCmdPrefix = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {},
}

var specialCharsCmdPrefix = &cobra.Command{
Use: "special-chars",
Short: "Directive: special chars",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var finalComps []string
for _, comp := range completionsWithSpecialCharacters {
if strings.HasPrefix(comp, toComplete) {
finalComps = append(finalComps, comp)
}
}
return finalComps, cobra.ShellCompDirectiveDefault
},
Run: func(cmd *cobra.Command, args []string) {},
}

var noSpaceCmdPrefix = &cobra.Command{
Use: "nospace",
Short: "Directive: no space",
Expand Down Expand Up @@ -244,6 +260,7 @@ func main() {
noFileCmdPrefix,
noFileNoSpaceCmdPrefix,
defaultCmdPrefix,
specialCharsCmdPrefix,
)

noPrefixCmd.AddCommand(
Expand Down
22 changes: 11 additions & 11 deletions tests/bash/comp-test-lib.bash
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!bash

# COLOR codes
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
RED="$(echo -e '\033[0;31m')"
GREEN="$(echo -e '\033[0;32m')"
NC="$(echo -e '\033[0m')"
_compTests_nofile=/tmp/comptests.bash.nofile
_compTests_nospace=/tmp/comptests.bash.nospace
# Global variable to keep track of if a test has failed.
Expand Down Expand Up @@ -55,8 +55,8 @@ shopt -s expand_aliases
_completionTests_verifyCompletion() {
_completionTests_reset

local cmdLine=$1
local expected=$2
local cmdLine="$1"
local expected="$2"
local currentFailure=0

local nofile=0
Expand Down Expand Up @@ -114,12 +114,12 @@ _completionTests_verifyCompletion() {
if [ "${#result}" -gt 50 ]; then
resultOut="${result:0:50} <truncated>"
fi
echo -e "${GREEN}SUCCESS: \"$cmdLine\" completes to \"$resultOut\"$NC"
echo "${GREEN}SUCCESS: \"$cmdLine\" completes to \"$resultOut\"$NC"
return 0
fi

_completionTests_TEST_FAILED=1
echo -e "${RED}ERROR: \"$cmdLine\" should complete to \"$expected\" but we got \"$result\"$NC"
echo "${RED}ERROR: \"$cmdLine\" should complete to \"$expected\" but we got \"$result\"$NC"
return 1
}

Expand All @@ -143,10 +143,10 @@ _completionTests_timing() {
timing=$({ time { _completionTests_complete "$1" > /dev/null; } } 2>&1)
if (( $(echo "$timing > ${2}" | bc -l) )); then
_completionTests_TEST_FAILED=1
echo -e "${RED}<= TIMING => ${3}: 1000 completions took ${timing} seconds > ${2-0.1} seconds limit$NC"
echo "${RED}<= TIMING => ${3}: 1000 completions took ${timing} seconds > ${2-0.1} seconds limit$NC"
return 1
else
echo -e "${GREEN}<= TIMING => ${3}: 1000 completions took ${timing} seconds < ${2-0.1} seconds limit$NC"
echo "${GREEN}<= TIMING => ${3}: 1000 completions took ${timing} seconds < ${2-0.1} seconds limit$NC"
return 0
fi
}
Expand Down Expand Up @@ -222,11 +222,11 @@ _completionTests_checkDirective() {
[ -f $_compTests_nospace ] && realnospace=1

if [ $requestnofile -ne $realnofile ]; then
echo -e "${RED}ERROR: \"$cmdLine\" expected nofile=$requestnofile but got nofile=$realnofile$NC"
echo "${RED}ERROR: \"$cmdLine\" expected nofile=$requestnofile but got nofile=$realnofile$NC"
return 1
fi
if [ $requestnospace -ne $realnospace ]; then
echo -e "${RED}ERROR: \"$cmdLine\" expected nospace=$requestnospace but got nospace=$realnospace$NC"
echo "${RED}ERROR: \"$cmdLine\" expected nospace=$requestnospace but got nospace=$realnospace$NC"
return 1
fi

Expand Down
30 changes: 25 additions & 5 deletions tests/bash/comp-tests.bash
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ verifyDebug() {
_completionTests_verifyCompletion "testprog help comp" "completion" nofile
if ! test -s $debugfile; then
# File should not be empty
echo -e "${RED}ERROR: No debug logs were printed to ${debugfile}${NC}"
echo "${RED}ERROR: No debug logs were printed to ${debugfile}${NC}"
_completionTests_TEST_FAILED=1
else
echo -e "${GREEN}SUCCESS: Debug logs were printed to ${debugfile}${NC}"
echo "${GREEN}SUCCESS: Debug logs were printed to ${debugfile}${NC}"
fi
unset BASH_COMP_DEBUG_FILE
}
Expand All @@ -27,11 +27,11 @@ verifyRedirect() {
_completionTests_verifyCompletion "testprog completion bash > notexist" ""
if test -f notexist; then
# File should not exist
echo -e "${RED}ERROR: completion mistakenly created the file 'notexist'${NC}"
echo "${RED}ERROR: completion mistakenly created the file 'notexist'${NC}"
_completionTests_TEST_FAILED=1
rm -f notexist
else
echo -e "${GREEN}SUCCESS: No extra file created, as expected${NC}"
echo "${GREEN}SUCCESS: No extra file created, as expected${NC}"
fi
}

Expand Down Expand Up @@ -154,6 +154,26 @@ _completionTests_verifyCompletion "testprog --customComp f" "firstComp forthComp
_completionTests_verifyCompletion "testprog --customComp=" "firstComp secondComp forthComp" nofile
_completionTests_verifyCompletion "testprog --customComp=f" "firstComp forthComp" nofile

#################################################
# Special characters
#################################################
if [ "$BASHCOMP_VERSION" = bash2 ]; then
(
BASH_COMP_NO_SORT=1
_completionTests_verifyCompletion "testprog prefix special-chars bash" "bash space bash\\escape bash\\ escaped\\ space bash>redirect bash#comment bash\$var bash|pipe bash;semicolon"
_completionTests_verifyCompletion "testprog prefix special-chars bash\\e" "bash\\escape"
# TODO: completionTests_verifyCompletion doesn't support testing something
# like this.
#_completionTests_verifyCompletion "testprog prefix special-chars bash\\ e" "bash\\ escaped\\ space"
_completionTests_verifyCompletion "testprog prefix special-chars bash>r" "bash>redirect"
_completionTests_verifyCompletion "testprog prefix special-chars bash#c" "bash#comment"
_completionTests_verifyCompletion "testprog prefix special-chars bash\$v" 'bash$var'
_completionTests_verifyCompletion "testprog prefix special-chars bash|p" "bash|pipe"
_completionTests_verifyCompletion "testprog prefix special-chars bash;s" "bash;semicolon"
)
fi


#################################################
# Special cases
#################################################
Expand Down Expand Up @@ -278,4 +298,4 @@ fi

# This must be the last call. It allows to exit with an exit code
# that reflects the final status of all the tests.
_completionTests_exit
_completionTests_exit

0 comments on commit dd845de

Please sign in to comment.