• 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\n" "" "a\nb\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# TODO: what exactly is -x supposed to do? why does coreutils output "one"?
56#testing "-x" "xargs -x -s 9 || echo expected" "one\nexpected\n" "" "one\ntwo\nthree"
57
58# TODO: test for -L? what exactly is the difference between -n and -L?
59
60#testing "-n exact match"
61#testing "-s exact match"
62#testing "-s impossible"
63