tests: convert 'if test "$VERBOSE" = yes; then' to test ... &&
[coreutils/ericb.git] / tests / misc / stdbuf
blob8302c4896ff128e71197db7c2da7bbb190aa3404
1 #!/bin/sh
2 # Exercise stdbuf functionality
4 # Copyright (C) 2009-2010 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 . $srcdir/test-lib.sh
20 test "$VERBOSE" = yes && { stdbuf --version; mv --version; }
22 getlimits_
23 require_built_ stdbuf
25 # stdbuf fails when the absolute top build dir name contains e.g., space, TAB, NL
26 lf='
28 case $abs_top_builddir in
29 *[\\\"\#\$\&\'\`$lf\ \ ]*)
30 skip_test_ "unsafe absolute build directory name: $abs_top_builddir";;
31 esac
33 # Use a fifo rather than a pipe in the tests below
34 # so that the producer (uniq) will wait until the
35 # consumer (dd) opens the fifo therefore increasing
36 # the chance that dd will read the data from each
37 # write separately.
38 mkfifo fifo || framework_failure
41 # Verify input parameter checking
42 stdbuf -o1 true || fail=1 # verify size syntax
43 stdbuf -oK true || fail=1 # verify size syntax
44 stdbuf -o0 true || fail=1 # verify unbuffered syntax
45 stdbuf -oL true || fail=1 # verify line buffered syntax
46 stdbuf -ol true # Capital 'L' required
47 test $? = 125 || fail=1 # Internal error is a particular status
48 stdbuf -o$SIZE_OFLOW true # size too large
49 test $? = 125 || fail=1
50 stdbuf -iL true # line buffering stdin disallowed
51 test $? = 125 || fail=1
52 stdbuf -i0 -o0 -e0 true || fail=1 #check all files
53 stdbuf -o1 . # invalid command
54 test $? = 126 || fail=1
55 stdbuf -o1 no_such # no such command
56 test $? = 127 || fail=1
58 # Ensure line buffering stdout takes effect
59 stdbuf_linebuffer()
61 local delay="$1"
63 printf '1\n' > exp
64 dd count=1 if=fifo > out 2> err &
65 (printf '1\n'; sleep $delay; printf '2\n') | stdbuf -oL uniq > fifo
66 wait # for dd to complete
67 compare out exp
70 retry_delay_ stdbuf_linebuffer .1 6 || fail=1
72 stdbuf_unbuffer()
74 local delay="$1"
76 # Ensure un buffering stdout takes effect
77 printf '1\n' > exp
78 dd count=1 if=fifo > out 2> err &
79 (printf '1\n'; sleep $delay; printf '2\n') | stdbuf -o0 uniq > fifo
80 wait # for dd to complete
81 compare out exp
84 retry_delay_ stdbuf_unbuffer .1 6 || fail=1
86 # Ensure un buffering stdin takes effect
87 # The following works for me, but is racy. I.E. we're depending
88 # on dd to run and close the fifo before the second write by uniq.
89 # If we add a sleep, then we're just testing -oL
90 # printf '3\n' > exp
91 # dd count=1 if=fifo > /dev/null 2> err &
92 # printf '1\n\2\n3\n' | (stdbuf -i0 -oL uniq > fifo; cat) > out
93 # wait # for dd to complete
94 # compare out exp || fail=1
95 # One could remove the need for dd (used to close the fifo to get uniq to quit
96 # early), if head -n1 read stdin char by char. Note uniq | head -c2 doesn't
97 # suffice due to the buffering implicit in the pipe. sed currently does read
98 # stdin char by char, so we can test with `sed 1q`. However I'm wary about
99 # adding this dependency on a program outside of coreutils.
100 # printf '2\n' > exp
101 # printf '1\n2\n' | (stdbuf -i0 sed 1q >/dev/null; cat) > out
102 # compare out exp || fail=1
104 # Ensure block buffering stdout takes effect
105 # We don't currently test block buffering failures as
106 # this doesn't work on on GLIBC-2.7 or GLIBC-2.9 at least.
107 # stdbuf_blockbuffer()
109 # local delay="$1"
111 # printf '1\n2\n' > exp
112 # dd count=1 if=fifo > out 2> err &
113 # (printf '1\n'; sleep $delay; printf '2\n') | stdbuf -o4 uniq > fifo
114 # wait # for dd to complete
115 # compare out exp
118 # retry_delay_ stdbuf_blockbuffer .1 6 || fail=1
120 Exit $fail