summaryrefslogtreecommitdiff
path: root/tests/posix2.tests
blob: 5eea7e0e19617aebea4c1a26f8af3ce8b1d3eb58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#! /bin/sh
# posix-2.sh - Simple identification tests for POSIX.2 features
# commonly missing or incorrectly implemented.
# Time-stamp: <96/04/10 16:43:48 gildea>
# By Stephen Gildea <gildea@x.org> March 1995
#
# Copyright (c) 1995  Stephen Gildea
# Permission is hereby granted to deal in this Software without restriction.
# THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
#
# MODIFIED BY chet@po.cwru.edu to make part of the bash test suite.
# last change: Wed Jun 19 12:24:24 EDT 1996
#
# some of the tests:
#
# shell functions (do we care?)
# var=${var:-val}
# unset
# set --
# IFS parsing
## not exiting with -e and failed "if", the way Ultrix does (Ultrix 4.2?)
# "$@" expands to zero arguments if passed zero arguments
# $SHELL -c 'echo $1' bad good
# test -x
# positional parameters greater than 9
# arithmetic expansion $(( ... ))
# getopts

# For some tests we must run a sub-shell; $TESTSHELL says what to use.
# If set, TESTSHELL must be an absolute pathname.
# For example, on HP-UX 9, /bin/posix/sh is the supposedly-compliant shell.
TESTSHELL=${THIS_SH:-$PWD/../bash}

# these tests create temp files with names $TMPDIR/conf*
: ${TMPDIR:=/tmp}

exitval=0
numtests=0

echo "Testing for POSIX.2 conformance"

newtest()
{
    numtests=$(($numtests + 1))
}

testfail()
{
    echo "$1 test failed"
    exitval=$(($exitval + 1))
}

newtest
empty=""
test "${empty:-ok}" = ok || testfail "empty var colon"
newtest
test "${empty-bad}" = "" || testfail "got \"${empty-bad}\": empty var nocolon"
newtest
test "${unsetvar-ok}" = ok || testfail "unset var"
newtest
unset empty
test "${empty-ok}" = ok || testfail "unset"

newtest
set -- -Z
test "x$1" = x-Z || testfail '\"set -- arg\"'
# this should empty the argument list
newtest
set --
test $# = 0 || testfail "still $# args: \"set --\""

# IFS parsing:
newtest
names=one/good/three
saved_ifs="$IFS"
IFS=/
set $names lose
test "$2" = good || testfail "got \"$2\": IFS parsing"
IFS="$saved_ifs"

# "$@" with 0 arguments should expand to 0 arguments
newtest
cat > $TMPDIR/conftest1 << EOF
$TMPDIR/conftest2 "\$@"
EOF
cat > $TMPDIR/conftest2 << "EOF"
#! /bin/sh
echo $#
EOF
chmod +x $TMPDIR/conftest1 $TMPDIR/conftest2
numargs=$($TESTSHELL $TMPDIR/conftest1)
if [ "$?" != 0 ]; then
    testfail 'running $@'
else
    test "$numargs" = 0 || testfail '"$@" got '"$numargs args: expansion w 0 args"
fi
rm -f $TMPDIR/conftest1 $TMPDIR/conftest2

newtest
val=$("$TESTSHELL" -c 'echo $1' csh good)
test "$val" = good || testfail "got \"$val\": sh -c"

newtest
# do these tests in a sub-shell because failure will exit
val=$("$TESTSHELL" -c 'echo ${10}' 0  1 2 3 4 5 6 7 8 9 ten 11 2> /dev/null)
test "$val" = ten || testfail "accessing more than 9 positional params"

a=abc_def_ghi
export a
newtest; val=`"$TESTSHELL" -c 'echo "${a%_*}"' 2> /dev/null`
test "$val" = abc_def || testfail "parameter % op"
newtest; val=`"$TESTSHELL" -c 'echo "${a%%_*}"' 2> /dev/null`
test "$val" = abc || testfail "parameter %% op"
newtest; val=`"$TESTSHELL" -c 'echo "${a#*_}"' 2> /dev/null`
test "$val" = def_ghi || testfail "parameter # op"
newtest; val=`"$TESTSHELL" -c 'echo "${a##*_}"' 2> /dev/null`
test "$val" = ghi || testfail "parameter ## op"

newtest
"$TESTSHELL" -c 'export a=value' 2> /dev/null || testfail "export with value"

newtest
a=5; test "$(( ($a+1)/2 ))" = 3 || testfail "arithmetic expansion"

# does "test" support the -x switch?
newtest
touch $TMPDIR/conftest
chmod -x $TMPDIR/conftest
test -x $TMPDIR/conftest && testfail "negative test -x"
chmod +x $TMPDIR/conftest
test -x $TMPDIR/conftest || testfail "positive test -x"
rm -f $TMPDIR/conftest

newtest
test "$OPTIND" = 1 || testfail "OPTIND initial value"

newtest
getopts a: store -a aoptval
if [ "$OPTIND" != 3 ] || [ "$store" != a ] || [ "$OPTARG" != aoptval ]; then
    testfail "getopts"
fi

if [ $exitval = 0 ]; then
    echo "All tests passed"
else
    echo "$exitval of $numtests tests failed"
fi
exit $exitval