Skip to content

Commit

Permalink
fix context reporting for relative_files
Browse files Browse the repository at this point in the history
fix reporting of contexts when `relative_files = True`

fixes #900
  • Loading branch information
davidszotten authored and nedbat committed Dec 21, 2019
1 parent 7dc3772 commit ade1f2c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 40 deletions.
2 changes: 1 addition & 1 deletion coverage/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def data_for_file(self, fr, analysis):
arcs_executed = analysis.arcs_executed()

if self.config.show_contexts:
contexts_by_lineno = analysis.data.contexts_by_lineno(fr.filename)
contexts_by_lineno = analysis.data.contexts_by_lineno(analysis.filename)

lines = []

Expand Down
5 changes: 2 additions & 3 deletions coverage/jsonreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def report(self, morfs, outfile=None):
for file_reporter, analysis in get_analysis_to_report(self.coverage, morfs):
measured_files[file_reporter.relative_filename()] = self.report_one_file(
coverage_data,
file_reporter,
analysis
)

Expand Down Expand Up @@ -71,7 +70,7 @@ def report(self, morfs, outfile=None):

return self.total.n_statements and self.total.pc_covered

def report_one_file(self, coverage_data, file_reporter, analysis):
def report_one_file(self, coverage_data, analysis):
"""Extract the relevant report data for a single file"""
nums = analysis.numbers
self.total += nums
Expand All @@ -90,7 +89,7 @@ def report_one_file(self, coverage_data, file_reporter, analysis):
}
if self.config.json_show_contexts:
reported_file['contexts'] = analysis.data.contexts_by_lineno(
file_reporter.filename
analysis.filename,
)
if coverage_data.has_arcs():
reported_file['summary'].update({
Expand Down
17 changes: 17 additions & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,3 +1127,20 @@ def test_no_contexts_warns_no_contexts(self):
self.start_import_stop(cov, "two_tests")
with self.assert_warnings(cov, ["No contexts were measured"]):
cov.html_report()

def test_dynamic_contexts_relative_files(self):
self.make_file("two_tests.py", self.SOURCE)
self.make_file("config", "[run]\nrelative_files = True")
cov = coverage.Coverage(source=["."], config_file="config")
cov.set_option("run:dynamic_context", "test_function")
cov.set_option("html:show_contexts", True)
mod = self.start_import_stop(cov, "two_tests")
d = self.html_data_from_cov(cov, mod)
context_labels = [self.EMPTY, 'two_tests.test_one', 'two_tests.test_two']
expected_lines = [self.OUTER_LINES, self.TEST_ONE_LINES, self.TEST_TWO_LINES]
for label, expected in zip(context_labels, expected_lines):
actual = [
ld.number for ld in d.lines
if label == ld.contexts_label or label in (ld.contexts or ())
]
assert sorted(expected) == sorted(actual)
81 changes: 45 additions & 36 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,42 +103,51 @@ def test_simple_line_coverage(self):
self._assert_expected_json_report(cov, expected_result)

def test_context(self):
cov = coverage.Coverage(context="cool_test")
cov.config.json_show_contexts = True
expected_result = {
'meta': {
"version": coverage.__version__,
"branch_coverage": False,
"show_contexts": True,
},
'files': {
'a.py': {
'executed_lines': [1, 2],
'missing_lines': [3],
'excluded_lines': [],
"contexts": {
"1": [
"cool_test"
],
"2": [
"cool_test"
]
},
'summary': {
'excluded_lines': 0,
'missing_lines': 1,
'covered_lines': 2,
'num_statements': 3,
'percent_covered': 66.66666666666667
for relative_files in [False, True]:
config_file = os.path.join(self.temp_dir, "config")
with open(config_file, 'w') as handle:
handle.write(
"[run]\nrelative_files = {}".format(relative_files)
)
cov = coverage.Coverage(
context="cool_test",
config_file=config_file
)
cov.config.json_show_contexts = True
expected_result = {
'meta': {
"version": coverage.__version__,
"branch_coverage": False,
"show_contexts": True,
},
'files': {
'a.py': {
'executed_lines': [1, 2],
'missing_lines': [3],
'excluded_lines': [],
"contexts": {
"1": [
"cool_test"
],
"2": [
"cool_test"
]
},
'summary': {
'excluded_lines': 0,
'missing_lines': 1,
'covered_lines': 2,
'num_statements': 3,
'percent_covered': 66.66666666666667
}
}
},
'totals': {
'excluded_lines': 0,
'missing_lines': 1,
'covered_lines': 2,
'num_statements': 3,
'percent_covered': 66.66666666666667
}
},
'totals': {
'excluded_lines': 0,
'missing_lines': 1,
'covered_lines': 2,
'num_statements': 3,
'percent_covered': 66.66666666666667
}
}
self._assert_expected_json_report(cov, expected_result)
self._assert_expected_json_report(cov, expected_result)

0 comments on commit ade1f2c

Please sign in to comment.