xargs.test 3.6 KB

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