From 1fa03dc4951fe02c872e4ff6fd3ca5fb6720a177 Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Wed, 10 Oct 2018 12:46:55 -0400 Subject: [PATCH] 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 --- reno/config.py | 14 ++++++++++++-- reno/tests/test_config.py | 5 +++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/reno/config.py b/reno/config.py index 03ae26b..3aa6583 100644 --- a/reno/config.py +++ b/reno/config.py @@ -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: diff --git a/reno/tests/test_config.py b/reno/tests/test_config.py index 39cf94e..6251aa7 100644 --- a/reno/tests/test_config.py +++ b/reno/tests/test_config.py @@ -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: