On Sun, Nov 13, 2016 at 9:24 AM, Bernhard Voelker
<[email protected]> wrote:
> On 11/13/2016 03:38 AM, Jim Meyering wrote:
>> I noticed many tests that compare directly with "$?". However, we now
>> have the "returns_" function (from init.sh) that can be used to make
>> the resulting code a little less error-prone: all on one line/stmt, it
>> is harder to accidentally insert code that accidentally clobbers "$?".
>>
>> Here's an example of what most of these changes look like:
>>
>>   -ls -l --time-style=XX > out 2> err
>>   -test $? = 2 || fail=1
>>   +returns_ 2 ls -l --time-style=XX > out 2> err || fail=1
>
> Nice work, thanks!
>
> BTW: what was your search pattern?  I mean, is there a reason
> you left places like the following alone?

Good catch. Thank you. Amended patch below.
I did the conversion in two steps. Initially I searched only for
single-digit values on the RHS, then realized my error and
found/converted the multi-digit ones, too, initially in a separate
commit. Could have sworn I merged them, but reflog shows no trace.

However, I did deliberately leave two unconverted, e.g.,

tests/ls/readdir-mountpoint-inode.sh-while read dir; do
tests/ls/readdir-mountpoint-inode.sh-  readdir_inode=$(inode_via_readdir "$dir")
tests/ls/readdir-mountpoint-inode.sh:  test $? = 77 && continue
tests/ls/readdir-mountpoint-inode.sh-  stat_inode=$(timeout 1 stat
--format=%i "$dir")
tests/ls/readdir-mountpoint-inode.sh-  # If stat fails or says the
inode is 0, skip $dir.
--
tests/misc/chroot-fail.sh-  returns_ 127 chroot / no_such || fail=1 #
no such command
tests/misc/chroot-fail.sh-else
tests/misc/chroot-fail.sh:  test $? = 125 || fail=1
tests/misc/chroot-fail.sh-  can_chroot_root=0
tests/misc/chroot-fail.sh-fi

We may want to convert those, too (if it can be done cleanly), so that
we can add an exception-free syntax-check. Or just add an exception
for each.

>   $ git grep -B1 -F '$? = ' -- tests
>   ..
>   tests/misc/stdbuf.sh-stdbuf -ol true # Capital 'L' required
>   tests/misc/stdbuf.sh:test $? = 125 || fail=1 # Internal error is a 
> particular status
>   ..
>
>   $ git grep -B1 -F '$? != ' -- tests
>   tests/du/8gb.sh-dd bs=1 seek=8G of=big < /dev/null 2> /dev/null
>   tests/du/8gb.sh:if test $? != 0; then
>   ..
>
> Furthermore, we need to tweak a syntax-check, see attached.

Good catch. Please push.

Thanks again for catching those.
I've attached both the delta and the merged V2.
diff --git a/tests/ls/stat-vs-dirent.sh b/tests/ls/stat-vs-dirent.sh
index b9f3cd1..fa2087f 100755
--- a/tests/ls/stat-vs-dirent.sh
+++ b/tests/ls/stat-vs-dirent.sh
@@ -23,8 +23,7 @@ print_ver_ ls
 root_dev_ino=$(stat --format=%d-%i /)
 t=$(pwd)
 while :; do
-  ls -i1 "$t" > tmp
-  if test $? = 0; then
+  if ls -i1 "$t" > tmp; then
     # Extract the inode number from the first line of output from ls -i1.
     # This value comes from dirent.d_ino, on systems with d_ino support.
     d_ino=$(sed -n '1s/^ *\([0-9][0-9]*\) .*/\1/p;q' tmp)
diff --git a/tests/misc/head-write-error.sh b/tests/misc/head-write-error.sh
index 4c021f5..1e8e566 100755
--- a/tests/misc/head-write-error.sh
+++ b/tests/misc/head-write-error.sh
@@ -36,14 +36,13 @@ printf '%s\n' "head: error writing 'standard output'" > exp
 for item in lines bytes; do
   for N in 0 1; do
     # pipe case
-    yes | timeout 10s head --$item=-$N > /dev/full 2> errt && fail=1
-    test $? = 124 && fail=1
+    yes | returns_ 1 timeout 10s head --$item=-$N > /dev/full 2> errt || fail=1
     sed 's/\(head:.*\):.*/\1/' errt > err
     compare exp err || fail=1

     # seekable case
-    timeout 10s head --$item=-$N bigseek > /dev/full 2> errt && fail=1
-    test $? = 124 && fail=1
+    returns_ 1 timeout 10s head --$item=-$N bigseek > /dev/full 2> errt \
+        || fail=1
     sed 's/\(head:.*\):.*/\1/' errt > err
     compare exp err || fail=1
   done
diff --git a/tests/misc/nice.sh b/tests/misc/nice.sh
index 85523eb..36d6f91 100755
--- a/tests/misc/nice.sh
+++ b/tests/misc/nice.sh
@@ -79,8 +79,7 @@ if test x$(nice -n -1 nice 2> /dev/null) = x0 ; then
   compare exp err || fail=1
   # Failure to write advisory message is fatal.  Buggy through coreutils 8.0.
   if test -w /dev/full && test -c /dev/full; then
-    nice -n -1 nice > out 2> /dev/full
-    test $? = 125 || fail=1
+    returns_ 125 nice -n -1 nice > out 2> /dev/full || fail=1
     compare /dev/null out || fail=1
   fi
 else
diff --git a/tests/misc/nohup.sh b/tests/misc/nohup.sh
index ad60185..e9c8c38 100755
--- a/tests/misc/nohup.sh
+++ b/tests/misc/nohup.sh
@@ -70,8 +70,7 @@ if test -w /dev/full && test -c /dev/full; then

   exec >/dev/tty
   test -t 1 || exit 0
-  nohup echo hi 2> /dev/full
-  test $? = 125 || fail=1
+  returns_ 125 nohup echo hi 2> /dev/full || fail=1
   test -f nohup.out || fail=1
   compare /dev/null nohup.out || fail=1
   rm -f nohup.out
@@ -118,9 +117,7 @@ EOF

 # Make sure it fails with exit status of 125 when given too few arguments,
 # except that POSIX requires 127 in this case.
-nohup >/dev/null 2>&1
-test $? = 125 || fail=1
-POSIXLY_CORRECT=1 nohup >/dev/null 2>&1
-test $? = 127 || fail=1
+returns_ 125 nohup >/dev/null 2>&1 || fail=1
+POSIXLY_CORRECT=1 returns_ 127 nohup >/dev/null 2>&1 || fail=1

 Exit $fail
diff --git a/tests/misc/stdbuf.sh b/tests/misc/stdbuf.sh
index 7b9aed5..31f02b7 100755
--- a/tests/misc/stdbuf.sh
+++ b/tests/misc/stdbuf.sh
@@ -43,19 +43,17 @@ stdbuf -o1 true || fail=1 # verify size syntax
 stdbuf -oK true || fail=1 # verify size syntax
 stdbuf -o0 true || fail=1 # verify unbuffered syntax
 stdbuf -oL true || fail=1 # verify line buffered syntax
-stdbuf -ol true # Capital 'L' required
-test $? = 125 || fail=1 # Internal error is a particular status
-stdbuf -o$SIZE_OFLOW true # size too large
-test $? = 125 || fail=1
-stdbuf -iL true # line buffering stdin disallowed
-test $? = 125 || fail=1
-stdbuf true # a buffering mode must be specified
-test $? = 125 || fail=1
+
+# Capital 'L' required
+# Internal error is a particular status
+returns_ 125 stdbuf -ol true || fail=1
+
+returns_ 125 stdbuf -o$SIZE_OFLOW true || fail=1 # size too large
+returns_ 125 stdbuf -iL true || fail=1 # line buffering stdin disallowed
+returns_ 125 stdbuf true || fail=1 # a buffering mode must be specified
 stdbuf -i0 -o0 -e0 true || fail=1 #check all files
-stdbuf -o1 . # invalid command
-test $? = 126 || fail=1
-stdbuf -o1 no_such # no such command
-test $? = 127 || fail=1
+returns_ 126 stdbuf -o1 . || fail=1 # invalid command
+returns_ 127 stdbuf -o1 no_such || fail=1 # no such command

 # Terminate any background processes
 cleanup_() { kill $pid 2>/dev/null && wait $pid; }
diff --git a/tests/misc/sync.sh b/tests/misc/sync.sh
index 5bf569c..cd89c5b 100755
--- a/tests/misc/sync.sh
+++ b/tests/misc/sync.sh
@@ -45,8 +45,7 @@ fi
 if test "$fail" != '1'; then
   # Ensure a fifo doesn't block
   mkfifo_or_skip_ fifo
-  timeout 10 sync fifo
-  test $? = 124 && fail=1
+  returns_ 124 timeout 10 sync fifo && fail=1
 fi

 Exit $fail
diff --git a/tests/tail-2/pid.sh b/tests/tail-2/pid.sh
index 9e73b84..ba67b46 100755
--- a/tests/tail-2/pid.sh
+++ b/tests/tail-2/pid.sh
@@ -31,8 +31,7 @@ for mode in '' '---disable-inotify'; do
   tail -f $mode here & pid=$!

   # Ensure that tail --pid=PID does not exit when PID is alive.
-  timeout 1 tail -f -s.1 --pid=$pid $mode here
-  test $? = 124 || fail=1
+  returns_ 124 timeout 1 tail -f -s.1 --pid=$pid $mode here || fail=1

   cleanup_

