Skip to content

Commit

Permalink
Test reporter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Mar 28, 2020
1 parent 626e5c5 commit d132cd2
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 22 deletions.
47 changes: 31 additions & 16 deletions myst_parser/docutils_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,21 @@ def render(self, tokens: List[Token], options, env: AttrDict):
if f"render_{token.type}" in self.rules:
self.rules[f"render_{token.type}"](token)
else:
# TODO make this reporter warning
print(f"no render method for: {token.type}")
self.current_node.append(
self.reporter.warning(
f"No render method for: {token.type}", line=token.map[0]
)
)

# TODO log warning for duplicate reference definitions
# log warnings for duplicate reference definitions
# "duplicate_refs": [{"href": "ijk", "label": "B", "map": [4, 5], "title": ""}],
for dup_ref in self.env.get("duplicate_refs", []):
self.document.append(
self.reporter.warning(
f"Duplicate reference definition: {dup_ref['label']}",
line=dup_ref["map"][0] + 1,
)
)

if not self.config.get("output_footnotes", True):
return self.document
Expand All @@ -119,6 +130,8 @@ def render(self, tokens: List[Token], options, env: AttrDict):
if refnode["refname"] not in foot_refs:
foot_refs[refnode["refname"]] = True

# TODO log warning for duplicate footnote definitions

if foot_refs:
self.current_node.append(nodes.transition())
for footref in foot_refs:
Expand Down Expand Up @@ -166,8 +179,11 @@ def nested_render_text(self, text: str, lineno: int, disable_front_matter=True):
if f"render_{token.type}" in self.rules:
self.rules[f"render_{token.type}"](token)
else:
# TODO make this reporter warning
print(f"no render method for: {token.type}")
self.current_node.append(
self.reporter.warning(
f"No render method for: {token.type}", line=token.map[0]
)
)

@contextmanager
def current_node_context(self, node, append: bool = False):
Expand All @@ -189,7 +205,7 @@ def render_children(self, token):
def add_line_and_source_path(self, node, token):
"""Copy the line number and document source path to the docutils node."""
try:
node.line = token.map[0] + 1
node.line = token.map[0]
except (AttributeError, TypeError):
pass
node.source = self.document["source"]
Expand Down Expand Up @@ -402,11 +418,10 @@ def render_link_open(self, token):
self.render_children(token)

