OILS / test / osh-usage.sh View on Github | oils.pub

273 lines, 150 significant
1#!/usr/bin/env bash
2#
3# Test osh usage "from the outside".
4#
5# Usage:
6# test/osh-usage.sh <function name>
7
8: ${LIB_OSH=stdlib/osh}
9source $LIB_OSH/bash-strict.sh
10source $LIB_OSH/no-quotes.sh
11
12source test/common.sh # run-test-funcs
13
14DISABLED-test-oheap() {
15 # OHeap was disabled
16 if bin/osh -n --ast-format oheap -c 'echo hi'; then
17 die "Should have failed"
18 fi
19 echo OK
20}
21
22test-ast-formats() {
23 bin/osh -n -c 'echo hi'
24 bin/osh -n --ast-format text -c 'echo hi'
25
26 # Removed with oheap
27 return
28 local ast_bin=_tmp/smoke-ast.bin
29 bin/osh -n --ast-format oheap -c 'echo hi' > $ast_bin
30 ls -l $ast_bin
31 hexdump -C $ast_bin
32}
33
34# Read from a file.
35test-osh-file() {
36 echo ===== Hello
37 cat >_tmp/smoke-prog.sh <<EOF
38echo hi
39
40myfunc() {
41 echo "inside func"
42}
43myfunc 1 2 3
44
45# TODO: Test vars don't persist.
46(echo "in subshell"; echo "another")
47
48echo \$(echo ComSub)
49
50EOF
51 $OSH _tmp/smoke-prog.sh
52
53 echo ===== EMPTY
54 touch _tmp/empty.sh
55 $OSH _tmp/empty.sh
56
57 echo ===== NO TRAILING NEWLINE
58 echo -n 'echo hi' >_tmp/no-newline.sh
59 $OSH _tmp/no-newline.sh
60}
61
62# Read from stdin.
63test-osh-stdin() {
64 $OSH < _tmp/smoke-prog.sh
65
66 echo ===== EMPTY
67 $OSH < _tmp/empty.sh
68
69 echo ===== NO TRAILING NEWLINE
70 $OSH < _tmp/no-newline.sh
71
72 # Line continuation tests
73 $OSH <<EOF
74echo "
75hi
76"
77echo \\
78line continuation; echo two
79
80cat <<EOF_INNER
81here doc
82EOF_INNER
83
84echo \$(
85echo command sub
86)
87
88myfunc() {
89 echo hi
90}
91
92EOF
93}
94
95test-osh-interactive() {
96 set +o errexit
97 echo 'echo hi' | $OSH -i
98 nq-assert $? -eq 0
99
100 echo 'exit' | $OSH -i
101 nq-assert $? -eq 0
102
103 # Parse failure
104 echo ';' | $OSH -i
105 nq-assert $? -eq 2
106
107 # Bug fix: this shouldn't try execute 'echo OIL OIL'
108 # The line lexer wasn't getting reset on parse failures.
109 echo ';echo OIL OIL' | $OSH -i
110 nq-assert $? -eq 2
111
112 # Bug fix: c_parser.Peek() in main_loop.InteractiveLoop can raise exceptions
113 echo 'v=`echo \"`' | $OSH -i
114 nq-assert $? -eq 0
115}
116
117test-exit-builtin-interactive() {
118 set +o errexit
119 echo 'echo one; exit 42; echo two' | bin/osh -i
120 nq-assert $? -eq 42
121}
122
123test-rc-file() {
124 set +o errexit
125
126 local rc=_tmp/testrc
127 echo 'PS1="TESTRC$ "' > $rc
128
129 bin/osh -i --rcfile $rc < /dev/null
130 nq-assert $? -eq 0
131
132 bin/osh -i --rcfile /dev/null < /dev/null
133 nq-assert $? -eq 0
134
135 # oshrc is optional
136 # TODO: Could warn about nonexistent explicit --rcfile?
137 bin/osh -i --rcfile nonexistent__ < /dev/null
138 nq-assert $? -eq 0
139}
140
141test-noexec-fails-properly() {
142 set +o errexit
143 local tmp=_tmp/osh-usage-noexec.txt
144 bin/osh -n -c 'echo; echo; |' > $tmp
145 nq-assert $? -eq 2
146 read < $tmp
147 nq-assert $? -eq 1 # shouldn't have read any lines!
148 echo "$tmp appears empty, as expected"
149}
150
151test-help() {
152 local status
153
154 # TODO: Test the oil.ovm binary as well as bin/oil.py.
155 export PYTHONPATH='.:vendor/' # TODO: Put this in one place.
156
157 # Bundle usage.
158 nq-run status \
159 bin/oils_for_unix.py --help
160 nq-assert $status -eq 0
161
162 # Pass applet as first name.
163 nq-run status \
164 bin/oils_for_unix.py osh --help
165 nq-assert $status -eq 0
166
167 nq-run status \
168 bin/oils_for_unix.py ysh --help
169 nq-assert $status -eq 0
170
171 # Symlinks.
172 nq-run status \
173 bin/osh --help
174 nq-assert $status -eq 0
175
176 nq-run status \
177 bin/oils_for_unix.py --help
178 nq-assert $status -eq 0
179}
180
181test-version() {
182 local status
183
184 nq-run status \
185 bin/osh --version
186 nq-assert $? -eq 0
187}
188
189test-bash-version-emulation() {
190 local dir=_tmp/bash-version
191
192 local target=_bin/cxx-asan/osh
193 ninja $target
194
195 local osh=$PWD/$target
196
197 mkdir -p $dir
198 pushd $dir
199
200 ln -s -f $osh bash
201 ln -s -f $osh osh
202
203 #
204 # Test $BASH_VERSION
205 #
206 local code='echo BASH_VERSION=$BASH_VERSION'
207 local status stdout
208
209 # Unset when called as osh
210 nq-capture status stdout \
211 ./osh -c "$code"
212 nq-assert 0 -eq $status
213 nq-assert 'BASH_VERSION=' = "$stdout"
214
215 # Set when called as bash
216 nq-capture status stdout \
217 ./bash -c "$code"
218 nq-assert 0 -eq $status
219 nq-assert 'BASH_VERSION=5.3' = "$stdout"
220
221 #
222 # TEST $BASH_VERSINFO
223 #
224 code='echo BASH_VERSINFO=${BASH_VERSINFO[@]}'
225
226 # Unset when called as osh
227 nq-capture status stdout \
228 ./osh -c "$code"
229 nq-assert 0 -eq $status
230 nq-assert 'BASH_VERSINFO=' = "$stdout"
231
232 # Set when called as bash
233 nq-capture status stdout \
234 ./bash -c "$code"
235 nq-assert 0 -eq $status
236 nq-assert 'BASH_VERSINFO=5 3 0 0 release unknown' = "$stdout"
237
238 popd
239}
240
241
242DISABLED-test-symlink() {
243 local tmp=_tmp/osh-usage
244 mkdir -p $tmp
245
246 local repo_root=$PWD
247
248 # requires 'make'
249 local bundle=$PWD/_bin/oil.ovm
250 #local bundle=$repo_root/bin/oil.py
251
252 ln -s -f -v $bundle $tmp/osh
253 ln -s -f -v $bundle $tmp/bash
254
255 cd $tmp
256
257 ./osh -c 'echo $OILS_VERSION'
258 nq-assert $? -eq 0
259 ./bash -c 'echo $OILS_VERSION'
260 nq-assert $? -eq 0
261}
262
263# TODO: Use byo test for these two functions
264
265run-for-release() {
266 run-other-suite-for-release osh-usage run-test-funcs
267}
268
269soil-run() {
270 run-test-funcs
271}
272
273"$@"