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

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