def handle_cross_reference(self, token, destination):
# TODO use the docutils error reporting mechanisms, rather than raising
if not self.config.get("ignore_missing_refs", False):
raise NotImplementedError(
"reference not found in current document: {} (lines: {})".format(
destination, token.map
self.current_node.append(
self.reporter.warning(
f"Reference not found: {destination}", line=token.map[0]
)
)

Expand All @@ -426,7 +441,8 @@ def render_image(self, token):
img_node = nodes.image()
self.add_line_and_source_path(img_node, token)
img_node["uri"] = token.attrGet("src")
# TODO ideally we would render proper markup here
# TODO ideally we would render proper markup here,
# this probably requires an upstream change in sphinx
img_node["alt"] = self.renderInlineAsText(token.children)

self.current_node.append(img_node)
Expand All @@ -451,7 +467,7 @@ def render_front_matter(self, token):
data = yaml.safe_load(token.content)
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as error:
msg_node = self.reporter.error(
"Front matter block:\n" + str(error), line=token.map[0] + 1
"Front matter block:\n" + str(error), line=token.map[0]
)
msg_node += nodes.literal_block(token.content, token.content)
self.current_node += [msg_node]
Expand Down Expand Up @@ -606,7 +622,7 @@ def render_directive(self, token: Token):
) # type: (Directive, list)
if not directive_class:
error = self.reporter.error(
"Unknown directive type '{}'\n".format(name),
'Unknown directive type "{}".\n'.format(name),
# nodes.literal_block(content, content),
line=position,
)
Expand All @@ -619,7 +635,7 @@ def render_directive(self, token: Token):
)
except DirectiveParsingError as error:
error = self.reporter.error(
"Directive '{}':\n{}".format(name, error),
"Directive '{}': {}".format(name, error),
nodes.literal_block(content, content),
line=position,
)
Expand Down Expand Up @@ -669,7 +685,7 @@ def render_directive(self, token: Token):
result = [msg_node]
except MockingError as exc:
error = self.reporter.error(
"Directive '{}' cannot be mocked:\n{}: {}".format(
"Directive '{}' cannot be mocked: {}: {}".format(
name, exc.__class__.__name__, exc
),
nodes.literal_block(content, content),
Expand All @@ -691,7 +707,6 @@ def render_directive(self, token: Token):

def dict_to_docinfo(data):
"""Render a key/val pair as a docutils field node."""
# TODO this data could be used to support default option values for directives
docinfo = nodes.docinfo()

for key, value in data.items():
Expand Down
2 changes: 1 addition & 1 deletion myst_parser/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def run(self):
except Exception as error:
raise DirectiveError(
4,
'Directive "{}": error reading file: {}\n{error}.'.format(
'Directive "{}": error reading file: {}\n{}.'.format(
self.name, path, error
),
)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
],
keywords="markdown lexer parser development docutils sphinx",
python_requires=">=3.6",
install_requires=["markdown-it-py~=0.4"],
install_requires=["markdown-it-py~=0.4.1"],
extras_require={
"sphinx": ["pyyaml", "docutils>=0.15", "sphinx>=2,<3"],
"code_style": ["flake8<3.8.0,>=3.7.0", "black", "pre-commit==1.17.0"],
Expand Down
5 changes: 2 additions & 3 deletions tests/test_renderers/fixtures/directive_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ foo
<document source="notset">
<system_message level="3" line="1" source="notset" type="ERROR">
<paragraph>
Directive 'restructuredtext-test-directive':
Invalid options YAML: mapping values are not allowed here
Directive 'restructuredtext-test-directive': Invalid options YAML: mapping values are not allowed here
in "<unicode string>", line 2, column 8:
option2: b
^
Expand All @@ -163,7 +162,7 @@ Unknown Directive:
<document source="notset">
<system_message level="3" line="1" source="notset" type="ERROR">
<paragraph>
Unknown directive type 'unknown'
Unknown directive type "unknown".
<system_message level="1" line="1" source="notset" type="INFO">
<paragraph>
Problem retrieving directive entry from language module 'en': 'str' object has no attribute 'directives'.
Expand Down
65 changes: 65 additions & 0 deletions tests/test_renderers/fixtures/reporter_warnings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
Duplicate Reference definitions:
.
[a]: b
[a]: c
.
source/path:2: (WARNING/2) Duplicate reference definition: A
.

Missing Reference:
.
[a](b)
.
source/path:1: (WARNING/2) Reference not found: b
.

Unknown role:
.
abc

{xyz}`a`
.
source/path:3: (ERROR/3) Unknown interpreted text role "xyz".
.

Unknown directive:
.

```{xyz}
```
.
source/path:2: (ERROR/3) Unknown directive type "xyz".
.

Bad Front Matter:
.
---
a: {
---
.
source/path:1: (ERROR/3) Front matter block:
while parsing a flow node
expected the node content, but found '<stream end>'
in "<unicode string>", line 1, column 5:
a: {
^
.

Directive parsing error:
.

```{class}
```
.
source/path:2: (ERROR/3) Directive 'class': 1 argument(s) required, 0 supplied
.

Directive run error:
.

```{date}
x
```
.
source/path:2: (ERROR/3) Invalid context: the "date" directive can only be used within a substitution definition.
.
2 changes: 1 addition & 1 deletion tests/test_renderers/fixtures/syntax_elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ a: {
---
.
<document source="notset">
<system_message level="3" line="2" source="notset" type="ERROR">
<system_message level="3" line="1" source="notset" type="ERROR">
<paragraph>
Front matter block:
while parsing a flow node
Expand Down
27 changes: 27 additions & 0 deletions tests/test_renderers/test_error_reporting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from pathlib import Path

import pytest

from markdown_it.utils import read_fixture_file
from myst_parser.docutils_renderer import make_document
from myst_parser.main import to_docutils


FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures")


@pytest.mark.parametrize(
"line,title,input,expected",
read_fixture_file(FIXTURE_PATH.joinpath("reporter_warnings.md")),
)
def test_basic(line, title, input, expected):
document = make_document("source/path")
messages = []

def observer(msg_node):
if msg_node["level"] > 1:
messages.append(msg_node.astext())

document.reporter.attach_observer(observer)
to_docutils(input, document=document, renderer="docutils")
assert "\n".join(messages).rstrip() == expected.rstrip()

0 comments on commit d132cd2

Please sign in to comment.