summary refs log tree commit diff
path: root/tests
blob: 69724177cd8bd18f0a5c78c04e464db893075dd1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/sh
printf '1..47\n'

set -e

xe() { "${XE:-./xe}" "$@"; }

necho() { for a; do printf '%s\n' "$a"; done; }

check_output() {
	msg=$1
	expected="$(cat)"
	shift
	if output="$(eval "$@" 2>&1)"; then
		if [ "$output" = "$expected" ]; then
			printf 'ok - %s\n' "$msg"
			return
		fi
	fi
	printf 'not ok - %s\n' "$msg"
	if [ "$output" != "$expected" ]; then
		printf 'Unexpected output:\n%s\n' "$output" | sed 's/^/# /'
	fi
}

printf '# simple tests\n'

check_output 'single argument run' 'necho 1 2 3 | xe echo' <<EOF
1
2
3
EOF

check_output 'dual argument run' 'necho 1 2 3 4 5 | xe -N2 echo' <<EOF
1 2
3 4
5
EOF

check_output 'unlimited argument run' 'necho 1 2 3 4 5 | xe -N0 echo' <<EOF
1 2 3 4 5
EOF

check_output 'empty input run' 'true | xe echo a' <<EOF
EOF

check_output 'dry run' 'necho a b c | xe -n echo x' <<EOF
echo x a
echo x b
echo x c
EOF

check_output 'dry run quoting' 'necho a "b b" c | xe -n echo x' <<EOF
echo x a
echo x 'b b'
echo x c
EOF

check_output 'verbose run' 'necho a b c | xe -v echo x' <<EOF
echo x a
x a
echo x b
x b
echo x c
x c
EOF

check_output 'with no command' 'necho 1 2 3 | xe -N2' <<EOF
1
2
3
EOF

check_output 'using {}' 'necho 1 2 3 | xe echo a {} x' <<EOF
a 1 x
a 2 x
a 3 x
EOF

check_output 'using {} twice' 'necho 1 2 3 | xe echo {} x {}' <<EOF
1 x {}
2 x {}
3 x {}
EOF

check_output 'using -I%' 'necho 1 2 3 | xe -I% echo {} x %' <<EOF
{} x 1
{} x 2
{} x 3
EOF

check_output 'using -I "" to disable' 'necho 1 2 3 | xe -I "" echo {} x %' <<EOF
{} x % 1
{} x % 2
{} x % 3
EOF

check_output 'using {} with multiple arguments' 'necho 1 2 3 | xe -N2 echo a {} x {}' <<EOF
a 1 2 x {}
a 3 x {}
EOF

check_output 'using -0' 'printf "foo\0bar\0quux" | xe -0 echo' <<EOF
foo
bar
quux
EOF

check_output 'using -a' 'xe -a echo -- 1 2 3' <<EOF
1
2
3
EOF

check_output 'using -a with no arguments' 'xe -a echo' <<EOF
EOF

check_output 'using -a with no command' 'xe -N2 -a -- 1 2 3' <<EOF
1
2
3
EOF

check_output 'using -A%' 'xe -A% echo -- % 1 2 3' <<EOF
-- 1
-- 2
-- 3
EOF

check_output 'using -A% with no arguments' 'xe -A% echo || true' <<EOF
xe: '-A %' used but no separator '%' found in command line.
EOF

check_output 'using -A% with no command' 'xe -N2 -A% % 1 2 3' <<EOF
1
2
3
EOF

check_output 'using -f' 'necho notme | xe -f tests echo | grep ^notme || echo success' <<EOF
success
EOF

check_output 'using -s' 'necho 1 2 3 | xe -s "echo x\$1"' <<EOF
x1
x2
x3
EOF

check_output 'using -s with -N0' 'necho 1 2 3 | xe -N0 -s "echo x\$@"' <<EOF
x1 2 3
EOF

