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

145 lines, 46 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 "/$[get(ENV, "PS1")]/ /$[get(__defaults__, "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 "/$[get(ENV, "PS1")]/ /$[get(__defaults__, "PS1")]/"'
12
13## STDOUT:
14/ysh \$ / /null/
15/null/ /\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')
28
29assert [x === ENV.PWD]
30echo pass
31
32## STDOUT:
33x=$
34pass
35## END
36
37#### promptVal() with invalid chars
38
39# \D{} will be supported with date and time functions
40var x = io.promptVal('D')
41echo x=$x
42
43# something else
44var x = io.promptVal('/')
45echo x=$x
46
47var x = io.promptVal('ZZ')
48echo x=$x
49
50## status: 3
51## STDOUT:
52x=<Error: \D{} not in promptVal()>
53x=<Error: \/ is invalid or unimplemented in $PS1>
54## END
55
56
57#### ysh respects PS1
58
59setglobal ENV.PS1 = r'myprompt\$ '
60echo 'echo hi' | $[ENV.SH] -i
61
62## STDOUT:
63hi
64^D
65## END
66## stderr-json: "ysh myprompt$ ysh myprompt$ "
67
68#### ysh respects renderPrompt() over PS1
69
70setglobal ENV.PS1 = r'myprompt\$ '
71
72cat >yshrc <<'EOF'
73func renderPrompt(io) {
74 var parts = []
75 call parts->append('hi')
76 call parts->append(io.promptVal('$'))
77 call parts->append(' ')
78 return (join(parts))
79}
80EOF
81
82echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
83
84## STDOUT:
85hi
86^D
87## END
88## stderr-json: "hi$ hi$ "
89
90#### renderPrompt() doesn't return string
91
92setglobal ENV.PS1 = r'myprompt\$ '
93
94cat >yshrc <<'EOF'
95func renderPrompt(io) {
96 return ([42, 43])
97}
98EOF
99
100echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
101
102## STDOUT:
103hi
104^D
105## END
106## stderr-json: "<Error: renderPrompt() should return Str, got List> <Error: renderPrompt() should return Str, got List> "
107
108
109#### renderPrompt() raises error
110
111setglobal ENV.PS1 = r'myprompt\$ '
112
113cat >yshrc <<'EOF'
114func renderPrompt(io) {
115 error 'oops'
116}
117EOF
118
119echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
120
121## STDOUT:
122hi
123^D
124## END
125## stderr-json: "<Runtime error: oops><Runtime error: oops>"
126
127
128#### renderPrompt() has wrong signature
129
130setglobal ENV.PS1 = r'myprompt\$ '
131
132cat >yshrc <<'EOF'
133func renderPrompt() {
134 error 'oops'
135}
136EOF
137
138echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
139
140## STDOUT:
141hi
142^D
143## END
144## stderr-json: "<Runtime error: Func 'renderPrompt' takes no positional args, but got 1><Runtime error: Func 'renderPrompt' takes no positional args, but got 1>"
145