Integration tests for argument parsing.

This commit is contained in:
Dan Helfman 2014-12-20 11:37:25 -08:00
parent 2a45f43167
commit de40570db4
4 changed files with 54 additions and 5 deletions

5
NEWS
View file

@ -1,3 +1,8 @@
default
* Integration tests for argument parsing.
* Documentation updates about repository encryption.
0.0.2 0.0.2
* Configuration support for additional attic prune flags: keep_within, keep_hourly, keep_yearly, * Configuration support for additional attic prune flags: keep_within, keep_hourly, keep_yearly,

View file

@ -7,7 +7,11 @@ from atticmatic.attic import create_archive, prune_archives
from atticmatic.config import parse_configuration 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. 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( parser.add_argument(
'-c', '--config', '-c', '--config',
dest='config_filename', dest='config_filename',
default='/etc/atticmatic/config', default=DEFAULT_CONFIG_FILENAME,
help='Configuration filename', help='Configuration filename',
) )
parser.add_argument( parser.add_argument(
'--excludes', '--excludes',
dest='excludes_filename', dest='excludes_filename',
default='/etc/atticmatic/excludes', default=DEFAULT_EXCLUDES_FILENAME,
help='Excludes filename', help='Excludes filename',
) )
parser.add_argument( parser.add_argument(
@ -30,12 +34,12 @@ def parse_arguments():
help='Display verbose progress information', help='Display verbose progress information',
) )
return parser.parse_args() return parser.parse_args(arguments)
def main(): def main():
try: try:
args = parse_arguments() args = parse_arguments(*sys.argv[1:])
location_config, retention_config = parse_configuration(args.config_filename) location_config, retention_config = parse_configuration(args.config_filename)
create_archive(args.excludes_filename, args.verbose, **location_config) create_archive(args.excludes_filename, args.verbose, **location_config)

View file

View file

@ -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