1
2 #### sh -i
3 # Notes:
4 # - OSH prompt goes to stdout and bash goes to stderr
5 # - This test seems to fail on the system bash, but succeeds with spec-bin/bash
6 echo 'echo foo' | PS1='[prompt] ' $SH --rcfile /dev/null -i >out.txt 2>err.txt
7 fgrep -q '[prompt]' out.txt err.txt
8 echo match=$?
9 ## STDOUT:
10 match=0
11 ## END
12
13 #### \[\] are non-printing
14 PS1='\[foo\]\$'
15 echo "${PS1@P}"
16 ## STDOUT:
17 foo$
18 ## END
19
20 #### literal escapes
21 PS1='\a\e\r\n'
22 echo "${PS1@P}"
23 ## stdout-json: "\u0007\u001b\r\n\n"
24
25 #### special case for $
26 # NOTE: This might be broken for # but it's hard to tell since we don't have
27 # root. Could inject __TEST_EUID or something.
28 PS1='$'
29 echo "${PS1@P}"
30 PS1='\$'
31 echo "${PS1@P}"
32 PS1='\\$'
33 echo "${PS1@P}"
34 PS1='\\\$'
35 echo "${PS1@P}"
36 PS1='\\\\$'
37 echo "${PS1@P}"
38 ## STDOUT:
39 $
40 $
41 $
42 \$
43 \$
44 ## END
45
46 #### PS1 evaluation order
47 x='\'
48 y='h'
49 PS1='$x$y'
50 echo "${PS1@P}"
51 ## STDOUT:
52 \h
53 ## END
54
55 #### PS1 evaluation order 2
56 foo=foo_value
57 dir=$TMP/'$foo' # Directory name with a dollar!
58 mkdir -p $dir
59 cd $dir
60 PS1='\w $foo'
61 test "${PS1@P}" = "$PWD foo_value"
62 echo status=$?
63 ## STDOUT:
64 status=0
65 ## END
66
67 #### \1004
68 PS1='\1004$'
69 echo "${PS1@P}"
70 ## STDOUT:
71 @4$
72 ## END
73
74 #### \001 octal literals are supported
75 PS1='[\045]'
76 echo "${PS1@P}"
77 ## STDOUT:
78 [%]
79 ## END
80
81 #### \555 is beyond max octal byte of \377 and wrapped to m
82 PS1='\555$'
83 echo "${PS1@P}"
84 ## STDOUT:
85 m$
86 ## END
87
88 #### \x55 hex literals not supported
89 PS1='[\x55]'
90 echo "${PS1@P}"
91 ## STDOUT:
92 [\x55]
93 ## END
94
95 #### Single backslash
96 PS1='\'
97 echo "${PS1@P}"
98 ## BUG bash stdout-json: "\\\u0002\n"
99 ## STDOUT:
100 \
101 ## END
102
103 #### Escaped backslash
104 PS1='\\'
105 echo "${PS1@P}"
106 ## BUG bash stdout-json: "\\\u0002\n"
107 ## STDOUT:
108 \
109 ## END
110
111 #### \0001 octal literals are not supported
112 PS1='[\0455]'
113 echo "${PS1@P}"
114 ## STDOUT:
115 [%5]
116 ## END
117
118 #### \u0001 unicode literals not supported
119 PS1='[\u0001]'
120 USER=$(whoami)
121 test "${PS1@P}" = "[${USER}0001]"
122 echo status=$?
123 ## STDOUT:
124 status=0
125 ## END
126
127 #### constant string
128 PS1='$ '
129 echo "${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
139 PS1='\h '
140 test "${PS1@P}" = "$(hostname -s) " # short name
141 echo status=$?
142 PS1='\H '
143 test "${PS1@P}" = "$(hostname) "
144 echo status=$?
145 ## STDOUT:
146 status=0
147 status=0
148 ## END
149
150 #### username
151 PS1='\u '
152 USER=$(whoami)
153 test "${PS1@P}" = "${USER} "
154 echo status=$?
155 ## STDOUT:
156 status=0
157 ## END
158
159 #### current working dir
160 PS1='\w '
161 test "${PS1@P}" = "${PWD} "
162 echo status=$?
163 ## STDOUT:
164 status=0
165 ## END
166
167 #### \W is basename of working dir
168 PS1='\W '
169 test "${PS1@P}" = "$(basename $PWD) "
170 echo status=$?
171 ## STDOUT:
172 status=0
173 ## END
174
175 #### \A for 24 hour time
176 PS1='foo \A bar'
177 echo "${PS1@P}" | egrep -q 'foo [0-9][0-9]:[0-9][0-9] bar'
178 echo matched=$?
179 ## STDOUT:
180 matched=0
181 ## END
182
183 #### \D{%H:%M} for strftime
184 PS1='foo \D{%H:%M} bar'
185 echo "${PS1@P}" | egrep -q 'foo [0-9][0-9]:[0-9][0-9] bar'
186 echo matched=$?
187
188 PS1='foo \D{%H:%M:%S} bar'
189 echo "${PS1@P}" | egrep -q 'foo [0-9][0-9]:[0-9][0-9]:[0-9][0-9] bar'
190 echo matched=$?
191
192 ## STDOUT:
193 matched=0
194 matched=0
195 ## END
196
197 #### \D{} for locale specific strftime
198
199 # In bash y.tab.c uses %X when string is empty
200 # This doesn't seem to match exactly, but meh for now.
201
202 PS1='foo \D{} bar'
203 echo "${PS1@P}" | egrep -q '^foo [0-9][0-9]:[0-9][0-9]:[0-9][0-9]( ..)? bar$'
204 echo matched=$?
205 ## STDOUT:
206 matched=0
207 ## END
208
209 #### \s and \v for shell and version
210 PS1='foo \s bar'
211 echo "${PS1@P}" | egrep -q '^foo (bash|osh) bar$'
212 echo match=$?
213
214 PS1='foo \v bar'
215 echo "${PS1@P}" | egrep -q '^foo [0-9.]+ bar$'
216 echo match=$?
217
218 ## STDOUT:
219 match=0
220 match=0
221 ## END
222
223 #### @P with array
224 $SH -c 'echo ${@@P}' dummy a b c
225 echo status=$?
226 $SH -c 'echo ${*@P}' dummy a b c
227 echo status=$?
228 $SH -c 'a=(x y); echo ${a@P}' dummy a b c
229 echo status=$?
230 ## STDOUT:
231 a b c
232 status=0
233 a b c
234 status=0
235 x
236 status=0
237 ## END
238 ## OK osh STDOUT:
239 status=1
240 status=1
241 status=1
242 ## END
243
244 #### default PS1
245 #flags='--norc --noprofile'
246 flags='--rcfile /dev/null'
247
248 $SH $flags -i -c 'echo "_${PS1}_"'
249
250 ## STDOUT:
251 _\s-\v\$ _
252 ## END
253