Changeset - e82ee8e024f4
[Not reviewed]
0 3 0
Branko Majic (branko) - 8 years ago 2018-02-26 23:31:37
branko@majic.rs
GC-11: Expanded help to include some more information on tool usage.
3 files changed with 31 insertions and 0 deletions:
0 comments (0 inline, 0 general)
functional_tests/test_help.py
Show inline comments
 
@@ -36,24 +36,47 @@ def test_cli_works():
 
    # same as package name, he runs it.
 
    process = subprocess.Popen(["gimmecert"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
    stdout, stderr = process.communicate()
 
    stdout, stderr = stdout.decode(), stderr.decode()
 

	
 
    # John has a look at output, and notices that no error has been
 
    # reported. He also verifies the return code is non-zero, just to
 
    # be on the safe side.
 
    assert stderr == ''
 
    assert process.returncode == 0
 

	
 

	
 
def test_usage_help_shown():
 
    # Since John feels a bit lazy, he decides to skip reading the
 
    # documentation, and just run the tool to see if he gets any
 
    # useful help.
 
    process = subprocess.Popen(["gimmecert"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
    stdout, stderr = process.communicate()
 
    stdout, stderr = stdout.decode(), stderr.decode()
 

	
 
    # John is presented with short usage instructions.
 
    assert "usage: gimmecert [-h]" in stdout
 
    assert stderr == ''
 
    assert process.returncode == 0
 

	
 

	
 
def test_extended_help_shown():
 
    # John is still not quite sure how the tool works. Therefore he
 
    # decides to try out the -h flag to the command.
 
    process = subprocess.Popen(["gimmecert", "-h"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
    stdout, stderr = process.communicate()
 
    stdout, stderr = stdout.decode(), stderr.decode()
 

	
 
    # 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 == ''
 
    assert process.returncode == 0
 
    assert "usage: gimmecert [-h]" in stdout
 
    assert "Examples:" in stdout
 
    assert "optional arguments" in stdout
 
    # @TODO: Can't really test this without producing errors, but
 
    # possibly not needed.
 
    # assert "positional arguments" in stdout
 
    # @TODO: Can't test at the moment, should be added once the first
 
    # commands is implemented.
 
    # assert "command1|command2" in stdout
gimmecert/cli.py
Show inline comments
 
@@ -4,48 +4,50 @@
 
#
 
# 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/>.
 
#
 

	
 

	
 
import argparse
 

	
 

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

	
 
Examples:
 
"""
 

	
 

	
 
def get_parser():
 
    """
 
    Sets-up and returns a CLI argument parser.
 

	
 
    :returns: argparse.ArgumentParser -- argument parser for CLI.
 
    """
 

	
 
    parser = argparse.ArgumentParser(description=DESCRIPTION)
 

	
 
    parser.set_defaults(func=lambda args: parser.print_usage())
 

	
 
    return parser
 

	
 

	
 
def main():
 
    """
 
    This function is a CLI entry point for the tool. It is a thin
 
    wrapper around the argument parser, and underlying command
 
    implementation.
 

	
 
    In order for this to work, the parser needs to register the
tests/test_cli.py
Show inline comments
 
@@ -61,24 +61,30 @@ def test_parser_sets_up_default_callback_function():
 

	
 
    assert callable(parser.get_default('func'))
 

	
 

	
 
@mock.patch('gimmecert.cli.argparse.ArgumentParser.print_usage')
 
def test_parser_default_callback_function_calls_print_usage(mock_print_usage):
 
    parser = gimmecert.cli.get_parser()
 
    func = parser.get_default('func')
 
    func(mock.Mock())
 

	
 
    assert mock_print_usage.called
 

	
 

	
 
@mock.patch('gimmecert.cli.get_parser')
 
def test_main_invokes_parser_function(mock_get_parser):
 
    mock_parser = mock.Mock()
 
    mock_args = mock.Mock()
 

	
 
    mock_parser.parse_args.return_value = mock_args
 
    mock_get_parser.return_value = mock_parser
 

	
 
    gimmecert.cli.main()
 

	
 
    mock_args.func.assert_called_once_with(mock_args)
 

	
 

	
 
def test_parser_help_contains_examples():
 
    parser = gimmecert.cli.get_parser()
 

	
 
    assert 'Examples' in parser.description
0 comments (0 inline, 0 general)