Skip to content

Commit bf3955a

Browse files
committed
Add failing test for syntax that Bash 3 can't understand
This test fails with: /app/cli: line 117: conditional binary operator expected /app/cli: line 117: syntax error near `missingno' /app/cli: line 117: ` if [[ -v missingno ]]; then'
1 parent 783fc32 commit bf3955a

File tree

8 files changed

+317
-2
lines changed

8 files changed

+317
-2
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
creating user files in src
2+
skipped src/initialize.sh (exists)
3+
skipped src/test_command.sh (exists)
4+
created ./cli
5+
run ./cli --help to test your bash script
6+
cli - Sample application
7+
8+
Usage:
9+
cli [command]
10+
cli [command] --help | -h
11+
cli --version | -v
12+
13+
Commands:
14+
test Run test
15+

spec/bashly/integration/bash_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
before { system "docker pull bash:3 >/dev/null" }
66

77
it "errors gracefully" do
8-
command = "docker run --rm -v $PWD:/app bash:3 bash /app/download"
8+
command = "docker run --rm -v $PWD:/app bash:3 bash /app/cli"
99

10-
Dir.chdir "examples/minimal" do
10+
Dir.chdir "spec/fixtures/workspaces/bash-3-syntax" do
11+
system "bashly generate 2>&1 >/dev/null"
1112
expect(`#{command} 2>&1`).to match_approval('bash/error')
1213
end
1314
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This fixture tests that bash 3 exits with a helpful error even when faced with syntax it doesn't understand
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
#!/usr/bin/env bash
2+
# This script was generated by bashly (https://github.com/DannyBen/bashly)
3+
# Modifying it manually is not recommended
4+
5+
# :command.version_command
6+
version_command() {
7+
echo "$version"
8+
}
9+
10+
# :command.usage
11+
cli_usage() {
12+
if [[ -n $long_usage ]]; then
13+
printf "cli - Sample application\n"
14+
echo
15+
else
16+
printf "cli - Sample application\n"
17+
echo
18+
fi
19+
20+
printf "Usage:\n"
21+
printf " cli [command]\n"
22+
printf " cli [command] --help | -h\n"
23+
printf " cli --version | -v\n"
24+
echo
25+
# :command.usage_commands
26+
printf "Commands:\n"
27+
echo " test Run test"
28+
echo
29+
30+
if [[ -n $long_usage ]]; then
31+
printf "Options:\n"
32+
# :command.usage_fixed_flags
33+
echo " --help, -h"
34+
printf " Show this help\n"
35+
echo
36+
echo " --version, -v"
37+
printf " Show version number\n"
38+
echo
39+
40+
fi
41+
}
42+
43+
# :command.usage
44+
cli_test_usage() {
45+
if [[ -n $long_usage ]]; then
46+
printf "cli test - Run test\n"
47+
echo
48+
else
49+
printf "cli test - Run test\n"
50+
echo
51+
fi
52+
53+
printf "Usage:\n"
54+
printf " cli test\n"
55+
printf " cli test --help | -h\n"
56+
echo
57+
58+
if [[ -n $long_usage ]]; then
59+
printf "Options:\n"
60+
# :command.usage_fixed_flags
61+
echo " --help, -h"
62+
printf " Show this help\n"
63+
echo
64+
65+
fi
66+
}
67+
68+
# :command.normalize_input
69+
normalize_input() {
70+
local arg flags
71+
72+
while [[ $# -gt 0 ]]; do
73+
arg="$1"
74+
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
75+
input+=("${BASH_REMATCH[1]}")
76+
input+=("${BASH_REMATCH[2]}")
77+
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
78+
input+=("${BASH_REMATCH[1]}")
79+
input+=("${BASH_REMATCH[2]}")
80+
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
81+
flags="${BASH_REMATCH[1]}"
82+
for (( i=0 ; i < ${#flags} ; i++ )); do
83+
input+=("-${flags:i:1}")
84+
done
85+
else
86+
input+=("$arg")
87+
fi
88+
89+
shift
90+
done
91+
}
92+
# :command.inspect_args
93+
inspect_args() {
94+
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
95+
if (( ${#args[@]} )); then
96+
echo args:
97+
for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
98+
else
99+
echo args: none
100+
fi
101+
102+
if (( ${#other_args[@]} )); then
103+
echo
104+
echo other_args:
105+
echo "- \${other_args[*]} = ${other_args[*]}"
106+
for i in "${!other_args[@]}"; do
107+
echo "- \${other_args[$i]} = ${other_args[$i]}"
108+
done
109+
fi
110+
}
111+
112+
# :command.command_functions
113+
# :command.function
114+
cli_test_command() {
115+
# :src/test_command.sh
116+
# this syntax (`[[ -v`) cannot be parsed by bash 3
117+
if [[ -v missingno ]]; then
118+
echo the variable was not defined
119+
fi
120+
}
121+
122+
# :command.parse_requirements
123+
parse_requirements() {
124+
# :command.fixed_flag_filter
125+
case "$1" in
126+
--version | -v )
127+
version_command
128+
exit
129+
;;
130+
131+
--help | -h )
132+
long_usage=yes
133+
cli_usage
134+
exit
135+
;;
136+
137+
esac
138+
# :command.environment_variables_filter
139+
# :command.dependencies_filter
140+
# :command.command_filter
141+
action=$1
142+
143+
case $action in
144+
-* )
145+
;;
146+
147+
test )
148+
action="test"
149+
shift
150+
cli_test_parse_requirements "$@"
151+
shift $#
152+
;;
153+
154+
# :command.command_fallback
155+
* )
156+
cli_usage
157+
exit 1
158+
;;
159+
160+
esac
161+
# :command.required_args_filter
162+
# :command.required_flags_filter
163+
# :command.parse_requirements_while
164+
while [[ $# -gt 0 ]]; do
165+
key="$1"
166+
case "$key" in
167+
168+
-* )
169+
printf "invalid option: %s\n" "$key"
170+
exit 1
171+
;;
172+
173+
* )
174+
# :command.parse_requirements_case
175+
printf "invalid argument: %s\n" "$key"
176+
exit 1
177+
;;
178+
179+
esac
180+
done
181+
# :command.catch_all_filter
182+
# :command.default_assignments
183+
# :command.whitelist_filter
184+
}
185+
186+
# :command.parse_requirements
187+
cli_test_parse_requirements() {
188+
# :command.fixed_flag_filter
189+
case "$1" in
190+
--version | -v )
191+
version_command
192+
exit
193+
;;
194+
195+
--help | -h )
196+
long_usage=yes
197+
cli_test_usage
198+
exit
199+
;;
200+
201+
esac
202+
# :command.environment_variables_filter
203+
# :command.dependencies_filter
204+
# :command.command_filter
205+
action="test"
206+
# :command.required_args_filter
207+
# :command.required_flags_filter
208+
# :command.parse_requirements_while
209+
while [[ $# -gt 0 ]]; do
210+
key="$1"
211+
case "$key" in
212+
213+
-* )
214+
printf "invalid option: %s\n" "$key"
215+
exit 1
216+
;;
217+
218+
* )
219+
# :command.parse_requirements_case
220+
printf "invalid argument: %s\n" "$key"
221+
exit 1
222+
;;
223+
224+
esac
225+
done
226+
# :command.catch_all_filter
227+
# :command.default_assignments
228+
# :command.whitelist_filter
229+
}
230+
231+
# :command.initialize
232+
initialize() {
233+
version="0.1.0"
234+
long_usage=''
235+
set -e
236+
237+
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
238+
printf "bash version 4 or higher is required\n"
239+
exit 1
240+
fi
241+
242+
# :src/initialize.sh
243+
# Code here runs inside the initialize() function
244+
# Use it for anything that you need to run before any other function, like
245+
# setting environment vairables:
246+
# CONFIG_FILE=settings.ini
247+
#
248+
# Feel free to empty (but not delete) this file.
249+
}
250+
251+
# :command.run
252+
run() {
253+
declare -A args
254+
declare -a other_args
255+
declare -a input
256+
normalize_input "$@"
257+
parse_requirements "${input[@]}"
258+
259+
if [[ $action == "test" ]]; then
260+
if [[ ${args[--help]} ]]; then
261+
long_usage=yes
262+
cli_test_usage
263+
else
264+
cli_test_command
265+
fi
266+
267+
elif [[ $action == "root" ]]; then
268+
root_command
269+
fi
270+
}
271+
272+
initialize
273+
run "$@"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: cli
2+
help: Sample application
3+
version: 0.1.0
4+
5+
commands:
6+
- name: test
7+
help: Run test
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Code here runs inside the initialize() function
2+
# Use it for anything that you need to run before any other function, like
3+
# setting environment vairables:
4+
# CONFIG_FILE=settings.ini
5+
#
6+
# Feel free to empty (but not delete) this file.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# this syntax (`[[ -v`) cannot be parsed by bash 3
2+
if [[ -v missingno ]]; then
3+
echo the variable was not defined
4+
fi
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
# This fixture tests Bash 4+-only syntax
4+
# It is executed as part of the Runfile examples test
5+
6+
bundle exec bashly generate
7+
8+
./cli

0 commit comments

Comments
 (0)