Skip to content

fix context reporting for relative_files #902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test was much smaller than the json one, so i duplicated it. do you have a preference of approach?

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)
11 changes: 10 additions & 1 deletion tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,16 @@ def test_simple_line_coverage(self):
self._assert_expected_json_report(cov, expected_result)

def test_context(self):
cov = coverage.Coverage(context="cool_test")
for relative_files in [False, True]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

easier to look at this diff at https://github.com/nedbat/coveragepy/pull/902/files?w=1 (hiding whitespace changes)

i tried to use pytest parametrize but it doesn't work with unitttest tests

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In cases like this, i've used helper functions. I'll make that change. I really appreciate that you updated the tests, and thought about how best to update them. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

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': {
Expand Down