xref: /minix3/tests/bin/sh/t_expand.sh (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc# $NetBSD: t_expand.sh,v 1.2 2013/10/06 21:05:50 ast Exp $
211be35a1SLionel Sambuc#
311be35a1SLionel Sambuc# Copyright (c) 2007, 2009 The NetBSD Foundation, Inc.
411be35a1SLionel Sambuc# All rights reserved.
511be35a1SLionel Sambuc#
611be35a1SLionel Sambuc# Redistribution and use in source and binary forms, with or without
711be35a1SLionel Sambuc# modification, are permitted provided that the following conditions
811be35a1SLionel Sambuc# are met:
911be35a1SLionel Sambuc# 1. Redistributions of source code must retain the above copyright
1011be35a1SLionel Sambuc#    notice, this list of conditions and the following disclaimer.
1111be35a1SLionel Sambuc# 2. Redistributions in binary form must reproduce the above copyright
1211be35a1SLionel Sambuc#    notice, this list of conditions and the following disclaimer in the
1311be35a1SLionel Sambuc#    documentation and/or other materials provided with the distribution.
1411be35a1SLionel Sambuc#
1511be35a1SLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1611be35a1SLionel Sambuc# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1711be35a1SLionel Sambuc# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1811be35a1SLionel Sambuc# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1911be35a1SLionel Sambuc# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2011be35a1SLionel Sambuc# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2111be35a1SLionel Sambuc# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2211be35a1SLionel Sambuc# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2311be35a1SLionel Sambuc# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2411be35a1SLionel Sambuc# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2511be35a1SLionel Sambuc# POSSIBILITY OF SUCH DAMAGE.
2611be35a1SLionel Sambuc#
2711be35a1SLionel Sambuc
2811be35a1SLionel Sambuc#
2911be35a1SLionel Sambuc# This file tests the functions in expand.c.
3011be35a1SLionel Sambuc#
3111be35a1SLionel Sambuc
3211be35a1SLionel Sambucdelim_argv() {
3311be35a1SLionel Sambuc	str=
3411be35a1SLionel Sambuc	while [ $# -gt 0 ]; do
3511be35a1SLionel Sambuc		if [ -z "${str}" ]; then
3611be35a1SLionel Sambuc			str=">$1<"
3711be35a1SLionel Sambuc		else
3811be35a1SLionel Sambuc			str="${str} >$1<"
3911be35a1SLionel Sambuc		fi
4011be35a1SLionel Sambuc		shift
4111be35a1SLionel Sambuc	done
4211be35a1SLionel Sambuc	echo ${str}
4311be35a1SLionel Sambuc}
4411be35a1SLionel Sambuc
4511be35a1SLionel Sambucatf_test_case dollar_at
4611be35a1SLionel Sambucdollar_at_head() {
4711be35a1SLionel Sambuc	atf_set "descr" "Somewhere between 2.0.2 and 3.0 the expansion" \
4811be35a1SLionel Sambuc	                "of the \$@ variable had been broken.  Check for" \
4911be35a1SLionel Sambuc			"this behavior."
5011be35a1SLionel Sambuc}
5111be35a1SLionel Sambucdollar_at_body() {
5211be35a1SLionel Sambuc	# This one should work everywhere.
5311be35a1SLionel Sambuc	got=`echo "" "" | sed 's,$,EOL,'`
5411be35a1SLionel Sambuc	atf_check_equal ' EOL' '$got'
5511be35a1SLionel Sambuc
5611be35a1SLionel Sambuc	# This code triggered the bug.
5711be35a1SLionel Sambuc	set -- "" ""
5811be35a1SLionel Sambuc	got=`echo "$@" | sed 's,$,EOL,'`
5911be35a1SLionel Sambuc	atf_check_equal ' EOL' '$got'
6011be35a1SLionel Sambuc
6111be35a1SLionel Sambuc	set -- -
6211be35a1SLionel Sambuc	shift
6311be35a1SLionel Sambuc	n_arg() { echo $#; }
6411be35a1SLionel Sambuc	n_args=`n_arg "$@"`
6511be35a1SLionel Sambuc	atf_check_equal '0' '$n_args'
6611be35a1SLionel Sambuc}
6711be35a1SLionel Sambuc
6811be35a1SLionel Sambucatf_test_case dollar_at_with_text
6911be35a1SLionel Sambucdollar_at_with_text_head() {
7011be35a1SLionel Sambuc	atf_set "descr" "Test \$@ expansion when it is surrounded by text" \
7111be35a1SLionel Sambuc	                "within the quotes.  PR bin/33956."
7211be35a1SLionel Sambuc}
7311be35a1SLionel Sambucdollar_at_with_text_body() {
7411be35a1SLionel Sambuc	set --
7511be35a1SLionel Sambuc	atf_check_equal '' "$(delim_argv "$@")"
7611be35a1SLionel Sambuc	atf_check_equal '>foobar<' "$(delim_argv "foo$@bar")"
7711be35a1SLionel Sambuc	atf_check_equal '>foo  bar<' "$(delim_argv "foo $@ bar")"
7811be35a1SLionel Sambuc
7911be35a1SLionel Sambuc	set -- a b c
8011be35a1SLionel Sambuc	atf_check_equal '>a< >b< >c<' "$(delim_argv "$@")"
8111be35a1SLionel Sambuc	atf_check_equal '>fooa< >b< >cbar<' "$(delim_argv "foo$@bar")"
8211be35a1SLionel Sambuc	atf_check_equal '>foo a< >b< >c bar<' "$(delim_argv "foo $@ bar")"
8311be35a1SLionel Sambuc}
8411be35a1SLionel Sambuc
8511be35a1SLionel Sambucatf_test_case strip
8611be35a1SLionel Sambucstrip_head() {
8711be35a1SLionel Sambuc	atf_set "descr" "Checks that the %% operator works and strips" \
8811be35a1SLionel Sambuc	                "the contents of a variable from the given point" \
8911be35a1SLionel Sambuc			"to the end"
9011be35a1SLionel Sambuc}
9111be35a1SLionel Sambucstrip_body() {
9211be35a1SLionel Sambuc	line='#define bindir "/usr/bin" /* comment */'
9311be35a1SLionel Sambuc	stripped='#define bindir "/usr/bin" '
9411be35a1SLionel Sambuc	atf_expect_fail "PR bin/43469"
9511be35a1SLionel Sambuc	atf_check_equal '$stripped' '${line%%/\**}'
9611be35a1SLionel Sambuc}
9711be35a1SLionel Sambuc
9811be35a1SLionel Sambucatf_test_case varpattern_backslashes
9911be35a1SLionel Sambucvarpattern_backslashes_head() {
10011be35a1SLionel Sambuc	atf_set "descr" "Tests that protecting wildcards with backslashes" \
10111be35a1SLionel Sambuc	                "works in variable patterns."
10211be35a1SLionel Sambuc}
10311be35a1SLionel Sambucvarpattern_backslashes_body() {
10411be35a1SLionel Sambuc	line='/foo/bar/*/baz'
10511be35a1SLionel Sambuc	stripped='/foo/bar/'
10611be35a1SLionel Sambuc	atf_check_equal $stripped ${line%%\**}
10711be35a1SLionel Sambuc}
10811be35a1SLionel Sambuc
10911be35a1SLionel Sambucatf_test_case arithmetic
11011be35a1SLionel Sambucarithmetic_head() {
11111be35a1SLionel Sambuc	atf_set "descr" "POSIX requires shell arithmetic to use signed" \
11211be35a1SLionel Sambuc	                "long or a wider type.  We use intmax_t, so at" \
11311be35a1SLionel Sambuc			"least 64 bits should be available.  Make sure" \
11411be35a1SLionel Sambuc			"this is true."
11511be35a1SLionel Sambuc}
11611be35a1SLionel Sambucarithmetic_body() {
11711be35a1SLionel Sambuc	atf_check_equal '3' '$((1 + 2))'
11811be35a1SLionel Sambuc	atf_check_equal '2147483647' '$((0x7fffffff))'
11911be35a1SLionel Sambuc	atf_check_equal '9223372036854775807' '$(((1 << 63) - 1))'
12011be35a1SLionel Sambuc}
12111be35a1SLionel Sambuc
122*84d9c625SLionel Sambucatf_test_case iteration_on_null_parameter
123*84d9c625SLionel Sambuciteration_on_null_parameter_head() {
124*84d9c625SLionel Sambuc	atf_set "descr" "Check iteration of \$@ in for loop when set to null;" \
125*84d9c625SLionel Sambuc	                "the error \"sh: @: parameter not set\" is incorrect." \
126*84d9c625SLionel Sambuc	                "PR bin/48202."
127*84d9c625SLionel Sambuc}
128*84d9c625SLionel Sambuciteration_on_null_parameter_body() {
129*84d9c625SLionel Sambuc	s1=`/bin/sh -uc 'N=; set -- ${N};   for X; do echo "[$X]"; done' 2>&1`
130*84d9c625SLionel Sambuc	s2=`/bin/sh -uc 'N=; set -- ${N:-}; for X; do echo "[$X]"; done' 2>&1`
131*84d9c625SLionel Sambuc	atf_check_equal ''   '$s1'
132*84d9c625SLionel Sambuc	atf_check_equal '[]' '$s2'
133*84d9c625SLionel Sambuc}
134*84d9c625SLionel Sambuc
13511be35a1SLionel Sambucatf_init_test_cases() {
13611be35a1SLionel Sambuc	atf_add_test_case dollar_at
13711be35a1SLionel Sambuc	atf_add_test_case dollar_at_with_text
13811be35a1SLionel Sambuc	atf_add_test_case strip
13911be35a1SLionel Sambuc	atf_add_test_case varpattern_backslashes
14011be35a1SLionel Sambuc	atf_add_test_case arithmetic
141*84d9c625SLionel Sambuc	atf_add_test_case iteration_on_null_parameter
14211be35a1SLionel Sambuc}
143