t_expand.sh revision 1.1.2.3 1 1.1.2.3 yamt # $NetBSD: t_expand.sh,v 1.1.2.3 2014/05/22 11:42:17 yamt Exp $
2 1.1.2.2 yamt #
3 1.1.2.2 yamt # Copyright (c) 2007, 2009 The NetBSD Foundation, Inc.
4 1.1.2.2 yamt # All rights reserved.
5 1.1.2.2 yamt #
6 1.1.2.2 yamt # Redistribution and use in source and binary forms, with or without
7 1.1.2.2 yamt # modification, are permitted provided that the following conditions
8 1.1.2.2 yamt # are met:
9 1.1.2.2 yamt # 1. Redistributions of source code must retain the above copyright
10 1.1.2.2 yamt # notice, this list of conditions and the following disclaimer.
11 1.1.2.2 yamt # 2. Redistributions in binary form must reproduce the above copyright
12 1.1.2.2 yamt # notice, this list of conditions and the following disclaimer in the
13 1.1.2.2 yamt # documentation and/or other materials provided with the distribution.
14 1.1.2.2 yamt #
15 1.1.2.2 yamt # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1.2.2 yamt # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1.2.2 yamt # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1.2.2 yamt # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1.2.2 yamt # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1.2.2 yamt # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1.2.2 yamt # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1.2.2 yamt # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1.2.2 yamt # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1.2.2 yamt # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1.2.2 yamt # POSSIBILITY OF SUCH DAMAGE.
26 1.1.2.2 yamt #
27 1.1.2.2 yamt
28 1.1.2.2 yamt #
29 1.1.2.2 yamt # This file tests the functions in expand.c.
30 1.1.2.2 yamt #
31 1.1.2.2 yamt
32 1.1.2.2 yamt delim_argv() {
33 1.1.2.2 yamt str=
34 1.1.2.2 yamt while [ $# -gt 0 ]; do
35 1.1.2.2 yamt if [ -z "${str}" ]; then
36 1.1.2.2 yamt str=">$1<"
37 1.1.2.2 yamt else
38 1.1.2.2 yamt str="${str} >$1<"
39 1.1.2.2 yamt fi
40 1.1.2.3 yamt shift
41 1.1.2.2 yamt done
42 1.1.2.2 yamt echo ${str}
43 1.1.2.2 yamt }
44 1.1.2.2 yamt
45 1.1.2.2 yamt atf_test_case dollar_at
46 1.1.2.2 yamt dollar_at_head() {
47 1.1.2.2 yamt atf_set "descr" "Somewhere between 2.0.2 and 3.0 the expansion" \
48 1.1.2.2 yamt "of the \$@ variable had been broken. Check for" \
49 1.1.2.2 yamt "this behavior."
50 1.1.2.2 yamt }
51 1.1.2.2 yamt dollar_at_body() {
52 1.1.2.2 yamt # This one should work everywhere.
53 1.1.2.2 yamt got=`echo "" "" | sed 's,$,EOL,'`
54 1.1.2.2 yamt atf_check_equal ' EOL' '$got'
55 1.1.2.2 yamt
56 1.1.2.2 yamt # This code triggered the bug.
57 1.1.2.2 yamt set -- "" ""
58 1.1.2.2 yamt got=`echo "$@" | sed 's,$,EOL,'`
59 1.1.2.2 yamt atf_check_equal ' EOL' '$got'
60 1.1.2.2 yamt
61 1.1.2.2 yamt set -- -
62 1.1.2.2 yamt shift
63 1.1.2.2 yamt n_arg() { echo $#; }
64 1.1.2.2 yamt n_args=`n_arg "$@"`
65 1.1.2.2 yamt atf_check_equal '0' '$n_args'
66 1.1.2.2 yamt }
67 1.1.2.2 yamt
68 1.1.2.2 yamt atf_test_case dollar_at_with_text
69 1.1.2.2 yamt dollar_at_with_text_head() {
70 1.1.2.2 yamt atf_set "descr" "Test \$@ expansion when it is surrounded by text" \
71 1.1.2.2 yamt "within the quotes. PR bin/33956."
72 1.1.2.2 yamt }
73 1.1.2.2 yamt dollar_at_with_text_body() {
74 1.1.2.2 yamt set --
75 1.1.2.2 yamt atf_check_equal '' "$(delim_argv "$@")"
76 1.1.2.2 yamt atf_check_equal '>foobar<' "$(delim_argv "foo$@bar")"
77 1.1.2.2 yamt atf_check_equal '>foo bar<' "$(delim_argv "foo $@ bar")"
78 1.1.2.2 yamt
79 1.1.2.2 yamt set -- a b c
80 1.1.2.2 yamt atf_check_equal '>a< >b< >c<' "$(delim_argv "$@")"
81 1.1.2.2 yamt atf_check_equal '>fooa< >b< >cbar<' "$(delim_argv "foo$@bar")"
82 1.1.2.2 yamt atf_check_equal '>foo a< >b< >c bar<' "$(delim_argv "foo $@ bar")"
83 1.1.2.2 yamt }
84 1.1.2.2 yamt
85 1.1.2.2 yamt atf_test_case strip
86 1.1.2.2 yamt strip_head() {
87 1.1.2.2 yamt atf_set "descr" "Checks that the %% operator works and strips" \
88 1.1.2.2 yamt "the contents of a variable from the given point" \
89 1.1.2.2 yamt "to the end"
90 1.1.2.2 yamt }
91 1.1.2.2 yamt strip_body() {
92 1.1.2.2 yamt line='#define bindir "/usr/bin" /* comment */'
93 1.1.2.2 yamt stripped='#define bindir "/usr/bin" '
94 1.1.2.2 yamt atf_expect_fail "PR bin/43469"
95 1.1.2.2 yamt atf_check_equal '$stripped' '${line%%/\**}'
96 1.1.2.2 yamt }
97 1.1.2.2 yamt
98 1.1.2.2 yamt atf_test_case varpattern_backslashes
99 1.1.2.2 yamt varpattern_backslashes_head() {
100 1.1.2.2 yamt atf_set "descr" "Tests that protecting wildcards with backslashes" \
101 1.1.2.2 yamt "works in variable patterns."
102 1.1.2.2 yamt }
103 1.1.2.2 yamt varpattern_backslashes_body() {
104 1.1.2.2 yamt line='/foo/bar/*/baz'
105 1.1.2.2 yamt stripped='/foo/bar/'
106 1.1.2.2 yamt atf_check_equal $stripped ${line%%\**}
107 1.1.2.2 yamt }
108 1.1.2.2 yamt
109 1.1.2.2 yamt atf_test_case arithmetic
110 1.1.2.2 yamt arithmetic_head() {
111 1.1.2.2 yamt atf_set "descr" "POSIX requires shell arithmetic to use signed" \
112 1.1.2.2 yamt "long or a wider type. We use intmax_t, so at" \
113 1.1.2.2 yamt "least 64 bits should be available. Make sure" \
114 1.1.2.2 yamt "this is true."
115 1.1.2.2 yamt }
116 1.1.2.2 yamt arithmetic_body() {
117 1.1.2.2 yamt atf_check_equal '3' '$((1 + 2))'
118 1.1.2.2 yamt atf_check_equal '2147483647' '$((0x7fffffff))'
119 1.1.2.2 yamt atf_check_equal '9223372036854775807' '$(((1 << 63) - 1))'
120 1.1.2.2 yamt }
121 1.1.2.2 yamt
122 1.1.2.3 yamt atf_test_case iteration_on_null_parameter
123 1.1.2.3 yamt iteration_on_null_parameter_head() {
124 1.1.2.3 yamt atf_set "descr" "Check iteration of \$@ in for loop when set to null;" \
125 1.1.2.3 yamt "the error \"sh: @: parameter not set\" is incorrect." \
126 1.1.2.3 yamt "PR bin/48202."
127 1.1.2.3 yamt }
128 1.1.2.3 yamt iteration_on_null_parameter_body() {
129 1.1.2.3 yamt s1=`/bin/sh -uc 'N=; set -- ${N}; for X; do echo "[$X]"; done' 2>&1`
130 1.1.2.3 yamt s2=`/bin/sh -uc 'N=; set -- ${N:-}; for X; do echo "[$X]"; done' 2>&1`
131 1.1.2.3 yamt atf_check_equal '' '$s1'
132 1.1.2.3 yamt atf_check_equal '[]' '$s2'
133 1.1.2.3 yamt }
134 1.1.2.3 yamt
135 1.1.2.2 yamt atf_init_test_cases() {
136 1.1.2.2 yamt atf_add_test_case dollar_at
137 1.1.2.2 yamt atf_add_test_case dollar_at_with_text
138 1.1.2.2 yamt atf_add_test_case strip
139 1.1.2.2 yamt atf_add_test_case varpattern_backslashes
140 1.1.2.2 yamt atf_add_test_case arithmetic
141 1.1.2.3 yamt atf_add_test_case iteration_on_null_parameter
142 1.1.2.2 yamt }
143