Blame tests/test-help.sh

rpm-build 0fba15
#!/bin/bash
rpm-build 0fba15
#
rpm-build 0fba15
# Copyright (C) 2014 Owen Taylor <otaylor@redhat.com>
rpm-build 0fba15
#
rpm-build 0fba15
# SPDX-License-Identifier: LGPL-2.0+
rpm-build 0fba15
#
rpm-build 0fba15
# This library is free software; you can redistribute it and/or
rpm-build 0fba15
# modify it under the terms of the GNU Lesser General Public
rpm-build 0fba15
# License as published by the Free Software Foundation; either
rpm-build 0fba15
# version 2 of the License, or (at your option) any later version.
rpm-build 0fba15
#
rpm-build 0fba15
# This library is distributed in the hope that it will be useful,
rpm-build 0fba15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 0fba15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 0fba15
# Lesser General Public License for more details.
rpm-build 0fba15
#
rpm-build 0fba15
# You should have received a copy of the GNU Lesser General Public
rpm-build 0fba15
# License along with this library; if not, write to the
rpm-build 0fba15
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rpm-build 0fba15
# Boston, MA 02111-1307, USA.
rpm-build 0fba15
rpm-build 0fba15
set -euo pipefail
rpm-build 0fba15
rpm-build 0fba15
. $(dirname $0)/libtest.sh
rpm-build 0fba15
rpm-build 0fba15
echo "1..1"
rpm-build 0fba15
rpm-build 0fba15
test_usage_output() {
rpm-build 0fba15
    file=$1; shift
rpm-build 0fba15
    cmd=$1; shift
rpm-build 0fba15
    assert_file_has_content $file '^Usage'
rpm-build 0fba15
    # check that it didn't print twice somehow
rpm-build 0fba15
    if [ "$(grep --count '^Usage' $file)" != 1 ]; then
rpm-build 0fba15
      _fatal_print_file "$file" "File '$file' has '^Usage' more than once."
rpm-build 0fba15
    fi
rpm-build 0fba15
    assert_file_has_content $file "$cmd"
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
# check that we found at least one command with subcommands
rpm-build 0fba15
found_subcommands=0
rpm-build 0fba15
rpm-build 0fba15
test_recursive() {
rpm-build 0fba15
    local cmd=$1
rpm-build 0fba15
rpm-build 0fba15
    echo "$cmd" 1>&2
rpm-build 0fba15
    $cmd --help 1>out 2>err
rpm-build 0fba15
    # --help message goes to standard output
rpm-build 0fba15
    test_usage_output out "$cmd"
rpm-build 0fba15
    assert_file_empty err
rpm-build 0fba15
rpm-build 0fba15
    # Select the list of commands, for each line, remove the leading spaces, and take the first word(command) of each line
rpm-build 0fba15
    builtins=`sed -n '/^Builtin \("[^"]*" \)\?Commands:$/,/^$/p' 
rpm-build 0fba15
    if [ "$builtins" != "" ] ; then
rpm-build 0fba15
rpm-build 0fba15
        found_subcommands=1
rpm-build 0fba15
rpm-build 0fba15
        # A command with subcommands
rpm-build 0fba15
        # Running the command without a subcommand should produce the help output, but fail
rpm-build 0fba15
        rc=0
rpm-build 0fba15
        $cmd 1>out 2>err || rc=$?
rpm-build 0fba15
        if [ $rc = 0 ] ; then
rpm-build 0fba15
            assert_not_reached "missing subcommand but 0 exit status"
rpm-build 0fba15
        fi
rpm-build 0fba15
rpm-build 0fba15
        # error message and usage goes to standard error
rpm-build 0fba15
        test_usage_output err "$cmd"
rpm-build 0fba15
        assert_file_has_content err 'No \("[^"]*" sub\)\?command specified'
rpm-build 0fba15
        assert_file_empty out
rpm-build 0fba15
rpm-build 0fba15
        rc=0
rpm-build 0fba15
        $cmd non-existent-subcommand 1>out 2>err || rc=$?
rpm-build 0fba15
        if [ $rc = 0 ] ; then
rpm-build 0fba15
            assert_not_reached "non-existent subcommand but 0 exit status"
rpm-build 0fba15
        fi
rpm-build 0fba15
rpm-build 0fba15
        test_usage_output err "$cmd"
rpm-build 0fba15
        assert_file_has_content err 'Unknown \("[^"]*" sub\)\?command'
rpm-build 0fba15
        assert_file_empty out
rpm-build 0fba15
rpm-build 0fba15
        for subcmd in $builtins; do
rpm-build 0fba15
            case "$subcmd" in
rpm-build 0fba15
                (trivial-httpd)
rpm-build 0fba15
                    # Skip trivial-httpd if enabled, it doesn't work
rpm-build 0fba15
                    # uninstalled (and also doesn't produce the output
rpm-build 0fba15
                    # we expect).
rpm-build 0fba15
                    ;;
rpm-build 0fba15
                (*)
rpm-build 0fba15
                    test_recursive "$cmd $subcmd"
rpm-build 0fba15
                    ;;
rpm-build 0fba15
            esac
rpm-build 0fba15
        done
rpm-build 0fba15
    fi
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
test_recursive ostree
rpm-build 0fba15
if [ $found_subcommands != 1 ]; then
rpm-build 0fba15
  assert_not_reached "no ostree commands with subcommands found!"
rpm-build 0fba15
fi
rpm-build 0fba15
rpm-build 0fba15
echo "ok help option is properly supported"