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

253 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## 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#### \A for 24 hour time
175PS1='foo \A bar'
176echo "${PS1@P}" | egrep -q 'foo [0-9][0-9]:[0-9][0-9] bar'
177echo matched=$?
178## STDOUT:
179matched=0
180## END
181
182#### \D{%H:%M} for strftime
183PS1='foo \D{%H:%M} bar'
184echo "${PS1@P}" | egrep -q 'foo [0-9][0-9]:[0-9][0-9] bar'
185echo matched=$?
186
187PS1='foo \D{%H:%M:%S} bar'
188echo "${PS1@P}" | egrep -q 'foo [0-9][0-9]:[0-9][0-9]:[0-9][0-9] bar'
189echo matched=$?
190
191## STDOUT:
192matched=0
193matched=0
194## END
195
196#### \D{} for locale specific strftime
197
198# In bash y.tab.c uses %X when string is empty
199# This doesn't seem to match exactly, but meh for now.
200
201PS1='foo \D{} bar'
202echo "${PS1@P}" | egrep -q '^foo [0-9][0-9]:[0-9][0-9]:[0-9][0-9]( ..)? bar$'
203echo matched=$?
204## STDOUT:
205matched=0
206## END
207
208#### \s and \v for shell and version
209PS1='foo \s bar'
210echo "${PS1@P}" | egrep -q '^foo (bash|osh) bar$'
211echo match=$?
212
213PS1='foo \v bar'
214echo "${PS1@P}" | egrep -q '^foo [0-9.]+ bar$'
215echo match=$?
216
217## STDOUT:
218match=0
219match=0
220## END
221
222#### @P with array
223$SH -c 'echo ${@@P}' dummy a b c
224echo status=$?
225$SH -c 'echo ${*@P}' dummy a b c
226echo status=$?
227$SH -c 'a=(x y); echo ${a@P}' dummy a b c
228echo status=$?
229## STDOUT:
230a b c
231status=0
232a b c
233status=0
234x
235status=0
236## END
237## OK osh STDOUT:
238status=1
239status=1
240x
241status=0
242## END
243
244#### default PS1
245#flags='--norc --noprofile'
246flags='--rcfile /dev/null'
247
248$SH $flags -i -c 'echo "_${PS1}_"'
249
250## STDOUT:
251_\s-\v\$ _
252## END
253