• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3[ -f testing.sh ] && . testing.sh
4
5#testing "name" "command" "result" "infile" "stdin"
6
7testing "xargs" "xargs && echo yes" "hello\nyes\n" "" "hello"
8testing "spaces" "xargs" \
9	"one two three four\n" "" "one two\tthree  \nfour\n\n"
10
11testing "-n 0" "xargs -n 0 2>/dev/null || echo ok" "ok\n" \
12	"" "one \ntwo\n three"
13testing "-n 1" "xargs -n 1" "one\n" "" "one\n"
14testing "-n 2" "xargs -n 2" "one two\nthree\n" "" "one \ntwo\n three"
15testing "-n exact match" "xargs -n 3" "one two three\n" "" "one two three"
16testing "xargs2" "xargs -n2" "one two\nthree four\nfive\n" "" \
17	"one two three four five"
18testing "-s too long" "xargs -s 9 echo 2>/dev/null; echo \$?" \
19	"one\ntwo\n1\n" "" "one two three"
20testing "-s 13" "xargs -s 13 echo" "one two\nthree\n" "" "one \ntwo\n three"
21testing "-s 12" "xargs -s 12 echo" "one\ntwo\nthree\n" "" "one \ntwo\n three"
22# Check that we're accounting for the command *and* its arguments correctly.
23testing "-s counts args" "xargs -s 13 echo ' ' ' '" "    one\n" "" "one\n"
24# 5 is the minimum allowed for the default "echo".
25testing "-s 4 fails" "xargs -s 4 2>/dev/null || echo bad" "bad\n" "" ""
26testing "-s 5 no input okay" "xargs -s 5" "\n" "" ""
27testing "-s 5 with input bad" "xargs -s 5 2>/dev/null || echo bad" "bad\n" \
28  "" "a\n"
29
30touch one two three
31testing "command -opt" "xargs -n2 ls -1" "one\ntwo\nthree\n" "" \
32	"one two three"
33rm one two three
34
35testing "-0 -n1" "printf 'a\0b\0c\0d\0e\0f' | xargs -0 -n1 echo _" "_ a\n_ b\n_ c\n_ d\n_ e\n_ f\n" "" ""
36testing "-0 -n2" "printf 'a\0b\0c\0d\0e\0f' | xargs -0 -n2 echo _" "_ a b\n_ c d\n_ e f\n" "" ""
37
38testing "-t" "xargs -t 2>stderr ; cat stderr ; rm stderr" "one two\necho one two \n" "" "one\ntwo\n"
39
40testing "-E END" "xargs -E END" "a b ENDE\n" "" "a\nb\nENDE\nEND\nc\nd\n"
41
42testing "-r" "xargs -r echo x" "" "" ""
43testing "no -r" "xargs echo x" "x\n" "" ""
44
45# Exit value madness. 0 and 126/127 are normal.
46testing "true" "xargs true; echo \$?" "0\n" "" ""
47testing "command not found" "xargs does-not-exist 2>/dev/null; echo \$?" \
48  "127\n" "" ""
49# But 1-125 are flattened to 123.
50testing "false" "xargs false; echo \$?" "123\n" "" ""
51# 255 is special "abort early" magic.
52testing "exit 255" "xargs sh -c 'exit 255' 2>&1; echo \$?" \
53  "xargs: sh: exited with status 255; aborting\n124\n" "" ""
54
55# findutils maxes out at 131066 (they do the math wrong), kernel takes 131071.
56toyonly testing "max argument size" \
57  "head -c 131071 /dev/zero | tr '\0' x | xargs | wc -c" "131072\n" "" ""
58X=$(head -c 131066 /dev/zero | tr '\0' x)
59# This goes over the kernel's 10 meg limit
60testing "big input forces split" \
61  'for i in $(seq 1 100);do echo $X;done|{ xargs >/dev/null && echo yes;}' \
62  "yes\n" "" ""
63
64# -P max-proc
65testing "max-proc=1" "xargs -n 1 -P 1" "one\ntwo\nthree\n" "" "one\ntwo\nthree\n"
66testing "max-proc=2" "xargs -n 1 -P 2" "y\ny\ny\n" "" "y\ny\ny\n"
67testing "max-proc=3" "xargs -n 1 -P 3" "y\ny\ny\n" "" "y\ny\ny\n"
68# 3 seconds vs 2 seconds vs 1 second parallel sleep
69testing "parallel sleep" "
70  xargs_sleep() {
71    ( yes | head -3 | tr y 1 | time -p xargs -n 1 \${*} sleep ) 2>&1 |
72      sed -n 's/^real *\([0-9]*\)[.]\(.*\)/\1\2/p'
73  } &&
74  A=\`xargs_sleep\` &&
75  B=\`xargs_sleep -P 2\` &&
76  C=\`xargs_sleep -P 0\` &&
77  [ \${A} -gt \${B} -a \${B} -gt \${C} ] && echo OK || echo FAIL" "OK\n" "" ""
78
79# TODO: what exactly is -x supposed to do? why does coreutils output "one"?
80#testing "-x" "xargs -x -s 9 || echo expected" "one\nexpected\n" "" "one\ntwo\nthree"
81
82# TODO: test for -L? what exactly is the difference between -n and -L?
83
84#testing "-n exact match"
85#testing "-s exact match"
86#testing "-s impossible"
87