2014-09-25 11:05:45 +04:00
#!/bin/bash
#
# Copyright (C) 2014 Owen Taylor <otaylor@redhat.com>
#
2018-01-30 22:26:26 +03:00
# SPDX-License-Identifier: LGPL-2.0+
#
2014-09-25 11:05:45 +04:00
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
2021-12-07 04:20:55 +03:00
# License along with this library. If not, see <https://www.gnu.org/licenses/>.
2014-09-25 11:05:45 +04:00
2016-01-27 19:44:10 +03:00
set -euo pipefail
2014-09-25 11:05:45 +04:00
. $( dirname $0 ) /libtest.sh
echo "1..1"
2017-09-05 21:27:20 +03:00
test_usage_output( ) {
file = $1 ; shift
cmd = $1 ; shift
assert_file_has_content $file '^Usage'
# check that it didn't print twice somehow
if [ " $( grep --count '^Usage' $file ) " != 1 ] ; then
_fatal_print_file " $file " " File ' $file ' has '^Usage' more than once. "
fi
assert_file_has_content $file " $cmd "
}
# check that we found at least one command with subcommands
found_subcommands = 0
2014-09-25 11:05:45 +04:00
test_recursive( ) {
local cmd = $1
2014-10-21 18:20:58 +04:00
2014-09-25 11:05:45 +04:00
echo " $cmd " 1>& 2
$cmd --help 1>out 2>err
# --help message goes to standard output
2017-09-05 21:27:20 +03:00
test_usage_output out " $cmd "
2014-09-25 11:05:45 +04:00
assert_file_empty err
2017-09-05 21:27:20 +03:00
2017-10-13 19:15:13 +03:00
# Select the list of commands, for each line, remove the leading spaces, and take the first word(command) of each line
builtins = ` sed -n '/^Builtin \("[^"]*" \)\?Commands:$/,/^$/p' <out | tail -n +2 | sed -e 's/^[[:space:]]*//' | cut -d " " -f1`
2014-09-25 11:05:45 +04:00
if [ " $builtins " != "" ] ; then
2017-09-05 21:27:20 +03:00
found_subcommands = 1
2014-09-25 11:05:45 +04:00
# A command with subcommands
# Running the command without a subcommand should produce the help output, but fail
2017-09-05 21:27:20 +03:00
rc = 0
$cmd 1>out 2>err || rc = $?
if [ $rc = 0 ] ; then
assert_not_reached "missing subcommand but 0 exit status"
2014-09-25 11:05:45 +04:00
fi
2017-09-05 21:27:20 +03:00
2014-09-25 11:05:45 +04:00
# error message and usage goes to standard error
2017-09-05 21:27:20 +03:00
test_usage_output err " $cmd "
assert_file_has_content err 'No \("[^"]*" sub\)\?command specified'
assert_file_empty out
rc = 0
$cmd non-existent-subcommand 1>out 2>err || rc = $?
if [ $rc = 0 ] ; then
assert_not_reached "non-existent subcommand but 0 exit status"
fi
test_usage_output err " $cmd "
assert_file_has_content err 'Unknown \("[^"]*" sub\)\?command'
2014-09-25 11:05:45 +04:00
assert_file_empty out
2017-09-05 21:27:20 +03:00
for subcmd in $builtins ; do
2017-09-15 18:05:48 +03:00
case " $subcmd " in
( trivial-httpd)
# Skip trivial-httpd if enabled, it doesn't work
# uninstalled (and also doesn't produce the output
# we expect).
; ;
( *)
test_recursive " $cmd $subcmd "
; ;
esac
2014-09-25 11:05:45 +04:00
done
fi
}
2017-09-05 21:27:20 +03:00
test_recursive ostree
if [ $found_subcommands != 1 ] ; then
assert_not_reached "no ostree commands with subcommands found!"
fi
2014-09-25 11:05:45 +04:00
echo "ok help option is properly supported"