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

213 lines, 116 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-help() {
120 set +o errexit
121
122 # TODO: Test the oil.ovm binary as well as bin/oil.py.
123 export PYTHONPATH='.:vendor/' # TODO: Put this in one place.
124
125 # Bundle usage.
126 bin/oils_for_unix.py --help
127 nq-assert $? -eq 0
128
129 # Pass applet as first name.
130 bin/oils_for_unix.py osh --help
131 nq-assert $? -eq 0
132
133 bin/oils_for_unix.py ysh --help
134 nq-assert $? -eq 0
135
136 # Symlinks.
137 bin/osh --help
138 nq-assert $? -eq 0
139
140 bin/oils_for_unix.py --help
141 nq-assert $? -eq 0
142}
143
144test-exit-builtin-interactive() {
145 set +o errexit
146 echo 'echo one; exit 42; echo two' | bin/osh -i
147 nq-assert $? -eq 42
148}
149
150test-rc-file() {
151 set +o errexit
152
153 local rc=_tmp/testrc
154 echo 'PS1="TESTRC$ "' > $rc
155
156 bin/osh -i --rcfile $rc < /dev/null
157 nq-assert $? -eq 0
158
159 bin/osh -i --rcfile /dev/null < /dev/null
160 nq-assert $? -eq 0
161
162 # oshrc is optional
163 # TODO: Could warn about nonexistent explicit --rcfile?
164 bin/osh -i --rcfile nonexistent__ < /dev/null
165 nq-assert $? -eq 0
166}
167
168test-noexec-fails-properly() {
169 set +o errexit
170 local tmp=_tmp/osh-usage-noexec.txt
171 bin/osh -n -c 'echo; echo; |' > $tmp
172 nq-assert $? -eq 2
173 read < $tmp
174 nq-assert $? -eq 1 # shouldn't have read any lines!
175 echo "$tmp appears empty, as expected"
176}
177
178test-version() {
179 set +o errexit
180 bin/osh --version
181 nq-assert $? -eq 0
182}
183
184DISABLED-test-symlink() {
185 local tmp=_tmp/osh-usage
186 mkdir -p $tmp
187
188 local repo_root=$PWD
189
190 # requires 'make'
191 local bundle=$PWD/_bin/oil.ovm
192 #local bundle=$repo_root/bin/oil.py
193
194 ln -s -f -v $bundle $tmp/osh
195 ln -s -f -v $bundle $tmp/bash
196
197 cd $tmp
198
199 ./osh -c 'echo $OILS_VERSION'
200 nq-assert $? -eq 0
201 ./bash -c 'echo $OILS_VERSION'
202 nq-assert $? -eq 0
203}
204
205run-for-release() {
206 run-other-suite-for-release osh-usage run-test-funcs
207}
208
209soil-run() {
210 run-test-funcs
211}
212
213"$@"