From 4671d69730ee7737dc559eb86353967862271c79 Mon Sep 17 00:00:00 2001 From: Bizordec Date: Fri, 15 Dec 2023 13:07:07 +0300 Subject: [PATCH] add test_incorrect_xml_syntax --- sphinxcontrib/datatemplates/directive.py | 2 +- tests/test_errors.py | 11 ++++++++++ .../test-incorrect-xml-syntax/conf.py | 2 ++ .../test-incorrect-xml-syntax/index.rst | 2 ++ .../test-incorrect-xml-syntax/sample.xml | 20 +++++++++++++++++++ .../templates/sample.tmpl | 1 + 6 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/testdata/test-incorrect-xml-syntax/conf.py create mode 100644 tests/testdata/test-incorrect-xml-syntax/index.rst create mode 100644 tests/testdata/test-incorrect-xml-syntax/sample.xml create mode 100644 tests/testdata/test-incorrect-xml-syntax/templates/sample.tmpl diff --git a/sphinxcontrib/datatemplates/directive.py b/sphinxcontrib/datatemplates/directive.py index 9a0bcea..0ff4610 100644 --- a/sphinxcontrib/datatemplates/directive.py +++ b/sphinxcontrib/datatemplates/directive.py @@ -208,7 +208,7 @@ def run(self): nodes.literal_block(self.block_text, self.block_text), line=self.lineno) return [error] - except json.decoder.JSONDecodeError as err: + except (json.decoder.JSONDecodeError, ET.ParseError) as err: error = self.state_machine.reporter.error( f"Error in source file '{relative_resolved_path}': {err}", nodes.literal_block(self.block_text, self.block_text), diff --git a/tests/test_errors.py b/tests/test_errors.py index 6f5df6f..932716e 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -91,3 +91,14 @@ def test_incorrect_yaml_syntax(app: SphinxTestApp, warning: StringIO): ' in "sample.yaml", line 12, column 3' ) assert expected_error_str in warning.getvalue() + + +@pytest.mark.sphinx("html", testroot="incorrect-xml-syntax") +def test_incorrect_xml_syntax(app: SphinxTestApp, warning: StringIO): + app.builder.build_all() + expected_error_str = ( + f"{app.srcdir / 'index.rst'}:1: " + "ERROR: Error in source file 'sample.xml': " + "not well-formed (invalid token): line 2, column 4" + ) + assert expected_error_str in warning.getvalue() diff --git a/tests/testdata/test-incorrect-xml-syntax/conf.py b/tests/testdata/test-incorrect-xml-syntax/conf.py new file mode 100644 index 0000000..8090b56 --- /dev/null +++ b/tests/testdata/test-incorrect-xml-syntax/conf.py @@ -0,0 +1,2 @@ +extensions = ["sphinxcontrib.datatemplates"] +templates_path = ["templates"] diff --git a/tests/testdata/test-incorrect-xml-syntax/index.rst b/tests/testdata/test-incorrect-xml-syntax/index.rst new file mode 100644 index 0000000..be626c6 --- /dev/null +++ b/tests/testdata/test-incorrect-xml-syntax/index.rst @@ -0,0 +1,2 @@ +.. datatemplate:xml:: sample.xml + :template: sample.tmpl diff --git a/tests/testdata/test-incorrect-xml-syntax/sample.xml b/tests/testdata/test-incorrect-xml-syntax/sample.xml new file mode 100644 index 0000000..2119e50 --- /dev/null +++ b/tests/testdata/test-incorrect-xml-syntax/sample.xml @@ -0,0 +1,20 @@ +value1 + + list item 1 + list item 2 + list item 3 + + + + a + b + c + + + A + B + C + + + \ No newline at end of file diff --git a/tests/testdata/test-incorrect-xml-syntax/templates/sample.tmpl b/tests/testdata/test-incorrect-xml-syntax/templates/sample.tmpl new file mode 100644 index 0000000..4e6b8e2 --- /dev/null +++ b/tests/testdata/test-incorrect-xml-syntax/templates/sample.tmpl @@ -0,0 +1 @@ +{{data.find('key1').text}}