Thu Mar 31 16:21:10 2016 UTC ()
PR bin/51027 - tests for shell positional parameters, including
testing that $10 is correctly parsed as ${1}0 and not as ${10}.
More than that though. (from kre@)


(christos)
diff -r1.6 -r1.7 src/tests/bin/sh/t_expand.sh

cvs diff -r1.6 -r1.7 src/tests/bin/sh/t_expand.sh (expand / switch to unified diff)

--- src/tests/bin/sh/t_expand.sh 2016/03/08 14:26:54 1.6
+++ src/tests/bin/sh/t_expand.sh 2016/03/31 16:21:10 1.7
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: t_expand.sh,v 1.6 2016/03/08 14:26:54 christos Exp $ 1# $NetBSD: t_expand.sh,v 1.7 2016/03/31 16:21:10 christos Exp $
2# 2#
3# Copyright (c) 2007, 2009 The NetBSD Foundation, Inc. 3# Copyright (c) 2007, 2009 The NetBSD Foundation, Inc.
4# All rights reserved. 4# All rights reserved.
5# 5#
6# Redistribution and use in source and binary forms, with or without 6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions 7# modification, are permitted provided that the following conditions
8# are met: 8# are met:
9# 1. Redistributions of source code must retain the above copyright 9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer. 10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright 11# 2. Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the 12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution. 13# documentation and/or other materials provided with the distribution.
14# 14#
@@ -217,24 +217,164 @@ iteration_on_null_or_null_parameter_body @@ -217,24 +217,164 @@ iteration_on_null_or_null_parameter_body
217} 217}
218 218
219atf_test_case iteration_on_null_or_missing_parameter 219atf_test_case iteration_on_null_or_missing_parameter
220iteration_on_null_or_missing_parameter_head() { 220iteration_on_null_or_missing_parameter_head() {
221 atf_set "descr" \ 221 atf_set "descr" \
222 'Check expansion of missing parameter as default for another null' 222 'Check expansion of missing parameter as default for another null'
223} 223}
224iteration_on_null_or_missing_parameter_body() { 224iteration_on_null_or_missing_parameter_body() {
225 # atf_expect_fail 'PR bin/50834' 225 # atf_expect_fail 'PR bin/50834'
226 atf_check -o empty -e empty ${TEST_SH} -c \ 226 atf_check -o empty -e empty ${TEST_SH} -c \
227 'N=; set -- ${N:-}; for X; do echo "[$X]"; done' 227 'N=; set -- ${N:-}; for X; do echo "[$X]"; done'
228} 228}
229 229
 230nl='
 231'
 232reset()
 233{
 234 TEST_NUM=0
 235 TEST_FAILURES=''
 236 TEST_FAIL_COUNT=0
 237 TEST_ID="$1"
 238}
 239
 240check()
 241{
 242 fail=false
 243 TEMP_FILE=$( mktemp OUT.XXXXXX )
 244 TEST_NUM=$(( $TEST_NUM + 1 ))
 245 MSG=
 246
 247 # our local shell (ATF_SHELL) better do quoting correctly...
 248 # some of the tests expect us to expand $nl internally...
 249 CMD="$1"
 250
 251 result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )"
 252 STATUS=$?
 253
 254 if [ "${STATUS}" -ne "$3" ]; then
 255 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
 256 MSG="${MSG} expected exit code $3, got ${STATUS}"
 257
 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;;
 262 esac
 263 fi
 264
 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}" )"
 270 fail=true
 271 fi
 272 else
 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"
 277 fail=true
 278 fi
 279 fi
 280 rm -f "${TEMP_FILE}"
 281
 282 # Remove newlines (use local shell for this)
 283 oifs="$IFS"
 284 IFS="$nl"
 285 result="$(echo $result)"
 286 IFS="$oifs"
 287 if [ "$2" != "$result" ]
 288 then
 289 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
 290 MSG="${MSG} Expected output '$2', received '$result'"
 291 fail=true
 292 fi
 293
 294 if $fail
 295 then
 296 MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
 297 MSG="${MSG} Full command: <<${CMD}>>"
 298 fi
 299
 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 ))
 306 return 0
 307 }
 308 $fail && atf_fail "Test[$TEST_NUM] of '$1' failed${nl}${MSG}"
 309 return 0
 310}
 311
 312results()
 313{
 314 test -z "${TEST_ID}" && return 0
 315 test -z "${TEST_FAILURES}" && return 0
 316
 317 echo >&2 "=========================================="
 318 echo >&2 "While testing '${TEST_ID}'"
 319 echo >&2 " - - - - - - - - - - - - - - - - -"
 320 echo >&2 "${TEST_FAILURES}"
 321 atf_fail \
 322 "Test ${TEST_ID}: $TEST_FAIL_COUNT subtests (of $TEST_NUM) failed - see stderr"
 323}
 324
 325atf_test_case shell_params
 326shell_params_head() {
 327 atf_set "descr" "Test correct operation of the numeric parameters"
 328}
 329shell_params_body() {
 330 atf_require_prog mktemp
 331
 332 reset shell_params
 333
 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"' \
 336 '13: a0 j a0' 0
 337 check 'x="$0"; set -- a b; y="$0";
 338 [ "x${x}y" = "x${y}y" ] && echo OK || echo x="$x" y="$y"' \
 339 'OK' 0
 340 check "${TEST_SH} -c 'echo 0=\$0 1=\$1 2=\$2' a b c" '0=a 1=b 2=c' 0
 341
 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
 344
 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
 349
 350 check 'set -- a b c; echo "$#: ${1-A} ${2-B} ${3-C} ${4-D} ${5-E}"' \
 351 '3: a b c D E' 0
 352 check 'set -- a "" c "" e
 353 echo "$#: ${1:-A} ${2:-B} ${3:-C} ${4:-D} ${5:-E}"' \
 354 '5: a B c D e' 0
 355 check 'set -- a "" c "" e
 356 echo "$#: ${1:+A} ${2:+B} ${3:+C} ${4:+D} ${5:+E}"' \
 357 '5: A C E' 0
 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
 365
 366 results
 367}
 368
230atf_init_test_cases() { 369atf_init_test_cases() {
231 atf_add_test_case dollar_at 370 atf_add_test_case dollar_at
232 atf_add_test_case dollar_at_with_text 371 atf_add_test_case dollar_at_with_text
233 atf_add_test_case strip 372 atf_add_test_case strip
234 atf_add_test_case varpattern_backslashes 373 atf_add_test_case varpattern_backslashes
235 atf_add_test_case arithmetic 374 atf_add_test_case arithmetic
236 atf_add_test_case iteration_on_null_parameter 375 atf_add_test_case iteration_on_null_parameter
237 atf_add_test_case iteration_on_quoted_null_parameter 376 atf_add_test_case iteration_on_quoted_null_parameter
238 atf_add_test_case iteration_on_null_or_null_parameter 377 atf_add_test_case iteration_on_null_or_null_parameter
239 atf_add_test_case iteration_on_null_or_missing_parameter 378 atf_add_test_case iteration_on_null_or_missing_parameter
 379 atf_add_test_case shell_params
240} 380}