From 95e112372df94ce8bea75774cc8f1474b5dc3f99 2018-02-28 15:13:40 From: Branko Majic Date: 2018-02-28 15:13:40 Subject: [PATCH] GC-3: Added dummy command for CA initialisation: - Fixed test for the help command (help output now includes multiple comands). - Added dummy/skeleton implementation for init command parser. --- diff --git a/functional_tests/test_help.py b/functional_tests/test_help.py index c8399def54f233ce5f331cbfdd0db07b68d3a6dd..69a00a3a07af228f34be96ba47a6df3ea757d2b4 100644 --- a/functional_tests/test_help.py +++ b/functional_tests/test_help.py @@ -69,7 +69,7 @@ def test_extended_help_shown(): assert "Examples:" in stdout_h_flag assert "optional arguments" in stdout_h_flag # Subcommands listed. - assert "{help}" in stdout_h_flag + assert "help" in stdout_h_flag # @TODO: Can't really test this without producing errors, but # possibly not needed. # assert "positional arguments" in stdout_h_flag diff --git a/functional_tests/test_init.py b/functional_tests/test_init.py new file mode 100644 index 0000000000000000000000000000000000000000..49f6c0f02299a8c1c60fda2b055fc07213a1ba3a --- /dev/null +++ b/functional_tests/test_init.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2018 Branko Majic +# +# This file is part of Gimmecert. +# +# Gimmecert 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. +# +# Gimmecert 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 +# Gimmecert. If not, see . +# + + +from .base import run_command + + +def test_init_command_available_with_help(): + # John has decided it is time to try out the tool. He starts off + # by running the short usage help. + stdout, stderr, returncode = run_command("gimmecert", "-h") + + # Looking at output, John notices the init command. + assert returncode == 0 + assert stderr == "" + assert 'init' in stdout + + # John decides to look at a more detailed description of this + # command before proceeding. + stdout, stderr, returncode = run_command("gimmecert", "init", "-h") + + # John notices that this command has some useful usage + # instructions, which allows him to study the available arguments. + assert returncode == 0 + assert stderr == "" + assert stdout.startswith("usage: gimmecert init") diff --git a/gimmecert/cli.py b/gimmecert/cli.py index 218f4112b2f7c91491c2d86e7748354d695f4fd9..aa95063660a74aaae9a6f84fcd37e92859c3f3cb 100644 --- a/gimmecert/cli.py +++ b/gimmecert/cli.py @@ -32,6 +32,18 @@ Examples: """ +@subcommand_parser +def setup_init_subcommand_parser(parser, subparsers): + subparser = subparsers.add_parser('init', description='Initialise CA hierarchy.') + + def init(args): + pass + + subparser.set_defaults(func=init) + + return subparser + + @subcommand_parser def setup_help_subcommand_parser(parser, subparsers): subparser = subparsers.add_parser('help', description='shows help') diff --git a/tests/test_cli.py b/tests/test_cli.py index 55809df657ede4fe0dba763056395aebbe1fbc2d..06a3aebb2cdc1b907a4b09473b497e3014c38e6f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -134,3 +134,27 @@ def test_help_subcommand_sets_function_callback(): subparser = gimmecert.cli.setup_help_subcommand_parser(parser, subparsers) assert callable(subparser.get_default('func')) + + +def test_setup_init_subcommand_parser_registered(): + registered_functions = gimmecert.decorators.get_subcommand_parser_setup_functions() + + assert gimmecert.cli.setup_init_subcommand_parser in registered_functions + + +def test_setup_init_subcommand_returns_parser(): + parser = argparse.ArgumentParser() + subparsers = parser.add_subparsers() + + subparser = gimmecert.cli.setup_init_subcommand_parser(parser, subparsers) + + assert isinstance(subparser, argparse.ArgumentParser) + + +def test_setup_init_subcommand_sets_function_callback(): + parser = argparse.ArgumentParser() + subparsers = parser.add_subparsers() + + subparser = gimmecert.cli.setup_init_subcommand_parser(parser, subparsers) + + assert callable(subparser.get_default('func'))