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