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

145 lines, 47 significant
1## our_shell: ysh
2
3#### default prompt doesn't confuse OSH and YSH
4
5# Special ysh prefix if PS1 is set
6PS1='\$ ' $SH -i -c 'echo "[$PS1]"'
7
8# No prefix if it's not set, since we already have \s for YSH
9$SH -i -c 'echo "[$PS1]"'
10
11## STDOUT:
12[ysh \$ ]
13[\s-\v\$ ]
14## END
15
16#### promptVal() with various values
17
18shopt -s ysh:upgrade
19
20var x = io.promptVal('$')
21
22# We're not root, so it should be $
23echo x=$x
24
25var x = io.promptVal('w')
26if (x === PWD) {
27 echo pass
28} else {
29 echo fail
30}
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
59export PS1='myprompt\$ '
60echo 'echo hi' | $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
70export PS1='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' | $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
92export PS1='myprompt\$ '
93
94cat >yshrc <<'EOF'
95func renderPrompt(io) {
96 return ([42, 43])
97}
98EOF
99
100echo 'echo hi' | $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
111export PS1='myprompt\$ '
112
113cat >yshrc <<'EOF'
114func renderPrompt(io) {
115 error 'oops'
116}
117EOF
118
119echo 'echo hi' | $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
130export PS1='myprompt\$ '
131
132cat >yshrc <<'EOF'
133func renderPrompt() {
134 error 'oops'
135}
136EOF
137
138echo 'echo hi' | $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