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