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

160 lines, 50 significant
1## our_shell: ysh
2
3#### default prompt doesn't confuse OSH and YSH
4
5# Special ysh prefix if PS1 is set
6
7# TODO: use PS1= temp binding
8setglobal ENV.PS1 = r'\$ '
9$[ENV.SH] -i -c 'echo "/$[get(ENV, "PS1")]/ /$[get(__defaults__, "PS1")]/"'
10call propView(ENV)->erase('PS1')
11
12# No prefix if it's not set, since we already have \s for YSH
13$[ENV.SH] -i -c 'echo "/$[get(ENV, "PS1")]/ /$[get(__defaults__, "PS1")]/"'
14
15## STDOUT:
16/ysh \$ / /null/
17/null/ /\s-\v\$ /
18## END
19
20#### promptVal() with $ and w
21
22var x = io.promptVal('$')
23
24# We're not root, so it should be $
25echo x=$x
26
27var x = io.promptVal('w')
28
29assert [x === $(pwd)]
30echo pass
31
32## STDOUT:
33x=$
34pass
35## END
36
37#### promptVal('w') respects ENV.HOME
38
39# ENV not set in spec tests
40var x = io.promptVal('w')
41assert [x.startsWith('/')]
42
43# now set it
44setglobal ENV.HOME = '/home'
45
46var x = io.promptVal('w')
47assert [x.startsWith('~')]
48
49## STDOUT:
50## END
51
52#### promptVal() with invalid chars
53
54# \D{} will be supported with date and time functions
55var x = io.promptVal('D')
56echo x=$x
57
58# something else
59var x = io.promptVal('/')
60echo x=$x
61
62var x = io.promptVal('ZZ')
63echo x=$x
64
65## status: 3
66## STDOUT:
67x=<Error: \D{} not in promptVal()>
68x=<Error: \/ is invalid or unimplemented in $PS1>
69## END
70
71
72#### ysh respects PS1
73
74setglobal ENV.PS1 = r'myprompt\$ '
75echo 'echo hi' | $[ENV.SH] -i
76
77## STDOUT:
78hi
79^D
80## END
81## stderr-json: "ysh myprompt$ ysh myprompt$ "
82
83#### ysh respects renderPrompt() over PS1
84
85setglobal ENV.PS1 = r'myprompt\$ '
86
87cat >yshrc <<'EOF'
88func renderPrompt(io) {
89 var parts = []
90 call parts->append('hi')
91 call parts->append(io.promptVal('$'))
92 call parts->append(' ')
93 return (join(parts))
94}
95EOF
96
97echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
98
99## STDOUT:
100hi
101^D
102## END
103## stderr-json: "hi$ hi$ "
104
105#### renderPrompt() doesn't return string
106
107setglobal ENV.PS1 = r'myprompt\$ '
108
109cat >yshrc <<'EOF'
110func renderPrompt(io) {
111 return ([42, 43])
112}
113EOF
114
115echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
116
117## STDOUT:
118hi
119^D
120## END
121## stderr-json: "<Error: renderPrompt() should return Str, got List> <Error: renderPrompt() should return Str, got List> "
122
123
124#### renderPrompt() raises error
125
126setglobal ENV.PS1 = r'myprompt\$ '
127
128cat >yshrc <<'EOF'
129func renderPrompt(io) {
130 error 'oops'
131}
132EOF
133
134echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
135
136## STDOUT:
137hi
138^D
139## END
140## stderr-json: "<Runtime error: oops><Runtime error: oops>"
141
142
143#### renderPrompt() has wrong signature
144
145setglobal ENV.PS1 = r'myprompt\$ '
146
147cat >yshrc <<'EOF'
148func renderPrompt() {
149 error 'oops'
150}
151EOF
152
153echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
154
155## STDOUT:
156hi
157^D
158## END
159## stderr-json: "<Runtime error: Func 'renderPrompt' takes no positional args, but got 1><Runtime error: Func 'renderPrompt' takes no positional args, but got 1>"
160