@@ -44,8 +43,7 @@ for mode in '' '---disable-inotify'; do
   test $ret = 0 || fail=1

   # Ensure tail doesn't wait for data when PID is dead
-  timeout 10 tail -f -s10 --pid=$PID_T_MAX $mode empty
-  test $? = 124 && fail=1
+  returns_ 124 timeout 10 tail -f -s10 --pid=$PID_T_MAX $mode empty && fail=1
 done

 Exit $fail
diff --git a/tests/tail-2/wait.sh b/tests/tail-2/wait.sh
index 59f796a..1b54327 100755
--- a/tests/tail-2/wait.sh
+++ b/tests/tail-2/wait.sh
@@ -35,29 +35,23 @@ cleanup_() { kill $pid 2>/dev/null && wait $pid; }
 fastpoll='-s.1 --max-unchanged-stats=1'

 for mode in '' '---disable-inotify'; do
-  timeout 10 tail $fastpoll -f $mode not_here
-  test $? = 124 && fail=1
+  returns_ 124 timeout 10 tail $fastpoll -f $mode not_here && fail=1

   if test ! -r unreadable; then # can't test this when root
-    timeout 10 tail $fastpoll -f $mode unreadable
-    test $? = 124 && fail=1
+    returns_ 124 timeout 10 tail $fastpoll -f $mode unreadable && fail=1
   fi

-  timeout .1 tail $fastpoll -f $mode here 2>tail.err
-  test $? = 124 || fail=1
+  returns_ 124 timeout .1 tail $fastpoll -f $mode here 2>tail.err || fail=1

   # 'tail -F' must wait in any case.

-  timeout .1 tail $fastpoll -F $mode here 2>>tail.err
-  test $? = 124 || fail=1
+  returns_ 124 timeout .1 tail $fastpoll -F $mode here 2>>tail.err || fail=1

   if test ! -r unreadable; then # can't test this when root
-    timeout .1 tail $fastpoll -F $mode unreadable
-    test $? = 124 || fail=1
+    returns_ 124 timeout .1 tail $fastpoll -F $mode unreadable || fail=1
   fi

-  timeout .1 tail $fastpoll -F $mode not_here
-  test $? = 124 || fail=1
+  returns_ 124 timeout .1 tail $fastpoll -F $mode not_here || fail=1

   grep -Ev "$inotify_failed_re" tail.err > x
   mv x tail.err
From b1f0e9b209d34e8622178d565c75a029cd2bb205 Mon Sep 17 00:00:00 2001
From: Jim Meyering <[email protected]>
Date: Tue, 8 Nov 2016 19:57:41 -0600
Subject: [PATCH] tests: use "returns_" rather than explicit comparison with
 "$?"

* tests/chmod/no-x.sh: Likewise.
* tests/cp/no-deref-link1.sh: Likewise.
* tests/cp/no-deref-link2.sh: Likewise.
* tests/cp/no-deref-link3.sh: Likewise.
* tests/du/move-dir-while-traversing.sh: Likewise.
* tests/ls/infloop.sh: Likewise.
* tests/ls/stat-failed.sh: Likewise.
* tests/ls/stat-vs-dirent.sh: Likewise.
* tests/ls/time-style-diag.sh: Likewise.
* tests/misc/chroot-fail.sh: Likewise.
* tests/misc/env-null.sh: Likewise.
* tests/misc/env.sh: Likewise.
* tests/misc/head-write-error.sh: Likewise.
* tests/misc/nice-fail.sh: Likewise.
* tests/misc/nice.sh  : Likewise.
* tests/misc/nohup.sh : Likewise.
* tests/misc/printenv.sh: Likewise.
* tests/misc/stdbuf.sh: Likewise.
* tests/misc/sync.sh  : Likewise.
* tests/misc/timeout.sh: Likewise.
* tests/tail-2/follow-name.sh: Likewise.
* tests/tail-2/pid.sh : Likewise.
* tests/tail-2/wait.sh: Likewise.
---
 tests/chmod/no-x.sh                   |  3 +--
 tests/cp/no-deref-link1.sh            |  4 +---
 tests/cp/no-deref-link2.sh            |  4 +---
 tests/cp/no-deref-link3.sh            |  4 +---
 tests/du/move-dir-while-traversing.sh |  3 +--
 tests/ls/infloop.sh                   |  3 +--
 tests/ls/stat-failed.sh               |  3 +--
 tests/ls/stat-vs-dirent.sh            |  3 +--
 tests/ls/time-style-diag.sh           |  3 +--
 tests/misc/chroot-fail.sh             | 15 +++++----------
 tests/misc/env-null.sh                |  9 +++------
 tests/misc/env.sh                     | 18 ++++++------------
 tests/misc/head-write-error.sh        |  7 +++----
 tests/misc/nice-fail.sh               | 18 ++++++------------
 tests/misc/nice.sh                    |  3 +--
 tests/misc/nohup.sh                   |  9 +++------
 tests/misc/printenv.sh                | 12 ++++--------
 tests/misc/stdbuf.sh                  | 22 ++++++++++------------
 tests/misc/sync.sh                    |  3 +--
 tests/misc/timeout.sh                 | 12 ++++--------
 tests/tail-2/follow-name.sh           |  3 +--
 tests/tail-2/pid.sh                   |  6 ++----
 tests/tail-2/wait.sh                  | 18 ++++++------------
 23 files changed, 64 insertions(+), 121 deletions(-)

diff --git a/tests/chmod/no-x.sh b/tests/chmod/no-x.sh
index 304eb82..499f74a 100755
--- a/tests/chmod/no-x.sh
+++ b/tests/chmod/no-x.sh
@@ -48,10 +48,9 @@ compare exp out || fail=1

 cd a
 # This will fail with ''chmod: fts_read failed: Permission denied''
-chmod a-x . b 2> /dev/null && fail=1
 # chmod must exit with status 1.
 # Due to a bug in coreutils-5.93's fts.c, chmod would provoke
 # an abort (exit with status 134) on recent glibc-based systems.
-test $? = 1 || fail=1
+returns_ 1 chmod a-x . b 2> /dev/null || fail=1

 Exit $fail
diff --git a/tests/cp/no-deref-link1.sh b/tests/cp/no-deref-link1.sh
index b1754b7..4b7c057 100755
--- a/tests/cp/no-deref-link1.sh
+++ b/tests/cp/no-deref-link1.sh
@@ -29,10 +29,8 @@ cd ..

 # It should fail with a message something like this:
 #   ./cp: 'a/foo' and 'b/foo' are the same file
-cp -d a/foo b 2>/dev/null
-
 # Fail this test if the exit status is not 1
-test $? = 1 || fail=1
+returns_ 1 cp -d a/foo b 2>/dev/null || fail=1

 test "$(cat a/foo)" = $msg || fail=1

diff --git a/tests/cp/no-deref-link2.sh b/tests/cp/no-deref-link2.sh
index 25a8848..7f0ca48 100755
--- a/tests/cp/no-deref-link2.sh
+++ b/tests/cp/no-deref-link2.sh
@@ -29,10 +29,8 @@ cd ..

 # It should fail with a message something like this:
 #   cp: 'a' and 'b/foo' are the same file
-cp -d a b 2>/dev/null
-
 # Fail this test if the exit status is not 1
-test $? = 1 || fail=1
+returns_ 1 cp -d a b 2>/dev/null || fail=1

 test "$(cat a)" = $msg || fail=1

diff --git a/tests/cp/no-deref-link3.sh b/tests/cp/no-deref-link3.sh
index 897eb6b..137a501 100755
--- a/tests/cp/no-deref-link3.sh
+++ b/tests/cp/no-deref-link3.sh
@@ -26,10 +26,8 @@ ln -s a b

 # It should fail with a message something like this:
 #   cp: 'a' and 'b' are the same file
-cp -d a b 2>/dev/null
-
 # Fail this test if the exit status is not 1
-test $? = 1 || fail=1
+returns_ 1 cp -d a b 2>/dev/null || fail=1

 test "$(cat a)" = $msg || fail=1

diff --git a/tests/du/move-dir-while-traversing.sh 
b/tests/du/move-dir-while-traversing.sh
index e80e94c..218f0c4 100755
--- a/tests/du/move-dir-while-traversing.sh
+++ b/tests/du/move-dir-while-traversing.sh
@@ -88,9 +88,8 @@ retry_delay_ nonempty .1 5 || fail=1
 # and when it triggers, moves the parent, $t/3/a, up one level
 # so it's directly under $t.

-du -a $t d2 2> err
 # Before coreutils-8.10, du would abort.
-test $? = 1 || fail=1
+returns_ 1 du -a $t d2 2> err || fail=1

 # check for the new diagnostic
 printf "du: fts_read failed: $t/3/a/b: No such file or directory\n" > exp \
diff --git a/tests/ls/infloop.sh b/tests/ls/infloop.sh
index e06a9a4..a724d11 100755
--- a/tests/ls/infloop.sh
+++ b/tests/ls/infloop.sh
@@ -32,9 +32,8 @@ cat <<\EOF > exp-err || framework_failure_
 ls: loop/sub: not listing already-listed directory
 EOF

-timeout 10 ls -RL loop >out 2>err
 # Ensure that ls exits with status 2 upon detecting a cycle
-test $? = 2 || fail=1
+returns_ 2 timeout 10 ls -RL loop >out 2>err || fail=1

 compare exp-err err || fail=1
 compare exp-out out || fail=1
