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

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