Subject: bug#21760: timeout: Feature Request: --verbose ==> output if timeout

From: Bernhard Voelker <[email-masked]>
Date: Tue, 28 Nov 2017 23:48:00 +0100

On 11/28/2017 11:03 AM, Pádraig Brady wrote:
> Attached is a diff to change to using sed.

Thanks, I merged your changes.

Please note that this new check only works with the patch applied
discussed at:
   http://lists.gnu.org/archive/html/coreutils/2017-11/msg00037.html

Thanks & have a nice day,
Berny
>From ba8c567cf05f4a53c9d9a1a56cc21d39aef71354 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email-masked]>
Date: Tue, 28 Nov 2017 23:34:41 +0100
Subject: [PATCH 1/2] readlink: remove superfluous comma from usage output

* src/readlink.c (usage): Remove ',' after --quiet option.
---
 src/readlink.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/readlink.c b/src/readlink.c
index abbd35237..6b41f79a4 100644
--- a/src/readlink.c
+++ b/src/readlink.c
@@ -78,7 +78,7 @@ usage (int status)
 \n\
                                 without requirements on components existence\n\
   -n, --no-newline              do not output the trailing delimiter\n\
-  -q, --quiet,\n\
+  -q, --quiet\n\
   -s, --silent                  suppress most error messages (on by default)\n\
   -v, --verbose                 report error messages\n\
   -z, --zero                    end each output line with NUL, not newline\n\
-- 
2.15.0

>From 28760943767c521940855715eb245518bb26d2ff Mon Sep 17 00:00:00 2001
From: Bernhard Voelker <[email-masked]>
Date: Mon, 27 Nov 2017 23:46:51 +0100
Subject: [PATCH 2/2] tests: verify usage vs. getopt
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Verify that all options mentioned in usage are actually recognized
by the program.

* tests/misc/usage_vs_getopt.sh: Add test.
* tests/local.mk (all_tests): Reference it.

Co-authored-by: Pádraig Brady <[email-masked]>
---
 tests/local.mk                |  1 +
 tests/misc/usage_vs_getopt.sh | 92 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)
 create mode 100755 tests/misc/usage_vs_getopt.sh

diff --git a/tests/local.mk b/tests/local.mk
index 8ee7c5039..612316b61 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -422,6 +422,7 @@ all_tests =					\
   tests/misc/truncate-relative.sh		\
   tests/misc/tsort.pl				\
   tests/misc/tty.sh				\
+  tests/misc/usage_vs_getopt.sh			\
   tests/misc/unexpand.pl			\
   tests/misc/uniq.pl				\
   tests/misc/uniq-perf.sh			\
diff --git a/tests/misc/usage_vs_getopt.sh b/tests/misc/usage_vs_getopt.sh
new file mode 100755
index 000000000..1d6cf9242
--- /dev/null
+++ b/tests/misc/usage_vs_getopt.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+# Verify that all options mentioned in usage are recognized by getopt.
+
+# Copyright (C) 2017 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+
+checkprg () {
+  prg="$1"
+
+  # Get stderr output for unrecognized options for later use as a pattern.
+  # Also consider the expected exit status of each program.
+  rcexp=1
+  case "$prg" in
+    dir | ls | printenv | sort | tty | vdir ) rcexp=2 ;;
+    env | chroot | nice | nohup | stdbuf | timeout ) rcexp=125 ;;
+  esac
+  # Write the pattern for a long, unknown option into a pattern file.
+  o='thisoptiondoesnotexist'
+  returns_ $rcexp $prg --$o >/dev/null 2> err || fail=1
+  grep -F "$o" err || framework_failure_
+  sed -n "1s/--$o/OPT/p" < err > pat || framework_failure_
+
+  # Append the pattern for a short unkown option.
+  returns_ $rcexp $prg -/ >/dev/null 2> err || fail=1
+  grep -F "'/'" err || framework_failure_
+  sed -n "1s/'\/'/'OPT'/p" < err >> pat || framework_failure_
+
+  # Get output for --help.
+  $prg --help > help || fail=1
+
+  # Extract all options mention in the above --help output.
+  sed -n -e '/--version/q' \
+    -e 's/^ \{2,6\}-/-/; s/  .*//; s/[=[].*//; s/, /\'$'\n''/g; s/^-/-/p' help \
+    > opts || framework_failure_
+  cat opts  # for debugging purposes
+
+  # Test all options mentioned in usage (but --version).
+  while read opt; do
+    test "x$opt" = 'x--help' \
+      && continue
+    # Append --help to be on the safe side: the option under test either
+    # requires a further argument, or --help triggers usage(); so $prg should
+    # exit without performing its regular operation.
+    # Add a 2nd --help for the 'env -u --help' case.
+    $prg "$opt" --help --help </dev/null >out 2>err1
+    rc=$?
+    # In the --help case, i.e., when the option under test has been accepted,
+    # the exit code should be Zero.
+    if [ $rc = 0 ]; then
+      compare help out || fail=1
+    else
+      # Else $prg should have complained about a missing argument.
+      # Catch false positives.
+      case "$prg/$opt" in
+        'pr/-COLUMN') continue;;
+      esac
+      # Replace $opt in stderr output by the neutral placeholder.
+      # Handle both long and short option error messages.
+      sed -e "s/$opt/OPT/" -e "s/'.'/'OPT'/" < err1 > err || framework_failure_
+      # Fail if the stderr output matches the above provoked error.
+      grep -Ff pat err && { fail=1; cat err1; }
+    fi
+  done < opts
+}
+
+for prg in $built_programs; do
+  case "$prg" in
+    # Skip utilities entirely which have special option parsing.
+    '[' | expr | stty )
+      continue;;
+    # Wrap some utilities known by the shell by env.
+    echo | false | kill | printf | pwd | test | true )
+      prg="env $prg";;
+  esac
+  checkprg $prg
+done
+
+Exit $fail
-- 
2.15.0