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

From: Bernhard Voelker <[email-masked]>
Date: Tue, 28 Nov 2017 01:06:18 +0100

On 11/27/2017 07:21 PM, Pádraig Brady wrote:
> Blush. Please apply.

Thanks, pushed.

>> BTW: what do you think about a new syntax-check rule to verify that
>> all options in usage are actually supported?
>> Some of the option checks for some utilities might become complicated,
>> but maybe still worth ...
> 
> I've caught this particular issue myself in a couple of patches from others,
> so I'll write a syntax check later today hopefully.

I've also played a bit ... and it turned out to become a regular
test instead of a syntax-check rule.
It's a draft, and I only tested here on openSUSE:Tumbleweed,
but at least it seems to be a good start.  Maybe it should be
marked as expensive.

Have a nice day,
Berny
>From 0220fab9f0dfb2eb1849f07a33198f4923bc2abf Mon Sep 17 00:00:00 2001
From: Bernhard Voelker <[email-masked]>
Date: Mon, 27 Nov 2017 23:46:51 +0100
Subject: [PATCH] tests: verify usage vs. getopt [DRAFT]

* tests/misc/usage_vs_getopt.sh: Add test.
* tests/local.mk (all_tests): Reference it.
---
 tests/local.mk                |   1 +
 tests/misc/usage_vs_getopt.sh | 101 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 102 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..923bb30ec
--- /dev/null
+++ b/tests/misc/usage_vs_getopt.sh
@@ -0,0 +1,101 @@
+#!/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.
+  rcexp=1
+  case "$prg" in
+    expr ) rcexp=0 ;;
+    dir | ls | printenv | sort | tty | vdir ) rcexp=2 ;;
+    env | chroot | nice | nohup | stdbuf | timeout ) rcexp=125 ;;
+  esac
+  returns_ $rcexp $prg --thisoptiondoesnotexist >/dev/null 2> exp1 || fail=1
+  sed 's/--thisoptiondoesnotexist/OPT/' < exp1 > expl || framework_failure_
+  returns_ $rcexp $prg -/ >/dev/null 2> exp2 || fail=1
+  sed "s/'\/'/'OPT'/" < exp2 > exps || framework_failure_
+
+  # Get output for --help.
+  $prg --help > help || fail=1
+
+  # Get list of options of the utility, including --help but stopping
+  # at --version to avoid to get lines with explanations below the options
+  # list like e.g. in 'cut --help | grep -F -- "-M"'.
+  awk '/^[ ]*--version/ { exit; }
+       /^[ ]*-/ {
+         for(i=1;i<=NF;i++) {
+           if (substr($i,1,1)!="-")
+             break;
+           print gensub(/[[,=].*$/,"",1,$i)
+         }
+       }
+       ' help \
+    | grep . > opts || framework_failure_
+
+  # 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 either requires
+    # a further argument, or --help triggers usage(); so $prg should
+    # exit with failure in any case.
+    # Add a 2nd --help for the 'env -u --help' case.
+    $prg "$opt" --help --help </dev/null >out 2>err1
+    rc=$?
+    # In the --help case, the exit code should have been 0.
+    if [ $rc = 0 ]; then
+      compare help out || fail=1
+    else
+      # Catch false positives.
+      case "$prg/$opt" in
+        'pr/-COLUMN') continue;;
+      esac
+      # Else $prg should have complained about a missing argument.
+      # Replace $opt in stderr output by the neutral placeholder.
+      # Differentiate between long vs. short option error messages.
+      # Fail if the stderr output is identical to the above provoked error.
+      case "$opt" in
+        '--'*)
+          sed "s/$opt/OPT/" < err1 > err || framework_failure_
+          compare expl err && { fail=1; cat err1; }
+          ;;
+        *)
+          sed "s/'.'/'OPT'/" < err1 > err || framework_failure_
+          compare exps err && { fail=1; cat err1; }
+          ;;
+      esac
+    fi
+  done < opts
+}
+
+for prg in $built_programs; do
+  case "$prg" in
+    # Skip some utilities entirely.
+    '[' )
+      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