refactor handling of missing config files for better testing

Rather than testing that we log a message, place the handling in its
own method and verify that we call that. This allows us to change the
logging in the configuration class without counting messages and
updating the test.

Change-Id: Ic3067d8ab6699ceb82db7dd64f892a757f5cc12f
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2018-10-10 12:46:55 -04:00
parent caadd1d11c
commit 1fa03dc495
2 changed files with 15 additions and 4 deletions

View File

@ -218,7 +218,7 @@ class Config(object):
if os.path.isfile(filename):
break
else:
LOG.info('no configuration file in: %s', ', '.join(filenames))
self._report_missing_config_files(filenames)
return
try:
@ -226,10 +226,20 @@ class Config(object):
self._contents = yaml.safe_load(fd)
LOG.info('loaded configuration file %s', filename)
except IOError as err:
LOG.warning('did not load config file %s: %s', filename, err)
self._report_failure_config_file(filename, err)
else:
self.override(**self._contents)
def _report_missing_config_files(self, filenames):
# NOTE(dhellmann): This is extracted so we can mock it for
# testing.
LOG.info('no configuration file in: %s', ', '.join(filenames))
def _report_failure_config_file(self, filename, err):
# NOTE(dhellmann): This is extracted so we can mock it for
# testing.
LOG.warning('did not load config file %s: %s', filename, err)
def _rename_prelude_section(self, **kwargs):
key = 'prelude_section_name'
if key in kwargs and kwargs[key] != self._OPTS[key].default:

View File

@ -73,9 +73,10 @@ collapse_pre_releases: false
self.assertEqual(expected, actual)
def test_load_file_not_present(self):
with mock.patch.object(config.LOG, 'info') as logger:
missing = 'reno.config.Config._report_missing_config_files'
with mock.patch(missing) as error_handler:
config.Config(self.tempdir.path)
self.assertEqual(1, logger.call_count)
self.assertEqual(1, error_handler.call_count)
def _test_load_file(self, config_path):
with open(config_path, 'w') as fd: