OILS / spec / interactive.test.sh View on Github | oils.pub

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