OILS / test / osh-usage.sh View on Github | oilshell.org

222 lines, 122 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 bin/osh -n --ast-format abbrev-html -c 'echo hi'
26 bin/osh -n --ast-format html -c 'echo hi'
27
28 # Removed with oheap
29 return
30 local ast_bin=_tmp/smoke-ast.bin
31 bin/osh -n --ast-format oheap -c 'echo hi' > $ast_bin
32 ls -l $ast_bin
33 hexdump -C $ast_bin
34}
35
36# Read from a file.
37test-osh-file() {
38 echo ===== Hello
39 cat >_tmp/smoke-prog.sh <<EOF
40echo hi
41
42myfunc() {
43 echo "inside func"
44}
45myfunc 1 2 3
46
47# TODO: Test vars don't persist.
48(echo "in subshell"; echo "another")
49
50echo \$(echo ComSub)
51
52EOF
53 $OSH _tmp/smoke-prog.sh
54
55 echo ===== EMPTY
56 touch _tmp/empty.sh
57 $OSH _tmp/empty.sh
58
59 echo ===== NO TRAILING NEWLINE
60 echo -n 'echo hi' >_tmp/no-newline.sh
61 $OSH _tmp/no-newline.sh
62}
63
64# Read from stdin.
65test-osh-stdin() {
66 $OSH < _tmp/smoke-prog.sh
67
68 echo ===== EMPTY
69 $OSH < _tmp/empty.sh
70
71 echo ===== NO TRAILING NEWLINE
72 $OSH < _tmp/no-newline.sh
73
74 # Line continuation tests
75 $OSH <<EOF
76echo "
77hi
78"
79echo \\
80line continuation; echo two
81
82cat <<EOF_INNER
83here doc
84EOF_INNER
85
86echo \$(
87echo command sub
88)
89
90myfunc() {
91 echo hi
92}
93
94EOF
95}
96
97test-osh-interactive() {
98 set +o errexit
99 echo 'echo hi' | $OSH -i
100 nq-assert $? -eq 0
101
102 echo 'exit' | $OSH -i
103 nq-assert $? -eq 0
104
105 # Parse failure
106 echo ';' | $OSH -i
107 nq-assert $? -eq 2
108
109 # Bug fix: this shouldn't try execute 'echo OIL OIL'
110 # The line lexer wasn't getting reset on parse failures.
111 echo ';echo OIL OIL' | $OSH -i
112 nq-assert $? -eq 2
113
114 # Bug fix: c_parser.Peek() in main_loop.InteractiveLoop can raise exceptions
115 echo 'v=`echo \"`' | $OSH -i
116 nq-assert $? -eq 0
117}
118
119test-exit-builtin-interactive() {
120 set +o errexit
121 echo 'echo one; exit 42; echo two' | bin/osh -i
122 nq-assert $? -eq 42
123}
124
125test-rc-file() {
126 set +o errexit
127
128 local rc=_tmp/testrc
129 echo 'PS1="TESTRC$ "' > $rc
130
131 bin/osh -i --rcfile $rc < /dev/null
132 nq-assert $? -eq 0
133
134 bin/osh -i --rcfile /dev/null < /dev/null
135 nq-assert $? -eq 0
136
137 # oshrc is optional
138 # TODO: Could warn about nonexistent explicit --rcfile?
139 bin/osh -i --rcfile nonexistent__ < /dev/null
140 nq-assert $? -eq 0
141}
142
143test-noexec-fails-properly() {
144 set +o errexit
145 local tmp=_tmp/osh-usage-noexec.txt
146 bin/osh -n -c 'echo; echo; |' > $tmp
147 nq-assert $? -eq 2
148 read < $tmp
149 nq-assert $? -eq 1 # shouldn't have read any lines!
150 echo "$tmp appears empty, as expected"
151}
152
153test-help() {
154 local status
155
156 # TODO: Test the oil.ovm binary as well as bin/oil.py.
157 export PYTHONPATH='.:vendor/' # TODO: Put this in one place.
158
159 # Bundle usage.
160 nq-run status \
161 bin/oils_for_unix.py --help
162 nq-assert $status -eq 0
163
164 # Pass applet as first name.
165 nq-run status \
166 bin/oils_for_unix.py osh --help
167 nq-assert $status -eq 0
168
169 nq-run status \
170 bin/oils_for_unix.py ysh --help
171 nq-assert $status -eq 0
172
173 # Symlinks.
174 nq-run status \
175 bin/osh --help
176 nq-assert $status -eq 0
177
178 nq-run status \
179 bin/oils_for_unix.py --help
180 nq-assert $status -eq 0
181}
182
183test-version() {
184 local status
185
186 nq-run status \
187 bin/osh --version
188 nq-assert $? -eq 0
189}
190
191DISABLED-test-symlink() {
192 local tmp=_tmp/osh-usage
193 mkdir -p $tmp
194
195 local repo_root=$PWD
196
197 # requires 'make'
198 local bundle=$PWD/_bin/oil.ovm
199 #local bundle=$repo_root/bin/oil.py
200
201 ln -s -f -v $bundle $tmp/osh
202 ln -s -f -v $bundle $tmp/bash
203
204 cd $tmp
205
206 ./osh -c 'echo $OILS_VERSION'
207 nq-assert $? -eq 0
208 ./bash -c 'echo $OILS_VERSION'
209 nq-assert $? -eq 0
210}
211
212# TODO: Use byo test for these two functions
213
214run-for-release() {
215 run-other-suite-for-release osh-usage run-test-funcs
216}
217
218soil-run() {
219 run-test-funcs
220}
221
222"$@"