diff --git a/tests/ls/stat-failed.sh b/tests/ls/stat-failed.sh
index b0e11e6..2fb376d 100755
--- a/tests/ls/stat-failed.sh
+++ b/tests/ls/stat-failed.sh
@@ -28,8 +28,7 @@ ln -s / d/s || framework_failure_
 chmod 600 d || framework_failure_


-ls -Log d > out
-test $? = 1 || fail=1
+returns_ 1 ls -Log d > out || fail=1

 # Linux 2.6.32 client with Isilon OneFS always returns d_type==DT_DIR ('d')
 # Newer Linux 3.10.0 returns the more correct DT_UNKNOWN ('?')
diff --git a/tests/ls/stat-vs-dirent.sh b/tests/ls/stat-vs-dirent.sh
index b9f3cd1..fa2087f 100755
--- a/tests/ls/stat-vs-dirent.sh
+++ b/tests/ls/stat-vs-dirent.sh
@@ -23,8 +23,7 @@ print_ver_ ls
 root_dev_ino=$(stat --format=%d-%i /)
 t=$(pwd)
 while :; do
-  ls -i1 "$t" > tmp
-  if test $? = 0; then
+  if ls -i1 "$t" > tmp; then
     # Extract the inode number from the first line of output from ls -i1.
     # This value comes from dirent.d_ino, on systems with d_ino support.
     d_ino=$(sed -n '1s/^ *\([0-9][0-9]*\) .*/\1/p;q' tmp)
diff --git a/tests/ls/time-style-diag.sh b/tests/ls/time-style-diag.sh
index 04be4dd..0630418 100755
--- a/tests/ls/time-style-diag.sh
+++ b/tests/ls/time-style-diag.sh
@@ -19,8 +19,7 @@
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ ls

-ls -l --time-style=XX > out 2> err
-test $? = 2 || fail=1
+returns_ 2 ls -l --time-style=XX > out 2> err || fail=1

 cat <<\EOF > exp || fail=1
 ls: invalid argument 'XX' for 'time style'
diff --git a/tests/misc/chroot-fail.sh b/tests/misc/chroot-fail.sh
index bb840c7..48c6769 100755
--- a/tests/misc/chroot-fail.sh
+++ b/tests/misc/chroot-fail.sh
@@ -22,20 +22,15 @@ print_ver_ chroot pwd

 # These tests verify exact status of internal failure; since none of
 # them actually run a command, we don't need root privileges
-chroot # missing argument
-test $? = 125 || fail=1
-chroot --- / true # unknown option
-test $? = 125 || fail=1
+returns_ 125 chroot || fail=1 # missing argument
+returns_ 125 chroot --- / true || fail=1 # unknown option

 # chroot("/") succeeds for non-root users on some systems, but not all.
 if chroot / true ; then
   can_chroot_root=1
-  chroot / sh -c 'exit 2' # exit status propagation
-  test $? = 2 || fail=1
-  chroot / . # invalid command
-  test $? = 126 || fail=1
-  chroot / no_such # no such command
-  test $? = 127 || fail=1
+  returns_ 2 chroot / sh -c 'exit 2' || fail=1 # exit status propagation
+  returns_ 126 chroot / .  || fail=1# invalid command
+  returns_ 127 chroot / no_such || fail=1 # no such command
 else
   test $? = 125 || fail=1
   can_chroot_root=0
diff --git a/tests/misc/env-null.sh b/tests/misc/env-null.sh
index f84ff27..40fafed 100755
--- a/tests/misc/env-null.sh
+++ b/tests/misc/env-null.sh
@@ -38,8 +38,7 @@ env -i PATH="$PATH" printenv --null > out2 || fail=1
 compare out1 out2 || fail=1

 # env -0 does not work if a command is specified.
-env -0 echo hi > out
-test $? = 125 || fail=1
+returns_ 125 env -0 echo hi > out || fail=1
 compare /dev/null out || fail=1

 # Test env -0 on a one-variable environment.
@@ -51,11 +50,9 @@ compare exp out || fail=1
 printf 'b\nc=\0' > exp || framework_failure_
 env "$(printf 'a=b\nc=')" printenv -0 a > out || fail=1
 compare exp out || fail=1
-env -u a printenv -0 a > out
-test $? = 1 || fail=1
+returns_ 1 env -u a printenv -0 a > out || fail=1
 compare /dev/null out || fail=1
-env -u b "$(printf 'a=b\nc=')" printenv -0 b a > out
-test $? = 1 || fail=1
+returns_ 1 env -u b "$(printf 'a=b\nc=')" printenv -0 b a > out || fail=1
 compare exp out || fail=1

 Exit $fail
diff --git a/tests/misc/env.sh b/tests/misc/env.sh
index 666d777..9f939f1 100755
--- a/tests/misc/env.sh
+++ b/tests/misc/env.sh
@@ -43,16 +43,11 @@ echo a=b > exp || framework_failure_
 compare exp out || fail=1

 # These tests verify exact status of internal failure.
