Changeset - 5a9a3eede117
[Not reviewed]
0 4 0
Branko Majic (branko) - 6 years ago 2018-03-09 22:07:16
branko@majic.rs
GC-17: Refactored calls to help and usage printing functions:

- Introduced distinct help/usage functions in commands module.
- Wrap the help/usage in similar way to existing commands.
- Added unit tests for new commands and their invocation from CLI.
4 files changed with 129 insertions and 3 deletions:
0 comments (0 inline, 0 general)
gimmecert/cli.py
Show inline comments
 
@@ -24,7 +24,7 @@ import os
 
import sys
 

	
 
from .decorators import subcommand_parser, get_subcommand_parser_setup_functions
 
from .commands import init, server, ExitCode
 
from .commands import init, server, help_, usage, ExitCode
 

	
 

	
 
ERROR_GENERIC = 10
 
@@ -74,7 +74,13 @@ def setup_init_subcommand_parser(parser, subparsers):
 
def setup_help_subcommand_parser(parser, subparsers):
 
    subparser = subparsers.add_parser('help', description='shows help')
 

	
 
    subparser.set_defaults(func=lambda args: parser.print_help())
 
    def help_wrapper(args):
 
        status_code = help_(sys.stdout, sys.stderr, parser)
 

	
 
        if status_code != ExitCode.SUCCESS:
 
            exit(status_code)
 

	
 
    subparser.set_defaults(func=help_wrapper)
 

	
 
    return subparser
 

	
 
@@ -107,7 +113,13 @@ def get_parser():
 

	
 
    parser = argparse.ArgumentParser(description=DESCRIPTION, formatter_class=argparse.RawDescriptionHelpFormatter)
 

	
 
    parser.set_defaults(func=lambda args: parser.print_usage())
 
    def usage_wrapper(args):
 
        status_code = usage(sys.stdout, sys.stderr, parser)
 

	
 
        if status_code != ExitCode.SUCCESS:
 
            exit(status_code)
 

	
 
    parser.set_defaults(func=usage_wrapper)
 

	
 
    subparsers = parser.add_subparsers()
 

	
gimmecert/commands.py
Show inline comments
 
@@ -142,3 +142,47 @@ def server(stdout, stderr, project_directory, entity_name, extra_dns_names):
 
    gimmecert.storage.write_certificate(certificate, certificate_path)
 

	
 
    return ExitCode.SUCCESS
 

	
 

	
 
def help_(stdout, stderr, parser):
 
    """
 
    Output help for the user.
 

	
 
    :param stdout: Output stream where the informative messages should be written-out.
 
    :type stdout: io.IOBase
 

	
 
    :param stderr: Output stream where the error messages should be written-out.
 
    :type stderr: io.IOBase
 

	
 
    :param parser: Parser used for processing the CLI positional and optional arguments.
 
    :type parser: argparse.ArgumentParser
 

	
 
    :returns: Status code, one from gimmecert.commands.ExitCode.
 
    :rtype: int
 
    """
 

	
 
    parser.print_help(stdout)
 

	
 
    return ExitCode.SUCCESS
 

	
 

	
 
def usage(stdout, stderr, parser):
 
    """
 
    Output usage for the user.
 

	
 
    :param stdout: Output stream where the informative messages should be written-out.
 
    :type stdout: io.IOBase
 

	
 
    :param stderr: Output stream where the error messages should be written-out.
 
    :type stderr: io.IOBase
 

	
 
    :param parser: Parser used for processing the CLI positional and optional arguments.
 
    :type parser: argparse.ArgumentParser
 

	
 
    :returns: Status code, one from gimmecert.commands.ExitCode.
 
    :rtype: int
 
    """
 

	
 
    parser.print_usage(stdout)
 

	
 
    return ExitCode.SUCCESS
tests/test_cli.py
Show inline comments
 
@@ -333,3 +333,28 @@ def test_server_command_exists_with_error_if_hierarchy_not_initialised(tmpdir):
 
        gimmecert.cli.main()
 

	
 
    assert e_info.value.code != 0
 

	
 

	
 
@mock.patch('sys.argv', ['gimmecert', 'help'])
 
@mock.patch('gimmecert.cli.help_')
 
def test_help_command_invoked_with_correct_parameters(mock_help_):
 
    mock_help_.return_value = gimmecert.commands.ExitCode.SUCCESS
 

	
 
    gimmecert.cli.main()
 

	
 
    assert mock_help_.called
 
    assert mock_help_.call_count == 1
 

	
 

	
 
@mock.patch('sys.argv', ['gimmecert'])
 
@mock.patch('gimmecert.cli.usage')
 
def test_usage_command_invoked_with_correct_parameters(mock_usage):
 
    mock_usage.return_value = gimmecert.commands.ExitCode.SUCCESS
 

	
 
    gimmecert.cli.main()
 

	
 
    # Can't test calling arguments, since we'd need to mask
 
    # get_parser, and if we do that we mask the set_defaults and
 
    # what-not.
 
    assert mock_usage.called
 
    assert mock_usage.call_count == 1
tests/test_commands.py
Show inline comments
 
@@ -18,6 +18,7 @@
 
# Gimmecert.  If not, see <http://www.gnu.org/licenses/>.
 
#
 

	
 
import argparse
 
import io
 
import os
 

	
 
@@ -294,3 +295,47 @@ def test_init_command_stdout_and_stderr_if_hierarchy_already_initialised(tmpdir)
 

	
 
    assert "CA hierarchy has already been initialised" in stderr
 
    assert stdout == ""
 

	
 

	
 
def test_help_command_returns_success_status_code():
 
    status_code = gimmecert.commands.help_(io.StringIO(), io.StringIO(), argparse.ArgumentParser())
 

	
 
    assert status_code == gimmecert.commands.ExitCode.SUCCESS
 

	
 

	
 
def test_help_command_outputs_help():
 
    stdout_stream = io.StringIO()
 
    stderr_stream = io.StringIO()
 

	
 
    parser = argparse.ArgumentParser(description="This is help")
 

	
 
    gimmecert.commands.help_(stdout_stream, stderr_stream, parser)
 

	
 
    stdout = stdout_stream.getvalue()
 
    stderr = stderr_stream.getvalue()
 

	
 
    assert stderr == ""
 
    assert "usage" in stdout
 
    assert "--help" in stdout
 

	
 

	
 
def test_usage_command_returns_success_status_code():
 
    status_code = gimmecert.commands.usage(io.StringIO(), io.StringIO(), argparse.ArgumentParser())
 

	
 
    assert status_code == gimmecert.commands.ExitCode.SUCCESS
 

	
 

	
 
def test_usage_command_outputs_usage():
 
    stdout_stream = io.StringIO()
 
    stderr_stream = io.StringIO()
 

	
 
    parser = argparse.ArgumentParser(description="This is help")
 

	
 
    gimmecert.commands.usage(stdout_stream, stderr_stream, parser)
 

	
 
    stdout = stdout_stream.getvalue()
 
    stderr = stderr_stream.getvalue()
 

	
 
    assert stderr == ""
 
    assert "usage:" in stdout
 
    assert len(stdout.splitlines()) == 1
0 comments (0 inline, 0 general)