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

45 lines, 18 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
7#module yblocks || return 0
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 stderr
17
18 var stdout = ''
19 try {
20 eval (block) | read --all (&stdout)
21 }
22 # TODO: if 'block' contains a pipeline, we lose this magic var
23 var result = {status: _pipeline_status[0], stdout}
24
25 #echo 'result-1'
26 #pp line (result)
27
28 call out->setValue(result)
29}
30
31proc yb-capture-2(; out; ; block) {
32 ### capture status and stderr
33
34 var stderr = ''
35 try {
36 eval (block) 2>&1 | read --all (&stderr)
37 }
38 #pp line (_pipeline_status)
39
40 var result = {status: _pipeline_status[0], stderr}
41 #echo 'result-2'
42 #pp line (result)
43
44 call out->setValue(result)
45}