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 #### io->eval() with failing command - caller must handle
55
56 var c = ^(echo one; false; echo two)
57
58 try {
59 call io->eval(c)
60 }
61 pp test_ (_error)
62
63 call io->eval(c)
64
65 ## status: 1
66 ## STDOUT:
67 one
68 (Dict) {"code":1}
69 one
70 ## END
71
72 #### io->eval() with exit
73
74 var c = ^(echo one; exit; echo two)
75
76 try {
77 call io->eval(c)
78 }
79 echo 'we do not get here'
80 pp test_ (_error)
81
82
83 ## STDOUT:
84 one
85 ## END
86