OILS / stdlib / ysh / yblocks.ysh View on Github | oilshell.org

53 lines, 19 significant
1#!/usr/bin/env bash
2#
3# Testing library for bash and OSH.
4#
5# Capture status/stdout/stderr, and nq-assert those values.
6
7const __provide__ = :| yb-capture yb-capture-2 |
8
9: ${LIB_OSH=stdlib/osh}
10source $LIB_OSH/two.sh
11
12# There is no yb-run, because you can just use try { } and inspect _error.code
13# There is no yb-redir, because you can just use try >$tmp { } and inspect _error.code
14
15proc yb-capture(; out; ; block) {
16 ### capture status and stdout
17
18 var stdout = ''
19 try {
20 { call io->eval(block) } | read --all (&stdout)
21
22 # Note that this doesn't parse because of expression issue:
23 # call io->eval(block) | read --all (&stdout)
24 # used to be eval (block)
25 }
26 # TODO: if 'block' contains a pipeline, we lose this magic var
27 var result = {status: _pipeline_status[0], stdout}
28
29 #echo 'result-1'
30 #pp test_ (result)
31
32 call out->setValue(result)
33}
34
35proc yb-capture-2(; out; ; block) {
36 ### capture status and stderr
37
38 var stderr = ''
39 try {
40 redir 2>&1 { call io->eval(block); } | read --all (&stderr)
41
42 # Note that this doesn't parse because of expression issue:
43 # call io->eval(block) 2>&1 | read --all (&stderr)
44 # used to be eval (block) 2>&1
45 }
46 #pp test_ (_pipeline_status)
47
48 var result = {status: _pipeline_status[0], stderr}
49 #echo 'result-2'
50 #pp test_ (result)
51
52 call out->setValue(result)
53}