From 1723fcab0cb5cee628314d2b56d98764261b42af Mon Sep 17 00:00:00 2001 From: Bizordec Date: Fri, 15 Dec 2023 13:13:40 +0300 Subject: [PATCH] add test_incorrect_import_module --- sphinxcontrib/datatemplates/directive.py | 6 ++++++ tests/test_errors.py | 10 ++++++++++ tests/testdata/test-incorrect-import-module/conf.py | 2 ++ tests/testdata/test-incorrect-import-module/index.rst | 2 ++ .../test-incorrect-import-module/templates/sample.tmpl | 1 + 5 files changed, 21 insertions(+) create mode 100644 tests/testdata/test-incorrect-import-module/conf.py create mode 100644 tests/testdata/test-incorrect-import-module/index.rst create mode 100644 tests/testdata/test-incorrect-import-module/templates/sample.tmpl diff --git a/sphinxcontrib/datatemplates/directive.py b/sphinxcontrib/datatemplates/directive.py index 0ff4610..adfd700 100644 --- a/sphinxcontrib/datatemplates/directive.py +++ b/sphinxcontrib/datatemplates/directive.py @@ -208,6 +208,12 @@ def run(self): nodes.literal_block(self.block_text, self.block_text), line=self.lineno) return [error] + except ModuleNotFoundError: + error = self.state_machine.reporter.error( + f"Source module '{relative_resolved_path}' not found", + nodes.literal_block(self.block_text, self.block_text), + line=self.lineno) + return [error] except (json.decoder.JSONDecodeError, ET.ParseError) as err: error = self.state_machine.reporter.error( f"Error in source file '{relative_resolved_path}': {err}", diff --git a/tests/test_errors.py b/tests/test_errors.py index 932716e..7d47e4c 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -102,3 +102,13 @@ def test_incorrect_xml_syntax(app: SphinxTestApp, warning: StringIO): "not well-formed (invalid token): line 2, column 4" ) assert expected_error_str in warning.getvalue() + + +@pytest.mark.sphinx("html", testroot="incorrect-import-module") +def test_incorrect_import_module(app: SphinxTestApp, warning: StringIO): + app.builder.build_all() + expected_error_str = ( + f"{app.srcdir / 'index.rst'}:1: " + "ERROR: Source module 'some_module' not found" + ) + assert expected_error_str in warning.getvalue() diff --git a/tests/testdata/test-incorrect-import-module/conf.py b/tests/testdata/test-incorrect-import-module/conf.py new file mode 100644 index 0000000..8090b56 --- /dev/null +++ b/tests/testdata/test-incorrect-import-module/conf.py @@ -0,0 +1,2 @@ +extensions = ["sphinxcontrib.datatemplates"] +templates_path = ["templates"] diff --git a/tests/testdata/test-incorrect-import-module/index.rst b/tests/testdata/test-incorrect-import-module/index.rst new file mode 100644 index 0000000..21afea5 --- /dev/null +++ b/tests/testdata/test-incorrect-import-module/index.rst @@ -0,0 +1,2 @@ +.. datatemplate:import-module:: some_module + :template: sample.tmpl diff --git a/tests/testdata/test-incorrect-import-module/templates/sample.tmpl b/tests/testdata/test-incorrect-import-module/templates/sample.tmpl new file mode 100644 index 0000000..367d1bb --- /dev/null +++ b/tests/testdata/test-incorrect-import-module/templates/sample.tmpl @@ -0,0 +1 @@ +{{data}}