OILS / spec / builtin-history.test.sh View on Github | oils.pub

403 lines, 172 significant
1## tags: interactive
2## compare_shells: bash
3## oils_failures_allowed: 9
4## oils_cpp_failures_allowed: 7
5
6#### history -a
7rm -f tmp
8
9echo '
10history -c
11
12HISTFILE=tmp
13echo 1
14history -a
15cat tmp
16
17echo 2
18
19cat tmp
20' | $SH -i
21
22# match osh's behaviour of echoing ^D for EOF
23case $SH in bash) echo '^D' ;; esac
24
25## STDOUT:
261
27HISTFILE=tmp
28echo 1
29history -a
302
31HISTFILE=tmp
32echo 1
33history -a
34^D
35## END
36
37
38#### history -w writes out the in-memory history to the history file
39
40cd $TMP
41
42# Populate a history file with a command to be overwritten
43echo 'cmd old' > tmp
44HISTFILE=tmp
45history -c
46echo 'cmd new' > /dev/null
47history -w # Overwrite history file
48
49# Verify that old command is gone
50grep 'old' tmp > /dev/null
51echo "found=$?"
52## STDOUT:
53found=1
54## END
55
56
57#### history -r reads from the history file, and appends it to the current history
58
59cd $TMP
60printf "cmd orig%s\n" {1..10} > tmp
61HISTFILE=tmp
62
63history -c
64
65history -r
66history -r
67
68history | grep orig | wc -l
69
70## STDOUT:
7120
72## END
73
74
75#### history -n reads *new* commands from the history file, and appends them to the current history
76# NB: Based on line ranges, not contents
77
78cd $TMP
79
80printf "cmd orig%s\n" {1..10} > tmp1
81cp tmp1 tmp2
82printf "cmd new%s\n" {1..10} >> tmp2
83
84history -c
85HISTFILE=tmp1 history -r
86HISTFILE=tmp2 history -n
87
88history | grep orig | wc -l
89history | grep new | wc -l
90
91## STDOUT:
9210
9310
94## END
95
96
97#### history -c clears in-memory history
98
99$SH --norc -i <<'EOF'
100echo 'foo' > /dev/null
101echo 'bar' > /dev/null
102history -c
103history | wc -l
104EOF
105
106case $SH in bash) echo '^D' ;; esac
107
108## STDOUT:
1091
110^D
111## END
112
113
114#### history -d to delete 1 item
115
116cd $TMP
117HISTFILE=tmp
118printf "cmd orig%s\n" {1..3} > tmp
119history -c
120history -r
121history -d 1
122history | grep orig1 > /dev/null
123echo "status=$?"
124
125## STDOUT:
126status=1
127## END
128
129
130#### history -d to delete history from end
131# bash 4 doesn't support negative indices or ranges
132
133rm -f myhist
134export HISTFILE=myhist
135
136$SH --norc -i <<'EOF'
137
138echo 42
139echo 43
140echo 44
141
142history -a
143
144history -d 1
145echo status=$?
146
147# Invalid integers
148history -d -1
149echo status=$?
150history -d -2
151echo status=$?
152history -d 99
153echo status=$?
154
155case $SH in bash*) echo '^D' ;; esac
156
157EOF
158
159## STDOUT:
16042
16143
16244
163status=0
164status=2
165status=2
166status=2
167^D
168## END
169
170# bash-4.4 used to give more errors like OSH? Weird
171
172## STDOUT:
17342
17443
17544
176status=0
177status=0
178status=0
179status=1
180^D
181## END
182
183
184#### HISTFILE is defined initially
185echo '
186if test -n $HISTFILE; then echo exists; fi
187' | $SH -i
188
189# match osh's behaviour of echoing ^D for EOF
190case $SH in bash) echo '^D' ;; esac
191
192## STDOUT:
193exists
194^D
195## END
196
197#### HISTFILE must point to a file
198
199rm -f _tmp/does-not-exist
200
201echo '
202HISTFILE=_tmp/does-not-exist
203history -r
204echo status=$?
205' | $SH -i
206
207# match osh's behaviour of echoing ^D for EOF
208case $SH in bash) echo '^D' ;; esac
209
210## STDOUT:
211status=1
212^D
213## END
214
215#### HISTFILE set to array
216
217echo '
218HISTFILE=(a b c)
219history -a
220echo status=$?
221' | $SH -i
222
223case $SH in bash) echo '^D' ;; esac
224
225# note that bash actually writes the file 'a', since that's ${HISTFILE[0]}
226
227## STDOUT:
228status=1
229^D
230## END
231
232## OK bash STDOUT:
233status=0
234^D
235## END
236
237#### HISTFILE unset
238
239echo '
240unset HISTFILE
241history -a
242echo status=$?
243' | $SH -i
244
245case $SH in bash) echo '^D' ;; esac
246
247## STDOUT:
248status=1
249^D
250## END
251
252
253#### history usage
254
255history not-a-number
256echo status=$?
257
258history 3 too-many
259echo status=$?
260
261## STDOUT:
262status=2
263status=2
264## END
265
266## OK bash STDOUT:
267status=1
268status=1
269## END
270
271
272#### HISTSIZE shrinks the in-memory history when changed
273
274cd $TMP
275printf "cmd %s\n" {1..10} > tmp
276HISTFILE=tmp
277history -c
278history -r
279history | wc -l
280HISTSIZE=5
281history | wc -l
282
283## STDOUT:
28410
2855
286## END
287
288
289#### HISTFILESIZE shrinks the history file when changed
290
291cd $TMP
292printf "cmd %s\n" {1..10} > tmp
293HISTFILE=tmp
294HISTFILESIZE=5
295cat tmp | wc -l
296
297## STDOUT:
2985
299## END
300
301
302#### recording history can be toggled with set -o/+o history
303
304cd $TMP
305printf "echo %s\n" {1..3} > tmp
306HISTFILE=tmp $SH -i <<'EOF'
307set +o history
308echo "not recorded" >> /dev/null
309set -o history
310echo "recorded" >> /dev/null
311EOF
312
313case $SH in bash) echo '^D' ;; esac
314
315grep "not recorded" tmp >> /dev/null
316echo status=$?
317grep "recorded" tmp >> /dev/null
318echo status=$?
319
320## STDOUT:
321^D
322status=1
323status=0
324## END
325
326
327#### shopt histappend toggle check
328
329shopt -s histappend
330echo status=$?
331shopt -p histappend
332shopt -u histappend
333echo status=$?
334shopt -p histappend
335
336# match osh's behaviour of echoing ^D for EOF
337case $SH in bash) echo '^D' ;; esac
338
339## STDOUT:
340status=0
341shopt -s histappend
342status=0
343shopt -u histappend
344^D
345## END
346
347
348#### shopt histappend - osh ignores shopt and appends, bash sometimes overwrites
349# When set, bash always appends when exiting, no matter what.
350# When unset, bash will append anyway as long the # of new commands < the hist length
351# Either way, the file is truncated to HISTFILESIZE afterwards.
352# osh always appends
353
354cd $TMP
355
356export HISTSIZE=10
357export HISTFILESIZE=1000
358export HISTFILE=tmp
359
360histappend_test() {
361 local histopt
362 if [[ "$1" == true ]]; then
363 histopt='shopt -s histappend'
364 else
365 histopt='shopt -u histappend'
366 fi
367
368 printf "cmd orig%s\n" {1..10} > tmp
369
370 $SH --norc -i <<EOF
371 HISTSIZE=2 # Stifle the history down to 2 commands
372 $histopt
373 # Now run >2 commands to trigger bash's overwrite behavior
374 echo cmd new1 > /dev/null
375 echo cmd new2 > /dev/null
376 echo cmd new3 > /dev/null
377EOF
378
379 case $SH in bash) echo '^D' ;; esac
380}
381
382# If we force histappend, bash won't overwrite the history file
383histappend_test true
384grep "orig" tmp > /dev/null
385echo status=$?
386
387# If we don't force histappend, bash will overwrite the history file when the number of cmds exceeds HISTSIZE
388histappend_test false
389grep "orig" tmp > /dev/null
390echo status=$?
391
392## STDOUT:
393^D
394status=0
395^D
396status=1
397## OK osh STDOUT:
398^D
399status=0
400^D
401status=0
402## END
403