1 # $NetBSD: t_expand.sh,v 1.8 2016/04/29 18:29:17 christos Exp $
3 # Copyright (c) 2007, 2009 The NetBSD Foundation, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
27 # the implementation of "sh" to test
28 : ${TEST_SH:="/bin/sh"}
31 # This file tests the functions in expand.c.
36 while [ $# -gt 0 ]; do
37 if [ -z "${str}" ]; then
47 atf_test_case dollar_at
49 atf_set "descr" "Somewhere between 2.0.2 and 3.0 the expansion" \
50 "of the \$@ variable had been broken. Check for" \
54 # This one should work everywhere.
55 atf_check -s exit:0 -o inline:' EOL\n' -e empty \
56 ${TEST_SH} -c 'echo "" "" | '" sed 's,\$,EOL,'"
58 # This code triggered the bug.
59 atf_check -s exit:0 -o inline:' EOL\n' -e empty \
60 ${TEST_SH} -c 'set -- "" ""; echo "$@" | '" sed 's,\$,EOL,'"
62 atf_check -s exit:0 -o inline:'0\n' -e empty ${TEST_SH} -c \
63 'set -- -; shift; n_arg() { echo $#; }; n_arg "$@"'
66 atf_test_case dollar_at_with_text
67 dollar_at_with_text_head() {
68 atf_set "descr" "Test \$@ expansion when it is surrounded by text" \
69 "within the quotes. PR bin/33956."
71 dollar_at_with_text_body() {
77 while [ $# -gt 0 ]; do
78 if [ -z "${str}" ]; then
93 while [ $# -gt 0 ]; do
95 str="${str}${str:+ }>$1<"
108 atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
109 ". ./h-f${f}; "'set -- ; delim_argv "$@"'
110 atf_check -s exit:0 -o inline:'>foobar<\n' -e empty \
112 ". ./h-f${f}; "'set -- ; delim_argv "foo$@bar"'
113 atf_check -s exit:0 -o inline:'>foo bar<\n' -e empty \
115 ". ./h-f${f}; "'set -- ; delim_argv "foo $@ bar"'
117 atf_check -s exit:0 -o inline:'>a< >b< >c<\n' -e empty \
119 ". ./h-f${f}; "'set -- a b c; delim_argv "$@"'
120 atf_check -s exit:0 -o inline:'>fooa< >b< >cbar<\n' -e empty \
122 ". ./h-f${f}; "'set -- a b c; delim_argv "foo$@bar"'
123 atf_check -s exit:0 -o inline:'>foo a< >b< >c bar<\n' -e empty \
125 ". ./h-f${f}; "'set -- a b c; delim_argv "foo $@ bar"'
131 atf_set "descr" "Checks that the %% operator works and strips" \
132 "the contents of a variable from the given point" \
136 line='#define bindir "/usr/bin" /* comment */'
137 stripped='#define bindir "/usr/bin" '
139 # atf_expect_fail "PR bin/43469" -- now fixed
143 '${line%%'"'"'/*'"'"'*}' \
146 '"${line%%'"'"'/*'"'"'*}"' \
149 '${line%'"'"'/*'"'"'*}' \
152 '"${line%'"'"'/*'"'"'*}"'
154 atf_check -o inline:":$stripped:\n" -e empty ${TEST_SH} -c \
155 "line='${line}'; echo :${exp}:"
159 atf_test_case varpattern_backslashes
160 varpattern_backslashes_head() {
161 atf_set "descr" "Tests that protecting wildcards with backslashes" \
162 "works in variable patterns."
164 varpattern_backslashes_body() {
165 line='/foo/bar/*/baz'
167 atf_check -o inline:'/foo/bar/\n' -e empty ${TEST_SH} -c \
168 'line="/foo/bar/*/baz"; echo ${line%%\**}'
171 atf_test_case arithmetic
173 atf_set "descr" "POSIX requires shell arithmetic to use signed" \
174 "long or a wider type. We use intmax_t, so at" \
175 "least 64 bits should be available. Make sure" \
180 atf_check -o inline:'3' -e empty ${TEST_SH} -c \
181 'printf %s $((1 + 2))'
182 atf_check -o inline:'2147483647' -e empty ${TEST_SH} -c \
183 'printf %s $((0x7fffffff))'
184 atf_check -o inline:'9223372036854775807' -e empty ${TEST_SH} -c \
185 'printf %s $(((1 << 63) - 1))'
188 atf_test_case iteration_on_null_parameter
189 iteration_on_null_parameter_head() {
190 atf_set "descr" "Check iteration of \$@ in for loop when set to null;" \
191 "the error \"sh: @: parameter not set\" is incorrect." \
194 iteration_on_null_parameter_body() {
195 atf_check -o empty -e empty ${TEST_SH} -c \
196 'N=; set -- ${N}; for X; do echo "[$X]"; done'
199 atf_test_case iteration_on_quoted_null_parameter
200 iteration_on_quoted_null_parameter_head() {
202 'Check iteration of "$@" in for loop when set to null;'
204 iteration_on_quoted_null_parameter_body() {
205 atf_check -o inline:'[]\n' -e empty ${TEST_SH} -c \
206 'N=; set -- "${N}"; for X; do echo "[$X]"; done'
209 atf_test_case iteration_on_null_or_null_parameter
210 iteration_on_null_or_null_parameter_head() {
212 'Check expansion of null parameter as default for another null'
214 iteration_on_null_or_null_parameter_body() {
215 atf_check -o empty -e empty ${TEST_SH} -c \
216 'N=; E=; set -- ${N:-${E}}; for X; do echo "[$X]"; done'
219 atf_test_case iteration_on_null_or_missing_parameter
220 iteration_on_null_or_missing_parameter_head() {
222 'Check expansion of missing parameter as default for another null'
224 iteration_on_null_or_missing_parameter_body() {
225 # atf_expect_fail 'PR bin/50834'
226 atf_check -o empty -e empty ${TEST_SH} -c \
227 'N=; set -- ${N:-}; for X; do echo "[$X]"; done'
243 TEMP_FILE=$( mktemp OUT.XXXXXX )
244 TEST_NUM=$(( $TEST_NUM + 1 ))
247 # our local shell (ATF_SHELL) better do quoting correctly...
248 # some of the tests expect us to expand $nl internally...
251 result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )"
254 if [ "${STATUS}" -ne "$3" ]; then
255 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
256 MSG="${MSG} expected exit code $3, got ${STATUS}"
258 # don't actually fail just because of wrong exit code
259 # unless we either expected, or received "good"
260 case "$3/${STATUS}" in
261 (*/0|0/*) fail=true;;
265 if [ "$3" -eq 0 ]; then
266 if [ -s "${TEMP_FILE}" ]; then
267 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
268 MSG="${MSG} Messages produced on stderr unexpected..."
269 MSG="${MSG}${nl}$( cat "${TEMP_FILE}" )"
273 if ! [ -s "${TEMP_FILE}" ]; then
274 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
275 MSG="${MSG} Expected messages on stderr,"
276 MSG="${MSG} nothing produced"
282 # Remove newlines (use local shell for this)
285 result="$(echo $result)"
287 if [ "$2" != "$result" ]
289 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
290 MSG="${MSG} Expected output '$2', received '$result'"
296 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
297 MSG="${MSG} Full command: <<${CMD}>>"
300 $fail && test -n "$TEST_ID" && {
301 TEST_FAILURES="${TEST_FAILURES}${TEST_FAILURES:+${nl}}"
302 TEST_FAILURES="${TEST_FAILURES}${TEST_ID}[$TEST_NUM]:"
303 TEST_FAILURES="${TEST_FAILURES} Test of '$1' failed.";
304 TEST_FAILURES="${TEST_FAILURES}${nl}${MSG}"
305 TEST_FAIL_COUNT=$(( $TEST_FAIL_COUNT + 1 ))
308 $fail && atf_fail "Test[$TEST_NUM] of '$1' failed${nl}${MSG}"
314 test -z "${TEST_ID}" && return 0
315 test -z "${TEST_FAILURES}" && return 0
317 echo >&2 "=========================================="
318 echo >&2 "While testing '${TEST_ID}'"
319 echo >&2 " - - - - - - - - - - - - - - - - -"
320 echo >&2 "${TEST_FAILURES}"
322 "Test ${TEST_ID}: $TEST_FAIL_COUNT subtests (of $TEST_NUM) failed - see stderr"
325 atf_test_case shell_params
326 shell_params_head() {
327 atf_set "descr" "Test correct operation of the numeric parameters"
329 shell_params_body() {
330 atf_require_prog mktemp
334 check 'set -- a b c; echo "$#: $1 $2 $3"' '3: a b c' 0
335 check 'set -- a b c d e f g h i j k l m; echo "$#: ${1}0 ${10} $10"' \
337 check 'x="$0"; set -- a b; y="$0";
338 [ "x${x}y" = "x${y}y" ] && echo OK || echo x="$x" y="$y"' \
340 check "${TEST_SH} -c 'echo 0=\$0 1=\$1 2=\$2' a b c" '0=a 1=b 2=c' 0
342 echo 'echo 0="$0" 1="$1" 2="$2"' > helper.sh
343 check "${TEST_SH} helper.sh a b c" '0=helper.sh 1=a 2=b' 0
345 check 'set -- a bb ccc dddd eeeee ffffff ggggggg hhhhhhhh \
346 iiiiiiiii jjjjjjjjjj kkkkkkkkkkk
347 echo "${#}: ${#1} ${#2} ${#3} ${#4} ... ${#9} ${#10} ${#11}"' \
348 '11: 1 2 3 4 ... 9 10 11' 0
350 check 'set -- a b c; echo "$#: ${1-A} ${2-B} ${3-C} ${4-D} ${5-E}"' \
352 check 'set -- a "" c "" e
353 echo "$#: ${1:-A} ${2:-B} ${3:-C} ${4:-D} ${5:-E}"' \
355 check 'set -- a "" c "" e
356 echo "$#: ${1:+A} ${2:+B} ${3:+C} ${4:+D} ${5:+E}"' \
358 check 'set -- "abab*cbb"
359 echo "${1} ${1#a} ${1%b} ${1##ab} ${1%%b} ${1#*\*} ${1%\**}"' \
360 'abab*cbb bab*cbb abab*cb ab*cbb abab*cb cbb abab' 0
361 check 'set -- "abab?cbb"
362 echo "${1}:${1#*a}+${1%b*}-${1##*a}_${1%%b*}%${1#[ab]}=${1%?*}/${1%\?*}"' \
363 'abab?cbb:bab?cbb+abab?cb-b?cbb_a%bab?cbb=abab?cb/abab' 0
364 check 'set -- a "" c "" e; echo "${2:=b}"' '' 1
369 atf_init_test_cases() {
370 atf_add_test_case dollar_at
371 atf_add_test_case dollar_at_with_text
372 atf_add_test_case strip
373 atf_add_test_case varpattern_backslashes
374 atf_add_test_case arithmetic
375 atf_add_test_case iteration_on_null_parameter
376 atf_add_test_case iteration_on_quoted_null_parameter
377 atf_add_test_case iteration_on_null_or_null_parameter
378 atf_add_test_case iteration_on_null_or_missing_parameter
379 atf_add_test_case shell_params