-env --- # unknown option
-test $? = 125 || fail=1
-env -u # missing option argument
-test $? = 125 || fail=1
-env sh -c 'exit 2' # exit status propagation
-test $? = 2 || fail=2
-env . # invalid command
-test $? = 126 || fail=1
-env no_such # no such command
-test $? = 127 || fail=1
+returns_ 125 env --- || fail=1 # unknown option
+returns_ 125 env -u || fail=1 # missing option argument
+returns_ 2 env sh -c 'exit 2' || fail=1 # exit status propagation
+returns_ 126 env . || fail=1 # invalid command
+returns_ 127 env no_such || fail=1 # no such command

 # POSIX is clear that environ may, but need not be, sorted.
 # Environment variable values may contain newlines, which cannot be
@@ -131,8 +126,7 @@ case $(env -- -u pass) in
 esac

 # After options have ended, the first argument not containing = is a program.
-env a=b -- true
-test $? = 127 || fail=1
+returns_ 127 env a=b -- true || fail=1
 ln -s "simple_echo" ./-- || framework_failure_
 case $(env a=b -- true || echo fail) in
   *true) ;;
diff --git a/tests/misc/head-write-error.sh b/tests/misc/head-write-error.sh
index 4c021f5..1e8e566 100755
--- a/tests/misc/head-write-error.sh
+++ b/tests/misc/head-write-error.sh
@@ -36,14 +36,13 @@ printf '%s\n' "head: error writing 'standard output'" > exp
 for item in lines bytes; do
   for N in 0 1; do
     # pipe case
-    yes | timeout 10s head --$item=-$N > /dev/full 2> errt && fail=1
-    test $? = 124 && fail=1
+    yes | returns_ 1 timeout 10s head --$item=-$N > /dev/full 2> errt || fail=1
     sed 's/\(head:.*\):.*/\1/' errt > err
     compare exp err || fail=1

     # seekable case
-    timeout 10s head --$item=-$N bigseek > /dev/full 2> errt && fail=1
-    test $? = 124 && fail=1
+    returns_ 1 timeout 10s head --$item=-$N bigseek > /dev/full 2> errt \
+        || fail=1
     sed 's/\(head:.*\):.*/\1/' errt > err
     compare exp err || fail=1
   done
diff --git a/tests/misc/nice-fail.sh b/tests/misc/nice-fail.sh
index 92d00be..86e9d6f 100755
--- a/tests/misc/nice-fail.sh
+++ b/tests/misc/nice-fail.sh
@@ -22,17 +22,11 @@ print_ver_ nice


 # These tests verify exact status of internal failure.
-nice -n 1 # missing command
-test $? = 125 || fail=1
-nice --- # unknown option
-test $? = 125 || fail=1
-nice -n 1a # invalid adjustment
-test $? = 125 || fail=1
-nice sh -c 'exit 2' # exit status propagation
-test $? = 2 || fail=2
-nice . # invalid command
-test $? = 126 || fail=1
-nice no_such # no such command
-test $? = 127 || fail=1
+returns_ 125 nice -n 1 || fail=1 # missing command
+returns_ 125 nice --- || fail=1 # unknown option
+returns_ 125 nice -n 1a || fail=1 # invalid adjustment
+returns_ 2 nice sh -c 'exit 2' || fail=1 # exit status propagation
+returns_ 126 nice . || fail=1 # invalid command
+returns_ 127 nice no_such || fail=1 # no such command

 Exit $fail
diff --git a/tests/misc/nice.sh b/tests/misc/nice.sh
index 85523eb..36d6f91 100755
--- a/tests/misc/nice.sh
+++ b/tests/misc/nice.sh
@@ -79,8 +79,7 @@ if test x$(nice -n -1 nice 2> /dev/null) = x0 ; then
   compare exp err || fail=1
   # Failure to write advisory message is fatal.  Buggy through coreutils 8.0.
   if test -w /dev/full && test -c /dev/full; then
-    nice -n -1 nice > out 2> /dev/full
-    test $? = 125 || fail=1
+    returns_ 125 nice -n -1 nice > out 2> /dev/full || fail=1
     compare /dev/null out || fail=1
   fi
 else
diff --git a/tests/misc/nohup.sh b/tests/misc/nohup.sh
index ad60185..e9c8c38 100755
--- a/tests/misc/nohup.sh
+++ b/tests/misc/nohup.sh
@@ -70,8 +70,7 @@ if test -w /dev/full && test -c /dev/full; then

   exec >/dev/tty
   test -t 1 || exit 0
-  nohup echo hi 2> /dev/full
-  test $? = 125 || fail=1
+  returns_ 125 nohup echo hi 2> /dev/full || fail=1
   test -f nohup.out || fail=1
   compare /dev/null nohup.out || fail=1
   rm -f nohup.out
