t_expand.sh revision 1.18 1 1.18 kre # $NetBSD: t_expand.sh,v 1.18 2017/10/06 17:05:05 kre Exp $
2 1.1 jruoho #
3 1.1 jruoho # Copyright (c) 2007, 2009 The NetBSD Foundation, Inc.
4 1.1 jruoho # All rights reserved.
5 1.1 jruoho #
6 1.1 jruoho # Redistribution and use in source and binary forms, with or without
7 1.1 jruoho # modification, are permitted provided that the following conditions
8 1.1 jruoho # are met:
9 1.1 jruoho # 1. Redistributions of source code must retain the above copyright
10 1.1 jruoho # notice, this list of conditions and the following disclaimer.
11 1.1 jruoho # 2. Redistributions in binary form must reproduce the above copyright
12 1.1 jruoho # notice, this list of conditions and the following disclaimer in the
13 1.1 jruoho # documentation and/or other materials provided with the distribution.
14 1.1 jruoho #
15 1.1 jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1 jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1 jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1 jruoho # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1 jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1 jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1 jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1 jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1 jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1 jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 jruoho # POSSIBILITY OF SUCH DAMAGE.
26 1.1 jruoho #
27 1.6 christos # the implementation of "sh" to test
28 1.6 christos : ${TEST_SH:="/bin/sh"}
29 1.1 jruoho
30 1.1 jruoho #
31 1.1 jruoho # This file tests the functions in expand.c.
32 1.1 jruoho #
33 1.1 jruoho
34 1.1 jruoho delim_argv() {
35 1.1 jruoho str=
36 1.1 jruoho while [ $# -gt 0 ]; do
37 1.1 jruoho if [ -z "${str}" ]; then
38 1.1 jruoho str=">$1<"
39 1.1 jruoho else
40 1.1 jruoho str="${str} >$1<"
41 1.1 jruoho fi
42 1.2 ast shift
43 1.1 jruoho done
44 1.1 jruoho echo ${str}
45 1.1 jruoho }
46 1.1 jruoho
47 1.1 jruoho atf_test_case dollar_at
48 1.1 jruoho dollar_at_head() {
49 1.1 jruoho atf_set "descr" "Somewhere between 2.0.2 and 3.0 the expansion" \
50 1.1 jruoho "of the \$@ variable had been broken. Check for" \
51 1.1 jruoho "this behavior."
52 1.1 jruoho }
53 1.1 jruoho dollar_at_body() {
54 1.1 jruoho # This one should work everywhere.
55 1.6 christos atf_check -s exit:0 -o inline:' EOL\n' -e empty \
56 1.6 christos ${TEST_SH} -c 'echo "" "" | '" sed 's,\$,EOL,'"
57 1.1 jruoho
58 1.1 jruoho # This code triggered the bug.
59 1.6 christos atf_check -s exit:0 -o inline:' EOL\n' -e empty \
60 1.6 christos ${TEST_SH} -c 'set -- "" ""; echo "$@" | '" sed 's,\$,EOL,'"
61 1.6 christos
62 1.6 christos atf_check -s exit:0 -o inline:'0\n' -e empty ${TEST_SH} -c \
63 1.6 christos 'set -- -; shift; n_arg() { echo $#; }; n_arg "$@"'
64 1.1 jruoho }
65 1.1 jruoho
66 1.1 jruoho atf_test_case dollar_at_with_text
67 1.1 jruoho dollar_at_with_text_head() {
68 1.1 jruoho atf_set "descr" "Test \$@ expansion when it is surrounded by text" \
69 1.1 jruoho "within the quotes. PR bin/33956."
70 1.1 jruoho }
71 1.1 jruoho dollar_at_with_text_body() {
72 1.6 christos
73 1.6 christos cat <<'EOF' > h-f1
74 1.6 christos
75 1.6 christos delim_argv() {
76 1.6 christos str=
77 1.6 christos while [ $# -gt 0 ]; do
78 1.6 christos if [ -z "${str}" ]; then
79 1.6 christos str=">$1<"
80 1.6 christos else
81 1.6 christos str="${str} >$1<"
82 1.6 christos fi
83 1.6 christos shift
84 1.6 christos done
85 1.6 christos echo "${str}"
86 1.6 christos }
87 1.6 christos
88 1.6 christos EOF
89 1.6 christos cat <<'EOF' > h-f2
90 1.6 christos
91 1.6 christos delim_argv() {
92 1.6 christos str=
93 1.6 christos while [ $# -gt 0 ]; do
94 1.6 christos
95 1.6 christos str="${str}${str:+ }>$1<"
96 1.6 christos shift
97 1.6 christos
98 1.6 christos done
99 1.6 christos echo "${str}"
100 1.6 christos }
101 1.6 christos
102 1.6 christos EOF
103 1.6 christos
104 1.6 christos chmod +x h-f1 h-f2
105 1.6 christos
106 1.6 christos for f in 1 2
107 1.6 christos do
108 1.6 christos atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
109 1.6 christos ". ./h-f${f}; "'set -- ; delim_argv "$@"'
110 1.6 christos atf_check -s exit:0 -o inline:'>foobar<\n' -e empty \
111 1.6 christos ${TEST_SH} -c \
112 1.6 christos ". ./h-f${f}; "'set -- ; delim_argv "foo$@bar"'
113 1.6 christos atf_check -s exit:0 -o inline:'>foo bar<\n' -e empty \
114 1.6 christos ${TEST_SH} -c \
115 1.6 christos ". ./h-f${f}; "'set -- ; delim_argv "foo $@ bar"'
116 1.6 christos
117 1.6 christos atf_check -s exit:0 -o inline:'>a< >b< >c<\n' -e empty \
118 1.6 christos ${TEST_SH} -c \
119 1.6 christos ". ./h-f${f}; "'set -- a b c; delim_argv "$@"'
120 1.6 christos atf_check -s exit:0 -o inline:'>fooa< >b< >cbar<\n' -e empty \
121 1.6 christos ${TEST_SH} -c \
122 1.6 christos ". ./h-f${f}; "'set -- a b c; delim_argv "foo$@bar"'
123 1.6 christos atf_check -s exit:0 -o inline:'>foo a< >b< >c bar<\n' -e empty \
124 1.6 christos ${TEST_SH} -c \
125 1.6 christos ". ./h-f${f}; "'set -- a b c; delim_argv "foo $@ bar"'
126 1.6 christos done
127 1.1 jruoho }
128 1.1 jruoho
129 1.1 jruoho atf_test_case strip
130 1.1 jruoho strip_head() {
131 1.1 jruoho atf_set "descr" "Checks that the %% operator works and strips" \
132 1.1 jruoho "the contents of a variable from the given point" \
133 1.6 christos "to the end"
134 1.1 jruoho }
135 1.1 jruoho strip_body() {
136 1.1 jruoho line='#define bindir "/usr/bin" /* comment */'
137 1.1 jruoho stripped='#define bindir "/usr/bin" '
138 1.6 christos
139 1.6 christos # atf_expect_fail "PR bin/43469" -- now fixed
140 1.6 christos for exp in \
141 1.6 christos '${line%%/\**}' \
142 1.6 christos '${line%%"/*"*}' \
143 1.6 christos '${line%%'"'"'/*'"'"'*}' \
144 1.6 christos '"${line%%/\**}"' \
145 1.6 christos '"${line%%"/*"*}"' \
146 1.6 christos '"${line%%'"'"'/*'"'"'*}"' \
147 1.6 christos '${line%/\**}' \
148 1.6 christos '${line%"/*"*}' \
149 1.6 christos '${line%'"'"'/*'"'"'*}' \
150 1.6 christos '"${line%/\**}"' \
151 1.6 christos '"${line%"/*"*}"' \
152 1.6 christos '"${line%'"'"'/*'"'"'*}"'
153 1.6 christos do
154 1.6 christos atf_check -o inline:":$stripped:\n" -e empty ${TEST_SH} -c \
155 1.6 christos "line='${line}'; echo :${exp}:"
156 1.6 christos done
157 1.1 jruoho }
158 1.1 jruoho
159 1.14 kre atf_test_case wrap_strip
160 1.14 kre wrap_strip_head() {
161 1.14 kre atf_set "descr" "Checks that the %% operator works and strips" \
162 1.14 kre "the contents of a variable from the given point" \
163 1.14 kre 'to the end, and that \ \n sequences do not break it'
164 1.14 kre }
165 1.14 kre wrap_strip_body() {
166 1.14 kre line='#define bindir "/usr/bin" /* comment */'
167 1.14 kre stripped='#define bindir "/usr/bin" '
168 1.14 kre
169 1.14 kre for exp in \
170 1.14 kre '${line\
171 1.14 kre %%/\**}' \
172 1.14 kre '${line%%"/\
173 1.14 kre *"*}' \
174 1.14 kre '${line%%'"'"'/*'"'"'\
175 1.14 kre *}' \
176 1.14 kre '"${li\
177 1.14 kre ne%%/\**}"' \
178 1.14 kre '"${line%%"\
179 1.14 kre /*"*}"' \
180 1.14 kre '"${line%\
181 1.14 kre %'"'"'/*'"'"'*}"' \
182 1.14 kre '${line\
183 1.14 kre %\
184 1.14 kre /\*\
185 1.14 kre *\
186 1.14 kre }' \
187 1.14 kre '${line%"/*\
188 1.14 kre "*\
189 1.14 kre }' \
190 1.14 kre '${line\
191 1.14 kre %\
192 1.14 kre '"'"'/*'"'"'*}' \
193 1.14 kre '"$\
194 1.14 kre {li\
195 1.14 kre ne%\
196 1.14 kre '"'"'/*'"'"'*}"'
197 1.14 kre do
198 1.14 kre atf_check -o inline:":$stripped:\n" -e empty ${TEST_SH} -c \
199 1.14 kre "line='${line}'; echo :${exp}:"
200 1.14 kre done
201 1.14 kre }
202 1.14 kre
203 1.18 kre atf_test_case tilde
204 1.18 kre tilde_head() {
205 1.18 kre atf_set "descr" "Checks that the ~ expansions work"
206 1.18 kre }
207 1.18 kre tilde_body() {
208 1.18 kre for HOME in '' / /home/foo \
209 1.18 kre /a/very/long/home/directory/path/that/might/push/the/tilde/expansion/code/beyond/what/it/would/normally/ever/see/on/any/sane/system/and/perhaps/expose/some/bugs
210 1.18 kre do
211 1.18 kre export HOME
212 1.18 kre
213 1.18 kre atf_check -s exit:0 -e empty \
214 1.18 kre -o inline:'HOME\t'"${HOME}"'
215 1.18 kre ~\t'"${HOME}"'
216 1.18 kre ~/foobar\t'"${HOME}"'/foobar
217 1.18 kre "$V"\t'"${HOME}"'
218 1.18 kre "$X"\t~
219 1.18 kre "$Y"\t'"${HOME}"':'"${HOME}"'
220 1.18 kre "$YY"\t'"${HOME}"'/foo:'"${HOME}"'/bar
221 1.18 kre "$Z"\t'"${HOME}"'/~
222 1.18 kre ${U:-~}\t'"${HOME}"'
223 1.18 kre $V\t'"${HOME}"'
224 1.18 kre $X\t~
225 1.18 kre $Y\t'"${HOME}"':'"${HOME}"'
226 1.18 kre $YY\t'"${HOME}"'/foo:'"${HOME}"'/bar
227 1.18 kre $Z\t'"${HOME}"'/~
228 1.18 kre ${U:=~}\t'"${HOME}"'
229 1.18 kre ${UU:=~:~}\t'"${HOME}"':'"${HOME}"'
230 1.18 kre ${UUU:=~/:~}\t'"${HOME}"'/:'"${HOME}"'
231 1.18 kre ${U4:=~/:~/}\t'"${HOME}"'/:'"${HOME}"'/\n' \
232 1.18 kre ${TEST_SH} -s <<- \EOF
233 1.18 kre unset -v U UU UUU U4
234 1.18 kre V=~
235 1.18 kre X="~"
236 1.18 kre Y=~:~ YY=~/foo:~/bar
237 1.18 kre Z=~/~
238 1.18 kre printf '%s\t%s\n' \
239 1.18 kre 'HOME' "${HOME}" \
240 1.18 kre '~' ~ \
241 1.18 kre '~/foobar' ~/foobar \
242 1.18 kre '"$V"' "$V" \
243 1.18 kre '"$X"' "$X" \
244 1.18 kre '"$Y"' "$Y" \
245 1.18 kre '"$YY"' "$YY" \
246 1.18 kre '"$Z"' "$Z" \
247 1.18 kre '${U:-~}' ''${U:-~} \
248 1.18 kre '$V' ''$V \
249 1.18 kre '$X' ''$X \
250 1.18 kre '$Y' ''$Y \
251 1.18 kre '$YY' ''$YY \
252 1.18 kre '$Z' ''$Z \
253 1.18 kre '${U:=~}' ''${U:=~} \
254 1.18 kre '${UU:=~:~}' ''${UU:=~:~} \
255 1.18 kre '${UUU:=~/:~}' ''${UUU:=~/:~} \
256 1.18 kre '${U4:=~/:~/}' ''${U4:=~/:~/}
257 1.18 kre EOF
258 1.18 kre done
259 1.18 kre
260 1.18 kre # Testing ~user is harder, so, perhaps later...
261 1.18 kre }
262 1.18 kre
263 1.1 jruoho atf_test_case varpattern_backslashes
264 1.1 jruoho varpattern_backslashes_head() {
265 1.1 jruoho atf_set "descr" "Tests that protecting wildcards with backslashes" \
266 1.1 jruoho "works in variable patterns."
267 1.1 jruoho }
268 1.1 jruoho varpattern_backslashes_body() {
269 1.1 jruoho line='/foo/bar/*/baz'
270 1.1 jruoho stripped='/foo/bar/'
271 1.6 christos atf_check -o inline:'/foo/bar/\n' -e empty ${TEST_SH} -c \
272 1.6 christos 'line="/foo/bar/*/baz"; echo ${line%%\**}'
273 1.1 jruoho }
274 1.1 jruoho
275 1.1 jruoho atf_test_case arithmetic
276 1.1 jruoho arithmetic_head() {
277 1.1 jruoho atf_set "descr" "POSIX requires shell arithmetic to use signed" \
278 1.1 jruoho "long or a wider type. We use intmax_t, so at" \
279 1.1 jruoho "least 64 bits should be available. Make sure" \
280 1.1 jruoho "this is true."
281 1.1 jruoho }
282 1.1 jruoho arithmetic_body() {
283 1.6 christos
284 1.6 christos atf_check -o inline:'3' -e empty ${TEST_SH} -c \
285 1.6 christos 'printf %s $((1 + 2))'
286 1.6 christos atf_check -o inline:'2147483647' -e empty ${TEST_SH} -c \
287 1.6 christos 'printf %s $((0x7fffffff))'
288 1.6 christos atf_check -o inline:'9223372036854775807' -e empty ${TEST_SH} -c \
289 1.6 christos 'printf %s $(((1 << 63) - 1))'
290 1.1 jruoho }
291 1.1 jruoho
292 1.2 ast atf_test_case iteration_on_null_parameter
293 1.2 ast iteration_on_null_parameter_head() {
294 1.2 ast atf_set "descr" "Check iteration of \$@ in for loop when set to null;" \
295 1.2 ast "the error \"sh: @: parameter not set\" is incorrect." \
296 1.2 ast "PR bin/48202."
297 1.2 ast }
298 1.2 ast iteration_on_null_parameter_body() {
299 1.6 christos atf_check -o empty -e empty ${TEST_SH} -c \
300 1.6 christos 'N=; set -- ${N}; for X; do echo "[$X]"; done'
301 1.6 christos }
302 1.6 christos
303 1.6 christos atf_test_case iteration_on_quoted_null_parameter
304 1.6 christos iteration_on_quoted_null_parameter_head() {
305 1.6 christos atf_set "descr" \
306 1.6 christos 'Check iteration of "$@" in for loop when set to null;'
307 1.6 christos }
308 1.6 christos iteration_on_quoted_null_parameter_body() {
309 1.6 christos atf_check -o inline:'[]\n' -e empty ${TEST_SH} -c \
310 1.6 christos 'N=; set -- "${N}"; for X; do echo "[$X]"; done'
311 1.6 christos }
312 1.6 christos
313 1.6 christos atf_test_case iteration_on_null_or_null_parameter
314 1.6 christos iteration_on_null_or_null_parameter_head() {
315 1.6 christos atf_set "descr" \
316 1.6 christos 'Check expansion of null parameter as default for another null'
317 1.6 christos }
318 1.6 christos iteration_on_null_or_null_parameter_body() {
319 1.6 christos atf_check -o empty -e empty ${TEST_SH} -c \
320 1.6 christos 'N=; E=; set -- ${N:-${E}}; for X; do echo "[$X]"; done'
321 1.6 christos }
322 1.6 christos
323 1.6 christos atf_test_case iteration_on_null_or_missing_parameter
324 1.6 christos iteration_on_null_or_missing_parameter_head() {
325 1.6 christos atf_set "descr" \
326 1.6 christos 'Check expansion of missing parameter as default for another null'
327 1.6 christos }
328 1.6 christos iteration_on_null_or_missing_parameter_body() {
329 1.6 christos # atf_expect_fail 'PR bin/50834'
330 1.6 christos atf_check -o empty -e empty ${TEST_SH} -c \
331 1.6 christos 'N=; set -- ${N:-}; for X; do echo "[$X]"; done'
332 1.2 ast }
333 1.2 ast
334 1.10 kre ####### The remaining tests use the following helper functions ...
335 1.10 kre
336 1.7 christos nl='
337 1.7 christos '
338 1.7 christos reset()
339 1.7 christos {
340 1.7 christos TEST_NUM=0
341 1.7 christos TEST_FAILURES=''
342 1.7 christos TEST_FAIL_COUNT=0
343 1.7 christos TEST_ID="$1"
344 1.7 christos }
345 1.7 christos
346 1.7 christos check()
347 1.7 christos {
348 1.7 christos fail=false
349 1.7 christos TEMP_FILE=$( mktemp OUT.XXXXXX )
350 1.7 christos TEST_NUM=$(( $TEST_NUM + 1 ))
351 1.7 christos MSG=
352 1.7 christos
353 1.7 christos # our local shell (ATF_SHELL) better do quoting correctly...
354 1.7 christos # some of the tests expect us to expand $nl internally...
355 1.7 christos CMD="$1"
356 1.7 christos
357 1.7 christos result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )"
358 1.7 christos STATUS=$?
359 1.7 christos
360 1.7 christos if [ "${STATUS}" -ne "$3" ]; then
361 1.7 christos MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
362 1.7 christos MSG="${MSG} expected exit code $3, got ${STATUS}"
363 1.7 christos
364 1.7 christos # don't actually fail just because of wrong exit code
365 1.7 christos # unless we either expected, or received "good"
366 1.13 kre # or something else is detected as incorrect as well.
367 1.7 christos case "$3/${STATUS}" in
368 1.7 christos (*/0|0/*) fail=true;;
369 1.7 christos esac
370 1.7 christos fi
371 1.7 christos
372 1.7 christos if [ "$3" -eq 0 ]; then
373 1.7 christos if [ -s "${TEMP_FILE}" ]; then
374 1.7 christos MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
375 1.7 christos MSG="${MSG} Messages produced on stderr unexpected..."
376 1.7 christos MSG="${MSG}${nl}$( cat "${TEMP_FILE}" )"
377 1.7 christos fail=true
378 1.7 christos fi
379 1.7 christos else
380 1.7 christos if ! [ -s "${TEMP_FILE}" ]; then
381 1.7 christos MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
382 1.7 christos MSG="${MSG} Expected messages on stderr,"
383 1.7 christos MSG="${MSG} nothing produced"
384 1.7 christos fail=true
385 1.7 christos fi
386 1.7 christos fi
387 1.7 christos rm -f "${TEMP_FILE}"
388 1.7 christos
389 1.7 christos # Remove newlines (use local shell for this)
390 1.7 christos oifs="$IFS"
391 1.7 christos IFS="$nl"
392 1.7 christos result="$(echo $result)"
393 1.7 christos IFS="$oifs"
394 1.7 christos if [ "$2" != "$result" ]
395 1.7 christos then
396 1.7 christos MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
397 1.7 christos MSG="${MSG} Expected output '$2', received '$result'"
398 1.7 christos fail=true
399 1.7 christos fi
400 1.7 christos
401 1.7 christos if $fail
402 1.7 christos then
403 1.7 christos MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
404 1.7 christos MSG="${MSG} Full command: <<${CMD}>>"
405 1.7 christos fi
406 1.7 christos
407 1.7 christos $fail && test -n "$TEST_ID" && {
408 1.7 christos TEST_FAILURES="${TEST_FAILURES}${TEST_FAILURES:+${nl}}"
409 1.7 christos TEST_FAILURES="${TEST_FAILURES}${TEST_ID}[$TEST_NUM]:"
410 1.7 christos TEST_FAILURES="${TEST_FAILURES} Test of '$1' failed.";
411 1.7 christos TEST_FAILURES="${TEST_FAILURES}${nl}${MSG}"
412 1.7 christos TEST_FAIL_COUNT=$(( $TEST_FAIL_COUNT + 1 ))
413 1.7 christos return 0
414 1.7 christos }
415 1.13 kre $fail && atf_fail "Test[$TEST_NUM] failed: $(
416 1.13 kre # ATF does not like newlines in messages, so change them...
417 1.13 kre printf '%s' "${MSG}" | tr '\n' ';'
418 1.13 kre )"
419 1.7 christos return 0
420 1.7 christos }
421 1.7 christos
422 1.7 christos results()
423 1.7 christos {
424 1.10 kre test -n "$1" && atf_expect_fail "$1"
425 1.10 kre
426 1.7 christos test -z "${TEST_ID}" && return 0
427 1.7 christos test -z "${TEST_FAILURES}" && return 0
428 1.7 christos
429 1.7 christos echo >&2 "=========================================="
430 1.7 christos echo >&2 "While testing '${TEST_ID}'"
431 1.7 christos echo >&2 " - - - - - - - - - - - - - - - - -"
432 1.7 christos echo >&2 "${TEST_FAILURES}"
433 1.10 kre
434 1.7 christos atf_fail \
435 1.9 kre "Test ${TEST_ID}: $TEST_FAIL_COUNT (of $TEST_NUM) subtests failed - see stderr"
436 1.7 christos }
437 1.7 christos
438 1.10 kre ####### End helpers
439 1.10 kre
440 1.7 christos atf_test_case shell_params
441 1.7 christos shell_params_head() {
442 1.7 christos atf_set "descr" "Test correct operation of the numeric parameters"
443 1.7 christos }
444 1.7 christos shell_params_body() {
445 1.7 christos atf_require_prog mktemp
446 1.7 christos
447 1.7 christos reset shell_params
448 1.7 christos
449 1.7 christos check 'set -- a b c; echo "$#: $1 $2 $3"' '3: a b c' 0
450 1.7 christos check 'set -- a b c d e f g h i j k l m; echo "$#: ${1}0 ${10} $10"' \
451 1.7 christos '13: a0 j a0' 0
452 1.7 christos check 'x="$0"; set -- a b; y="$0";
453 1.7 christos [ "x${x}y" = "x${y}y" ] && echo OK || echo x="$x" y="$y"' \
454 1.7 christos 'OK' 0
455 1.7 christos check "${TEST_SH} -c 'echo 0=\$0 1=\$1 2=\$2' a b c" '0=a 1=b 2=c' 0
456 1.7 christos
457 1.7 christos echo 'echo 0="$0" 1="$1" 2="$2"' > helper.sh
458 1.8 christos check "${TEST_SH} helper.sh a b c" '0=helper.sh 1=a 2=b' 0
459 1.7 christos
460 1.7 christos check 'set -- a bb ccc dddd eeeee ffffff ggggggg hhhhhhhh \
461 1.7 christos iiiiiiiii jjjjjjjjjj kkkkkkkkkkk
462 1.7 christos echo "${#}: ${#1} ${#2} ${#3} ${#4} ... ${#9} ${#10} ${#11}"' \
463 1.7 christos '11: 1 2 3 4 ... 9 10 11' 0
464 1.7 christos
465 1.7 christos check 'set -- a b c; echo "$#: ${1-A} ${2-B} ${3-C} ${4-D} ${5-E}"' \
466 1.7 christos '3: a b c D E' 0
467 1.7 christos check 'set -- a "" c "" e
468 1.7 christos echo "$#: ${1:-A} ${2:-B} ${3:-C} ${4:-D} ${5:-E}"' \
469 1.7 christos '5: a B c D e' 0
470 1.7 christos check 'set -- a "" c "" e
471 1.7 christos echo "$#: ${1:+A} ${2:+B} ${3:+C} ${4:+D} ${5:+E}"' \
472 1.7 christos '5: A C E' 0
473 1.7 christos check 'set -- "abab*cbb"
474 1.7 christos echo "${1} ${1#a} ${1%b} ${1##ab} ${1%%b} ${1#*\*} ${1%\**}"' \
475 1.7 christos 'abab*cbb bab*cbb abab*cb ab*cbb abab*cb cbb abab' 0
476 1.7 christos check 'set -- "abab?cbb"
477 1.7 christos echo "${1}:${1#*a}+${1%b*}-${1##*a}_${1%%b*}%${1#[ab]}=${1%?*}/${1%\?*}"' \
478 1.7 christos 'abab?cbb:bab?cbb+abab?cb-b?cbb_a%bab?cbb=abab?cb/abab' 0
479 1.7 christos check 'set -- a "" c "" e; echo "${2:=b}"' '' 1
480 1.7 christos
481 1.15 kre check 'set -- a b c d; echo ${4294967297}' '' 0 # result 'a' => ${1}
482 1.15 kre check 'set -- a b c; echo ${01}' 'a' 0
483 1.15 kre check "${TEST_SH} -c 'echo 0=\${00} 1=\${01} 2=\${02}' a b c" \
484 1.15 kre '0=a 1=b 2=c' 0
485 1.15 kre
486 1.16 kre # by special request, for PaulG... (${0...} is not octal!)
487 1.17 kre
488 1.17 kre # Posix XCU 2.5.1 (Issue 7 TC2 pg 2349 lines 74835..6):
489 1.17 kre # The digits denoting the positional parameters shall always
490 1.17 kre # be interpreted as a decimal value, even if there is a leading zero.
491 1.17 kre
492 1.16 kre check \
493 1.16 kre 'set -- a b c d e f g h i j k l m; echo "$#: ${08} ${010} ${011}"' \
494 1.16 kre '13: h j k' 0
495 1.16 kre
496 1.7 christos results
497 1.7 christos }
498 1.7 christos
499 1.9 kre atf_test_case var_with_embedded_cmdsub
500 1.9 kre var_with_embedded_cmdsub_head() {
501 1.9 kre atf_set "descr" "Test expansion of vars with embedded cmdsub"
502 1.9 kre }
503 1.9 kre var_with_embedded_cmdsub_body() {
504 1.9 kre
505 1.9 kre reset var_with_embedded_cmdsub
506 1.9 kre
507 1.9 kre check 'unset x; echo ${x-$(echo a)}$(echo b)' 'ab' 0 #1
508 1.9 kre check 'unset x; echo ${x:-$(echo a)}$(echo b)' 'ab' 0 #2
509 1.9 kre check 'x=""; echo ${x-$(echo a)}$(echo b)' 'b' 0 #3
510 1.9 kre check 'x=""; echo ${x:-$(echo a)}$(echo b)' 'ab' 0 #4
511 1.9 kre check 'x=c; echo ${x-$(echo a)}$(echo b)' 'cb' 0 #5
512 1.9 kre check 'x=c; echo ${x:-$(echo a)}$(echo b)' 'cb' 0 #6
513 1.9 kre
514 1.9 kre check 'unset x; echo ${x+$(echo a)}$(echo b)' 'b' 0 #7
515 1.9 kre check 'unset x; echo ${x:+$(echo a)}$(echo b)' 'b' 0 #8
516 1.9 kre check 'x=""; echo ${x+$(echo a)}$(echo b)' 'ab' 0 #9
517 1.9 kre check 'x=""; echo ${x:+$(echo a)}$(echo b)' 'b' 0 #10
518 1.9 kre check 'x=c; echo ${x+$(echo a)}$(echo b)' 'ab' 0 #11
519 1.9 kre check 'x=c; echo ${x:+$(echo a)}$(echo b)' 'ab' 0 #12
520 1.9 kre
521 1.9 kre check 'unset x; echo ${x=$(echo a)}$(echo b)' 'ab' 0 #13
522 1.9 kre check 'unset x; echo ${x:=$(echo a)}$(echo b)' 'ab' 0 #14
523 1.9 kre check 'x=""; echo ${x=$(echo a)}$(echo b)' 'b' 0 #15
524 1.9 kre check 'x=""; echo ${x:=$(echo a)}$(echo b)' 'ab' 0 #16
525 1.9 kre check 'x=c; echo ${x=$(echo a)}$(echo b)' 'cb' 0 #17
526 1.9 kre check 'x=c; echo ${x:=$(echo a)}$(echo b)' 'cb' 0 #18
527 1.9 kre
528 1.9 kre check 'unset x; echo ${x?$(echo a)}$(echo b)' '' 2 #19
529 1.9 kre check 'unset x; echo ${x:?$(echo a)}$(echo b)' '' 2 #20
530 1.9 kre check 'x=""; echo ${x?$(echo a)}$(echo b)' 'b' 0 #21
531 1.9 kre check 'x=""; echo ${x:?$(echo a)}$(echo b)' '' 2 #22
532 1.9 kre check 'x=c; echo ${x?$(echo a)}$(echo b)' 'cb' 0 #23
533 1.9 kre check 'x=c; echo ${x:?$(echo a)}$(echo b)' 'cb' 0 #24
534 1.9 kre
535 1.9 kre check 'unset x; echo ${x%$(echo a)}$(echo b)' 'b' 0 #25
536 1.9 kre check 'unset x; echo ${x%%$(echo a)}$(echo b)' 'b' 0 #26
537 1.9 kre check 'x=""; echo ${x%$(echo a)}$(echo b)' 'b' 0 #27
538 1.9 kre check 'x=""; echo ${x%%$(echo a)}$(echo b)' 'b' 0 #28
539 1.9 kre check 'x=c; echo ${x%$(echo a)}$(echo b)' 'cb' 0 #29
540 1.9 kre check 'x=c; echo ${x%%$(echo a)}$(echo b)' 'cb' 0 #30
541 1.9 kre check 'x=aa; echo ${x%$(echo "*a")}$(echo b)' 'ab' 0 #31
542 1.9 kre check 'x=aa; echo ${x%%$(echo "*a")}$(echo b)' 'b' 0 #32
543 1.9 kre
544 1.9 kre check 'unset x; echo ${x#$(echo a)}$(echo b)' 'b' 0 #33
545 1.9 kre check 'unset x; echo ${x##$(echo a)}$(echo b)' 'b' 0 #34
546 1.9 kre check 'x=""; echo ${x#$(echo a)}$(echo b)' 'b' 0 #35
547 1.9 kre check 'x=""; echo ${x##$(echo a)}$(echo b)' 'b' 0 #36
548 1.9 kre check 'x=c; echo ${x#$(echo a)}$(echo b)' 'cb' 0 #37
549 1.9 kre check 'x=c; echo ${x##$(echo a)}$(echo b)' 'cb' 0 #38
550 1.9 kre check 'x=aa; echo ${x#$(echo "*a")}$(echo b)' 'ab' 0 #39
551 1.9 kre check 'x=aa; echo ${x##$(echo "*a")}$(echo b)' 'b' 0 #40
552 1.9 kre
553 1.9 kre results
554 1.9 kre }
555 1.9 kre
556 1.12 kre atf_test_case dollar_hash
557 1.12 kre dollar_hash_head() {
558 1.12 kre atf_set "descr" 'Test expansion of various aspects of $#'
559 1.12 kre }
560 1.12 kre dollar_hash_body() {
561 1.12 kre
562 1.12 kre #
563 1.12 kre # $# looks like it should be so simple that it doesn't really
564 1.12 kre # need a test of its own, and used in that way, it really doesn't.
565 1.12 kre # But when we add braces ${#} we need to deal with the three
566 1.12 kre # (almost 4) different meanings of a # inside a ${} expansion...
567 1.13 kre #
568 1.13 kre # Note that some of these are just how we treat expansions that
569 1.13 kre # are unspecified by posix (as noted below.)
570 1.12 kre #
571 1.12 kre # 1. ${#} is just $# (number of params)
572 1.12 kre # 1.a ${\#} is nothing at all (error: invalid expansion)
573 1.12 kre # 1.b ${\#...} (anything after) is the same (invalid)
574 1.12 kre # 2. ${#VAR} is the length of the value VAR
575 1.12 kre # 2.a Including ${##} - the length of ${#}
576 1.12 kre # 3 ${VAR#pat} is the value of VAR with leading pat removed
577 1.12 kre # 3.a Including ${VAR#} which just removes leading nothing
578 1.12 kre # This is relevant in case of ${VAR#${X}} with X=''
579 1.13 kre # nb: not required by posix, see XCU 2.6.2
580 1.12 kre # 3.b ${##} is not a case of 3.a but rather 2.a
581 1.12 kre # 3.c Yet ${##pat} is a case of 3.a
582 1.12 kre # Including ${##${X}} where X='' or X='#'
583 1.13 kre # nb: not required by posix, see XCU 2.6.2
584 1.12 kre # 3.d And ${#\#} is invalid (error)
585 1.12 kre # 3.e But ${##\#} removes a leading # from the value of $#
586 1.12 kre # (so is just $# as there is no leading # there)
587 1.13 kre # nb: not required by posix, see XCU 2.6.2
588 1.12 kre # 4 ${VAR##pat} is the value of VAR with longest pat removed
589 1.12 kre # 4.a Including ${VAR##} which removes the longest nothing
590 1.12 kre # 4.b Which in this case includes ${###} (so is == $#)
591 1.13 kre # nb: not required by posix, see XCU 2.6.2
592 1.12 kre # 4.c But not ${##\#} which is $# with a leading '#' removed
593 1.12 kre # (and so is also == $#), i.e.: like ${###} but different.
594 1.13 kre # nb: not required by posix, see XCU 2.6.2
595 1.12 kre # 4.d As is ${###\#} or just ${####} - remove # (so just $#)
596 1.13 kre # nb: not required by posix, see XCU 2.6.2
597 1.12 kre #
598 1.12 kre
599 1.12 kre reset dollar_hash
600 1.12 kre
601 1.12 kre check 'set -- ; echo $#' '0' 0 # 1
602 1.12 kre check 'set -- a b c; echo $#' '3' 0 # 2
603 1.12 kre check 'set -- a b c d e f g h i j; echo $#' '10' 0 # 3
604 1.12 kre # rule 1
605 1.12 kre check 'set -- ; echo ${#}' '0' 0 # 4
606 1.12 kre check 'set -- a b c; echo ${#}' '3' 0 # 5
607 1.12 kre check 'set -- a b c d e f g h i j; echo ${#}' '10' 0 # 6
608 1.12 kre # rule 1.a
609 1.12 kre check 'set -- a b c; echo ${\#}' '' 2 # 7
610 1.12 kre # rule 1.b
611 1.12 kre check 'set -- a b c; echo ${\#:-foo}' '' 2 # 8
612 1.12 kre # rule 2
613 1.12 kre check 'VAR=12345; echo ${#VAR}' '5' 0 # 9
614 1.12 kre check 'VAR=123456789012; echo ${#VAR}' '12' 0 #10
615 1.12 kre # rule 2.a
616 1.12 kre check 'set -- ; echo ${##}' '1' 0 #11
617 1.12 kre check 'set -- a b c; echo ${##}' '1' 0 #12
618 1.12 kre check 'set -- a b c d e f g h i j; echo ${##}' '2' 0 #13
619 1.12 kre # rule 3
620 1.12 kre check 'VAR=12345; echo ${VAR#1}' '2345' 0 #14
621 1.12 kre check 'VAR=12345; echo ${VAR#2}' '12345' 0 #15
622 1.12 kre check 'VAR=#2345; echo ${VAR#\#}' '2345' 0 #16
623 1.12 kre check 'X=1; VAR=12345; echo ${VAR#${X}}' '2345' 0 #17
624 1.12 kre check 'X=1; VAR=#2345; echo ${VAR#${X}}' '#2345' 0 #18
625 1.12 kre # rule 3.a
626 1.12 kre check 'VAR=12345; echo ${VAR#}' '12345' 0 #19
627 1.12 kre check 'X=; VAR=12345; echo ${VAR#${X}}' '12345' 0 #20
628 1.12 kre # rule 3.b (tested above, rule 2.a)
629 1.12 kre # rule 3.c
630 1.12 kre check 'set -- ; echo ${##0}' '' 0 #21
631 1.12 kre check 'set -- a b c; echo ${##1}' '3' 0 #22
632 1.12 kre check 'set -- a b c d e f g h i j; echo ${##1}' '0' 0 #23
633 1.12 kre check 'X=0; set -- ; echo ${##${X}}' '' 0 #24
634 1.12 kre check 'X=; set -- ; echo ${##${X}}' '0' 0 #25
635 1.12 kre check 'X=1; set -- a b c; echo ${##${X}}' '3' 0 #26
636 1.12 kre check 'X=1; set -- a b c d e f g h i j; echo ${##${X}}' '0' 0 #27
637 1.12 kre check 'X=; set -- a b c d e f g h i j; echo ${##${X}}' '10' 0 #28
638 1.12 kre check 'X=#; VAR=#2345; echo ${VAR#${X}}' '2345' 0 #29
639 1.12 kre check 'X=#; VAR=12345; echo ${VAR#${X}}' '12345' 0 #30
640 1.12 kre # rule 3.d
641 1.12 kre check 'set -- a b c; echo ${#\#}' '' 2 #31
642 1.12 kre # rule 3.e
643 1.12 kre check 'set -- ; echo ${##\#}' '0' 0 #32
644 1.12 kre check 'set -- a b c d e f g h i j; echo ${##\#}' '10' 0 #33
645 1.12 kre
646 1.12 kre # rule 4
647 1.12 kre check 'VAR=12345; echo ${VAR##1}' '2345' 0 #34
648 1.12 kre check 'VAR=12345; echo ${VAR##\1}' '2345' 0 #35
649 1.12 kre # rule 4.a
650 1.12 kre check 'VAR=12345; echo ${VAR##}' '12345' 0 #36
651 1.12 kre # rule 4.b
652 1.12 kre check 'set -- ; echo ${###}' '0' 0 #37
653 1.12 kre check 'set -- a b c d e f g h i j; echo ${###}' '10' 0 #38
654 1.12 kre # rule 4.c
655 1.12 kre check 'VAR=12345; echo ${VAR#\#}' '12345' 0 #39
656 1.12 kre check 'VAR=12345; echo ${VAR#\#1}' '12345' 0 #40
657 1.12 kre check 'VAR=#2345; echo ${VAR#\#}' '2345' 0 #41
658 1.12 kre check 'VAR=#12345; echo ${VAR#\#1}' '2345' 0 #42
659 1.12 kre check 'VAR=#2345; echo ${VAR#\#1}' '#2345' 0 #43
660 1.12 kre check 'set -- ; echo ${####}' '0' 0 #44
661 1.12 kre check 'set -- ; echo ${###\#}' '0' 0 #45
662 1.12 kre check 'set -- a b c d e f g h i j; echo ${####}' '10' 0 #46
663 1.12 kre check 'set -- a b c d e f g h i j; echo ${###\#}' '10' 0 #47
664 1.12 kre
665 1.12 kre # now check for some more utter nonsense, not mentioned in the rules
666 1.12 kre # above (doesn't need to be)
667 1.12 kre
668 1.12 kre check 'x=hello; set -- a b c; echo ${#x:-1}' '' 2 #48
669 1.12 kre check 'x=hello; set -- a b c; echo ${#x-1}' '' 2 #49
670 1.12 kre check 'x=hello; set -- a b c; echo ${#x:+1}' '' 2 #50
671 1.12 kre check 'x=hello; set -- a b c; echo ${#x+1}' '' 2 #51
672 1.12 kre check 'x=hello; set -- a b c; echo ${#x+1}' '' 2 #52
673 1.12 kre check 'x=hello; set -- a b c; echo ${#x:?msg}' '' 2 #53
674 1.12 kre check 'x=hello; set -- a b c; echo ${#x?msg}' '' 2 #54
675 1.12 kre check 'x=hello; set -- a b c; echo ${#x:=val}' '' 2 #55
676 1.12 kre check 'x=hello; set -- a b c; echo ${#x=val}' '' 2 #56
677 1.12 kre check 'x=hello; set -- a b c; echo ${#x#h}' '' 2 #57
678 1.12 kre check 'x=hello; set -- a b c; echo ${#x#*l}' '' 2 #58
679 1.12 kre check 'x=hello; set -- a b c; echo ${#x##*l}' '' 2 #59
680 1.12 kre check 'x=hello; set -- a b c; echo ${#x%o}' '' 2 #60
681 1.12 kre check 'x=hello; set -- a b c; echo ${#x%l*}' '' 2 #61
682 1.12 kre check 'x=hello; set -- a b c; echo ${#x%%l*}' '' 2 #62
683 1.12 kre
684 1.12 kre # but just to be complete, these ones should work
685 1.12 kre
686 1.12 kre check 'x=hello; set -- a b c; echo ${#%5}' '3' 0 #63
687 1.12 kre check 'x=hello; set -- a b c; echo ${#%3}' '' 0 #64
688 1.12 kre check 'x=hello; set -- a b c; echo ${#%?}' '' 0 #65
689 1.12 kre check 'X=#; set -- a b c; echo ${#%${X}}' '3' 0 #66
690 1.12 kre check 'X=3; set -- a b c; echo ${#%${X}}' '' 0 #67
691 1.12 kre check 'set -- a b c; echo ${#%%5}' '3' 0 #68
692 1.12 kre check 'set -- a b c; echo ${#%%3}' '' 0 #69
693 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%1}' '12' 0 #70
694 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%2}' '1' 0 #71
695 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%?}' '1' 0 #72
696 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%[012]}' '1' 0 #73
697 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%[0-4]}' '1' 0 #74
698 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%?2}' '' 0 #75
699 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%1*}' '' 0 #76
700 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%%2}' '1' 0 #77
701 1.12 kre check 'set -- a b c d e f g h i j k l; echo ${#%%1*}' '' 0 #78
702 1.12 kre
703 1.12 kre # and this lot are stupid, as $# is never unset or null, but they do work...
704 1.12 kre
705 1.12 kre check 'set -- a b c; echo ${#:-99}' '3' 0 #79
706 1.12 kre check 'set -- a b c; echo ${#-99}' '3' 0 #80
707 1.12 kre check 'set -- a b c; echo ${#:+99}' '99' 0 #81
708 1.12 kre check 'set -- a b c; echo ${#+99}' '99' 0 #82
709 1.12 kre check 'set -- a b c; echo ${#:?bogus}' '3' 0 #83
710 1.12 kre check 'set -- a b c; echo ${#?bogus}' '3' 0 #84
711 1.12 kre
712 1.12 kre # even this utter nonsense is OK, as while special params cannot be
713 1.12 kre # set this way, here, as $# is not unset, or null, the assignment
714 1.12 kre # never happens (isn't even attempted)
715 1.12 kre
716 1.12 kre check 'set -- a b c; echo ${#:=bogus}' '3' 0 #85
717 1.12 kre check 'set -- a b c; echo ${#=bogus}' '3' 0 #86
718 1.12 kre
719 1.13 kre for n in 0 1 10 25 100 #87 #88 #89 #90 #91
720 1.13 kre do
721 1.13 kre check "(exit $n)"'; echo ${#?}' "${#n}" 0
722 1.13 kre done
723 1.13 kre
724 1.13 kre results # results so far anyway...
725 1.13 kre
726 1.13 kre # now we have some harder to verify cases, as they (must) check unknown values
727 1.13 kre # and hence the resuls cannot just be built into the script, but we have
728 1.13 kre # to use some tricks to validate them, so for these, just do regular testing
729 1.13 kre # and don't attempt to use the check helper function, nor to include
730 1.13 kre # these tests in the result summary. If anything has already failed, we
731 1.13 kre # do not get this far... From here on, first failure ends this test.
732 1.13 kre
733 1.13 kre for opts in '' '-a' '-au' '-auf' '-aufe' # options safe enough to set
734 1.13 kre do
735 1.13 kre # Note the shell might have other (or these) opts set already
736 1.13 kre
737 1.13 kre RES=$(${TEST_SH} -c "test -n '${opts}' && set ${opts};
738 1.13 kre printf '%s' \"\$-\";printf ' %s\\n' \"\${#-}\"") ||
739 1.13 kre atf_fail '${#-} test exited with status '"$?"
740 1.13 kre LEN="${RES##* }"
741 1.13 kre DMINUS="${RES% ${LEN}}"
742 1.13 kre if [ "${#DMINUS}" != "${LEN}" ]
743 1.13 kre then
744 1.13 kre atf_fail \
745 1.13 kre '${#-} test'" produced ${LEN} for opts ${DMINUS} (${RES})"
746 1.13 kre fi
747 1.13 kre done
748 1.13 kre
749 1.13 kre for seq in a b c d e f g h i j
750 1.13 kre do
751 1.13 kre # now we are tryin to generate different pids for $$ and $!
752 1.13 kre # so we just run the test a number of times, and hope...
753 1.13 kre # On NetBSD pid randomisation will usually help the tests
754 1.13 kre
755 1.13 kre eval "$(${TEST_SH} -c \
756 1.13 kre '(exit 0)& BG=$! LBG=${#!};
757 1.13 kre printf "SH=%s BG=%s LSH=%s LBG=%s" "$$" "$BG" "${#$}" "$LBG";
758 1.13 kre wait')"
759 1.13 kre
760 1.13 kre if [ "${#SH}" != "${LSH}" ] || [ "${#BG}" != "${LBG}" ]
761 1.13 kre then
762 1.13 kre atf_fail \
763 1.13 kre '${#!] of '"${BG} was ${LBG}, expected ${#BG}"'; ${#$} of '"${SH} was ${LSH}, expected ${#SH}"
764 1.13 kre fi
765 1.13 kre done
766 1.12 kre }
767 1.12 kre
768 1.10 kre atf_test_case dollar_star
769 1.10 kre dollar_star_head() {
770 1.10 kre atf_set "descr" 'Test expansion of various aspects of $*'
771 1.10 kre }
772 1.10 kre dollar_star_body() {
773 1.10 kre
774 1.12 kre reset dollar_star
775 1.10 kre
776 1.10 kre check 'set -- a b c; echo $# $*' '3 a b c' 0 # 1
777 1.10 kre check 'set -- a b c; echo $# "$*"' '3 a b c' 0 # 2
778 1.10 kre check 'set -- a "b c"; echo $# $*' '2 a b c' 0 # 3
779 1.10 kre check 'set -- a "b c"; echo $# "$*"' '2 a b c' 0 # 4
780 1.10 kre check 'set -- a b c; set -- $* ; echo $# $*' '3 a b c' 0 # 5
781 1.10 kre check 'set -- a b c; set -- "$*" ; echo $# $*' '1 a b c' 0 # 6
782 1.10 kre check 'set -- a "b c"; set -- $* ; echo $# $*' '3 a b c' 0 # 7
783 1.10 kre check 'set -- a "b c"; set -- "$*" ; echo $# $*' \
784 1.10 kre '1 a b c' 0 # 8
785 1.10 kre
786 1.10 kre check 'IFS=". "; set -- a b c; echo $# $*' '3 a b c' 0 # 9
787 1.10 kre check 'IFS=". "; set -- a b c; echo $# "$*"' '3 a.b.c' 0 #10
788 1.10 kre check 'IFS=". "; set -- a "b c"; echo $# $*' '2 a b c' 0 #11
789 1.10 kre check 'IFS=". "; set -- a "b c"; echo $# "$*"' '2 a.b c' 0 #12
790 1.10 kre check 'IFS=". "; set -- a "b.c"; echo $# $*' '2 a b c' 0 #13
791 1.10 kre check 'IFS=". "; set -- a "b.c"; echo $# "$*"' '2 a.b.c' 0 #14
792 1.10 kre check 'IFS=". "; set -- a b c; set -- $* ; echo $# $*' \
793 1.10 kre '3 a b c' 0 #15
794 1.10 kre check 'IFS=". "; set -- a b c; set -- "$*" ; echo $# $*' \
795 1.10 kre '1 a b c' 0 #16
796 1.10 kre check 'IFS=". "; set -- a "b c"; set -- $* ; echo $# $*' \
797 1.10 kre '3 a b c' 0 #17
798 1.10 kre check 'IFS=". "; set -- a "b c"; set -- "$*" ; echo $# $*' \
799 1.10 kre '1 a b c' 0 #18
800 1.10 kre check 'IFS=". "; set -- a b c; set -- $* ; echo $# "$*"' \
801 1.10 kre '3 a.b.c' 0 #19
802 1.10 kre check 'IFS=". "; set -- a b c; set -- "$*" ; echo $# "$*"' \
803 1.10 kre '1 a.b.c' 0 #20
804 1.10 kre check 'IFS=". "; set -- a "b c"; set -- $* ; echo $# "$*"' \
805 1.10 kre '3 a.b.c' 0 #21
806 1.10 kre check 'IFS=". "; set -- a "b c"; set -- "$*" ; echo $# "$*"' \
807 1.10 kre '1 a.b c' 0 #22
808 1.10 kre
809 1.10 kre results
810 1.10 kre }
811 1.10 kre
812 1.10 kre atf_test_case dollar_star_in_word
813 1.10 kre dollar_star_in_word_head() {
814 1.10 kre atf_set "descr" 'Test expansion $* occurring in word of ${var:-word}'
815 1.10 kre }
816 1.10 kre dollar_star_in_word_body() {
817 1.10 kre
818 1.10 kre reset dollar_star_in_word
819 1.10 kre
820 1.10 kre unset xXx ; # just in case!
821 1.10 kre
822 1.10 kre # Note that the expected results for these tests are identical
823 1.10 kre # to those from the dollar_star test. It should never make
824 1.10 kre # a difference whether we expand $* or ${unset:-$*}
825 1.10 kre
826 1.10 kre # (note expanding ${unset:-"$*"} is different, that is not tested here)
827 1.10 kre
828 1.10 kre check 'set -- a b c; echo $# ${xXx:-$*}' '3 a b c' 0 # 1
829 1.10 kre check 'set -- a b c; echo $# "${xXx:-$*}"' '3 a b c' 0 # 2
830 1.10 kre check 'set -- a "b c"; echo $# ${xXx:-$*}' '2 a b c' 0 # 3
831 1.10 kre check 'set -- a "b c"; echo $# "${xXx:-$*}"' '2 a b c' 0 # 4
832 1.10 kre check 'set -- a b c; set -- ${xXx:-$*} ; echo $# $*' '3 a b c' 0 # 5
833 1.10 kre check 'set -- a b c; set -- "${xXx:-$*}" ; echo $# $*' '1 a b c' 0 # 6
834 1.10 kre check 'set -- a "b c"; set -- ${xXx:-$*} ; echo $# $*' '3 a b c' 0 # 7
835 1.10 kre check 'set -- a "b c"; set -- "${xXx:-$*}" ; echo $# $*' \
836 1.10 kre '1 a b c' 0 # 8
837 1.10 kre
838 1.10 kre check 'IFS=". "; set -- a b c; echo $# ${xXx:-$*}' '3 a b c' 0 # 9
839 1.10 kre check 'IFS=". "; set -- a b c; echo $# "${xXx:-$*}"' '3 a.b.c' 0 #10
840 1.10 kre check 'IFS=". "; set -- a "b c"; echo $# ${xXx:-$*}' '2 a b c' 0 #11
841 1.10 kre check 'IFS=". "; set -- a "b c"; echo $# "${xXx:-$*}"' '2 a.b c' 0 #12
842 1.10 kre check 'IFS=". "; set -- a "b.c"; echo $# ${xXx:-$*}' '2 a b c' 0 #13
843 1.10 kre check 'IFS=". "; set -- a "b.c"; echo $# "${xXx:-$*}"' '2 a.b.c' 0 #14
844 1.10 kre check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
845 1.10 kre '3 a b c' 0 #15
846 1.10 kre check 'IFS=". ";set -- a b c;set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
847 1.10 kre '1 a b c' 0 #16
848 1.10 kre check 'IFS=". ";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
849 1.10 kre '3 a b c' 0 #17
850 1.10 kre check 'IFS=". ";set -- a "b c";set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
851 1.10 kre '1 a b c' 0 #18
852 1.10 kre check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
853 1.10 kre '3 a.b.c' 0 #19
854 1.10 kre check 'IFS=". ";set -- a b c;set -- "$*";echo $# "$*"' \
855 1.10 kre '1 a.b.c' 0 #20
856 1.10 kre check 'IFS=". ";set -- a "b c";set -- $*;echo $# "$*"' \
857 1.10 kre '3 a.b.c' 0 #21
858 1.10 kre check 'IFS=". ";set -- a "b c";set -- "$*";echo $# "$*"' \
859 1.10 kre '1 a.b c' 0 #22
860 1.10 kre
861 1.10 kre results
862 1.10 kre }
863 1.10 kre
864 1.10 kre atf_test_case dollar_star_with_empty_ifs
865 1.10 kre dollar_star_with_empty_ifs_head() {
866 1.10 kre atf_set "descr" 'Test expansion of $* with IFS=""'
867 1.10 kre }
868 1.10 kre dollar_star_with_empty_ifs_body() {
869 1.10 kre
870 1.10 kre reset dollar_star_with_empty_ifs
871 1.10 kre
872 1.10 kre check 'IFS=""; set -- a b c; echo $# $*' '3 a b c' 0 # 1
873 1.10 kre check 'IFS=""; set -- a b c; echo $# "$*"' '3 abc' 0 # 2
874 1.10 kre check 'IFS=""; set -- a "b c"; echo $# $*' '2 a b c' 0 # 3
875 1.10 kre check 'IFS=""; set -- a "b c"; echo $# "$*"' '2 ab c' 0 # 4
876 1.10 kre check 'IFS=""; set -- a "b.c"; echo $# $*' '2 a b.c' 0 # 5
877 1.10 kre check 'IFS=""; set -- a "b.c"; echo $# "$*"' '2 ab.c' 0 # 6
878 1.10 kre check 'IFS=""; set -- a b c; set -- $* ; echo $# $*' \
879 1.10 kre '3 a b c' 0 # 7
880 1.10 kre check 'IFS=""; set -- a b c; set -- "$*" ; echo $# $*' \
881 1.10 kre '1 abc' 0 # 8
882 1.10 kre check 'IFS=""; set -- a "b c"; set -- $* ; echo $# $*' \
883 1.10 kre '2 a b c' 0 # 9
884 1.10 kre check 'IFS=""; set -- a "b c"; set -- "$*" ; echo $# $*' \
885 1.10 kre '1 ab c' 0 #10
886 1.10 kre check 'IFS=""; set -- a b c; set -- $* ; echo $# "$*"' \
887 1.10 kre '3 abc' 0 #11
888 1.10 kre check 'IFS=""; set -- a b c; set -- "$*" ; echo $# "$*"' \
889 1.10 kre '1 abc' 0 #12
890 1.10 kre check 'IFS=""; set -- a "b c"; set -- $* ; echo $# "$*"' \
891 1.10 kre '2 ab c' 0 #13
892 1.10 kre check 'IFS=""; set -- a "b c"; set -- "$*" ; echo $# "$*"' \
893 1.10 kre '1 ab c' 0 #14
894 1.10 kre
895 1.11 kre results # FIXED: 'PR bin/52090 expect 7 of 14 subtests to fail'
896 1.10 kre }
897 1.10 kre
898 1.10 kre atf_test_case dollar_star_in_word_empty_ifs
899 1.10 kre dollar_star_in_word_empty_ifs_head() {
900 1.10 kre atf_set "descr" 'Test expansion of ${unset:-$*} with IFS=""'
901 1.10 kre }
902 1.10 kre dollar_star_in_word_empty_ifs_body() {
903 1.10 kre
904 1.10 kre reset dollar_star_in_word_empty_ifs
905 1.10 kre
906 1.10 kre unset xXx ; # just in case
907 1.10 kre
908 1.10 kre # Note that the expected results for these tests are identical
909 1.10 kre # to those from the dollar_star_with_empty_ifs test. It should
910 1.10 kre # never make a difference whether we expand $* or ${unset:-$*}
911 1.10 kre
912 1.10 kre # (note expanding ${unset:-"$*"} is different, that is not tested here)
913 1.10 kre
914 1.10 kre check 'IFS="";set -- a b c;echo $# ${xXx:-$*}' '3 a b c' 0 # 1
915 1.10 kre check 'IFS="";set -- a b c;echo $# "${xXx:-$*}"' '3 abc' 0 # 2
916 1.10 kre check 'IFS="";set -- a "b c";echo $# ${xXx:-$*}' '2 a b c' 0 # 3
917 1.10 kre check 'IFS="";set -- a "b c";echo $# "${xXx:-$*}"' '2 ab c' 0 # 4
918 1.10 kre check 'IFS="";set -- a "b.c";echo $# ${xXx:-$*}' '2 a b.c' 0 # 5
919 1.10 kre check 'IFS="";set -- a "b.c";echo $# "${xXx:-$*}"' '2 ab.c' 0 # 6
920 1.10 kre check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
921 1.10 kre '3 a b c' 0 # 7
922 1.10 kre check 'IFS="";set -- a b c;set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
923 1.10 kre '1 abc' 0 # 8
924 1.10 kre check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
925 1.10 kre '2 a b c' 0 # 9
926 1.10 kre check 'IFS="";set -- a "b c";set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
927 1.10 kre '1 ab c' 0 #10
928 1.10 kre check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
929 1.10 kre '3 abc' 0 #11
930 1.10 kre check 'IFS="";set -- a b c;set -- "${xXx:-$*}";echo $# "${xXx:-$*}"' \
931 1.10 kre '1 abc' 0 #12
932 1.10 kre check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
933 1.10 kre '2 ab c' 0 #13
934 1.10 kre check 'IFS="";set -- a "b c";set -- "${xXx:-$*}";echo $# "${xXx:-$*}"' \
935 1.10 kre '1 ab c' 0 #14
936 1.10 kre
937 1.11 kre results # FIXED: 'PR bin/52090 expect 7 of 14 subtests to fail'
938 1.10 kre }
939 1.10 kre
940 1.10 kre atf_test_case dollar_star_in_quoted_word
941 1.10 kre dollar_star_in_quoted_word_head() {
942 1.10 kre atf_set "descr" 'Test expansion $* occurring in word of ${var:-"word"}'
943 1.10 kre }
944 1.10 kre dollar_star_in_quoted_word_body() {
945 1.10 kre
946 1.10 kre reset dollar_star_in_quoted_word
947 1.10 kre
948 1.10 kre unset xXx ; # just in case!
949 1.10 kre
950 1.10 kre check 'set -- a b c; echo $# ${xXx:-"$*"}' '3 a b c' 0 # 1
951 1.10 kre check 'set -- a "b c"; echo $# ${xXx:-"$*"}' '2 a b c' 0 # 2
952 1.10 kre check 'set -- a b c; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
953 1.10 kre '1 a b c' 0 # 3
954 1.10 kre check 'set -- a "b c"; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
955 1.10 kre '1 a b c' 0 # 4
956 1.10 kre check 'set -- a b c; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
957 1.10 kre '1 a b c' 0 # 5
958 1.10 kre check 'set -- a "b c"; set -- ${xXx:-"$*"} ; echo $# ${xXx-$*}' \
959 1.10 kre '1 a b c' 0 # 6
960 1.10 kre check 'set -- a b c; set -- ${xXx:-$*} ; echo $# ${xXx-"$*"}' \
961 1.10 kre '3 a b c' 0 # 7
962 1.10 kre check 'set -- a "b c"; set -- ${xXx:-$*} ; echo $# ${xXx-"$*"}' \
963 1.10 kre '3 a b c' 0 # 8
964 1.10 kre
965 1.10 kre check 'IFS=". "; set -- a b c; echo $# ${xXx:-"$*"}' '3 a.b.c' 0 # 9
966 1.10 kre check 'IFS=". "; set -- a "b c"; echo $# ${xXx:-"$*"}' '2 a.b c' 0 #10
967 1.10 kre check 'IFS=". "; set -- a "b.c"; echo $# ${xXx:-"$*"}' '2 a.b.c' 0 #11
968 1.10 kre check 'IFS=". ";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
969 1.10 kre '1 a.b.c' 0 #12
970 1.10 kre check 'IFS=". ";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
971 1.10 kre '1 a.b c' 0 #13
972 1.10 kre check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
973 1.10 kre '3 a.b.c' 0 #14
974 1.10 kre check 'IFS=". ";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
975 1.10 kre '3 a.b.c' 0 #15
976 1.10 kre check 'IFS=". ";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
977 1.10 kre '1 a b c' 0 #16
978 1.10 kre check 'IFS=". ";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
979 1.10 kre '1 a b c' 0 #17
980 1.10 kre
981 1.10 kre check 'IFS="";set -- a b c;echo $# ${xXx:-"$*"}' '3 abc' 0 #18
982 1.10 kre check 'IFS="";set -- a "b c";echo $# ${xXx:-"$*"}' '2 ab c' 0 #19
983 1.10 kre check 'IFS="";set -- a "b.c";echo $# ${xXx:-"$*"}' '2 ab.c' 0 #20
984 1.10 kre check 'IFS="";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
985 1.10 kre '1 abc' 0 #21
986 1.10 kre check 'IFS="";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
987 1.10 kre '1 ab c' 0 #22
988 1.10 kre check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
989 1.10 kre '3 abc' 0 #23
990 1.10 kre check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
991 1.10 kre '2 ab c' 0 #24
992 1.10 kre check 'IFS="";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
993 1.10 kre '1 abc' 0 #25
994 1.10 kre check 'IFS="";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
995 1.10 kre '1 ab c' 0 #26
996 1.10 kre
997 1.11 kre results # FIXED: 'PR bin/52090 - 2 of 26 subtests expected to fail'
998 1.10 kre }
999 1.10 kre
1000 1.1 jruoho atf_init_test_cases() {
1001 1.10 kre # Listed here in the order ATF runs them, not the order from above
1002 1.10 kre
1003 1.10 kre atf_add_test_case arithmetic
1004 1.1 jruoho atf_add_test_case dollar_at
1005 1.1 jruoho atf_add_test_case dollar_at_with_text
1006 1.12 kre atf_add_test_case dollar_hash
1007 1.10 kre atf_add_test_case dollar_star
1008 1.10 kre atf_add_test_case dollar_star_in_quoted_word
1009 1.10 kre atf_add_test_case dollar_star_in_word
1010 1.10 kre atf_add_test_case dollar_star_in_word_empty_ifs
1011 1.10 kre atf_add_test_case dollar_star_with_empty_ifs
1012 1.2 ast atf_add_test_case iteration_on_null_parameter
1013 1.6 christos atf_add_test_case iteration_on_quoted_null_parameter
1014 1.6 christos atf_add_test_case iteration_on_null_or_null_parameter
1015 1.6 christos atf_add_test_case iteration_on_null_or_missing_parameter
1016 1.7 christos atf_add_test_case shell_params
1017 1.10 kre atf_add_test_case strip
1018 1.18 kre atf_add_test_case tilde
1019 1.14 kre atf_add_test_case wrap_strip
1020 1.9 kre atf_add_test_case var_with_embedded_cmdsub
1021 1.10 kre atf_add_test_case varpattern_backslashes
1022 1.1 jruoho }
1023