From 9fdcb435d6ff5bb27806a02e37536c18c13fc11e Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Sat, 27 May 2023 21:26:44 +0200 Subject: [PATCH] Refactor plugin tests --- mkdocs/tests/base.py | 14 ++------------ mkdocs/tests/build_tests.py | 37 ++++++++++++++++++------------------ mkdocs/tests/plugin_tests.py | 10 +++++----- mkdocs/tests/search_tests.py | 4 ++-- 4 files changed, 28 insertions(+), 37 deletions(-) diff --git a/mkdocs/tests/base.py b/mkdocs/tests/base.py index 513e903d..28e3fd3c 100644 --- a/mkdocs/tests/base.py +++ b/mkdocs/tests/base.py @@ -34,6 +34,8 @@ def load_config(**cfg) -> MkDocsConfig: if 'docs_dir' not in cfg: # Point to an actual dir to avoid a 'does not exist' error on validation. cfg['docs_dir'] = os.path.join(path_base, 'docs') + if 'plugins' not in cfg: + cfg['plugins'] = [] conf = MkDocsConfig(config_file_path=cfg['config_file_path']) conf.load_dict(cfg) @@ -123,20 +125,8 @@ class PathAssertionMixin: msg = self._formatMessage(None, f"The path '{path}' is not a file that exists") raise self.failureException(msg) - def assertPathNotFile(self, *parts): - path = os.path.join(*parts) - if os.path.isfile(path): - msg = self._formatMessage(None, f"The path '{path}' is a file that exists") - raise self.failureException(msg) - def assertPathIsDir(self, *parts): path = os.path.join(*parts) if not os.path.isdir(path): msg = self._formatMessage(None, f"The path '{path}' is not a directory that exists") raise self.failureException(msg) - - def assertPathNotDir(self, *parts): - path = os.path.join(*parts) - if os.path.isfile(path): - msg = self._formatMessage(None, f"The path '{path}' is a directory that exists") - raise self.failureException(msg) diff --git a/mkdocs/tests/build_tests.py b/mkdocs/tests/build_tests.py index fa29bf6b..c86872e4 100644 --- a/mkdocs/tests/build_tests.py +++ b/mkdocs/tests/build_tests.py @@ -371,11 +371,13 @@ class BuildTests(PathAssertionMixin, unittest.TestCase): mock_open.assert_called_once() @tempdir(files={'index.md': 'page content'}) - @mock.patch( - 'mkdocs.plugins.PluginCollection.run_event', side_effect=PluginError('Error message.') - ) - def test_populate_page_read_plugin_error(self, docs_dir, mock_open): + def test_populate_page_read_plugin_error(self, docs_dir): + def on_page_markdown(*args, **kwargs): + raise PluginError('Error message.') + cfg = load_config(docs_dir=docs_dir) + cfg.plugins.events['page_markdown'].append(on_page_markdown) + file = File('index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']) page = Page('Foo', file, cfg) with self.assertLogs('mkdocs') as cm: @@ -385,13 +387,12 @@ class BuildTests(PathAssertionMixin, unittest.TestCase): '\n'.join(cm.output), "ERROR:mkdocs.commands.build:Error reading page 'index.md':", ) - mock_open.assert_called_once() # Test build._build_page @tempdir() def test_build_page(self, site_dir): - cfg = load_config(site_dir=site_dir, nav=['index.md'], plugins=[]) + cfg = load_config(site_dir=site_dir, nav=['index.md']) fs = [File('index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])] files = Files(fs) nav = get_navigation(files, cfg) @@ -406,7 +407,7 @@ class BuildTests(PathAssertionMixin, unittest.TestCase): @tempdir() @mock.patch('jinja2.environment.Template.render', return_value='') def test_build_page_empty(self, site_dir, render_mock): - cfg = load_config(site_dir=site_dir, nav=['index.md'], plugins=[]) + cfg = load_config(site_dir=site_dir, nav=['index.md']) fs = [File('index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])] files = Files(fs) nav = get_navigation(files, cfg) @@ -418,14 +419,14 @@ class BuildTests(PathAssertionMixin, unittest.TestCase): '\n'.join(cm.output), "INFO:mkdocs.commands.build:Page skipped: 'index.md'. Generated empty output.", ) - self.assertPathNotFile(site_dir, 'index.html') + self.assertPathNotExists(site_dir, 'index.html') render_mock.assert_called_once() @tempdir(files={'index.md': 'page content'}) @tempdir(files={'index.html': '

page content