@@ -118,9 +117,7 @@ EOF

 # Make sure it fails with exit status of 125 when given too few arguments,
 # except that POSIX requires 127 in this case.
-nohup >/dev/null 2>&1
-test $? = 125 || fail=1
-POSIXLY_CORRECT=1 nohup >/dev/null 2>&1
-test $? = 127 || fail=1
+returns_ 125 nohup >/dev/null 2>&1 || fail=1
+POSIXLY_CORRECT=1 returns_ 127 nohup >/dev/null 2>&1 || fail=1

 Exit $fail
diff --git a/tests/misc/printenv.sh b/tests/misc/printenv.sh
index 5df4146..2017e5d 100755
--- a/tests/misc/printenv.sh
+++ b/tests/misc/printenv.sh
@@ -39,8 +39,7 @@ if env -- printenv | grep '^ENV_TEST' >/dev/null ; then
 fi

 # Printing a single variable's value.
-env -- printenv ENV_TEST > out
-test $? = 1 || fail=1
+returns_ 1 env -- printenv ENV_TEST > out || fail=1
 compare /dev/null out || fail=1
 echo a > exp || framework_failure_
 ENV_TEST=a env -- printenv ENV_TEST > out || fail=1
@@ -60,10 +59,8 @@ EOF
 compare exp out || fail=1

 # Exit status reflects missing variable, but remaining arguments processed.
-ENV_TEST1=a env -- printenv ENV_TEST2 ENV_TEST1 > out
-test $? = 1 || fail=1
-ENV_TEST1=a env -- printenv ENV_TEST1 ENV_TEST2 >> out
-test $? = 1 || fail=1
+ENV_TEST1=a returns_ 1 env -- printenv ENV_TEST2 ENV_TEST1 > out || fail=1
+ENV_TEST1=a returns_ 1 env -- printenv ENV_TEST1 ENV_TEST2 >> out || fail=1
 cat <<EOF > exp || framework_failure_
 a
 a
@@ -78,8 +75,7 @@ compare exp out || fail=1

 # Silently reject invalid env-var names.
 # Bug present through coreutils 8.0.
-env a=b=c printenv a=b > out
-test $? = 1 || fail=1
+returns_ 1 env a=b=c printenv a=b > out || fail=1
 compare /dev/null out || fail=1

 Exit $fail
diff --git a/tests/misc/stdbuf.sh b/tests/misc/stdbuf.sh
index 7b9aed5..31f02b7 100755
--- a/tests/misc/stdbuf.sh
+++ b/tests/misc/stdbuf.sh
@@ -43,19 +43,17 @@ stdbuf -o1 true || fail=1 # verify size syntax
 stdbuf -oK true || fail=1 # verify size syntax
 stdbuf -o0 true || fail=1 # verify unbuffered syntax
 stdbuf -oL true || fail=1 # verify line buffered syntax
-stdbuf -ol true # Capital 'L' required
-test $? = 125 || fail=1 # Internal error is a particular status
-stdbuf -o$SIZE_OFLOW true # size too large
-test $? = 125 || fail=1
-stdbuf -iL true # line buffering stdin disallowed
-test $? = 125 || fail=1
-stdbuf true # a buffering mode must be specified
-test $? = 125 || fail=1
+
+# Capital 'L' required
+# Internal error is a particular status
+returns_ 125 stdbuf -ol true || fail=1
+
+returns_ 125 stdbuf -o$SIZE_OFLOW true || fail=1 # size too large
+returns_ 125 stdbuf -iL true || fail=1 # line buffering stdin disallowed
+returns_ 125 stdbuf true || fail=1 # a buffering mode must be specified
 stdbuf -i0 -o0 -e0 true || fail=1 #check all files
-stdbuf -o1 . # invalid command
-test $? = 126 || fail=1
-stdbuf -o1 no_such # no such command
-test $? = 127 || fail=1
+returns_ 126 stdbuf -o1 . || fail=1 # invalid command
+returns_ 127 stdbuf -o1 no_such || fail=1 # no such command

 # Terminate any background processes
 cleanup_() { kill $pid 2>/dev/null && wait $pid; }
diff --git a/tests/misc/sync.sh b/tests/misc/sync.sh
index 5bf569c..cd89c5b 100755
--- a/tests/misc/sync.sh
+++ b/tests/misc/sync.sh
@@ -45,8 +45,7 @@ fi
 if test "$fail" != '1'; then
   # Ensure a fifo doesn't block
   mkfifo_or_skip_ fifo
-  timeout 10 sync fifo
-  test $? = 124 && fail=1
+  returns_ 124 timeout 10 sync fifo && fail=1
 fi

 Exit $fail
diff --git a/tests/misc/timeout.sh b/tests/misc/timeout.sh
index dcee6d7..208c954 100755
--- a/tests/misc/timeout.sh
+++ b/tests/misc/timeout.sh
@@ -30,23 +30,19 @@ timeout 1d true || fail=1
 timeout 0 true || fail=1

 # exit status propagation
