Skip to content

Commit

Permalink
Make branch coverage accessible via the API
Browse files Browse the repository at this point in the history
  • Loading branch information
eleanorjboyd committed Nov 20, 2024
1 parent 3ed5915 commit defc4ca
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions coverage/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ def analysis2(
coverage data.
"""
analysis = self._analyze(morf)
analysis = self.analyze(morf)
return (
analysis.filename,
sorted(analysis.statements),
Expand All @@ -935,7 +935,7 @@ def analysis2(
analysis.missing_formatted(),
)

def _analyze(self, morf: TMorf) -> Analysis:
def analyze(self, morf: TMorf) -> Analysis:
"""Analyze a module or file. Private for now."""
self._init()
self._post_init()
Expand Down
2 changes: 1 addition & 1 deletion coverage/report_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_analysis_to_report(

for fr, morf in sorted(fr_morfs):
try:
analysis = coverage._analyze(morf)
analysis = coverage.analyze(morf)
except NotPython:
# Only report errors for .py files, and only if we didn't
# explicitly suppress those errors.
Expand Down
1 change: 1 addition & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ only. :ref:`dbschema` explains more.
api_module
api_plugin
api_coveragedata
api_analysis
dbschema
10 changes: 10 additions & 0 deletions doc/api_analysis.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.. Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
.. For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
.. _api_analysis:

The Analysis class
------------------

.. autoclass:: coverage.results.Analysis
:members: branch_stats
4 changes: 2 additions & 2 deletions tests/coveragetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def check_coverage(
del sys.modules[modname]

# Get the analysis results, and check that they are right.
analysis = cov._analyze(mod)
analysis = cov.analyze(mod)
statements = sorted(analysis.statements)
if lines:
if isinstance(lines[0], int):
Expand Down Expand Up @@ -493,7 +493,7 @@ def get_missing_arc_description(self, cov: Coverage, start: TLineNo, end: TLineN
assert self.last_module_name is not None
filename = self.last_module_name + ".py"
fr = cov._get_file_reporter(filename)
arcs_executed = cov._analyze(filename).arcs_executed
arcs_executed = cov.analyze(filename).arcs_executed
return fr.missing_arc_description(start, end, arcs_executed)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ def fun2(x):
# Import the Python file, executing it.
self.start_import_stop(cov, "missing")

nums = cov._analyze("missing.py").numbers
nums = cov.analyze("missing.py").numbers
assert nums.n_files == 1
assert nums.n_statements == 7
assert nums.n_excluded == 1
Expand Down
2 changes: 1 addition & 1 deletion tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def test_plugin2_with_branch(self) -> None:
# The way plugin2 works, a file named foo_7.html will be claimed to
# have 7 lines in it. If render() was called with line number 4,
# then the plugin will claim that lines 4 and 5 were executed.
analysis = cov._analyze("foo_7.html")
analysis = cov.analyze("foo_7.html")
assert analysis.statements == {1, 2, 3, 4, 5, 6, 7}
# Plugins don't do branch coverage yet.
assert analysis.has_arcs is True
Expand Down

0 comments on commit defc4ca

Please sign in to comment.