Skip to content

Commit cb810ae

Browse files
committed
gh-152409: Save the trace --file counts when --no-report is used
The counts were only persisted at the end of CoverageResults.write_results(), which main() skips when --no-report is given, so --no-report --file silently discarded them. Move that step into CoverageResults.save_counts() (still called from write_results()) and call it on the --no-report branch too.
1 parent 3fa72d5 commit cb810ae

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

Lib/test/test_trace.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from pickle import dump
2+
from pickle import dump, load
33
import sys
44
from test.support import captured_stdout, requires_resource
55
from test.support.os_helper import (TESTFN, rmtree, unlink)
@@ -561,6 +561,28 @@ def f():
561561
self.assertIn('lines cov% module (path)', stdout)
562562
self.assertIn(f'6 100.0% {modulename} ({filename})', stdout)
563563

564+
def test_count_no_report_accumulates_counts(self):
565+
# --no-report must still save the --file counts so they accumulate.
566+
filename = f'{TESTFN}.py'
567+
countsfile = f'{TESTFN}.counts'
568+
with open(filename, 'w', encoding='utf-8') as fd:
569+
self.addCleanup(unlink, filename)
570+
self.addCleanup(unlink, countsfile)
571+
fd.write('for i in range(3):\n pass\n')
572+
argv = ('-m', 'trace', '--count', '--no-report',
573+
'--file', countsfile, filename)
574+
assert_python_ok(*argv, PYTHONIOENCODING='utf-8')
575+
self.assertTrue(os.path.exists(countsfile))
576+
with open(countsfile, 'rb') as fd:
577+
counts = load(fd)[0]
578+
self.assertTrue(counts)
579+
# A second run accumulates into the same file.
580+
assert_python_ok(*argv, PYTHONIOENCODING='utf-8')
581+
with open(countsfile, 'rb') as fd:
582+
accumulated = load(fd)[0]
583+
self.assertEqual(accumulated,
584+
{key: 2 * value for key, value in counts.items()})
585+
564586
def test_run_as_module(self):
565587
assert_python_ok('-m', 'trace', '-l', '--module', 'timeit', '-n', '1')
566588
assert_python_failure('-m', 'trace', '-l', '--module', 'not_a_module_zzz')

Lib/trace.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,11 @@ def write_results(self, show_missing=True, summary=False, coverdir=None, *,
287287
n_lines, n_hits, modulename, filename = sums[m]
288288
print(f"{n_lines:5d} {n_hits/n_lines:.1%} {modulename} ({filename})")
289289

290+
self.save_counts()
291+
292+
def save_counts(self):
293+
"""Save the accumulated counts to ``self.outfile`` if one was given."""
290294
if self.outfile:
291-
# try and store counts and module info into self.outfile
292295
try:
293296
with open(self.outfile, 'wb') as f:
294297
pickle.dump((self.counts, self.calledfuncs, self.callers),
@@ -744,6 +747,8 @@ def parse_ignore_dir(s):
744747

745748
if not opts.no_report:
746749
results.write_results(opts.missing, opts.summary, opts.coverdir)
750+
else:
751+
results.save_counts()
747752

748753
if __name__=='__main__':
749754
main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix the :mod:`trace` command-line tool not saving the ``--file`` counts
2+
when ``--no-report`` is used, which prevented accumulating counts over
3+
several runs. Patch by tonghuaroot.

0 commit comments

Comments
 (0)