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

342 lines, 174 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
265PS1='foo \j bar'
266echo "${PS1@P}" | egrep -q 'foo 0 bar'
267echo matched=$?
268sleep 999 &
269echo "${PS1@P}" | egrep -q 'foo 1 bar'
270echo matched=$?
271kill %%
272fg
273echo "${PS1@P}" | egrep -q 'foo 0 bar'
274echo matched=$?
275
276## STDOUT:
277matched=0
278matched=0
279matched=0
280## END
281
282#### \l for TTY device basename
283PS1='foo \l bar'
284tty_device_basename="$(basename "$(tty)")"
285echo "${PS1@P}" | egrep -q "foo $tty_device_basename bar"
286echo matched=$?
287
288## STDOUT:
289matched=0
290## END
291
292#### \! for history number
293PS1='foo \! bar'
294history -c # clear history
295echo "${PS1@P}" | egrep -q "foo 1 bar"
296echo matched=$?
297echo "_${PS1@P}" | egrep -q "foo 2 bar"
298echo matched=$?
299
300## STDOUT:
301matched=0
302matched=0
303## END
304
305#### \# for command number
306(
307osh -i <<EOF
308PS1='foo \# bar'
309echo "${PS1@P}"
310EOF
311) | tee >(egrep -q 'foo 2 bar') >(egrep -q 'foo 3 bar')
312echo matched=$?
313
314## STDOUT:
315matched=0
316## END
317
318#### @P with array
319$SH -c 'echo ${@@P}' dummy a b c
320echo status=$?
321$SH -c 'echo ${*@P}' dummy a b c
322echo status=$?
323$SH -c 'a=(x y); echo ${a@P}' dummy a b c
324echo status=$?
325## STDOUT:
326a b c
327status=0
328a b c
329status=0
330x
331status=0
332## END
333
334#### default PS1
335#flags='--norc --noprofile'
336flags='--rcfile /dev/null'
337
338$SH $flags -i -c 'echo "_${PS1}_"'
339
340## STDOUT:
341_\s-\v\$ _
342## END