OILS / spec / ysh-method-io.test.sh View on Github | oils.pub

129 lines, 60 significant
1## our_shell: ysh
2## oils_failures_allowed: 0
3
4#### captureStdout() is like $()
5
6proc 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
14p
15
16## STDOUT:
17(Str) "one\ncaptured"
18## END
19
20#### captureStdout() failure
21
22var 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
27try {
28 var x = io.captureStdout(c)
29}
30# This has {"code": 3} because it's an expression error. Should probably
31pp test_ (_error)
32
33var 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
42var c = ^(echo one; echo two)
43var status = io->eval(c)
44
45# doesn't return anything
46echo status=$status
47
48## STDOUT:
49one
50two
51status=null
52## END
53
54#### captureAll() is like $() but captures stderr and status
55
56proc 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
71p
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
79var 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
84try {
85 var x = io.captureAll(c)
86}
87# This has {"code": 3} because it's an expression error. Should probably
88pp test_ (_error)
89pp 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
99var c = ^(echo one; false; echo two)
100
101try {
102 call io->eval(c)
103}
104pp test_ (_error)
105
106call io->eval(c)
107
108## status: 1
109## STDOUT:
110one
111(Dict) {"code":1}
112one
113## END
114
115#### io->eval() with exit
116
117var c = ^(echo one; exit; echo two)
118
119try {
120 call io->eval(c)
121}
122echo 'we do not get here'
123pp test_ (_error)
124
125
126## STDOUT:
127one
128## END
129