Skip to content

Commit 872fd89

Browse files
committed
back to 100% coverage
1 parent b768aaa commit 872fd89

File tree

6 files changed

+25
-21
lines changed

6 files changed

+25
-21
lines changed

tests/cli/test_main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,18 @@ def test_files_but_config_file_missing(self):
194194
self.assertIn("Error: file not found: pippo.py", result.output)
195195
self.assertEqual(1, result.exit_code)
196196

197-
def test_files_with_format_option(self):
197+
def test_files_with_format_json(self):
198198
with text_file(DEFAULT_CONFIG_FILE_YAML, self.ok_config_yaml):
199199
result = self.xrlint("-f", "json", *self.files)
200200
self.assertIn('"results": [\n', result.output)
201201
self.assertEqual(0, result.exit_code)
202202

203+
def test_files_with_format_html(self):
204+
with text_file(DEFAULT_CONFIG_FILE_YAML, self.ok_config_yaml):
205+
result = self.xrlint("-f", "html", *self.files)
206+
self.assertIn("<h3>Results</h3>", result.output)
207+
self.assertEqual(0, result.exit_code)
208+
203209
def test_file_does_not_match(self):
204210
with text_file(DEFAULT_CONFIG_FILE_YAML, no_match_config_yaml):
205211
result = self.xrlint("test.zarr")

tests/formatters/test_html.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest import TestCase
22

3-
from xrlint.formatters.html import Html
3+
from xrlint.formatters.html import Html, HtmlText
44

55
from .helpers import get_context, get_test_results
66

@@ -13,7 +13,8 @@ def test_html(self):
1313
context=get_context(),
1414
results=results,
1515
)
16-
self.assertIsInstance(text, str)
16+
self.assertIsInstance(text, HtmlText)
17+
self.assertIs(text, text._repr_html_())
1718
self.assertIn("</p>", text)
1819

1920
def test_html_with_meta(self):
@@ -23,5 +24,6 @@ def test_html_with_meta(self):
2324
context=get_context(),
2425
results=results,
2526
)
26-
self.assertIsInstance(text, str)
27+
self.assertIsInstance(text, HtmlText)
28+
self.assertIs(text, text._repr_html_())
2729
self.assertIn("</p>", text)

xrlint/cli/engine.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22
import os
33
from collections.abc import Iterable, Iterator
4-
from types import MethodType
54

65
import click
76
import fsspec
@@ -244,8 +243,7 @@ def format_results(self, results: Iterable[Result]) -> str:
244243
formatter_kwargs = {}
245244
# noinspection PyArgumentList
246245
formatter_op = formatter.op_class(**formatter_kwargs)
247-
text = formatter_op.format(self, self._result_stats.collect(results))
248-
return _bind_repr_html(text) if output_format == "html" else text
246+
return formatter_op.format(self, self._result_stats.collect(results))
249247

250248
def write_report(self, report: str) -> None:
251249
"""Write the validation report provided as plain text."""
@@ -267,14 +265,3 @@ def init_config_file(cls) -> None:
267265
with open(file_path, "w") as f:
268266
f.write(INIT_CONFIG_YAML)
269267
click.echo(f"Configuration template written to {file_path}")
270-
271-
272-
def _bind_repr_html(text: str) -> str:
273-
"""Allow displaying `text` as HTML in Jupyter notebooks."""
274-
text._repr_html_ = MethodType(_repr_html_, text)
275-
return text
276-
277-
278-
def _repr_html_(self: str) -> str:
279-
"""Method to be bound to `self` for texts that represent HTML."""
280-
return self

xrlint/formatters/html.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def format(
5757
)
5858
lines.append("</div>")
5959

60-
return "\n".join(lines)
60+
return HtmlText("\n".join(lines))
6161

6262

6363
def format_result(result: Result) -> list[str]:
@@ -130,3 +130,11 @@ def _format_result_data(data: list[list[str]]) -> list[str]:
130130
lines.append(" </tr>")
131131
lines.append("</table>")
132132
return lines
133+
134+
135+
class HtmlText(str):
136+
"""Allow displaying `text` as HTML in Jupyter notebooks."""
137+
138+
def _repr_html_(self: str) -> str:
139+
"""Represent HTML text as HTML."""
140+
return self

xrlint/formatters/simple.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ def _format_rule_id(self, m: Message, r: Result) -> str:
107107
rule_text = m.rule_id or ""
108108
if self.styled and rule_text:
109109
rule_url = r.get_docs_url_for_rule(m.rule_id)
110-
rule_text = format_styled(rule_text, fg="blue", href=rule_url)
110+
if rule_url:
111+
rule_text = format_styled(rule_text, fg="blue", href=rule_url)
111112
return rule_text
112113

113114
def _format_summary(self, error_count, warning_count) -> str:

xrlint/result.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def get_docs_url_for_rule(self, rule_id: str) -> str | None:
156156
return f"{CORE_DOCS_URL}#{rule_name}"
157157
try:
158158
plugin = self.config_object.get_plugin(plugin_name)
159-
rule = self.config_object.get_rule(rule_name)
159+
rule = self.config_object.get_rule(rule_id)
160160
return rule.meta.docs_url or plugin.meta.docs_url
161161
except ValueError:
162162
return None

0 commit comments

Comments
 (0)