Skip to content

Commit

Permalink
feat: JSON report now has an explicit format version indicator nedbat…
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jan 15, 2024
1 parent 2bbcb06 commit 576cb3e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ development at the same time, such as 4.5.x and 5.0.
Unreleased
----------

Nothing yet.
- Fix: the JSON report now includes an explicit format version number, closing
`issue 1732`_.

.. _issue 1732: https://github.com/nedbat/coveragepy/issues/1732


.. scriv-start-here
Expand Down
5 changes: 5 additions & 0 deletions coverage/jsonreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
from coverage.data import CoverageData


# "Version 1" had no format number at all.
# 2: add the meta.format field.
FORMAT_VERSION = 2

class JsonReporter:
"""A reporter for writing JSON coverage results."""

Expand All @@ -44,6 +48,7 @@ def report(self, morfs: Optional[Iterable[TMorf]], outfile: IO[str]) -> float:
coverage_data = self.coverage.get_data()
coverage_data.set_query_contexts(self.config.report_contexts)
self.report_data["meta"] = {
"format": FORMAT_VERSION,
"version": __version__,
"timestamp": datetime.datetime.now().isoformat(),
"branch_coverage": coverage_data.has_arcs(),
Expand Down
10 changes: 6 additions & 4 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import coverage
from coverage import Coverage
from coverage.jsonreport import FORMAT_VERSION

from tests.coveragetest import UsingModulesMixin, CoverageTest

Expand All @@ -26,7 +27,7 @@ def _assert_expected_json_report(
expected_result: Dict[str, Any],
) -> None:
"""
Helper for tests that handles the common ceremony so the tests can be clearly show the
Helper that handles common ceremonies so tests can clearly show the
consequences of setting various arguments.
"""
self.make_file("a.py", """\
Expand All @@ -49,13 +50,16 @@ def _assert_expected_json_report(
datetime.strptime(parsed_result['meta']['timestamp'], "%Y-%m-%dT%H:%M:%S.%f")
)
del (parsed_result['meta']['timestamp'])
expected_result["meta"].update({
"format": FORMAT_VERSION,
"version": coverage.__version__,
})
assert parsed_result == expected_result

def test_branch_coverage(self) -> None:
cov = coverage.Coverage(branch=True)
expected_result = {
'meta': {
"version": coverage.__version__,
"branch_coverage": True,
"show_contexts": False,
},
Expand Down Expand Up @@ -107,7 +111,6 @@ def test_simple_line_coverage(self) -> None:
cov = coverage.Coverage()
expected_result = {
'meta': {
"version": coverage.__version__,
"branch_coverage": False,
"show_contexts": False,
},
Expand Down Expand Up @@ -152,7 +155,6 @@ def run_context_test(self, relative_files: bool) -> None:
cov = coverage.Coverage(context="cool_test", config_file="config")
expected_result = {
'meta': {
"version": coverage.__version__,
"branch_coverage": False,
"show_contexts": True,
},
Expand Down

0 comments on commit 576cb3e

Please sign in to comment.