From 12397a5781664bf43da98454db07cdfdec3ab815 Mon Sep 17 00:00:00 2001 From: "RUANG (James Roy)" Date: Wed, 4 Dec 2024 06:33:13 +0800 Subject: [PATCH] gh-112192: Increase the trace module coverage precision to one decimal (#126972) --- Lib/test/test_regrtest.py | 2 +- Lib/test/test_trace.py | 4 ++-- Lib/trace.py | 7 +++---- .../Library/2024-11-18-23-18-27.gh-issue-112192.DRdRgP.rst | 1 + 4 files changed, 7 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-11-18-23-18-27.gh-issue-112192.DRdRgP.rst diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index d4f4a69a7a38c1..0ab7a23aca1df8 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -1138,7 +1138,7 @@ def test_coverage(self): output = self.run_tests("--coverage", test) self.check_executed_tests(output, [test], stats=1) regex = (r'lines +cov% +module +\(path\)\n' - r'(?: *[0-9]+ *[0-9]{1,2}% *[^ ]+ +\([^)]+\)+)+') + r'(?: *[0-9]+ *[0-9]{1,2}\.[0-9]% *[^ ]+ +\([^)]+\)+)+') self.check_line(output, regex) def test_wait(self): diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py index 93966ee31d0a01..e7e42531916d0d 100644 --- a/Lib/test/test_trace.py +++ b/Lib/test/test_trace.py @@ -412,7 +412,7 @@ def test_issue9936(self): coverage = {} for line in stdout: lines, cov, module = line.split()[:3] - coverage[module] = (int(lines), int(cov[:-1])) + coverage[module] = (float(lines), float(cov[:-1])) # XXX This is needed to run regrtest.py as a script modname = trace._fullmodname(sys.modules[modname].__file__) self.assertIn(modname, coverage) @@ -553,7 +553,7 @@ def f(): stdout = stdout.decode() self.assertEqual(status, 0) self.assertIn('lines cov% module (path)', stdout) - self.assertIn(f'6 100% {modulename} ({filename})', stdout) + self.assertIn(f'6 100.0% {modulename} ({filename})', stdout) def test_run_as_module(self): assert_python_ok('-m', 'trace', '-l', '--module', 'timeit', '-n', '1') diff --git a/Lib/trace.py b/Lib/trace.py index bb3d34fd8d6550..a87bc6d61a884f 100644 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -279,14 +279,13 @@ def write_results(self, show_missing=True, summary=False, coverdir=None, *, n_hits, n_lines = self.write_results_file(coverpath, source, lnotab, count, encoding) if summary and n_lines: - percent = int(100 * n_hits / n_lines) - sums[modulename] = n_lines, percent, modulename, filename + sums[modulename] = n_lines, n_hits, modulename, filename if summary and sums: print("lines cov% module (path)") for m in sorted(sums): - n_lines, percent, modulename, filename = sums[m] - print("%5d %3d%% %s (%s)" % sums[m]) + n_lines, n_hits, modulename, filename = sums[m] + print(f"{n_lines:5d} {n_hits/n_lines:.1%} {modulename} ({filename})") if self.outfile: # try and store counts and module info into self.outfile diff --git a/Misc/NEWS.d/next/Library/2024-11-18-23-18-27.gh-issue-112192.DRdRgP.rst b/Misc/NEWS.d/next/Library/2024-11-18-23-18-27.gh-issue-112192.DRdRgP.rst new file mode 100644 index 00000000000000..b169c1508d0d30 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-11-18-23-18-27.gh-issue-112192.DRdRgP.rst @@ -0,0 +1 @@ +In the :mod:`trace` module, increase the coverage precision (``cov%``) to one decimal.