1 # $NetBSD: t_varquote.sh,v 1.5 2016/03/27 14:50:01 christos Exp $
3 # Copyright (c) 2007 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"}
30 # Variable quoting test.
35 atf_fail "expected [$2], found [$1]" 1>&2
41 atf_set "descr" "Basic checks for variable quoting"
45 cat <<-'EOF' > script.sh
52 printf '%s\n' "T${T}: expected [$2], found [$1]"
59 check "$foo" '${a:-foo}'
67 foo=${a:-${b:-"'{}'"}}
70 # ${ } The ' are inside ".." so are literal (not quotes).
74 # The rules for quoting in ${var-word} expressions are somewhat
75 # weird, in the following there is not one quoted string being
76 # assigned to foo (with internally quoted sub-strings), rather
77 # it is a mixed quoted/unquoted string, with parts that are
78 # quoted, separated by 2 unquoted sections...
79 # qqqqqqqqqq uuuuuuuuuu qq uuuu qqqq
80 foo="${a:-${b:-"${c:-${d:-"x}"}}y}"}}z}"
90 # And believe it or not, this is the one that gives
91 # most problems, with 3 different observed outputs...
92 # qqqqq qq q is one interpretation
93 # qqqqq QQQQ q is another (most common)
94 # (the third is syntax error...)
100 OUT=$( ${TEST_SH} script.sh 2>&1 )
106 atf_fail "script.sh unexpectedly said: ${OUT}"
110 atf_test_case nested_quotes_multiword
111 nested_quotes_multiword_head() {
112 atf_set "descr" "Tests that having nested quoting in a multi-word" \
113 "string works (PR bin/43597)"
115 nested_quotes_multiword_body() {
116 atf_check -s eq:0 -o match:"first-word second-word" -e empty \
117 ${TEST_SH} -c 'echo "${foo:="first-word"} second-word"'
120 atf_test_case default_assignment_with_arith
121 default_assignment_with_arith_head() {
122 atf_set "descr" "Tests default variable assignment with arithmetic" \
123 "string works (PR bin/50827)"
125 default_assignment_with_arith_body() {
126 atf_check -s eq:0 -o empty -e empty ${TEST_SH} -c ': "${x=$((1))}"'
127 atf_check -s eq:0 -o match:1 -e empty ${TEST_SH} -c 'echo "${x=$((1))}"'
130 atf_init_test_cases() {
131 atf_add_test_case all
132 atf_add_test_case nested_quotes_multiword
133 atf_add_test_case default_assignment_with_arith