Skip to content

Commit 1cbf5ed

Browse files
authored
Merge pull request #313 from python-jsonschema/verbose-output-checked-files
Show checked filenames with `--verbose`
2 parents a7d90d0 + 04c046b commit 1cbf5ed

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

src/check_jsonschema/checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def _build_result(self) -> CheckResult:
7171
validator = self.get_validator(path, data)
7272
for err in validator.iter_errors(data):
7373
result.record_validation_error(path, err)
74+
else:
75+
result.record_validation_success(path)
7476
return result
7577

7678
def _run(self) -> None:

src/check_jsonschema/reporter.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, *, verbosity: int, **kwargs: t.Any) -> None:
2424
super().__init__(**kwargs)
2525

2626
@abc.abstractmethod
27-
def report_success(self) -> None:
27+
def report_success(self, result: CheckResult) -> None:
2828
raise NotImplementedError
2929

3030
@abc.abstractmethod
@@ -33,7 +33,7 @@ def report_errors(self, result: CheckResult) -> None:
3333

3434
def report_result(self, result: CheckResult) -> None:
3535
if result.success:
36-
self.report_success()
36+
self.report_success(result)
3737
else:
3838
self.report_errors(result)
3939

@@ -51,11 +51,15 @@ def __init__(
5151
def _echo(self, s: str, *, indent: int = 0) -> None:
5252
click.echo(" " * indent + s, file=self.stream)
5353

54-
def report_success(self) -> None:
54+
def report_success(self, result: CheckResult) -> None:
5555
if self.verbosity < 1:
5656
return
5757
ok = click.style("ok", fg="green")
5858
self._echo(f"{ok} -- validation done")
59+
if self.verbosity > 1:
60+
self._echo("The following files were checked:")
61+
for filename in result.successes:
62+
self._echo(f" {filename}")
5963

6064
def _format_validation_error_message(
6165
self, err: jsonschema.ValidationError, filename: str | None = None
@@ -140,10 +144,12 @@ def _dump(self, data: t.Any) -> None:
140144
else:
141145
click.echo(json.dumps(data, separators=(",", ":")))
142146

143-
def report_success(self) -> None:
147+
def report_success(self, result: CheckResult) -> None:
144148
report_obj: dict[str, t.Any] = {"status": "ok"}
145149
if self.verbosity > 0:
146150
report_obj["errors"] = []
151+
if self.verbosity > 1:
152+
report_obj["checked_paths"] = list(result.successes)
147153
self._dump(report_obj)
148154

149155
def _dump_error_map(
@@ -192,6 +198,8 @@ def _dump_parse_errors(
192198

193199
def report_errors(self, result: CheckResult) -> None:
194200
report_obj: dict[str, t.Any] = {"status": "fail"}
201+
if self.verbosity > 1:
202+
report_obj["checked_paths"] = list(result.successes)
195203
if self.verbosity > 0:
196204
report_obj["errors"] = list(self._dump_error_map(result.validation_errors))
197205
report_obj["parse_errors"] = list(

src/check_jsonschema/result.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ class CheckResult:
99
def __init__(self) -> None:
1010
self.validation_errors: dict[str, list[jsonschema.ValidationError]] = {}
1111
self.parse_errors: dict[str, list[ParseError]] = {}
12+
self.successes: list[str] = []
1213

1314
@property
1415
def success(self) -> bool:
1516
return not (bool(self.parse_errors) or bool(self.validation_errors))
1617

18+
def record_validation_success(self, path: pathlib.Path) -> None:
19+
self.successes.append(str(path))
20+
1721
def record_validation_error(
1822
self, path: pathlib.Path, err: jsonschema.ValidationError
1923
) -> None:

tests/unit/test_reporters.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import textwrap
23

34
import pytest
45
from jsonschema import Draft7Validator
@@ -8,7 +9,9 @@
89

910

1011
def _make_success_result():
11-
return CheckResult()
12+
res = CheckResult()
13+
res.successes.append("foo.json")
14+
return res
1215

1316

1417
@pytest.mark.parametrize("verbosity", (0, 1, 2))
@@ -18,29 +21,41 @@ def test_text_format_success(capsys, verbosity, use_report_result_path):
1821
if use_report_result_path:
1922
reporter.report_result(_make_success_result())
2023
else:
21-
reporter.report_success()
24+
reporter.report_success(_make_success_result())
2225
captured = capsys.readouterr()
2326
assert captured.err == ""
2427
if verbosity == 0:
2528
assert captured.out == ""
26-
else:
29+
elif verbosity == 1:
2730
assert captured.out == "ok -- validation done\n"
31+
else:
32+
assert captured.out == textwrap.dedent(
33+
"""\
34+
ok -- validation done
35+
The following files were checked:
36+
foo.json
37+
"""
38+
)
2839

2940

30-
@pytest.mark.parametrize("verbosity", (0, 1))
41+
@pytest.mark.parametrize("verbosity", (0, 1, 2))
3142
@pytest.mark.parametrize("use_report_result_path", (False, True))
3243
def test_json_format_success(capsys, verbosity, use_report_result_path):
3344
reporter = JsonReporter(verbosity=verbosity, pretty=False)
3445
if use_report_result_path:
3546
reporter.report_result(_make_success_result())
3647
else:
37-
reporter.report_success()
48+
reporter.report_success(_make_success_result())
3849
captured = capsys.readouterr()
3950
assert captured.err == ""
4051
if verbosity == 0:
4152
assert captured.out == '{"status":"ok"}\n'
42-
else:
53+
elif verbosity == 1:
4354
assert captured.out == '{"status":"ok","errors":[]}\n'
55+
else:
56+
assert (
57+
captured.out == '{"status":"ok","errors":[],"checked_paths":["foo.json"]}\n'
58+
)
4459

4560

4661
def test_text_format_validation_error_message_simple():

0 commit comments

Comments
 (0)