1# history-y.tst: yash-specific test of the history built-in
2
3if ! testee -c 'command -bv history' >/dev/null; then
4    skip="true"
5fi
6
7test_oE -e 0 'history is a semi-special built-in'
8command -V history
9__IN__
10history: a semi-special built-in
11__OUT__
12
13cat >rcfile1 <<\__END__
14PS1= PS2= HISTFILE=$PWD/$histfile HISTSIZE=$histsize
15unset HISTRMDUP
16__END__
17
18(
19export histfile=histfile$LINENO histsize=100
20
21# Prepare the first history entry w/o running a test case.
22testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
23echo foo 1
24echo foo 2
25echo foo 3
26echo foo 4
27echo foo 5
28echo foo 6
29echo foo 7
30echo foo 8
31echo foo 9
32echo foo 10
33echo foo 11
34echo foo 12
35echo foo 13
36echo foo 14
37echo foo 15
38echo foo 16
39echo foo 17
40echo foo 18
41echo foo 19
42echo foo 20
43__END__
44
45test_oE -e 0 'whole history is printed by default' -i +m --rcfile="rcfile1"
46history
47__IN__
481	echo foo 1
492	echo foo 2
503	echo foo 3
514	echo foo 4
525	echo foo 5
536	echo foo 6
547	echo foo 7
558	echo foo 8
569	echo foo 9
5710	echo foo 10
5811	echo foo 11
5912	echo foo 12
6013	echo foo 13
6114	echo foo 14
6215	echo foo 15
6316	echo foo 16
6417	echo foo 17
6518	echo foo 18
6619	echo foo 19
6720	echo foo 20
6821	history
69__OUT__
70
71)
72
73(
74export histfile=histfile$LINENO histsize=100
75
76# Prepare the first history entry w/o running a test case.
77testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
78echo foo 1
79echo foo 2
80echo foo 3
81echo foo 4
82echo foo 5
83echo foo 6
84echo foo 7
85echo foo 8
86echo foo 9
87echo foo 10
88__END__
89
90test_oE -e 0 'printing specified number of entries' -i +m --rcfile="rcfile1"
91history 7
92__IN__
935	echo foo 5
946	echo foo 6
957	echo foo 7
968	echo foo 8
979	echo foo 9
9810	echo foo 10
9911	history 7
100__OUT__
101
102test_OE -e 0 'clearing history (-c)' -i +m --rcfile="rcfile1"
103:
104history -c; history
105__IN__
106
107test_OE -e 0 'clearing history (--clear)' -i +m --rcfile="rcfile1"
108:
109history --clear; history
110__IN__
111
112)
113
114(
115export histfile=histfile$LINENO histsize=100
116
117# Prepare the first history entry w/o running a test case.
118testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
119echo foo 1
120echo foo 2
121echo foo 3
122echo foo 4
123__END__
124
125test_oE -e 0 'deleting entry (-d)' -i +m --rcfile="rcfile1"
126history -d 3; history
127__IN__
1281	echo foo 1
1292	echo foo 2
1304	echo foo 4
1315	history -d 3; history
132__OUT__
133
134)
135
136(
137export histfile=histfile$LINENO histsize=100
138
139# Prepare the first history entry w/o running a test case.
140testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
141echo foo 1
142echo foo 2
143echo foo 3
144echo foo 4
145echo foo 5
146echo foo 6
147__END__
148
149test_oE -e 0 'deleting entries (-d, --delete)' -i +m --rcfile="rcfile1"
150history -d 2 --delete=echo; history
151__IN__
1521	echo foo 1
1533	echo foo 3
1544	echo foo 4
1555	echo foo 5
1567	history -d 2 --delete=echo; history
157__OUT__
158
159test_Oe -e 1 'deleting non-existing entry (-d)' -i +m --rcfile="rcfile1"
160history -d XXX
161__IN__
162history: no such history entry beginning with `XXX'
163__ERR__
164#`
165
166# Essential part of the test of the -F option is missing because we cannot test
167# it.
168
169test_OE -e 0 'refreshing history file (-F)' -i +m --rcfile="rcfile1"
170history -F
171__IN__
172
173test_OE -e 0 'refreshing history file (--flush-file)' -i +m --rcfile="rcfile1"
174history --flush-file
175__IN__
176
177)
178
179(
180export in=./in$LINENO
181
182cat >"$in" <<\__END__
183echo bar 1\
184echo bar 2
185__END__
186
187(
188export histfile=histfile$LINENO histsize=100
189
190# Prepare the first history entry w/o running a test case.
191testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
192echo foo 1
193echo foo 2
194__END__
195
196test_oE -e 0 'reading entries (-r)' -i +m --rcfile="rcfile1"
197history -r "$in"
198history
199__IN__
2001	echo foo 1
2012	echo foo 2
2023	history -r "$in"
2034	echo bar 1\
2045	echo bar 2
2056	history
206__OUT__
207
208)
209
210(
211export histfile=histfile$LINENO histsize=100
212
213# Prepare the first history entry w/o running a test case.
214testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
215echo foo 1
216echo foo 2
217__END__
218
219test_oE -e 0 'reading entries (--read)' -i +m --rcfile="rcfile1"
220history --read="$in"
221history
222__IN__
2231	echo foo 1
2242	echo foo 2
2253	history --read="$in"
2264	echo bar 1\
2275	echo bar 2
2286	history
229__OUT__
230
231)
232
233test_O -d -e 1 'reading commands from non-existing file'
234history -r _no_such_file_
235__IN__
236
237)
238
239(
240export histfile=histfile$LINENO histsize=100
241
242# Prepare the first history entry w/o running a test case.
243testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
244echo foo 1
245echo foo 2
246__END__
247
248test_oE 'replacing entry (-s)' -i +m --rcfile="rcfile1"
249history -s 'echo bar X'
250echo [$?]
251history
252__IN__
253[0]
2541	echo foo 1
2552	echo foo 2
2563	echo bar X
2574	echo [$?]
2585	history
259__OUT__
260
261)
262
263(
264export histfile=histfile$LINENO histsize=100
265
266# Prepare the first history entry w/o running a test case.
267testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
268echo foo 1
269echo foo 2
270__END__
271
272test_oE 'replacing entry (--set)' -i +m --rcfile="rcfile1"
273history --set='echo bar X'
274echo [$?]
275history
276__IN__
277[0]
2781	echo foo 1
2792	echo foo 2
2803	echo bar X
2814	echo [$?]
2825	history
283__OUT__
284
285)
286
287(
288export histfile=histfile$LINENO histsize=100
289
290# Prepare the first history entry w/o running a test case.
291testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
292echo foo 1
293echo foo 2
294__END__
295
296test_oE 'replacing and adding entries (-s, --set)' -i +m --rcfile="rcfile1"
297history -s 'echo bar X' --set='echo bar Y' -s 'echo bar Z'
298echo [$?]
299history
300__IN__
301[0]
3021	echo foo 1
3032	echo foo 2
3043	echo bar X
3054	echo bar Y
3065	echo bar Z
3076	echo [$?]
3087	history
309__OUT__
310
311)
312
313(
314export histfile=histfile$LINENO histsize=100
315
316test_oE 'adding entry to empty history (-s)' -i +m --rcfile="rcfile1"
317history -c; history -s 'echo foo 1'
318echo [$?]
319history
320__IN__
321[0]
3221	echo foo 1
3232	echo [$?]
3243	history
325__OUT__
326
327)
328
329(
330export histfile=histfile$LINENO histsize=100 out=./out$LINENO
331
332# Prepare the first history entry w/o running a test case.
333testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
334echo foo 1
335echo foo 2
336__END__
337
338test_oE 'writing entries to new file (-w)' -i +m --rcfile="rcfile1"
339history -w "$out"
340echo [$?]
341cat "$out"
342__IN__
343[0]
344echo foo 1
345echo foo 2
346history -w "$out"
347__OUT__
348
349)
350
351(
352export histfile=histfile$LINENO histsize=100 out=./out$LINENO
353
354# Prepare the first history entry w/o running a test case.
355testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
356echo foo 1
357echo foo 2
358__END__
359
360test_oE 'writing entries to new file (--write)' -i +m --rcfile="rcfile1"
361history --write="$out"
362echo [$?]
363cat "$out"
364__IN__
365[0]
366echo foo 1
367echo foo 2
368history --write="$out"
369__OUT__
370
371)
372
373(
374export histfile=histfile$LINENO histsize=100 out=./out$LINENO
375
376# Prepare the first history entry w/o running a test case.
377testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
378echo foo 1
379echo foo 2
380__END__
381
382cat >"$out" <<\__END__
383This file is overwritten.
384__END__
385
386test_oE 'writing entries to existing file (-w)' -i +m --rcfile="rcfile1"
387history -w "$out"
388echo [$?]
389cat "$out"
390__IN__
391[0]
392echo foo 1
393echo foo 2
394history -w "$out"
395__OUT__
396
397)
398
399(
400export histfile=histfile$LINENO histsize=100 out=./out$LINENO
401
402# Prepare the first history entry w/o running a test case.
403testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
404echo foo 1
405echo foo 2
406__END__
407
408>"$out"
409chmod a-w "$out"
410
411# Skip if we're root.
412if { echo >>"$out"; } 2>/dev/null; then
413    skip="true"
414fi
415
416test_O -d -e 1 'writing entries to protected file (-w)' -i +m --rcfile="rcfile1"
417history -w "$out"
418__IN__
419
420)
421
422(
423export histfile=histfile$LINENO histsize=100 in=./in$LINENO out=./out$LINENO
424
425cat >"$in" <<\__END__
426echo bar 1
427echo bar 2
428echo bar 3
429__END__
430
431# Prepare the first history entry w/o running a test case.
432testee -is +m --rcfile="rcfile1" >/dev/null <<\__END__
433echo foo 1
434echo foo 2
435echo foo 3
436__END__
437
438test_oE 'combination of options and operand' -i +m --rcfile="rcfile1"
439history -d 2 -w "$out" -cF -r "$in" -s 'set' 2
440echo [$?]
441history
442echo ---
443cat "$out"
444__IN__
4452	echo bar 2
4463	set
447[0]
4481	echo bar 1
4492	echo bar 2
4503	set
4514	echo [$?]
4525	history
453---
454echo foo 1
455echo foo 3
456history -d 2 -w "$out" -cF -r "$in" -s 'set' 2
457__OUT__
458
459)
460
461test_Oe -e 2 'too many operands'
462history 1 2
463__IN__
464history: too many operands are specified
465__ERR__
466
467test_Oe -e 2 'invalid option'
468history --no-such-option
469__IN__
470history: `--no-such-option' is not a valid option
471__ERR__
472#`
473
474(
475export histfile=histfile$LINENO histsize=100
476
477test_O -d -e 1 'printing to closed stream' -i +m --rcfile="rcfile1"
478:
479history >&-
480__IN__
481
482)
483
484# vim: set ft=sh ts=8 sts=4 sw=4 noet:
485