'}) @mock.patch('mkdocs.utils.write_file') def test_build_page_dirty_modified(self, site_dir, docs_dir, mock_write_file): - cfg = load_config(docs_dir=docs_dir, site_dir=site_dir, nav=['index.md'], plugins=[]) + cfg = load_config(docs_dir=docs_dir, site_dir=site_dir, nav=['index.md']) fs = [File('index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])] files = Files(fs) nav = get_navigation(files, cfg) @@ -442,7 +443,7 @@ class BuildTests(PathAssertionMixin, unittest.TestCase): @tempdir(files={'testing.html': '

page content

'}) @mock.patch('mkdocs.utils.write_file') def test_build_page_dirty_not_modified(self, site_dir, mock_write_file): - cfg = load_config(site_dir=site_dir, nav=['testing.md'], plugins=[]) + cfg = load_config(site_dir=site_dir, nav=['testing.md']) fs = [File('testing.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])] files = Files(fs) nav = get_navigation(files, cfg) @@ -458,7 +459,7 @@ class BuildTests(PathAssertionMixin, unittest.TestCase): @tempdir() def test_build_page_custom_template(self, site_dir): - cfg = load_config(site_dir=site_dir, nav=['index.md'], plugins=[]) + cfg = load_config(site_dir=site_dir, nav=['index.md']) fs = [File('index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])] files = Files(fs) nav = get_navigation(files, cfg) @@ -474,7 +475,7 @@ class BuildTests(PathAssertionMixin, unittest.TestCase): @tempdir() @mock.patch('mkdocs.utils.write_file', side_effect=OSError('Error message.')) def test_build_page_error(self, site_dir, mock_write_file): - cfg = load_config(site_dir=site_dir, nav=['index.md'], plugins=[]) + cfg = load_config(site_dir=site_dir, nav=['index.md']) fs = [File('index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])] files = Files(fs) nav = get_navigation(files, cfg) @@ -493,11 +494,12 @@ class BuildTests(PathAssertionMixin, unittest.TestCase): mock_write_file.assert_called_once() @tempdir() - @mock.patch( - 'mkdocs.plugins.PluginCollection.run_event', side_effect=PluginError('Error message.') - ) - def test_build_page_plugin_error(self, site_dir, mock_write_file): - cfg = load_config(site_dir=site_dir, nav=['index.md'], plugins=[]) + def test_build_page_plugin_error(self, site_dir): + def on_page_context(*args, **kwargs): + raise PluginError('Error message.') + + cfg = load_config(site_dir=site_dir, nav=['index.md']) + cfg.plugins.events['page_context'].append(on_page_context) fs = [File('index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])] files = Files(fs) nav = get_navigation(files, cfg) @@ -513,7 +515,6 @@ class BuildTests(PathAssertionMixin, unittest.TestCase): '\n'.join(cm.output), "ERROR:mkdocs.commands.build:Error building page 'index.md':", ) - mock_write_file.assert_called_once() # Test build.build diff --git a/mkdocs/tests/plugin_tests.py b/mkdocs/tests/plugin_tests.py index 64f84741..316bd5c7 100644 --- a/mkdocs/tests/plugin_tests.py +++ b/mkdocs/tests/plugin_tests.py @@ -265,27 +265,27 @@ class TestPluginCollection(unittest.TestCase): build_errors.append(error) cfg = load_config(site_dir=site_dir) - cfg['plugins']['errorplugin'] = PluginRaisingError(error_on='pre_page') + cfg.plugins['errorplugin'] = PluginRaisingError(error_on='pre_page') with self.assertLogs('mkdocs', level='ERROR'): self.assertRaises(Abort, build.build, cfg) cfg = load_config(site_dir=site_dir) - cfg['plugins']['errorplugin'] = PluginRaisingError(error_on='page_markdown') + cfg.plugins['errorplugin'] = PluginRaisingError(error_on='page_markdown') with self.assertLogs('mkdocs', level='ERROR'): self.assertRaises(Abort, build.build, cfg) cfg = load_config(site_dir=site_dir) - cfg['plugins']['errorplugin'] = PluginRaisingError(error_on='page_content') + cfg.plugins['errorplugin'] = PluginRaisingError(error_on='page_content') with self.assertLogs('mkdocs', level='ERROR'): self.assertRaises(Abort, build.build, cfg) cfg = load_config(site_dir=site_dir) - cfg['plugins']['errorplugin'] = PluginRaisingError(error_on='post_page') + cfg.plugins['errorplugin'] = PluginRaisingError(error_on='post_page') with self.assertLogs('mkdocs', level='ERROR'): self.assertRaises(ValueError, build.build, cfg) cfg = load_config(site_dir=site_dir) - cfg['plugins']['errorplugin'] = PluginRaisingError(error_on='') + cfg.plugins['errorplugin'] = PluginRaisingError(error_on='') build.build(cfg) self.assertEqual(len(build_errors), 4) diff --git a/mkdocs/tests/search_tests.py b/mkdocs/tests/search_tests.py index ada861ca..dd8acb24 100644 --- a/mkdocs/tests/search_tests.py +++ b/mkdocs/tests/search_tests.py @@ -476,8 +476,8 @@ class SearchIndexTests(unittest.TestCase): self.assertEqual(errors, []) self.assertEqual(warnings, []) - base_cfg = load_config() - base_cfg['plugins']['search'].config.indexing = option + base_cfg = load_config(plugins=['search']) + base_cfg.plugins['search'].config.indexing = option pages = [ test_page('Home', 'index.md', base_cfg),