1 ## our_shell: ysh
2 ## oils_failures_allowed: 0
3
4 #### captureStdout() is like $()
5
6 proc p {
7 var captured = 'captured'
8 var cmd = ^(echo one; echo $captured)
9
10 var stdout = io.captureStdout(cmd)
11 pp test_ (stdout)
12 }
13
14 p
15
16 ## STDOUT:
17 (Str) "one\ncaptured"
18 ## END
19
20 #### captureStdout() failure
21
22 var c = ^(echo one; false; echo two)
23
24 # Hm this prints a message, but no stack trace
25 # Should make it fail I think
26
27 try {
28 var x = io.captureStdout(c)
29 }
30 # This has {"code": 3} because it's an expression error. Should probably
31 pp test_ (_error)
32
33 var x = io.captureStdout(c)
34
35 ## status: 4
36 ## STDOUT:
37 (Dict) {"status":1,"code":4,"message":"captureStdout(): command failed with status 1"}
38 ## END
39
40 #### io->eval() is like eval builtin
41
42 var c = ^(echo one; echo two)
43 var status = io->eval(c)
44
45 # doesn't return anything
46 echo status=$status
47
48 ## STDOUT:
49 one
50 two
51 status=null
52 ## END
53
54 #### captureAll() is like $() but captures stderr and status
55
56 proc p {
57 var captured = 'captured'
58 var cmd = ^(
59 echo stdout
60 echo stderr >&2
61 echo $captured
62 echo $captured >&2
63 printf '\000'
64 exit 42
65 )
66
67 var result = io.captureAll(cmd)
68 pp test_ (result)
69 }
70
71 p
72
73 ## STDOUT:
74 (Dict) {"stdout":"stdout\ncaptured\n\u0000","stderr":"stderr\ncaptured\n","status":42}
75 ## END
76
77 #### captureAll() doesn't fail
78
79 var c = ^(shopt --unset verbose_errexit; echo out; echo err >&2; false; echo two)
80
81 # Hm this prints a message, but no stack trace
82 # Should make it fail I think
83
84 try {
85 var x = io.captureAll(c)
86 }
87 # This has {"code": 3} because it's an expression error. Should probably
88 pp test_ (_error)
89 pp test_ (x)
90
91 ## status: 0
92 ## STDOUT:
93 (Dict) {"code":0}
94 (Dict) {"stdout":"out\n","stderr":"err\n","status":1}
95 ## END
96
97 #### io->eval() with failing command - caller must handle
98
99 var c = ^(echo one; false; echo two)
100
101 try {
102 call io->eval(c)
103 }
104 pp test_ (_error)
105
106 call io->eval(c)
107
108 ## status: 1
109 ## STDOUT:
110 one
111 (Dict) {"code":1}
112 one
113 ## END
114
115 #### io->eval() with exit
116
117 var c = ^(echo one; exit; echo two)
118
119 try {
120 call io->eval(c)
121 }
122 echo 'we do not get here'
123 pp test_ (_error)
124
125
126 ## STDOUT:
127 one
128 ## END
129
130 #### io.glob() respects shell filters
131 touch -- .gitignore -foo.txt foo.txt foo.md
132
133 #GLOBIGNORE='*.md'
134
135 pp test_ (io.glob('*'))
136
137 shopt -s dotglob
138 shopt --unset no_dash_glob
139 pp test_ (io.glob('*'))
140
141 # This meandes dotglob, no_dash_glob
142 GLOBIGNORE='*.md'
143 pp test_ (io.glob('*'))
144
145 ## STDOUT:
146 (List) ["foo.md","foo.txt"]
147 (List) ["-foo.txt",".gitignore","foo.md","foo.txt"]
148 (List) ["-foo.txt",".gitignore","foo.txt"]
149 ## END
150
151
152 #### io.libcGlob() does not respect shell filters
153 touch -- .gitignore -foo.txt foo.txt
154
155 pp test_ (io.libcGlob('*'))
156
157 # NO effect
158 shopt -s dotglob
159 shopt --unset no_dash_glob
160 pp test_ (io.libcGlob('*'))
161
162 GLOBIGNORE='*.md'
163 pp test_ (io.libcGlob('*'))
164
165 ## STDOUT:
166 (List) ["-foo.txt","foo.txt"]
167 (List) ["-foo.txt","foo.txt"]
168 (List) ["-foo.txt","foo.txt"]
169 ## END
170
171