aboutsummaryrefslogtreecommitdiff
path: root/tests/sh.test
blob: 0759398d875ba90607bdae3b13f7027f24d98d1d (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/bin/echo no

# Testing shell corner cases _within_ a shell script is kind of hard.

[ -f testing.sh ] && . testing.sh

# TODO make fake pty wrapper for test infrastructure

#testing "name" "command" "result" "infile" "stdin"

# texpect "name" "command" E/O/I"string"

# Use "bash" name for host, "sh" for toybox
[ -z "$SH" ] && { [ -z "$TEST_HOST" ] && SH="sh" || export SH="bash" ; }
# Prompt changes for root/normal user
[ $(id -u) -eq 0 ] && P='# ' || P='$ '
# run sufficiently isolated shell child process to get predictable results
SS="env -i PATH=${PATH@Q} PS1='\\$ ' $SH --noediting --noprofile --norc -is"

shxpect() {
  X="$1"
  shift
  txpect "$X" "$SS" E"$P" "$@" X0
}

shxpect "prompt and exit" I$'exit\n'
shxpect "prompt and echo" I$'echo hello\n' O$'hello\n' E"$P"
shxpect "redirect err" I$'echo > /dev/full\n' E E"$P"
shxpect "wait for <(exit)" I$'cat <(echo hello 1>&2)\n' E$'hello\n' E"$P"

# Test the sh -c stuff before changing EVAL
testing '-c "" exit status 0' '$SH -c "" && echo $?' '0\n' '' ''
testing '-c args' "\$SH -c 'echo \$0,\$1,\$2,\$3' one two three four five" \
  "one,two,three,four\n" "" ""
testing '-c args2' "\$SH -c 'echo \${10}' a b c d e f g h i j k l" "k\n" "" ""
testing '-c arg split' \
  "$SH -c 'for i in a\"\$@\"b;do echo =\$i=;done;echo \$0' 123 456 789" \
  "=a456=\n=789b=\n123\n" "" ""
testing '-c arg count' "$SH -c 'echo \$#' 9 8 7 6 1 2 3 4" "7\n" "" ""
testing "exec3" '$C -c "{ exec readlink /proc/self/fd/0;} < /proc/self/exe"' \
  "$(readlink -f $C)\n" "" ""
testing 'arg shift' "$SH -c '"'for i in "" 2 1 1 1; do echo $? $1; shift $i; done'"' one two three four five" \
  "0 two\n0 three\n0 five\n0\n1\n" "" ""

# The bash man page is lying when it says $_ starts with an absolute path.
ln -s $(which $SH) bash
testing 'non-absolute $_' "./bash -c 'echo \$_'" './bash\n' '' ''
rm bash

testing 'exec exitval' "$SH -c 'exec echo hello' && echo \$?" "hello\n0\n" "" ""
testing 'simple script' '$SH input' 'input\n' 'echo $0' ''
testing 'simple script2' '$SH ./input two;echo $?' './input+two\n42\n' \
  '\necho $0+$1\n\nexit 42' ''

mkdir sub
echo echo hello > sub/script
testing 'simple script in $PATH' "PATH='$PWD/sub:$PATH' $SH script" \
  'hello\n' '' ''
rm -rf sub

testing "script file" "chmod +x input; ./input" "hello\n" "#!$C\necho hello" ""

testing 'IFS $*' "$SH -c 'IFS=xy; echo \"\$*\"' one two tyree" "twoxtyree\n" \
  "" ""

testing 'default exports' \
  "env -i \"$(which $SH)\" --noprofile --norc -c env | sort" \
  "PWD=$(pwd)\nSHLVL=1\n_=$(which env)\n" "" ""

# Change EVAL to call sh -c for us, using "bash" explicitly for the host.
export EVAL="$SH -c"

testing "smoketest" "echo hello" "hello\n" "" ""
testing "eval" "eval echo hello" "hello\n" "" ""
testing "eval2" "eval 'echo hello'; echo $?" "hello\n0\n" "" ""
testing "eval3" 'X="echo hello"; eval "$X"' "hello\n" "" ""
testing "eval4" 'eval printf '=%s=' \" hello \"' "= hello =" "" ""
NOSPACE=1 testing "eval5" 'eval echo \" hello \" | wc' ' 1 1 8' "" ""
testing "exec" "exec echo hello" "hello\n" "" ""
testing "exec2" "exec echo hello; echo $?" "hello\n" "" "" 

# ; | && ||
testing "semicolon" "echo one;echo two" "one\ntwo\n" "" ""
testing "simple pipe" "echo hello | cat" "hello\n" "" ""
testing "&&" "true && echo hello" "hello\n" "" ""
testing "&&2" "false && echo hello" "" "" ""
testing "||" "true || echo hello" "" "" ""
testing "||2" "false || echo hello" "hello\n" "" ""
testing "&& ||" "true && false && potato || echo hello" "hello\n" "" ""

# redirection

testing "redir1" "cat < input" "hello\n" "hello\n" ""
testing "redir2" "echo blah >out; cat out" "blah\n" "" ""
testing "redir3" "echo more >>out; cat out" "blah\nmore\n" "" ""
testing "redir4" "touch /not/exist 2>out||grep -o /not/exist out" \
  "/not/exist\n" "" ""
testing "redir5" "ls out /not/exist &> out2 || wc -l < out2" "2\n" "" ""
testing "redir6" "ls out /not/exist &>>-abc || wc -l < ./-abc" "2\n" "" ""
testing "redir7" "ls out /not/exist |& wc -l" "2\n" "" ""
testing "redir8" 'echo -n $(<input)' "boing" "boing\n" ""
shxpect "redir9" I$'echo hello > out 2>/does/not/exist\n' E E"$P" \
  I$'wc -l < out\n' O$'0\n'
testing "redir10" 'echo hello 3<&3' "hello\n" "" ""
testing "redir11" 'if :;then echo one;fi {abc}<input; cat <&$abc' \
  "one\npotato\n" "potato\n" ""
rm -f out out2 ./-abc

# expansion

testing "tilde expansion" "echo ~" "$HOME\n" "" ""
testing "tilde2" "echo ~/dir" "$HOME/dir\n" "" ""
testing "bracket expansion" \
  "echo {A{a,b}B{c,d}C}" "{AaBcC} {AaBdC} {AbBcC} {AbBdC}\n" "" ""
testing "brackets2" "echo {A{a,b}B{c,d}C,D}" "AaBcC AaBdC AbBcC AbBdC D\n" "" ""
testing "brackets3" 'echo {A"b,c"D}' "{Ab,cD}\n" "" ""
testing "brackets4" 'echo {A"bc",D}' "Abc D\n" "" ""
testing "brackets5" 'echo {A,B,C' "{A,B,C\n" "" ""
testing "brackets6" 'echo {{{{A,B},C}D},E}' "{AD} {BD} {CD} E\n" "" ""
testing "brackets7" 'echo {{{a,b},c,{d,e}},f}' "a b c d e f\n" "" ""
testing "brackets8" 'echo A{a{b,c{B,C}D}d{e,f},g{h,i}j}E' \
  "AabdeE AabdfE AacBDdeE AacBDdfE AacCDdeE AacCDdfE AghjE AgijE\n" "" ""
testing "brackets9" 'echo A{B{C,D}E{N,O},F{G,H}I}J{K,L}M' \
  "ABCENJKM ABCENJLM ABCEOJKM ABCEOJLM ABDENJKM ABDENJLM ABDEOJKM ABDEOJLM AFGIJKM AFGIJLM AFHIJKM AFHIJLM\n" "" ""
for i in /root /var/root /; do [ -e $i ] && EXPECT=$i && break; done
testing "bracket+tilde" "echo {~,~root}/pwd" "$HOME/pwd $EXPECT/pwd\n" "" ""

#testing "backtick1" 'X=fred; echo `echo $x`' 'fred\n' "" ""
#testing "backtick2" 'X=fred; echo `x=y; echo $x`' 'y\n' "" ""
testing '$(( ) )' 'echo ab$((echo hello) | tr e x)cd' "abhxllocd\n" "" ""

testing "continue" 'for i in a b c; do for j in d e f; do echo $i $j; continue 2; done; done' \
  "a d\nb d\nc d\n" "" ""

# <glinda>A variable occurred</glinda>

testing "expand" 'echo $PWD' "$(pwd)\n" "" ""
testing "expand2" 'echo "$PWD"' "$(pwd)\n" "" ""
testing "expand3" 'echo "$"PWD' '$PWD\n' "" ""
testing "expand4" 'P=x; echo "$P"WD' 'xWD\n' "" ""
testing "dequote" "echo one 'two' ''three 'fo'ur '\\'" \
  'one two three four \\\n' '' ''

testing "leading variable assignment" 'abc=def env | grep ^abc=; echo $abc' \
  "abc=def\n\n" "" ""
testing "leading variable assignments" \
  "abc=def ghi=jkl env | egrep '^(abc|ghi)=' | sort; echo \$abc \$ghi" \
  "abc=def\nghi=jkl\n\n" "" ""

testing "{1..5}" "echo {1..5}" "1 2 3 4 5\n" "" ""
testing "{5..1}" "echo {5..1}" "5 4 3 2 1\n" "" ""
testing "{5..1..2}" "echo {5..1..2}" "5 3 1\n" "" ""
testing "{a..z..-3}" "echo {a..z..-3}" "a d g j m p s v y\n" "" ""

#$ IFS=x X=xyxz; for i in abc${X}def; do echo =$i=; done
#=abc=
#=y=
#=zdef=

testing "IFS whitespace before/after" \
  'IFS=" x"; A=" x " B=" x" C="x " D=x E="   "; for i in $A $B $C $D L$A L$B L$C L$D $A= $B= $C= $D= L$A= L$B= L$C= L$D=; do echo -n {$i}; done' \
  "{}{}{}{}{L}{L}{L}{L}{}{=}{}{=}{}{=}{}{=}{L}{=}{L}{=}{L}{=}{L}{=}" "" ""
testing "quotes and whitespace" \
  'A="   abc   def   "; for i in ""$A""; do echo =$i=; done' \
  "==\n=abc=\n=def=\n==\n" "" ""
testing "quotes and whitespace2" \
  'A="   abc   def   "; for i in """"$A""; do echo =$i=; done' \
  "==\n=abc=\n=def=\n==\n" "" ""
testing "quotes and whitespace3" \
  'A="   abc   def   "; for i in ""x""$A""; do echo =$i=; done' \
  "=x=\n=abc=\n=def=\n==\n" "" ""

testing "IFS" 'IFS=x; A=abx; echo -n "$A"' "abx" "" ""
testing "IFS2" 'IFS=x; A=abx; echo -n $A' "ab" "" ""
testing "IFS3" 'IFS=x; echo "$(echo abx)"' "abx\n" "" ""
testing "IFS4" "IFS=x; echo \"\$(echo ab' ')\"" "ab \n" "" ""
testing "IFS5" 'IFS=xy; for i in abcxdefyghi; do echo =$i=; done' \
  "=abc def ghi=\n" "" ""

testing '$*' 'cc(){ for i in $*;do echo =$i=;done;};cc "" "" "" "" ""' \
  "" "" ""
testing '$*2' 'cc(){ for i in "$*";do echo =$i=;done;};cc ""' \
  "==\n" "" ""
testing '$*3... Flame. Flames. Flames, on the side of my face...' \
  'cc(){ for i in "$*";do echo =$i=;done;};cc "" ""' "= =\n" "" ""
testing 'why... oh.' \
  'cc() { echo ="$*"=; for i in =$*=; do echo -$i-; done;}; cc "" ""; echo and; cc ""' \
  '= =\n-=-\n-=-\nand\n==\n-==-\n' "" ""
testing 'really?' 'cc() { for i in $*; do echo -$i-; done;}; cc "" "" ""' \
  "" "" ""
testing 'Sigh.' 'cc() { echo =$1$2=;}; cc "" ""' "==\n" "" ""
testing '$*4' 'cc(){ for i in "$*";do echo =$i=;done;};cc "" "" "" "" ""' \
  "= =\n" "" ""
testing '$*5' 'cc(){ for i in "$*";do echo =$i=;done;};cc "" "abc" ""' \
  "= abc =\n" "" ""

# creating empty arguments without quotes
testing '$* + IFS' \
  'IFS=x; cc(){ for i in $*; do echo =$i=;done;};cc xabcxx' \
  "==\n=abc=\n==\n" "" ""
testing '$@' 'cc(){ for i in "$@";do echo =$i=;done;};cc "" "" "" "" ""' \
  "==\n==\n==\n==\n==\n" "" ""
testing "IFS10" 'IFS=bcd; A=abcde; for i in $A; do echo =$i=; done' \
  "=a=\n==\n==\n=e=\n" "" ""
testing "IFS11" \
  'IFS=x; chicken() { for i in $@$@; do echo =$i=; done;}; chicken one "" abc dxf ghi' \
  "=one=\n==\n=abc=\n=d=\n=f=\n=ghione=\n==\n=abc=\n=d=\n=f=\n=ghi=\n" "" ""
testing "IFS12" 'IFS=3;chicken(){ return 3;}; chicken;echo 3$?3' '3 3\n' "" ""

testing "IFS combinations" \
  'IFS=" x"; A=" x " B=" x" C="x " D=x E="   "; for i in $A $B $C $D L$A L$B L$C L$D $A= $B= $C= $D= L$A= L$B= L$C= L$D=; do echo -n {$i}; done' \
  "{}{}{}{}{L}{L}{L}{L}{}{=}{}{=}{}{=}{}{=}{L}{=}{L}{=}{L}{=}{L}{=}" "" ""

testing "! isn't special" "echo !" "!\n" "" ""
testing "! by itself" '!; echo $?' "1\n" "" ""
testing "! true" '! true; echo $?' "1\n" "" ""
testing "! ! true" '! ! true; echo $?' "0\n" "" ""
testing "! syntax err" '! echo 2>/dev/null < doesnotexist; echo $?' "0\n" "" ""

# The bash man page doesn't say quote removal here, and yet:
testing "case quoting" 'case a in "a") echo hello;; esac' 'hello\n' "" ""

testing "subshell splitting" 'for i in $(true); do echo =$i=; done' "" "" ""
#testing "subshell split 2"

# variable assignment argument splitting only performed for "$@"
testing "assignment splitting" 'X="one two"; Y=$X; echo $Y' "one two\n" "" ""
testing "argument splitting" \
  'chicken() { for i in a"$@"b;do echo =$i=;done;}; chicken 123 456 789' \
  "=a123=\n=456=\n=789b=\n" "" ""
testing "assignment splitting2" 'pop(){ X="$@";};pop one two three; echo $X' \
  "one two three\n" "" ""

#testing "leading assignments don't affect current line" \
#  'VAR=12345 echo ${VAR}a' "a\n" "" ""
#testing "can't have space before first : but yes around arguments" \
#  'BLAH=abcdefghi; echo ${BLAH: 1 : 3 }' "bcd\n" "" ""

testing "subshell exit err" '(exit 42); echo $?' "42\n" "" ""

# Same thing twice, but how do we cmp if exec exited?
#testing 'exec and $$' testing 'echo $$;exec readlink /proc/self' 

X="$(realpath $(which readlink))"
testing "exec in paren" \
  '(exec readlink /proc/self/exe);echo hello' "$X\nhello\n" "" ""
testing "exec in brackets" \
  "{ exec readlink /proc/self/exe;};echo hi" "$X\n" "" ""

NOSPACE=1 testing "curly brackets and pipe" \
  '{ echo one; echo two ; } | tee blah.txt; wc blah.txt' \
  "one\ntwo\n2 2 8 blah.txt\n" "" ""
NOSPACE=1 testing "parentheses and pipe" \
  '(echo two;echo three)|tee blah.txt;wc blah.txt' \
  "two\nthree\n2 2 10 blah.txt\n" "" ""
testing "pipe into parentheses" \
  'echo hello | (read i <input; echo $i; read i; echo $i)' \
  "there\nhello\n" "there\n" ""

testing "\$''" $'echo $\'abc\\\'def\\nghi\'' "abc'def\nghi\n" '' ''
testing "shift shift" 'shift; shift; shift; echo $? hello' "1 hello\n" "" ""
testing 'search cross $*' 'chicken() { echo ${*/b c/ghi}; }; chicken a b c d' \
  "a b c d\n" "" ""
testing 'eval $IFS' 'IFS=x; X=x; eval abc=a${X}b 2>/dev/null; echo $abc' \
  "\n" '' ''
testing '${@:3:5}' 'chicken() { for i in "${@:3:5}"; do echo =$i=; done; } ; chicken ab cd ef gh ij kl mn op qr' \
  '=ef=\n=gh=\n=ij=\n=kl=\n=mn=\n' '' ''
testing '${@:3:5}' 'chicken() { for i in "${*:3:5}"; do unset IFS; echo =$i=; done; } ; IFS=x chicken ab cd ef gh ij kl mn op qr' \
  '=efxghxijxklxmn=\n' '' ''
testing 'sequence check' 'IFS=x; X=abxcd; echo ${X/bxc/g}' 'agd\n' '' ''

# TODO: The txpect plumbing does not work right yet even on TEST_HOST
#txpect "backtick0" "$SS" "E$P" 'IX=fred; echo `echo \\\\$x`'$'\n' 'Ofred' "E$P" X0
#txpect "backtick1" "$SS" "E$P" 'IX=fred; echo `echo $x`'$'\n' 'Ofred'$'\n' "E$P" X0
#txpect "backtick2" "$SS" "E$P" 'IX=fred; echo `x=y; echo $x`' $'Oy\n' "E$P" X0

shxpect '${ with newline' I$'HELLO=abc; echo ${HELLO/b/\n' E"> " I$'}\n' O$'a c\n'

shxpect 'here1' I$'POTATO=123; cat << EOF\n' E"> " \
  I$'$POTATO\n' E"> " I$'EOF\n' O$'123\n'
shxpect 'here2' I$'POTATO=123; cat << E"O"F\n' E"> " \
  I$'$POTATO\n' E"> " I$'EOF\n' O$'$POTATO\n'
testing 'here3' 'abc(){ cat <<< x"$@"yz;};abc one two "three  four"' \
  "xone two three  fouryz\n" "" ""

testing '${#}' 'X=abcdef; echo ${#X}' "6\n" "" ""

shxpect '${/newline/}' I$'x=$\'\na\';echo ${x/\n' E'> ' I$'/b}\n' O$'ba\n' E'> '

# Race condition (in bash, but not in toysh) can say 43.
testing 'SECONDS' 'SECONDS=41; sleep 1; echo $SECONDS' '42\n' '' ''
testing 'SECONDS2' 'readonly SECONDS; SECONDS=0; echo $SECONDS' '' '' '' # bash!
testing 'SECONDS3' 'SECONDS=123+456; echo $SECONDS' '0\n' '' '' #bash!!
testing 'LINENO' $'echo $LINENO\necho $LINENO' '0\n1\n' '' ''
testing 'EUID' 'echo $EUID' "$(id -u)\n" '' ''
testing 'UID' 'echo $UID' "$(id -ur)\n" '' ''
# TODO finish variable list from shell init

# $# $? $- $$ $! $0
# always exported: PWD SHLVL _
#  ./bash -c 'echo $_' prints $BASH, but PATH search shows path? Hmmm...
# ro: UID PPID EUID $
# IFS LINENO
# PATH HOME SHELL USER LOGNAME SHLVL HOSTNAME HOSTTYPE MACHTYPE OSTYPE OLDPWD
# PS0 PS1='$ ' PS2='> ' PS3 PS4 BASH BASH_VERSION
# ENV - if [ -n "$ENV" ]; then . "$ENV"; fi # BASH_ENV - synonym for ENV
# FUNCNEST - maximum function nesting level (abort when above)
# REPLY - set by input with no args
# OPTARG OPTIND - set by getopts builtin
# OPTERR

# maybe not: EXECIGNORE, FIGNORE, GLOBIGNORE

#BASHPID - synonym for $$ HERE
#BASH_SUBSHELL - SHLVL synonym
#BASH_EXECUTION_STRING - -c argument
#
#automatically set:
#OPTARG - set by getopts builtin
#OPTIND - set by getopts builtin
#
#PROMPT_COMMAND PROMPT_DIRTRIM PS0 PS1 PS2 PS3 PS4
#
#unsettable (assignments ignored before then)
#LINENO SECONDS RANDOM
#GROUPS - id -g
#HISTCMD - history number
#
#TMOUT - used by read