1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -eu
4set -o pipefail
5
6# Note: 'grep ... >/dev/null' instead of just 'grep -q' is used intentionally
7#       here, since 'grep -q' exits on the first match causing SIGPIPE being
8#       sent to the sender.
9
10BINARY="${1:?}"
11export SYSTEMD_LOG_LEVEL=info
12
13if [[ ! -x "$BINARY" ]]; then
14    echo "$BINARY is not an executable"
15    exit 1
16fi
17
18# output width
19if "$BINARY" --help | grep -v 'default:' | grep -E '.{80}.' >/dev/null; then
20    echo "$(basename "$BINARY") --help output is too wide:"
21    "$BINARY" --help | awk 'length > 80' | grep -E --color=yes '.{80}'
22    exit 1
23fi
24
25# --help prints something. Also catches case where args are ignored.
26if ! "$BINARY" --help | grep . >/dev/null; then
27    echo "$(basename "$BINARY") --help output is empty"
28    exit 2
29fi
30
31# no --help output to stderr
32if "$BINARY" --help 2>&1 1>/dev/null | grep .; then
33    echo "$(basename "$BINARY") --help prints to stderr"
34    exit 3
35fi
36
37# error output to stderr
38if ! ("$BINARY" --no-such-parameter 2>&1 1>/dev/null || :) | grep . >/dev/null; then
39    echo "$(basename "$BINARY") with an unknown parameter does not print to stderr"
40    exit 4
41fi
42
43# --help and -h are equivalent
44if ! diff <("$BINARY" -h) <("$BINARY" --help); then
45    echo "$(basename "$BINARY") --help and -h are not identical"
46    exit 5
47fi
48