1#!/bin/sh
2# Ensure --version and --help options are processed before
3# any other options by some programs.
4
5# Copyright (C) 2019-2023 Free Software Foundation, Inc.
6
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
20. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
21print_ver_ yes
22
23programs="cksum dd hostid hostname link logname nohup
24sleep tsort unlink uptime users whoami yes"
25
26# All these variations should show the help/version screen
27# regardless of their position on the command line arguments.
28for prog in $programs; do
29  # skip if the program is not built (e.g., hostname)
30  case " $built_programs " in
31  *" $prog "*) ;;
32  *) continue;;
33  esac
34
35  for opt in --help --version; do
36    rm -f exp out1 out2 out3 || framework_failure_
37
38    # Get the reference output
39    env $prog $opt >exp || fail=1
40
41    # Test: some other argument AFTER --help/--version.
42    env $prog $opt AFTER > out1 || fail=1
43    compare exp out1 || fail=1
44
45    # nohup is an exception: stops processing after first non-option parameter.
46    # E.g., "nohup df --help" should run "df --help", not "df --help".
47    if [ "$prog" = nohup ]; then
48      rm -f exp  || framework_failure_
49      df $opt > exp || framework_failure_
50      BEFORE=df
51    else
52      BEFORE=BEFORE
53    fi
54
55    # Test: some other argument BEFORE --help/--version.
56    env $prog $BEFORE $opt > out2 || fail=1
57    compare exp out2 || fail=1
58
59    # Test: some other arguments BEFORE and AFTER --help/--version.
60    env $prog $BEFORE $opt AFTER > out3 || fail=1
61    compare exp out3 || fail=1
62  done
63done
64
65# After end-of-options marker (--), the options should not be parsed.
66# Test with 'yes', and assume the common code will work the
67# same for the other programs.
68cat >exp <<\EOF || framework_failure_
69--help
70EOF
71env yes -- --help | head -n1 > out
72compare exp out || fail=1
73
74Exit $fail
75