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

349 lines, 182 significant
1## compare_shells: bash
2## oils_failures_allowed: 8
3
4#### sh -i
5# Notes:
6# - OSH prompt goes to stdout and bash goes to stderr
7# - This test seems to fail on the system bash, but succeeds with spec-bin/bash
8echo 'echo foo' | PS1='[prompt] ' $SH --rcfile /dev/null -i >out.txt 2>err.txt
9fgrep -q '[prompt]' out.txt err.txt
10echo match=$?
11## STDOUT:
12match=0
13## END
14
15#### \[\] are non-printing
16PS1='\[foo\]\$'
17echo "${PS1@P}"
18## STDOUT:
19foo$
20## END
21
22#### literal escapes
23PS1='\a\e\r\n'
24echo "${PS1@P}"
25## stdout-json: "\u0007\u001b\r\n\n"
26
27#### special case for $
28# NOTE: This might be broken for # but it's hard to tell since we don't have
29# root. Could inject __TEST_EUID or something.
30PS1='$'
31echo "${PS1@P}"
32PS1='\$'
33echo "${PS1@P}"
34PS1='\\$'
35echo "${PS1@P}"
36PS1='\\\$'
37echo "${PS1@P}"
38PS1='\\\\$'
39echo "${PS1@P}"
40## STDOUT:
41$
42$
43$
44\$
45\$
46## END
47
48#### PS1 evaluation order
49x='\'
50y='h'
51PS1='$x$y'
52echo "${PS1@P}"
53## STDOUT:
54\h
55## END
56
57#### PS1 evaluation order 2
58foo=foo_value
59dir=$TMP/'$foo' # Directory name with a dollar!
60mkdir -p $dir
61cd $dir
62PS1='\w $foo'
63test "${PS1@P}" = "$PWD foo_value"
64echo status=$?
65## STDOUT:
66status=0
67## END
68
69#### \1004
70PS1='\1004$'
71echo "${PS1@P}"
72## STDOUT:
73@4$
74## END
75
76#### \001 octal literals are supported
77PS1='[\045]'
78echo "${PS1@P}"
79## STDOUT:
80[%]
81## END
82
83#### \555 is beyond max octal byte of \377 and wrapped to m
84PS1='\555$'
85echo "${PS1@P}"
86## STDOUT:
87m$
88## END
89
90#### \x55 hex literals not supported
91PS1='[\x55]'
92echo "${PS1@P}"
93## STDOUT:
94[\x55]
95## END
96
97#### Single backslash
98PS1='\'
99echo "${PS1@P}"
100## STDOUT:
101\
102## END
103
104#### Escaped backslash
105PS1='\\'
106echo "${PS1@P}"
107## STDOUT:
108\
109## END
110
111#### \0001 octal literals are not supported
112PS1='[\0455]'
113echo "${PS1@P}"
114## STDOUT:
115[%5]
116## END
117
118#### \u0001 unicode literals not supported
119PS1='[\u0001]'
120USER=$(whoami)
121test "${PS1@P}" = "[${USER}0001]"
122echo status=$?
123## STDOUT:
124status=0
125## END
126
127#### constant string
128PS1='$ '
129echo "${PS1@P}"
130## STDOUT:
131$
132## END
133
134#### hostname
135
136# NOTE: This test is not hermetic. On my machine the short and long host name
137# are the same.
138
139PS1='\h '
140test "${PS1@P}" = "$(hostname -s) " # short name
141echo status=$?
142PS1='\H '
143test "${PS1@P}" = "$(hostname) "
144echo status=$?
145## STDOUT:
146status=0
147status=0
148## END
149
150#### username
151PS1='\u '
152USER=$(whoami)
153test "${PS1@P}" = "${USER} "
154echo status=$?
155## STDOUT:
156status=0
157## END
158
159#### current working dir
160PS1='\w '
161test "${PS1@P}" = "${PWD} "
162echo status=$?
163## STDOUT:
164status=0
165## END
166
167#### \W is basename of working dir
168PS1='\W '
169test "${PS1@P}" = "$(basename $PWD) "
170echo status=$?
171## STDOUT:
172status=0
173## END
174
175#### \t for 24h time (HH:MM:SS)
176PS1='foo \t bar'
177echo "${PS1@P}" | egrep -q 'foo [0-2][0-9]:[0-5][0-9]:[0-5][0-9] bar'
178echo matched=$?
179
180## STDOUT:
181matched=0
182## END
183
184#### \T for 12h time (HH:MM:SS)
185PS1='foo \T bar'
186echo "${PS1@P}" | egrep -q 'foo [0-1][0-9]:[0-5][0-9]:[0-5][0-9] bar'
187echo matched=$?
188
189## STDOUT:
190matched=0
191## END
192
193#### \@ for 12h time (HH:MM AM/PM)
194PS1='foo \@ bar'
195echo "${PS1@P}" | egrep -q 'foo [0-1][0-9]:[0-5][0-9] (A|P)M bar'
196echo matched=$?
197
198## STDOUT:
199matched=0
200## END
201
202#### \A for 24h time (HH:MM)
203PS1='foo \A bar'
204echo "${PS1@P}" | egrep -q 'foo [0-2][0-9]:[0-5][0-9] bar'
205echo matched=$?
206## STDOUT:
207matched=0
208## END
209
210#### \d for date
211PS1='foo \d bar'
212echo "${PS1@P}" | egrep -q 'foo [A-Z][a-z]+ [A-Z][a-z]+ [0-9]+ bar'
213echo matched=$?
214
215## STDOUT:
216matched=0
217## END
218
219#### \D{%H:%M} for strftime
220PS1='foo \D{%H:%M} bar'
221echo "${PS1@P}" | egrep -q 'foo [0-9][0-9]:[0-9][0-9] bar'
222echo matched=$?
223
224PS1='foo \D{%H:%M:%S} bar'
225echo "${PS1@P}" | egrep -q 'foo [0-9][0-9]:[0-9][0-9]:[0-9][0-9] bar'
226echo matched=$?
227
228## STDOUT:
229matched=0
230matched=0
231## END
232
233#### \D{} for locale specific strftime
234
235# In bash y.tab.c uses %X when string is empty
236# This doesn't seem to match exactly, but meh for now.
237
238PS1='foo \D{} bar'
239echo "${PS1@P}" | egrep -q '^foo [0-9][0-9]:[0-9][0-9]:[0-9][0-9]( ..)? bar$'
240echo matched=$?
241## STDOUT:
242matched=0
243## END
244
245#### \s for shell, \v for major.minor version, and \V for full version
246PS1='foo \s bar'
247echo "${PS1@P}" | egrep -q '^foo (bash|osh) bar$'
248echo match=$?
249
250PS1='foo \v bar'
251echo "${PS1@P}" | egrep -q '^foo [0-9]+\.[0-9]+ bar$'
252echo match=$?
253
254PS1='foo \V bar'
255echo "${PS1@P}" | egrep -q '^foo [0-9]+\.[0-9]+\.[0-9]+ bar$'
256echo match=$?
257
258## STDOUT:
259match=0
260match=0
261match=0
262## END
263
264
265#### \j for number of jobs
266set -m # enable job control
267PS1='foo \j bar'
268echo "${PS1@P}" | egrep -q 'foo 0 bar'
269echo matched=$?
270sleep 5 &
271echo "${PS1@P}" | egrep -q 'foo 1 bar'
272echo matched=$?
273kill %%
274fg
275echo "${PS1@P}" | egrep -q 'foo 0 bar'
276echo matched=$?
277
278## STDOUT:
279matched=0
280matched=0
281sleep 5
282matched=0
283## END
284
285#### \l for TTY device basename
286PS1='foo \l bar'
287# FIXME this never an actual TTY when using ./test/spec.sh
288tty="$(tty)"
289if [[ "$tty" == "not a tty" ]]; then
290 expected="tty"
291else
292 expected="$(basename "$tty")"
293fi
294echo "${PS1@P}" | egrep -q "foo $expected bar"
295echo matched=$?
296
297## STDOUT:
298matched=0
299## END
300
301#### \! for history number
302set -o history # enable history
303PS1='foo \! bar'
304history -c # clear history
305echo "${PS1@P}" | egrep -q "foo 1 bar"
306echo matched=$?
307echo "_${PS1@P}" | egrep -q "foo 3 bar"
308echo matched=$?
309
310## STDOUT:
311matched=0
312matched=0
313## END
314
315#### \# for command number
316PS1='foo \# bar'
317prev_cmd_num="$(echo "${PS1@P}" | egrep -o 'foo [0-9]+ bar' | sed -E 's/foo ([0-9]+) bar/\1/')"
318echo "${PS1@P}" | egrep -q "foo $((prev_cmd_num + 1)) bar"
319echo matched=$?
320
321## STDOUT:
322matched=0
323## END
324
325#### @P with array
326$SH -c 'echo ${@@P}' dummy a b c
327echo status=$?
328$SH -c 'echo ${*@P}' dummy a b c
329echo status=$?
330$SH -c 'a=(x y); echo ${a@P}' dummy a b c
331echo status=$?
332## STDOUT:
333a b c
334status=0
335a b c
336status=0
337x
338status=0
339## END
340
341#### default PS1
342#flags='--norc --noprofile'
343flags='--rcfile /dev/null'
344
345$SH $flags -i -c 'echo "_${PS1}_"'
346
347## STDOUT:
348_\s-\v\$ _
349## END