Skip to content

Commit 79be865

Browse files
committed
tests: Add some basic tests for the completions getter
1 parent 1ccb775 commit 79be865

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

.github/workflows/test.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Test
2+
on: [push]
3+
jobs:
4+
bash-completions-checker:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- name: Check out repository code
8+
uses: actions/checkout@v3
9+
- name: Run tests
10+
run: |
11+
bash tests/test-bash-completions-getter.sh
12+
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env bash
2+
# set -x
3+
4+
this_dir="$(dirname "$0")"
5+
src_dir="$this_dir/.."
6+
bash_completer="$src_dir/bash-completions-getter.sh"
7+
8+
exec 535>&2
9+
our_stderr=535
10+
11+
exec 999>/dev/null
12+
completion_out=999
13+
14+
if [ -n "$DEBUG" ]; then
15+
completion_out=$our_stderr
16+
17+
if [ "$DEBUG" -gt 1 ]; then
18+
set -x
19+
fi
20+
fi
21+
22+
check_completion() {
23+
local OIFS=$IFS
24+
local input="$1"
25+
local expected_completions=("$2")
26+
local expected_options=("$3")
27+
local expected_exit_code=${4:-0};
28+
local -a output
29+
30+
echo "Checking completion $input" >&2
31+
32+
# shellcheck source=./bash-completions-getter.sh disable=SC1091
33+
source "$bash_completer"
34+
mapfile -t output < <(test_bash_completion "$input" 2>&$completion_out; echo $?)
35+
echo -n " output: " >&2; printf "'%s'," "${output[@]}" >&2; echo >&2
36+
37+
if [ "${#output[@]}" -eq 0 ]; then
38+
echo "Unepxected output"
39+
exit 1
40+
fi
41+
42+
local exit_code="${output[-1]}"
43+
unset 'output[-1]';
44+
echo " exit code: $exit_code" >&2
45+
46+
if [ "$exit_code" -ne "$expected_exit_code" ]; then
47+
echo "! invalid exit code, expecting: $expected_exit_code";
48+
[ -n "$message" ] && echo "$message"
49+
exit 1
50+
fi
51+
52+
if [ "$exit_code" -ne 0 ]; then
53+
return 0
54+
fi
55+
56+
IFS=' '
57+
local options=("${output[0]}")
58+
IFS=$OIFS
59+
local completions=("${output[@]:1}")
60+
61+
echo -n " options: " >&2; printf "'%s'," "${options[@]}" >&2; echo >&2
62+
echo -n " completions: " >&2; printf "'%s'," "${completions[@]}" >&2; echo >&2
63+
64+
if [[ "${completions[*]}" != "${expected_completions[*]}" ]]; then
65+
echo -n "! invalid completions, expecting: "; printf "'%s'," "${expected_completions[@]}"; echo
66+
exit 1
67+
fi
68+
69+
if [[ "${options[*]}" != "${expected_options[*]}" ]]; then
70+
echo -n "! invalid options, expecting: "; printf "'%s'," "${expected_options[@]}"; echo
71+
exit 1
72+
fi
73+
}
74+
75+
expect_failure() {
76+
check_completion "$1" '' '' 1
77+
}
78+
79+
function test_complete_function() {
80+
if [[ "$1" != "$EXPECTED_COMPLETE_PROGRAM" ]]; then
81+
echo "Unexpected complete program: '$1'" >&$our_stderr
82+
return 1
83+
fi
84+
85+
if [[ "$2" != "$EXPECTED_COMPLETE_WORD" ]]; then
86+
echo "Unexpected complete word: '$2'" >&$our_stderr
87+
return 2
88+
fi
89+
90+
if [[ "$3" != "$EXPECTED_COMPLETE_PRE_WORD" ]]; then
91+
echo "Unexpected complete previous word: '$3'" >&$our_stderr
92+
return 3
93+
fi
94+
95+
COMPREPLY=("${TEST_COMPLETE_REPLY[@]}")
96+
}
97+
98+
expect_failure hopefully-invalid-command-completion
99+
100+
complete -W "foo" foo-with-word
101+
check_completion foo-with-word "foo"
102+
103+
complete -o nospace -W "foo" foo-with-word-and-option
104+
check_completion foo-with-word-and-option "foo" nospace
105+
106+
complete -W "foo --bar" foo-with-multiple-words
107+
check_completion foo-with-multiple-words "foo --bar"
108+
109+
complete -o nospace -W "foo" foo-with-word-and-option
110+
check_completion foo-with-word-and-option "foo" nospace
111+
112+
function foo_complete_function_simple() {
113+
EXPECTED_COMPLETE_PROGRAM=foo-with-function-simple
114+
EXPECTED_COMPLETE_WORD="''"
115+
EXPECTED_COMPLETE_PRE_WORD="'foo-with-function-simple'"
116+
TEST_COMPLETE_REPLY=(a --b --c)
117+
test_complete_function "$@"
118+
}
119+
120+
complete -F foo_complete_function_simple foo-with-function-simple
121+
check_completion 'foo-with-function-simple ' "a --b --c"
122+
123+
124+
function foo_complete_function() {
125+
EXPECTED_COMPLETE_PROGRAM=foo-with-function
126+
EXPECTED_COMPLETE_WORD="'word'"
127+
EXPECTED_COMPLETE_PRE_WORD="'pre-word'"
128+
TEST_COMPLETE_REPLY=(-c -D efgh)
129+
test_complete_function "$@"
130+
}
131+
132+
complete -F foo_complete_function foo-with-function
133+
check_completion 'foo-with-function pre-word word' "-c -D efgh"

0 commit comments

Comments
 (0)