blob: 6e217c45e0ef1fd85962e2930afe86646d719d4f (
plain)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
#! /usr/bin/ksh
#
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
#
# Copyright 2014 Garrett D'Amore <garrett@damore.org>
# Copyright (c) 2017, Joyent, Inc.
#
XARGS=${XARGS:=/usr/bin/xargs}
test_start() {
print "TEST STARTING ${1}: ${2}"
}
test_pass() {
print "TEST PASS: ${1}"
}
test_fail() {
print "TEST FAIL: ${1}: ${2}"
exit -1
}
checkrv() {
if [[ $? -ne 0 ]]; then
test_fail $1 "exit failure"
fi
}
compare() {
if [[ "$2" != "$3" ]]; then
test_fail $1 "compare mismatch, got [$2] expected [$3]"
fi
}
test1() {
t=test1
test_start $t "-I handling"
comp=$(echo foo bar baz other | $XARGS -I THING echo '** THING **')
checkrv $t
good='** foo bar baz other **'
compare $t "$comp" "$good"
test_pass $t
}
test2() {
t=test2
test_start $t "-n 1 handling"
comp=$(echo foo bar baz other | $XARGS -n 1 echo '***')
checkrv $t
good='*** foo
*** bar
*** baz
*** other'
compare $t "$comp" "$good"
test_pass $t
}
test3() {
t=test3
test_start $t "-I before -n 1"
comp=$(echo foo bar baz other | $XARGS -I THING -n1 echo '** THING **')
checkrv $t
good='** THING ** foo
** THING ** bar
** THING ** baz
** THING ** other'
compare $t "$comp" "$good"
test_pass $t
}
test4() {
t=test4
test_start $t "-n 1 before -I"
comp=$(echo foo bar baz other | $XARGS -n 1 -I THING echo '** THING **')
checkrv $t
good='** foo bar baz other **'
compare $t "$comp" "$good"
test_pass $t
}
test5() {
t=test5
test_start $t "-i multiple lines handling"
comp=$(printf "abc def\nxyz\n123" | $XARGS -n1 -i echo '[{}]')
checkrv $t
good='[abc def]
[xyz]
[123]'
compare $t "$comp" "$good"
test_pass $t
}
test6() {
t=test6
test_start $t "-E handling"
comp=$(printf "abc def xyx\n_\n123\nnope" | $XARGS -edef echo)
checkrv $t
good='abc'
compare $t "$comp" "$good"
test_pass $t
}
test7() {
t=test7
test_start $t "newlines in arguments"
comp=$(printf "abc def\nxyz\n\n123 456\n789" | $XARGS echo)
checkrv $t
good='abc def xyz 123 456 789'
compare $t "$comp" "$good"
test_pass $t
}
test8() {
t=test8
test_start $t "limited counts via -n3"
comp=$(printf "abc def ghi jkl mno 123 456 789" | $XARGS -n 3 echo '**' )
checkrv $t
good='** abc def ghi
** jkl mno 123
** 456 789'
compare $t "$comp" "$good"
test_pass $t
}
test9() {
t=test9
test_start $t "multiple lines via -L2"
comp=$(printf "abc def\n123 456\npeterpiper" | $XARGS -L2 echo '**')
checkrv $t
good='** abc def 123 456
** peterpiper'
compare $t "$comp" "$good"
test_pass $t
}
test10() {
t=test10
test_start $t "argument sizes"
comp=$(printf "abc def 123 456 peter alpha\n" | $XARGS -s15 echo)
checkrv $t
good='abc def
123 456
peter
alpha'
compare $t "$comp" "$good"
test_pass $t
}
test11() {
t=test11
test_start $t "bare -e"
comp=$(printf "abc def _ end of string" | $XARGS -e echo '**')
checkrv $t
good='** abc def _ end of string'
compare $t "$comp" "$good"
test_pass $t
}
test12() {
t=test12
test_start $t "-E ''"
comp=$(printf "abc def _ end of string" | $XARGS -E '' echo '**')
checkrv $t
good='** abc def _ end of string'
compare $t "$comp" "$good"
test_pass $t
}
test13() {
t=test13
test_start $t "end of string (no -E or -e)"
comp=$(printf "abc def _ end of string" | $XARGS echo '**')
checkrv $t
good='** abc def'
compare $t "$comp" "$good"
test_pass $t
}
test14() {
t=test14
test_start $t "trailing blank with -L"
comp=$(printf "abc def \n123 456\npeter\nbogus" | $XARGS -L2 echo '**')
checkrv $t
good='** abc def 123 456 peter
** bogus'
compare $t "$comp" "$good"
test_pass $t
}
test15() {
t=test15
test_start $t "leading and embedded blanks with -i"
comp=$(printf "abc def\n xyz bogus\nnext" | $XARGS -i echo '** {}')
checkrv $t
good='** abc def
** xyz bogus
** next'
compare $t "$comp" "$good"
test_pass $t
}
test16() {
t=test16
test_start $t "single character replstring"
comp=$(echo foo bar baz other | $XARGS -I X echo '** X **')
checkrv $t
good='** foo bar baz other **'
compare $t "$comp" "$good"
test_pass $t
}
test17() {
t=test17
test_start $t "null byte separators"
comp=$(print 'foo bar baz\000more data' | $XARGS -n1 -0 echo '**')
checkrv $t
good='** foo bar baz
** more data'
compare $t "$comp" "$good"
test_pass $t
}
test18() {
t=test18
test_start $t "escape characters"
comp=$(printf 'foo\\ bar second" "arg third' | $XARGS -n1 echo '**')
checkrv $t
good='** foo bar
** second arg
** third'
compare $t "$comp" "$good"
test_pass $t
}
test19() {
t=test19
test_start $t "bad -P option (negative value)"
$XARGS -P -3 </dev/null 2>/dev/null
if [[ $? -eq 2 ]]; then
test_pass $t
else
test_fail $t
fi
}
test20() {
t=test20
test_start $t "bad -P option (bad string)"
$XARGS -P as3f </dev/null 2>/dev/null
if [[ $? -eq 2 ]]; then
test_pass $t
else
test_fail $t
fi
}
test21() {
t=test21
test_start $t "bad -P option (extraneous characters)"
$XARGS -P 2c </dev/null 2>/dev/null
if [[ $? -eq 2 ]]; then
test_pass $t
else
test_fail $t
fi
}
test1
test2
test3
test4
test5
test6
test7
test8
test9
test10
test11
test12
test13
test14
test15
test16
test17
test18
test19
test20
test21
|