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

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