summary refs log tree commit diff
path: root/tests
blob: e810c0db2f7d6674cad5c3f39283293217f56a47 (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
#!/bin/sh
printf '1..42\n'

set -e

: ${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' <<EOF
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

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 '# 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