Changeset - 95e112372df9
[Not reviewed]
0 3 1
Branko Majic (branko) - 7 years ago 2018-02-28 15:13:40
branko@majic.rs
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.
4 files changed with 80 insertions and 1 deletions:
0 comments (0 inline, 0 general)
functional_tests/test_help.py
Show inline comments
 
@@ -60,25 +60,25 @@ def test_extended_help_shown():
 
    # decides to try out the -h flag to the command.
 
    stdout_h_flag, stderr_h_flag, returncode_h_flag = run_command("gimmecert", "-h")
 

	
 
    # In doing so, John is presented with much more extensive
 
    # instructions that provide him with better idea on how to use the
 
    # tool.
 
    assert stderr_h_flag == ''
 
    assert returncode_h_flag == 0
 
    assert "usage: gimmecert [-h]" in stdout_h_flag
 
    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
 

	
 
    # John also notices the help command in the list. He tries that
 
    # one out as well.
 
    stdout_help, stderr_help, returncode_help = run_command("gimmecert", "help")
 

	
 
    # John is presented with identical results as when running just
 
    # with the -h flag.
 
    assert returncode_help == 0
 
    assert stdout_help == stdout_h_flag
functional_tests/test_init.py
Show inline comments
 
new file 100644
 
# -*- 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 <http://www.gnu.org/licenses/>.
 
#
 

	
 

	
 
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")
gimmecert/cli.py
Show inline comments
 
@@ -23,24 +23,36 @@ import argparse
 

	
 
from .decorators import subcommand_parser, get_subcommand_parser_setup_functions
 

	
 

	
 
DESCRIPTION = """\
 
Issues server and client X.509 certificates using a local CA
 
hierarchy.
 

	
 
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')
 

	
 
    subparser.set_defaults(func=lambda args: parser.print_help())
 

	
 
    return subparser
 

	
 

	
 
def get_parser():
 
    """
 
    Sets-up and returns a CLI argument parser.
tests/test_cli.py
Show inline comments
 
@@ -125,12 +125,36 @@ def test_help_subcommand_returns_parser():
 
    subparser = gimmecert.cli.setup_help_subcommand_parser(parser, subparsers)
 

	
 
    assert isinstance(subparser, argparse.ArgumentParser)
 

	
 

	
 
def test_help_subcommand_sets_function_callback():
 
    parser = argparse.ArgumentParser()
 
    subparsers = parser.add_subparsers()
 

	
 
    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'))
0 comments (0 inline, 0 general)