OILS / spec / interactive.test.sh View on Github | oilshell.org

343 lines, 136 significant
1## tags: dev-minimal interactive
2## compare_shells: bash
3
4#### 'exit' in oshrc (regression)
5cat >$TMP/oshrc <<EOF
6echo one
7exit 42
8echo two
9EOF
10$SH --rcfile $TMP/oshrc -i -c 'echo hello'
11## status: 42
12## STDOUT:
13one
14## END
15
16#### fatal errors continue
17# NOTE: tried here doc, but sys.stdin.isatty() fails. Could we fake it?
18$SH --rcfile /dev/null -i -c '
19echo $(( 1 / 0 ))
20echo one
21exit 42
22'
23## status: 42
24## STDOUT:
25one
26## END
27
28#### interactive shell loads rcfile (when combined with -c)
29$SH -c 'echo 1'
30cat >$TMP/rcfile <<EOF
31echo RCFILE
32EOF
33$SH --rcfile $TMP/rcfile -i -c 'echo 2'
34## STDOUT:
351
36RCFILE
372
38## END
39
40#### interactive shell loads files in rcdir (when combined with -c)
41
42$SH -c 'echo A'
43
44cat >$TMP/rcfile <<EOF
45echo 'rcfile first'
46EOF
47
48mkdir -p $TMP/rcdir
49
50cat >$TMP/rcdir/file1 <<EOF
51echo rcdir 1
52EOF
53
54cat >$TMP/rcdir/file2 <<EOF
55echo rcdir 2
56EOF
57
58# --rcdir only
59$SH --rcdir $TMP/rcdir -i -c 'echo B'
60
61$SH --rcfile $TMP/rcfile --rcdir $TMP/rcdir -i -c 'echo C'
62
63## STDOUT:
64A
65rcdir 1
66rcdir 2
67B
68rcfile first
69rcdir 1
70rcdir 2
71C
72## END
73
74## N-I bash status: 2
75## N-I bash STDOUT:
76A
77## END
78
79#### nonexistent --rcdir is ignored
80case $SH in bash) exit ;; esac
81
82$SH --rcdir $TMP/__does-not-exist -i -c 'echo hi'
83echo status=$?
84
85## STDOUT:
86hi
87status=0
88## END
89## N-I bash STDOUT:
90## END
91
92#### shell doesn't load rcfile/rcdir if --norc is given
93
94$SH -c 'echo A'
95
96cat >$TMP/rcfile <<EOF
97echo rcfile
98EOF
99
100mkdir -p $TMP/rcdir
101cat >$TMP/rcdir/file1 <<EOF
102echo rcdir 1
103EOF
104
105cat >$TMP/rcdir/file2 <<EOF
106echo rcdir 2
107EOF
108
109$SH --norc --rcfile $TMP/rcfile -c 'echo C'
110case $SH in bash) exit ;; esac
111
112$SH --norc --rcfile $TMP/rcfile --rcdir $TMP/rcdir -c 'echo D'
113
114## STDOUT:
115A
116C
117D
118## END
119## OK bash STDOUT:
120A
121C
122## END
123
124
125#### interactive shell runs PROMPT_COMMAND after each command
126export PS1='' # OSH prints prompt to stdout
127
128case $SH in
129 *bash|*osh)
130 $SH --rcfile /dev/null -i << EOF
131PROMPT_COMMAND='echo PROMPT'
132echo one
133echo two
134EOF
135 ;;
136esac
137
138# Paper over difference with OSH
139case $SH in *bash) echo '^D';; esac
140
141## STDOUT:
142PROMPT
143one
144PROMPT
145two
146PROMPT
147^D
148## END
149
150
151#### parse error in PROMPT_COMMAND
152export PS1='' # OSH prints prompt to stdout
153
154case $SH in
155 *bash|*osh)
156 $SH --rcfile /dev/null -i << EOF
157PROMPT_COMMAND=';'
158echo one
159echo two
160EOF
161 ;;
162esac
163
164# Paper over difference with OSH
165case $SH in *bash) echo '^D';; esac
166
167## STDOUT:
168one
169two
170^D
171## END
172
173#### runtime error in PROMPT_COMMAND
174export PS1='' # OSH prints prompt to stdout
175
176case $SH in
177 *bash|*osh)
178 $SH --rcfile /dev/null -i << 'EOF'
179PROMPT_COMMAND='echo PROMPT $(( 1 / 0 ))'
180echo one
181echo two
182EOF
183 ;;
184esac
185
186# Paper over difference with OSH
187case $SH in *bash) echo '^D';; esac
188
189## STDOUT:
190one
191two
192^D
193## END
194
195#### Error message with bad oshrc file (currently ignored)
196cd $TMP
197echo 'foo >' > bad_oshrc
198
199$SH --rcfile bad_oshrc -i -c 'echo hi' 2>stderr.txt
200echo status=$?
201
202# bash prints two lines
203grep --max-count 1 -o 'bad_oshrc:' stderr.txt
204
205## STDOUT:
206hi
207status=0
208bad_oshrc:
209## END
210
211
212#### PROMPT_COMMAND can see $?, like bash
213
214# bug fix #853
215
216export PS1='' # OSH prints prompt to stdout
217
218case $SH in
219 *bash|*osh)
220 $SH --rcfile /dev/null -i << 'EOF'
221myfunc() { echo last_status=$?; }
222PROMPT_COMMAND='myfunc'
223( exit 42 )
224( exit 43 )
225echo ok
226EOF
227 ;;
228esac
229
230# Paper over difference with OSH
231case $SH in *bash) echo '^D';; esac
232## STDOUT:
233last_status=0
234last_status=42
235last_status=43
236ok
237last_status=0
238^D
239## END
240
241#### PROMPT_COMMAND that writes to BASH_REMATCH
242export PS1=''
243
244case $SH in
245 *bash|*osh)
246 $SH --rcfile /dev/null -i << 'EOF'
247PROMPT_COMMAND='[[ clobber =~ (.)(.)(.) ]]; echo ---'
248echo one
249[[ bar =~ (.)(.)(.) ]]
250echo ${BASH_REMATCH[@]}
251EOF
252 ;;
253esac
254
255# Paper over difference with OSH
256case $SH in *bash) echo '^D';; esac
257
258## STDOUT:
259---
260one
261---
262---
263bar b a r
264---
265^D
266## END
267## OK bash STDOUT:
268---
269one
270---
271---
272clo c l o
273---
274^D
275## END
276
277
278#### NO ASSERTIONS: Are startup files sourced before or after job control?
279
280cat >myrc <<'EOF'
281
282# from test/process-table-portable.sh
283PS_COLS='pid,ppid,pgid,sid,tpgid,comm'
284
285show-shell-state() {
286 local prefix=$1
287
288 echo -n "$prefix: "
289
290 echo "pid = $$"
291
292 # Hm TPGID has changed in both OSH and bash
293 # I guess that's because because ps itself becomes the leader of the process
294 # group
295
296 ps -o $PS_COLS $$
297}
298
299show-shell-state myrc
300
301
302EOF
303
304$SH --rcfile myrc -i -c 'show-shell-state main'
305
306## status: 0
307
308# No assertions
309# TODO: spec test framework should be expanded to properly support these
310# comparisons.
311# The --details flag is useful
312
313
314#### HISTFILE is written in interactive shell
315
316rm -f myhist
317export HISTFILE=myhist
318echo 'echo hist1; echo hist2' | $SH --norc -i
319
320if test -n "$BASH_VERSION"; then
321 echo '^D' # match OSH for now
322fi
323
324cat myhist
325# cat ~/.config/oil/history_osh
326
327## STDOUT:
328hist1
329hist2
330^D
331echo hist1; echo hist2
332## END
333
334
335#### HISTFILE default value
336
337# it ends with _history
338$SH --norc -i -c 'echo HISTFILE=$HISTFILE' | egrep -q '_history$'
339echo status=$?
340
341## STDOUT:
342status=0
343## END