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}
|
9 | source $LIB_OSH/bash-strict.sh
|
10 | source $LIB_OSH/no-quotes.sh
|
11 |
|
12 | source test/common.sh # run-test-funcs
|
13 |
|
14 | DISABLED-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 |
|
22 | test-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.
|
35 | test-osh-file() {
|
36 | echo ===== Hello
|
37 | cat >_tmp/smoke-prog.sh <<EOF
|
38 | echo hi
|
39 |
|
40 | myfunc() {
|
41 | echo "inside func"
|
42 | }
|
43 | myfunc 1 2 3
|
44 |
|
45 | # TODO: Test vars don't persist.
|
46 | (echo "in subshell"; echo "another")
|
47 |
|
48 | echo \$(echo ComSub)
|
49 |
|
50 | EOF
|
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.
|
63 | test-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
|
74 | echo "
|
75 | hi
|
76 | "
|
77 | echo \\
|
78 | line continuation; echo two
|
79 |
|
80 | cat <<EOF_INNER
|
81 | here doc
|
82 | EOF_INNER
|
83 |
|
84 | echo \$(
|
85 | echo command sub
|
86 | )
|
87 |
|
88 | myfunc() {
|
89 | echo hi
|
90 | }
|
91 |
|
92 | EOF
|
93 | }
|
94 |
|
95 | test-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 |
|
117 | test-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 |
|
123 | test-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 |
|
141 | test-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 |
|
151 | test-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 |
|
181 | test-version() {
|
182 | local status
|
183 |
|
184 | nq-run status \
|
185 | bin/osh --version
|
186 | nq-assert $? -eq 0
|
187 | }
|
188 |
|
189 | DISABLED-test-symlink() {
|
190 | local tmp=_tmp/osh-usage
|
191 | mkdir -p $tmp
|
192 |
|
193 | local repo_root=$PWD
|
194 |
|
195 | # requires 'make'
|
196 | local bundle=$PWD/_bin/oil.ovm
|
197 | #local bundle=$repo_root/bin/oil.py
|
198 |
|
199 | ln -s -f -v $bundle $tmp/osh
|
200 | ln -s -f -v $bundle $tmp/bash
|
201 |
|
202 | cd $tmp
|
203 |
|
204 | ./osh -c 'echo $OILS_VERSION'
|
205 | nq-assert $? -eq 0
|
206 | ./bash -c 'echo $OILS_VERSION'
|
207 | nq-assert $? -eq 0
|
208 | }
|
209 |
|
210 | # TODO: Use byo test for these two functions
|
211 |
|
212 | run-for-release() {
|
213 | run-other-suite-for-release osh-usage run-test-funcs
|
214 | }
|
215 |
|
216 | soil-run() {
|
217 | run-test-funcs
|
218 | }
|
219 |
|
220 | "$@"
|