Skip to content

Commit ade1f2c

Browse files
davidszottennedbat
authored andcommitted
fix context reporting for relative_files
fix reporting of contexts when `relative_files = True` fixes #900
1 parent 7dc3772 commit ade1f2c

File tree

4 files changed

+65
-40
lines changed

4 files changed

+65
-40
lines changed

coverage/html.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def data_for_file(self, fr, analysis):
9595
arcs_executed = analysis.arcs_executed()
9696

9797
if self.config.show_contexts:
98-
contexts_by_lineno = analysis.data.contexts_by_lineno(fr.filename)
98+
contexts_by_lineno = analysis.data.contexts_by_lineno(analysis.filename)
9999

100100
lines = []
101101

coverage/jsonreport.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def report(self, morfs, outfile=None):
4343
for file_reporter, analysis in get_analysis_to_report(self.coverage, morfs):
4444
measured_files[file_reporter.relative_filename()] = self.report_one_file(
4545
coverage_data,
46-
file_reporter,
4746
analysis
4847
)
4948

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

7271
return self.total.n_statements and self.total.pc_covered
7372

74-
def report_one_file(self, coverage_data, file_reporter, analysis):
73+
def report_one_file(self, coverage_data, analysis):
7574
"""Extract the relevant report data for a single file"""
7675
nums = analysis.numbers
7776
self.total += nums
@@ -90,7 +89,7 @@ def report_one_file(self, coverage_data, file_reporter, analysis):
9089
}
9190
if self.config.json_show_contexts:
9291
reported_file['contexts'] = analysis.data.contexts_by_lineno(
93-
file_reporter.filename
92+
analysis.filename,
9493
)
9594
if coverage_data.has_arcs():
9695
reported_file['summary'].update({

tests/test_html.py

+17
Original file line numberDiff line numberDiff line change
@@ -1127,3 +1127,20 @@ def test_no_contexts_warns_no_contexts(self):
11271127
self.start_import_stop(cov, "two_tests")
11281128
with self.assert_warnings(cov, ["No contexts were measured"]):
11291129
cov.html_report()
1130+
1131+
def test_dynamic_contexts_relative_files(self):
1132+
self.make_file("two_tests.py", self.SOURCE)
1133+
self.make_file("config", "[run]\nrelative_files = True")
1134+
cov = coverage.Coverage(source=["."], config_file="config")
1135+
cov.set_option("run:dynamic_context", "test_function")
1136+
cov.set_option("html:show_contexts", True)
1137+
mod = self.start_import_stop(cov, "two_tests")
1138+
d = self.html_data_from_cov(cov, mod)
1139+
context_labels = [self.EMPTY, 'two_tests.test_one', 'two_tests.test_two']
1140+
expected_lines = [self.OUTER_LINES, self.TEST_ONE_LINES, self.TEST_TWO_LINES]
1141+
for label, expected in zip(context_labels, expected_lines):
1142+
actual = [
1143+
ld.number for ld in d.lines
1144+
if label == ld.contexts_label or label in (ld.contexts or ())
1145+
]
1146+
assert sorted(expected) == sorted(actual)

tests/test_json.py

+45-36
Original file line numberDiff line numberDiff line change
@@ -103,42 +103,51 @@ def test_simple_line_coverage(self):
103103
self._assert_expected_json_report(cov, expected_result)
104104

105105
def test_context(self):
106-
cov = coverage.Coverage(context="cool_test")
107-
cov.config.json_show_contexts = True
108-
expected_result = {
109-
'meta': {
110-
"version": coverage.__version__,
111-
"branch_coverage": False,
112-
"show_contexts": True,
113-
},
114-
'files': {
115-
'a.py': {
116-
'executed_lines': [1, 2],
117-
'missing_lines': [3],
118-
'excluded_lines': [],
119-
"contexts": {
120-
"1": [
121-
"cool_test"
122-
],
123-
"2": [
124-
"cool_test"
125-
]
126-
},
127-
'summary': {
128-
'excluded_lines': 0,
129-
'missing_lines': 1,
130-
'covered_lines': 2,
131-
'num_statements': 3,
132-
'percent_covered': 66.66666666666667
106+
for relative_files in [False, True]:
107+
config_file = os.path.join(self.temp_dir, "config")
108+
with open(config_file, 'w') as handle:
109+
handle.write(
110+
"[run]\nrelative_files = {}".format(relative_files)
111+
)
112+
cov = coverage.Coverage(
113+
context="cool_test",
114+
config_file=config_file
115+
)
116+
cov.config.json_show_contexts = True
117+
expected_result = {
118+
'meta': {
119+
"version": coverage.__version__,
120+
"branch_coverage": False,
121+
"show_contexts": True,
122+
},
123+
'files': {
124+
'a.py': {
125+
'executed_lines': [1, 2],
126+
'missing_lines': [3],
127+
'excluded_lines': [],
128+
"contexts": {
129+
"1": [
130+
"cool_test"
131+
],
132+
"2": [
133+
"cool_test"
134+
]
135+
},
136+
'summary': {
137+
'excluded_lines': 0,
138+
'missing_lines': 1,
139+
'covered_lines': 2,
140+
'num_statements': 3,
141+
'percent_covered': 66.66666666666667
142+
}
133143
}
144+
},
145+
'totals': {
146+
'excluded_lines': 0,
147+
'missing_lines': 1,
148+
'covered_lines': 2,
149+
'num_statements': 3,
150+
'percent_covered': 66.66666666666667
134151
}
135-
},
136-
'totals': {
137-
'excluded_lines': 0,
138-
'missing_lines': 1,
139-
'covered_lines': 2,
140-
'num_statements': 3,
141-
'percent_covered': 66.66666666666667
142152
}
143-
}
144-
self._assert_expected_json_report(cov, expected_result)
153+
self._assert_expected_json_report(cov, expected_result)

0 commit comments

Comments
 (0)