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

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