From de40570db44d56b70c53f975a05880a5c92bb1ca Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sat, 20 Dec 2014 11:37:25 -0800 Subject: [PATCH] Integration tests for argument parsing. --- NEWS | 5 +++ atticmatic/command.py | 14 ++++--- atticmatic/tests/integration/__init__.py | 0 atticmatic/tests/integration/test_command.py | 40 ++++++++++++++++++++ 4 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 atticmatic/tests/integration/__init__.py create mode 100644 atticmatic/tests/integration/test_command.py diff --git a/NEWS b/NEWS index e9e6290..17bf8d3 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,8 @@ +default + + * Integration tests for argument parsing. + * Documentation updates about repository encryption. + 0.0.2 * Configuration support for additional attic prune flags: keep_within, keep_hourly, keep_yearly, diff --git a/atticmatic/command.py b/atticmatic/command.py index d47f63a..4623c47 100644 --- a/atticmatic/command.py +++ b/atticmatic/command.py @@ -7,7 +7,11 @@ from atticmatic.attic import create_archive, prune_archives from atticmatic.config import parse_configuration -def parse_arguments(): +DEFAULT_CONFIG_FILENAME = '/etc/atticmatic/config' +DEFAULT_EXCLUDES_FILENAME = '/etc/atticmatic/excludes' + + +def parse_arguments(*arguments): ''' Parse the command-line arguments from sys.argv and return them as an ArgumentParser instance. ''' @@ -15,13 +19,13 @@ def parse_arguments(): parser.add_argument( '-c', '--config', dest='config_filename', - default='/etc/atticmatic/config', + default=DEFAULT_CONFIG_FILENAME, help='Configuration filename', ) parser.add_argument( '--excludes', dest='excludes_filename', - default='/etc/atticmatic/excludes', + default=DEFAULT_EXCLUDES_FILENAME, help='Excludes filename', ) parser.add_argument( @@ -30,12 +34,12 @@ def parse_arguments(): help='Display verbose progress information', ) - return parser.parse_args() + return parser.parse_args(arguments) def main(): try: - args = parse_arguments() + args = parse_arguments(*sys.argv[1:]) location_config, retention_config = parse_configuration(args.config_filename) create_archive(args.excludes_filename, args.verbose, **location_config) diff --git a/atticmatic/tests/integration/__init__.py b/atticmatic/tests/integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/atticmatic/tests/integration/test_command.py b/atticmatic/tests/integration/test_command.py new file mode 100644 index 0000000..3a5e794 --- /dev/null +++ b/atticmatic/tests/integration/test_command.py @@ -0,0 +1,40 @@ +import sys + +from nose.tools import assert_raises + +from atticmatic import command as module + + +def test_parse_arguments_with_no_arguments_uses_defaults(): + parser = module.parse_arguments() + + assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME + assert parser.excludes_filename == module.DEFAULT_EXCLUDES_FILENAME + assert parser.verbose == False + + +def test_parse_arguments_with_filename_arguments_overrides_defaults(): + parser = module.parse_arguments('--config', 'myconfig', '--excludes', 'myexcludes') + + assert parser.config_filename == 'myconfig' + assert parser.excludes_filename == 'myexcludes' + assert parser.verbose == False + + +def test_parse_arguments_with_verbose_flag_overrides_default(): + parser = module.parse_arguments('--verbose') + + assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME + assert parser.excludes_filename == module.DEFAULT_EXCLUDES_FILENAME + assert parser.verbose == True + + +def test_parse_arguments_with_invalid_arguments_exits(): + original_stderr = sys.stderr + sys.stderr = sys.stdout + + try: + with assert_raises(SystemExit): + module.parse_arguments('--posix-me-harder') + finally: + sys.stderr = original_stderr