OILS / spec / pipeline.test.sh View on Github | oilshell.org

234 lines, 81 significant
1## oils_failures_allowed: 0
2## compare_shells: bash dash mksh zsh
3
4#
5# Tests for pipelines.
6# NOTE: Grammatically, ! is part of the pipeline:
7#
8# pipeline : pipe_sequence
9# | Bang pipe_sequence
10
11#### Brace group in pipeline
12{ echo one; echo two; } | tac
13## stdout-json: "two\none\n"
14
15#### For loop starts pipeline
16for w in one two; do
17 echo $w
18done | tac
19## stdout-json: "two\none\n"
20
21#### While Loop ends pipeline
22seq 3 | while read i
23do
24 echo ".$i"
25done
26## stdout-json: ".1\n.2\n.3\n"
27
28#### Redirect in Pipeline
29echo hi 1>&2 | wc -l
30## stdout: 0
31## BUG zsh stdout: 1
32
33#### Pipeline comments
34echo abcd | # input
35 # blank line
36tr a-z A-Z # transform
37## stdout: ABCD
38
39#### Exit code is last status
40echo a | egrep '[0-9]+'
41## status: 1
42
43#### PIPESTATUS
44return3() {
45 return 3
46}
47{ sleep 0.03; exit 1; } | { sleep 0.02; exit 2; } | { sleep 0.01; return3; }
48echo ${PIPESTATUS[@]}
49## stdout: 1 2 3
50## N-I dash status: 2
51## N-I dash stdout-json: ""
52## N-I zsh status: 0
53## N-I zsh stdout-json: "\n"
54
55#### PIPESTATUS is set on simple commands, but NOT in OSH
56case $SH in dash|zsh) exit ;; esac
57
58false
59echo pipestatus ${PIPESTATUS[@]}
60
61## STDOUT:
62pipestatus 1
63## END
64## OK osh STDOUT:
65pipestatus
66## END
67## N-I dash/zsh STDOUT:
68## END
69
70#### PIPESTATUS with shopt -s lastpipe
71shopt -s lastpipe
72return3() {
73 return 3
74}
75{ sleep 0.03; exit 1; } | { sleep 0.02; exit 2; } | { sleep 0.01; return3; }
76echo ${PIPESTATUS[@]}
77## stdout: 1 2 3
78## N-I dash status: 2
79## N-I dash stdout-json: ""
80## N-I zsh status: 0
81## N-I zsh stdout-json: "\n"
82
83#### |&
84stdout_stderr.py |& cat
85## STDOUT:
86STDERR
87STDOUT
88## END
89## status: 0
90## N-I dash/mksh stdout-json: ""
91## N-I dash status: 2
92## N-I osh stdout-json: ""
93## N-I osh status: 1
94
95#### ! turns non-zero into zero
96! $SH -c 'exit 42'; echo $?
97## stdout: 0
98## status: 0
99
100#### ! turns zero into 1
101! $SH -c 'exit 0'; echo $?
102## stdout: 1
103## status: 0
104
105#### ! in if
106if ! echo hi; then
107 echo TRUE
108else
109 echo FALSE
110fi
111## stdout-json: "hi\nFALSE\n"
112## status: 0
113
114#### ! with ||
115! echo hi || echo FAILED
116## stdout-json: "hi\nFAILED\n"
117## status: 0
118
119#### ! with { }
120! { echo 1; echo 2; } || echo FAILED
121## stdout-json: "1\n2\nFAILED\n"
122## status: 0
123
124#### ! with ( )
125! ( echo 1; echo 2 ) || echo FAILED
126## stdout-json: "1\n2\nFAILED\n"
127## status: 0
128
129#### ! is not a command
130v='!'
131$v echo hi
132## status: 127
133
134#### Evaluation of argv[0] in pipeline occurs in child
135${cmd=echo} hi | wc -l
136echo "cmd=$cmd"
137## STDOUT:
1381
139cmd=
140## END
141## BUG zsh STDOUT:
1421
143cmd=echo
144## END
145
146#### bash/dash/mksh run the last command is run in its own process
147echo hi | read line
148echo "line=$line"
149## stdout: line=hi
150## OK bash/dash/mksh stdout: line=
151
152#### shopt -s lastpipe (always on in OSH)
153shopt -s lastpipe
154echo hi | read line
155echo "line=$line"
156## stdout: line=hi
157## N-I dash/mksh stdout: line=
158
159#### shopt -s lastpipe (always on in OSH)
160shopt -s lastpipe
161i=0
162seq 3 | while read line; do
163 (( i++ ))
164done
165echo i=$i
166## stdout: i=3
167## N-I dash/mksh stdout: i=0
168
169
170#### SIGPIPE causes pipeline to die (regression for issue #295)
171cat /dev/urandom | sleep 0.1
172echo ${PIPESTATUS[@]}
173
174# hm bash gives '1 0' which seems wrong
175
176## STDOUT:
177141 0
178## END
179## BUG bash STDOUT:
1801 0
181## END
182## N-I zsh stdout:
183## N-I dash status: 2
184## N-I dash stdout-json: ""
185
186#### Nested pipelines
187{ sleep 0.1 | seq 3; } | cat
188{ sleep 0.1 | seq 10; } | { cat | cat; } | wc -l
189## STDOUT:
1901
1912
1923
19310
194## END
195
196#### Pipeline in eval
197ls /dev/null | eval 'cat | cat' | wc -l
198## STDOUT:
1991
200## END
201
202
203#### shopt -s lastpipe and shopt -s no_last_fork interaction
204
205case $SH in dash) exit ;; esac
206
207$SH -c '
208shopt -s lastpipe
209set -o errexit
210set -o pipefail
211
212ls | false | wc -l'
213echo status=$?
214
215# Why does this give status 0? It should fail
216
217$SH -c '
218shopt -s lastpipe
219shopt -s no_fork_last # OSH only
220set -o errexit
221set -o pipefail
222
223ls | false | wc -l'
224echo status=$?
225
226## STDOUT:
2270
228status=1
2290
230status=1
231## END
232
233## N-I dash STDOUT:
234## END