From 3f99dc6db2ce9ba3dd28dd4594f2a36c4b0f9f46 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sun, 26 Jul 2015 21:06:03 -0700 Subject: [PATCH] As a convenience to new users, allow a missing default excludes file. --- NEWS | 3 +- atticmatic/backends/shared.py | 8 ++--- atticmatic/command.py | 7 +++-- atticmatic/tests/integration/test_command.py | 29 +++++++++++++++++++ atticmatic/tests/unit/backends/test_shared.py | 17 ++++++++++- 5 files changed, 56 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 5f6243c..db51ca3 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,6 @@ -0.1.2-dev +0.1.2 + * As a convenience to new users, allow a missing default excludes file. * New issue tracker, linked from documentation. 0.1.1 diff --git a/atticmatic/backends/shared.py b/atticmatic/backends/shared.py index eb167b5..8abd064 100644 --- a/atticmatic/backends/shared.py +++ b/atticmatic/backends/shared.py @@ -14,10 +14,11 @@ from atticmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS def create_archive(excludes_filename, verbosity, source_directories, repository, command): ''' - Given an excludes filename, a vebosity flag, a space-separated list of source directories, a - local or remote repository path, and a command to run, create an attic archive. + Given an excludes filename (or None), a vebosity flag, a space-separated list of source + directories, a local or remote repository path, and a command to run, create an attic archive. ''' sources = tuple(source_directories.split(' ')) + exclude_flags = ('--exclude-from', excludes_filename) if excludes_filename else () verbosity_flags = { VERBOSITY_SOME: ('--stats',), VERBOSITY_LOTS: ('--verbose', '--stats'), @@ -25,13 +26,12 @@ def create_archive(excludes_filename, verbosity, source_directories, repository, full_command = ( command, 'create', - '--exclude-from', excludes_filename, '{repo}::{hostname}-{timestamp}'.format( repo=repository, hostname=platform.node(), timestamp=datetime.now().isoformat(), ), - ) + sources + verbosity_flags + ) + sources + exclude_flags + verbosity_flags subprocess.check_call(full_command) diff --git a/atticmatic/command.py b/atticmatic/command.py index 4578c1e..13e7b43 100644 --- a/atticmatic/command.py +++ b/atticmatic/command.py @@ -18,17 +18,20 @@ def parse_arguments(command_name, *arguments): parse the arguments and return them as an ArgumentParser instance. Use the command name to determine the default configuration and excludes paths. ''' + config_filename_default = DEFAULT_CONFIG_FILENAME_PATTERN.format(command_name) + excludes_filename_default = DEFAULT_EXCLUDES_FILENAME_PATTERN.format(command_name) + parser = ArgumentParser() parser.add_argument( '-c', '--config', dest='config_filename', - default=DEFAULT_CONFIG_FILENAME_PATTERN.format(command_name), + default=config_filename_default, help='Configuration filename', ) parser.add_argument( '--excludes', dest='excludes_filename', - default=DEFAULT_EXCLUDES_FILENAME_PATTERN.format(command_name), + default=excludes_filename_default if os.path.exists(excludes_filename_default) else None, help='Excludes filename', ) parser.add_argument( diff --git a/atticmatic/tests/integration/test_command.py b/atticmatic/tests/integration/test_command.py index fd72595..346612b 100644 --- a/atticmatic/tests/integration/test_command.py +++ b/atticmatic/tests/integration/test_command.py @@ -1,5 +1,7 @@ +import os import sys +from flexmock import flexmock from nose.tools import assert_raises from atticmatic import command as module @@ -9,6 +11,8 @@ COMMAND_NAME = 'foomatic' def test_parse_arguments_with_no_arguments_uses_defaults(): + flexmock(os.path).should_receive('exists').and_return(True) + parser = module.parse_arguments(COMMAND_NAME) assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME_PATTERN.format(COMMAND_NAME) @@ -17,6 +21,8 @@ def test_parse_arguments_with_no_arguments_uses_defaults(): def test_parse_arguments_with_filename_arguments_overrides_defaults(): + flexmock(os.path).should_receive('exists').and_return(True) + parser = module.parse_arguments(COMMAND_NAME, '--config', 'myconfig', '--excludes', 'myexcludes') assert parser.config_filename == 'myconfig' @@ -24,7 +30,29 @@ def test_parse_arguments_with_filename_arguments_overrides_defaults(): assert parser.verbosity == None +def test_parse_arguments_with_missing_default_excludes_file_sets_filename_to_none(): + flexmock(os.path).should_receive('exists').and_return(False) + + parser = module.parse_arguments(COMMAND_NAME) + + assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME_PATTERN.format(COMMAND_NAME) + assert parser.excludes_filename == None + assert parser.verbosity == None + + +def test_parse_arguments_with_missing_overridden_excludes_file_retains_filename(): + flexmock(os.path).should_receive('exists').and_return(False) + + parser = module.parse_arguments(COMMAND_NAME, '--excludes', 'myexcludes') + + assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME_PATTERN.format(COMMAND_NAME) + assert parser.excludes_filename == 'myexcludes' + assert parser.verbosity == None + + def test_parse_arguments_with_verbosity_flag_overrides_default(): + flexmock(os.path).should_receive('exists').and_return(True) + parser = module.parse_arguments(COMMAND_NAME, '--verbosity', '1') assert parser.config_filename == module.DEFAULT_CONFIG_FILENAME_PATTERN.format(COMMAND_NAME) @@ -33,6 +61,7 @@ def test_parse_arguments_with_verbosity_flag_overrides_default(): def test_parse_arguments_with_invalid_arguments_exits(): + flexmock(os.path).should_receive('exists').and_return(True) original_stderr = sys.stderr sys.stderr = sys.stdout diff --git a/atticmatic/tests/unit/backends/test_shared.py b/atticmatic/tests/unit/backends/test_shared.py index c779c36..8fe236f 100644 --- a/atticmatic/tests/unit/backends/test_shared.py +++ b/atticmatic/tests/unit/backends/test_shared.py @@ -29,7 +29,8 @@ def insert_datetime_mock(): ).mock -CREATE_COMMAND = ('attic', 'create', '--exclude-from', 'excludes', 'repo::host-now', 'foo', 'bar') +CREATE_COMMAND_WITHOUT_EXCLUDES = ('attic', 'create', 'repo::host-now', 'foo', 'bar') +CREATE_COMMAND = CREATE_COMMAND_WITHOUT_EXCLUDES + ('--exclude-from', 'excludes') def test_create_archive_should_call_attic_with_parameters(): @@ -46,6 +47,20 @@ def test_create_archive_should_call_attic_with_parameters(): ) +def test_create_archive_with_none_excludes_filename_should_call_attic_without_excludes(): + insert_subprocess_mock(CREATE_COMMAND_WITHOUT_EXCLUDES) + insert_platform_mock() + insert_datetime_mock() + + module.create_archive( + excludes_filename=None, + verbosity=None, + source_directories='foo bar', + repository='repo', + command='attic', + ) + + def test_create_archive_with_verbosity_some_should_call_attic_with_stats_parameter(): insert_subprocess_mock(CREATE_COMMAND + ('--stats',)) insert_platform_mock()