From e50fd04750cdc5175910b31b5da83713175cd00e Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sat, 8 Jul 2017 23:01:41 -0700 Subject: [PATCH] Adding test coverage report. Making tests a little less brittle. --- borgmatic/config/validate.py | 5 +++-- .../tests/integration/config/test_validate.py | 17 ++++++++--------- tox.ini | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/borgmatic/config/validate.py b/borgmatic/config/validate.py index 5dae6f383..82b4f93b9 100644 --- a/borgmatic/config/validate.py +++ b/borgmatic/config/validate.py @@ -39,6 +39,7 @@ def parse_configuration(config_filename, schema_filename): have permissions to read the file, or Validation_error if the config does not match the schema. ''' try: + config = yaml.round_trip_load(open(config_filename)) schema = yaml.round_trip_load(open(schema_filename)) except yaml.error.YAMLError as error: raise Validation_error(config_filename, (str(error),)) @@ -49,7 +50,7 @@ def parse_configuration(config_filename, schema_filename): for field_name, field_schema in section_schema['map'].items(): field_schema.pop('example') - validator = pykwalify.core.Core(source_file=config_filename, schema_data=schema) + validator = pykwalify.core.Core(source_data=config, schema_data=schema) parsed_result = validator.validate(raise_exception=False) if validator.validation_errors: @@ -58,7 +59,7 @@ def parse_configuration(config_filename, schema_filename): return parsed_result -def display_validation_error(validation_error): +def display_validation_error(validation_error): # pragma: no cover ''' Given a Validation_error, display its error messages to stderr. ''' diff --git a/borgmatic/tests/integration/config/test_validate.py b/borgmatic/tests/integration/config/test_validate.py index 4769551a2..897ea410a 100644 --- a/borgmatic/tests/integration/config/test_validate.py +++ b/borgmatic/tests/integration/config/test_validate.py @@ -17,15 +17,14 @@ def test_schema_filename_returns_plausable_path(): def mock_config_and_schema(config_yaml): ''' - Set up mocks for the config config YAML string and the default schema so that pykwalify consumes - them when parsing the configuration. This is a little brittle in that it's relying on the code - under test to open() the respective files in a particular order. + Set up mocks for the config config YAML string and the default schema so that the code under + test consumes them when parsing the configuration. ''' - schema_stream = open(module.schema_filename()) config_stream = io.StringIO(config_yaml) - builtins = flexmock(sys.modules['builtins']).should_call('open').mock - builtins.should_receive('open').and_return(schema_stream).and_return(config_stream) - flexmock(os.path).should_receive('exists').and_return(True) + schema_stream = open(module.schema_filename()) + builtins = flexmock(sys.modules['builtins']) + builtins.should_receive('open').with_args('config.yaml').and_return(config_stream) + builtins.should_receive('open').with_args('schema.yaml').and_return(schema_stream) def test_parse_configuration_transforms_file_into_mapping(): @@ -95,9 +94,9 @@ def test_parse_configuration_raises_for_missing_schema_file(): def test_parse_configuration_raises_for_syntax_error(): - mock_config_and_schema('invalid = yaml') + mock_config_and_schema('foo:\nbar') - with pytest.raises(module.Validation_error): + with pytest.raises(ValueError): module.parse_configuration('config.yaml', 'schema.yaml') diff --git a/tox.ini b/tox.ini index 7f6a37540..159847d76 100644 --- a/tox.ini +++ b/tox.ini @@ -5,4 +5,4 @@ skipsdist=True [testenv] usedevelop=True deps=-rtest_requirements.txt -commands = py.test --cov=borgmatic borgmatic [] +commands = py.test --cov-report term-missing:skip-covered --cov=borgmatic borgmatic []