check_output 'using -s with -a' 'xe -s "echo x\$@" -a 1 2 3' <<EOF
x1
x2
x3
EOF

check_output 'using -s with -a' 'xe -a -s "echo x\$@" 1 2 3' <<EOF
x1
x2
x3
EOF

check_output 'using -s with -a' 'xe -a -s "echo x\$@" -- 1 2 3' <<EOF
x1
x2
x3
EOF

check_output 'with ITER' 'xe -a -s "echo \$ITER" -- a b c' <<EOF
1
2
3
EOF

check_output 'is eager' '{ echo 1; sleep 1; echo 11 >/dev/stderr; echo 2; } | xe echo' <<EOF
1
11
2
EOF

check_output 'using -L' '{ echo 1; sleep 1; echo 2; sleep 1; echo 3; } | xe -j2 -L -s "printf \$1; sleep 1; echo \$1"' <<EOF
11
22
33
EOF

check_output 'using -LL' '{ echo 1; sleep 1; echo 2; sleep 1; echo 3; } | xe -j2 -LL -s "printf \$1; sleep 1; echo \$1"' <<EOF
0001= 11
0002= 22
0003= 33
EOF

check_output 'using -vvL' '{ echo 1; sleep 1; echo 2; sleep 1; echo 3; } | xe -j2 -vvL -s "printf \$1; sleep 1; echo \$1" | wc -l' <<EOF
9
EOF

printf '# error handling\n'

check_output 'exit code on success' 'true | xe; echo $?' <<EOF
0
EOF

check_output 'exit code on other error' 'true | xe -j NaN 2>/dev/null || echo $?' <<EOF
1
EOF

check_output 'exit code on when command fails with 1-125' 'necho a | xe -s "exit 42" || echo $?' <<EOF
123
EOF

check_output 'exit code on when command fails with 255' 'necho a | xe -s "exit 255" 2>/dev/null || echo $?' <<EOF
124
EOF

check_output 'exit code when process was killed' 'necho a | xe -s "kill -HUP \$\$" 2>/dev/null || echo $?' <<EOF
125
EOF

check_output 'exit code when command cannot be run' 'necho a | xe /dev/null/calc.exe 2>/dev/null || echo $?' <<EOF
126
EOF

check_output 'exit code when command was not found' 'necho a | xe /bin/calc.exe 2>/dev/null || echo $?' <<EOF
127
EOF

check_output 'exit code on empty input when run with -R' 'true | xe -R echo a || echo $?' <<EOF
122
EOF

check_output 'doesn'\''t stop on errors by default' 'necho a b c | xe -s "if [ b = \$1 ]; then false; else echo \$1; fi" || echo $?' <<EOF
a
c
123
EOF

check_output 'stops on first error with -F' 'necho a b c | xe -F -s "if [ b = \$1 ]; then false; else echo \$1; fi" 2>/dev/null || echo $?' <<EOF
a
123
EOF

check_output 'should close stdin when arguments were read from it' 'necho a b c | xe -s "sed q"' <<EOF
EOF

check_output 'should not close stdin when arguments were read from command line' 'yes | xe -a -s "sed q" -- 1 2 3' <<EOF
y
y
y
EOF

check_output 'should not close stdin when arguments were read from file' 'yes | xe -f tests -s "sed q" | sed 3q' <<EOF
y
y
y
EOF

printf '# regressions\n'

check_output '0fb64a4 quoting of empty strings' 'printf "foo\n\n" | xe -N2 -v true' <<EOF
true foo ''
EOF

printf '# limit checks, expecting maximal POSIX limits available\n'

check_output 'argscap check' 'head -c 17711 /dev/zero | tr "\0" "\012" | xe -N0 -s "echo \$#"' <<EOF
8187
8187
1337
EOF

bloat() { perl -e 'print "x"x8000, "\n" for 1..42'; }
check_output 'argslen check' 'bloat | xe -N0 -s "echo \$#"' <<EOF
16
16
10
EOF