OILS / spec / prompt.test.sh View on Github | oilshell.org

255 lines, 127 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## BUG bash stdout-json: "\\\u0002\n"
100## STDOUT:
101\
102## END
103
104#### Escaped backslash
105PS1='\\'
106echo "${PS1@P}"
107## BUG bash stdout-json: "\\\u0002\n"
108## STDOUT:
109\
110## END
111
112#### \0001 octal literals are not supported
113PS1='[\0455]'
114echo "${PS1@P}"
115## STDOUT:
116[%5]
117## END
118
119#### \u0001 unicode literals not supported
120PS1='[\u0001]'
121USER=$(whoami)
122test "${PS1@P}" = "[${USER}0001]"
123echo status=$?
124## STDOUT:
125status=0
126## END
127
128#### constant string
129PS1='$ '
130echo "${PS1@P}"
131## STDOUT:
132$
133## END
134
135#### hostname
136
137# NOTE: This test is not hermetic. On my machine the short and long host name
138# are the same.
139
140PS1='\h '
141test "${PS1@P}" = "$(hostname -s) " # short name
142echo status=$?
143PS1='\H '
144test "${PS1@P}" = "$(hostname) "
145echo status=$?
146## STDOUT:
147status=0
148status=0
149## END
150
151#### username
152PS1='\u '
153USER=$(whoami)
154test "${PS1@P}" = "${USER} "
155echo status=$?
156## STDOUT:
157status=0
158## END
159
160#### current working dir
161PS1='\w '
162test "${PS1@P}" = "${PWD} "
163echo status=$?
164## STDOUT:
165status=0
166## END
167
168#### \W is basename of working dir
169PS1='\W '
170test "${PS1@P}" = "$(basename $PWD) "
171echo status=$?
172## STDOUT:
173status=0
174## END
175
176#### \A for 24 hour time
177PS1='foo \A bar'
178echo "${PS1@P}" | egrep -q 'foo [0-9][0-9]:[0-9][0-9] bar'
179echo matched=$?
180## STDOUT:
181matched=0
182## END
183
184#### \D{%H:%M} for strftime
185PS1='foo \D{%H:%M} bar'
186echo "${PS1@P}" | egrep -q 'foo [0-9][0-9]:[0-9][0-9] bar'
187echo matched=$?
188
189PS1='foo \D{%H:%M:%S} bar'
190echo "${PS1@P}" | egrep -q 'foo [0-9][0-9]:[0-9][0-9]:[0-9][0-9] bar'
191echo matched=$?
192
193## STDOUT:
194matched=0
195matched=0
196## END
197
198#### \D{} for locale specific strftime
199
200# In bash y.tab.c uses %X when string is empty
201# This doesn't seem to match exactly, but meh for now.
202
203PS1='foo \D{} bar'
204echo "${PS1@P}" | egrep -q '^foo [0-9][0-9]:[0-9][0-9]:[0-9][0-9]( ..)? bar$'
205echo matched=$?
206## STDOUT:
207matched=0
208## END
209
210#### \s and \v for shell and version
211PS1='foo \s bar'
212echo "${PS1@P}" | egrep -q '^foo (bash|osh) bar$'
213echo match=$?
214
215PS1='foo \v bar'
216echo "${PS1@P}" | egrep -q '^foo [0-9.]+ bar$'
217echo match=$?
218
219## STDOUT:
220match=0
221match=0
222## END
223
224#### @P with array
225$SH -c 'echo ${@@P}' dummy a b c
226echo status=$?
227$SH -c 'echo ${*@P}' dummy a b c
228echo status=$?
229$SH -c 'a=(x y); echo ${a@P}' dummy a b c
230echo status=$?
231## STDOUT:
232a b c
233status=0
234a b c
235status=0
236x
237status=0
238## END
239## OK osh STDOUT:
240status=1
241status=1
242x
243status=0
244## END
245
246#### default PS1
247#flags='--norc --noprofile'
248flags='--rcfile /dev/null'
249
250$SH $flags -i -c 'echo "_${PS1}_"'
251
252## STDOUT:
253_\s-\v\$ _
254## END
255