Jim Meyering wrote: > Eric Blake wrote: >> On 11/20/2011 06:11 AM, Bruno Haible wrote: >>> Fix test failures on OSF/1 5.1. >>> * tests/ignore-mmap: Pass a regular file, not /dev/null, to 'compare'. >>> * tests/max-count-vs-context: Likewise. >> >> Autoconf learned that it is always more efficient to use 'test -s file' >> rather than comparison against /dev/null when you are checking for >> (non-)emptiness (one less fork, and in the case of buggy compare that >> can't handle /dev/null, one less portability problem). >> >>> +++ tests/max-count-vs-context 2011-11-20 14:08:59.000000000 +0100 >>> @@ -13,11 +13,12 @@ >>> EOF >>> >>> sed 4q in > exp || framework_failure_ >>> +: > errexp || framework_failure_ >>> >>> fail=0 >>> grep -m1 -A5 needle in > out 2>err || fail=1 >>> >>> compare out exp || fail=1 >>> -compare err /dev/null || fail=1 >>> +compare err errexp || fail=1 >> >> That is, rather than creating an empty errexp, I'd rather see this test >> rewritten to use a form of test -s. > > How about having compare "know" about /dev/null. > Then it can perform the test -s and warn if the file is nonempty. > With that, all existing (and there are many) /dev/null-using > compare uses will benefit.
I've implemented that. What do you think? >From 87a00b2ab8e274c4a832f7e630944d38010f585c Mon Sep 17 00:00:00 2001 From: Jim Meyering <[email protected]> Date: Mon, 21 Nov 2011 21:50:23 +0100 Subject: [PATCH] init.sh: work around OSF/1 5.1's mishandling of /dev/null * tests/init.sh: Make our compare function slightly more portable. Reported by Bruno Haible in http://thread.gmane.org/gmane.comp.gnu.grep.bugs/4020 --- ChangeLog | 7 +++++++ tests/init.sh | 45 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8173a2e..8136dd8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2011-11-21 Jim Meyering <[email protected]> + + init.sh: work around OSF/1 5.1's mishandling of /dev/null + * tests/init.sh: Make our compare function slightly more portable. + Reported by Bruno Haible in + http://thread.gmane.org/gmane.comp.gnu.grep.bugs/4020 + 2011-11-20 Simon Josefsson <[email protected]> * m4/manywarnings.m4: Add more warnings from gcc 4.6.2. diff --git a/tests/init.sh b/tests/init.sh index c78e5b6..bd18a2d 100644 --- a/tests/init.sh +++ b/tests/init.sh @@ -221,11 +221,33 @@ export MALLOC_PERTURB_ # a partition, or to undo any other global state changes. cleanup_ () { :; } +# Arrange not to let diff or cmp operate on /dev/null, +# since on some systems (at least OSF/1 5.1), that doesn't work. +# When there are not two arguments, return 2. +# When one argument is /dev/null and the other is not empty, +# cat the nonempty file to stderr and return 1. +# Otherwise, return 0. +compare_dev_null_ () +{ + local err + test $# = 2 || return 2 + + if test "$1" = /dev/null; then + test -s "$2" && err="$2" + elif test "$2" = /dev/null; then + test -s "$1" && err="$1" + fi + test -z "$err" && return 0 + + cat "$err" 1>&2 + return 1 +} + if diff_out_=`( diff -u "$0" "$0" < /dev/null ) 2>/dev/null`; then if test -z "$diff_out_"; then - compare () { diff -u "$@"; } + compare_ () { diff -u "$@"; } else - compare () + compare_ () { if diff -u "$@" > diff.out; then # No differences were found, but Solaris 'diff' produces output @@ -241,9 +263,9 @@ if diff_out_=`( diff -u "$0" "$0" < /dev/null ) 2>/dev/null`; then fi elif diff_out_=`( diff -c "$0" "$0" < /dev/null ) 2>/dev/null`; then if test -z "$diff_out_"; then - compare () { diff -c "$@"; } + compare_ () { diff -c "$@"; } else - compare () + compare_ () { if diff -c "$@" > diff.out; then # No differences were found, but AIX and HP-UX 'diff' produce output @@ -259,11 +281,22 @@ elif diff_out_=`( diff -c "$0" "$0" < /dev/null ) 2>/dev/null`; then } fi elif ( cmp --version < /dev/null 2>&1 | grep GNU ) > /dev/null 2>&1; then - compare () { cmp -s "$@"; } + compare_ () { cmp -s "$@"; } else - compare () { cmp "$@"; } + compare_ () { cmp "$@"; } fi +# Given compare_dev_null_'s preprocessing, for 0 or 2, defer to compare_. +# Otherwise, differences have already been printed, so return 1. +compare () +{ + compare_dev_null_ "$@" + case $? in + 0|2) compare_ "$@";; + 1) return 1;; + esac +} + # An arbitrary prefix to help distinguish test directories. testdir_prefix_ () { printf gt; } -- 1.7.8.rc2.3.g0911