-timeout 10 sh -c 'exit 2'
-test $? = 2 || fail=1
+returns_ 2 timeout 10 sh -c 'exit 2' || fail=1

 # timeout
-timeout .1 sleep 10
-test $? = 124 || fail=1
+returns_ 124 timeout .1 sleep 10 || fail=1

 # exit status propagation even on timeout
-timeout --preserve-status .1 sleep 10
 # exit status should be 128+TERM
-test $? = 124 && fail=1
+returns_ 124 timeout --preserve-status .1 sleep 10 && fail=1

 # kill delay. Note once the initial timeout triggers,
 # the exit status will be 124 even if the command
 # exits on its own accord.
-timeout -s0 -k1 .1 sleep 10
-test $? = 124 && fail=1
+returns_ 124 timeout -s0 -k1 .1 sleep 10 && fail=1

 # Ensure 'timeout' is immune to parent's SIGCHLD handler
 # Use a subshell and an exec to work around a bug in FreeBSD 5.0 /bin/sh.
diff --git a/tests/tail-2/follow-name.sh b/tests/tail-2/follow-name.sh
index ea5c5d9..f75da7a 100755
--- a/tests/tail-2/follow-name.sh
+++ b/tests/tail-2/follow-name.sh
@@ -24,8 +24,7 @@ tail: cannot open 'no-such' for reading: No such file or 
directory
 tail: no files remaining
 EOF

-timeout 10 tail --follow=name no-such > out 2> err
-test $? = 1 || fail=1
+returns_ 1 timeout 10 tail --follow=name no-such > out 2> err || fail=1

 # Remove an inconsequential inotify warning so
 # we can compare against the above error
diff --git a/tests/tail-2/pid.sh b/tests/tail-2/pid.sh
index 9e73b84..ba67b46 100755
--- a/tests/tail-2/pid.sh
+++ b/tests/tail-2/pid.sh
@@ -31,8 +31,7 @@ for mode in '' '---disable-inotify'; do
   tail -f $mode here & pid=$!

   # Ensure that tail --pid=PID does not exit when PID is alive.
-  timeout 1 tail -f -s.1 --pid=$pid $mode here
-  test $? = 124 || fail=1
+  returns_ 124 timeout 1 tail -f -s.1 --pid=$pid $mode here || fail=1

   cleanup_

@@ -44,8 +43,7 @@ for mode in '' '---disable-inotify'; do
   test $ret = 0 || fail=1

   # Ensure tail doesn't wait for data when PID is dead
-  timeout 10 tail -f -s10 --pid=$PID_T_MAX $mode empty
-  test $? = 124 && fail=1
+  returns_ 124 timeout 10 tail -f -s10 --pid=$PID_T_MAX $mode empty && fail=1
 done

 Exit $fail
diff --git a/tests/tail-2/wait.sh b/tests/tail-2/wait.sh
index 59f796a..1b54327 100755
--- a/tests/tail-2/wait.sh
+++ b/tests/tail-2/wait.sh
@@ -35,29 +35,23 @@ cleanup_() { kill $pid 2>/dev/null && wait $pid; }
 fastpoll='-s.1 --max-unchanged-stats=1'

 for mode in '' '---disable-inotify'; do
-  timeout 10 tail $fastpoll -f $mode not_here
-  test $? = 124 && fail=1
+  returns_ 124 timeout 10 tail $fastpoll -f $mode not_here && fail=1

   if test ! -r unreadable; then # can't test this when root
-    timeout 10 tail $fastpoll -f $mode unreadable
-    test $? = 124 && fail=1
+    returns_ 124 timeout 10 tail $fastpoll -f $mode unreadable && fail=1
   fi

-  timeout .1 tail $fastpoll -f $mode here 2>tail.err
-  test $? = 124 || fail=1
+  returns_ 124 timeout .1 tail $fastpoll -f $mode here 2>tail.err || fail=1

   # 'tail -F' must wait in any case.

-  timeout .1 tail $fastpoll -F $mode here 2>>tail.err
-  test $? = 124 || fail=1
+  returns_ 124 timeout .1 tail $fastpoll -F $mode here 2>>tail.err || fail=1

   if test ! -r unreadable; then # can't test this when root
-    timeout .1 tail $fastpoll -F $mode unreadable
-    test $? = 124 || fail=1
+    returns_ 124 timeout .1 tail $fastpoll -F $mode unreadable || fail=1
   fi

-  timeout .1 tail $fastpoll -F $mode not_here
-  test $? = 124 || fail=1
+  returns_ 124 timeout .1 tail $fastpoll -F $mode not_here || fail=1

   grep -Ev "$inotify_failed_re" tail.err > x
   mv x tail.err
-- 
2.9.3

Reply via email to