Skip to content

Commit 47ab154

Browse files
selikserhiy-storchaka
authored andcommitted
bpo-31908: Fix output of cover files for trace module command-line tool. (GH-4205)
Previously emitted cover files only when --missing option was used.
1 parent fb7e799 commit 47ab154

File tree

3 files changed

+60
-23
lines changed

3 files changed

+60
-23
lines changed

Lib/test/test_trace.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
from test.support import TESTFN, rmtree, unlink, captured_stdout
44
from test.support.script_helper import assert_python_ok, assert_python_failure
5+
import textwrap
56
import unittest
67

78
import trace
@@ -365,6 +366,46 @@ def test_ignored(self):
365366
# Matched before.
366367
self.assertTrue(ignore.names(jn('bar', 'baz.py'), 'baz'))
367368

369+
# Created for Issue 31908 -- CLI utility not writing cover files
370+
class TestCoverageCommandLineOutput(unittest.TestCase):
371+
372+
codefile = 'tmp.py'
373+
coverfile = 'tmp.cover'
374+
375+
def setUp(self):
376+
with open(self.codefile, 'w') as f:
377+
f.write(textwrap.dedent('''\
378+
x = 42
379+
if []:
380+
print('unreachable')
381+
'''))
382+
383+
def tearDown(self):
384+
unlink(self.codefile)
385+
unlink(self.coverfile)
386+
387+
def test_cover_files_written_no_highlight(self):
388+
argv = '-m trace --count'.split() + [self.codefile]
389+
status, stdout, stderr = assert_python_ok(*argv)
390+
self.assertTrue(os.path.exists(self.coverfile))
391+
with open(self.coverfile) as f:
392+
self.assertEqual(f.read(),
393+
" 1: x = 42\n"
394+
" 1: if []:\n"
395+
" print('unreachable')\n"
396+
)
397+
398+
def test_cover_files_written_with_highlight(self):
399+
argv = '-m trace --count --missing'.split() + [self.codefile]
400+
status, stdout, stderr = assert_python_ok(*argv)
401+
self.assertTrue(os.path.exists(self.coverfile))
402+
with open(self.coverfile) as f:
403+
self.assertEqual(f.read(), textwrap.dedent('''\
404+
1: x = 42
405+
1: if []:
406+
>>>>>> print('unreachable')
407+
'''))
408+
368409
class TestCommandLine(unittest.TestCase):
369410

370411
def test_failures(self):

Lib/trace.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ def _unsettrace():
7373

7474
PRAGMA_NOCOVER = "#pragma NO COVER"
7575

76-
# Simple rx to find lines with no code.
77-
rx_blank = re.compile(r'^\s*(#.*)?$')
78-
7976
class _Ignore:
8077
def __init__(self, modules=None, dirs=None):
8178
self._mods = set() if not modules else set(modules)
@@ -278,16 +275,15 @@ def write_results(self, show_missing=True, summary=False, coverdir=None):
278275
lnotab = _find_executable_linenos(filename)
279276
else:
280277
lnotab = {}
281-
if lnotab:
282-
source = linecache.getlines(filename)
283-
coverpath = os.path.join(dir, modulename + ".cover")
284-
with open(filename, 'rb') as fp:
285-
encoding, _ = tokenize.detect_encoding(fp.readline)
286-
n_hits, n_lines = self.write_results_file(coverpath, source,
287-
lnotab, count, encoding)
288-
if summary and n_lines:
289-
percent = int(100 * n_hits / n_lines)
290-
sums[modulename] = n_lines, percent, modulename, filename
278+
source = linecache.getlines(filename)
279+
coverpath = os.path.join(dir, modulename + ".cover")
280+
with open(filename, 'rb') as fp:
281+
encoding, _ = tokenize.detect_encoding(fp.readline)
282+
n_hits, n_lines = self.write_results_file(coverpath, source,
283+
lnotab, count, encoding)
284+
if summary and n_lines:
285+
percent = int(100 * n_hits / n_lines)
286+
sums[modulename] = n_lines, percent, modulename, filename
291287

292288

293289
if summary and sums:
@@ -306,6 +302,7 @@ def write_results(self, show_missing=True, summary=False, coverdir=None):
306302

307303
def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None):
308304
"""Return a coverage results file in path."""
305+
# ``lnotab`` is a dict of executable lines, or a line number "table"
309306

310307
try:
311308
outfile = open(path, "w", encoding=encoding)
@@ -324,17 +321,13 @@ def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None):
324321
outfile.write("%5d: " % lines_hit[lineno])
325322
n_hits += 1
326323
n_lines += 1
327-
elif rx_blank.match(line):
328-
outfile.write(" ")
329-
else:
330-
# lines preceded by no marks weren't hit
331-
# Highlight them if so indicated, unless the line contains
324+
elif lineno in lnotab and not PRAGMA_NOCOVER in line:
325+
# Highlight never-executed lines, unless the line contains
332326
# #pragma: NO COVER
333-
if lineno in lnotab and not PRAGMA_NOCOVER in line:
334-
outfile.write(">>>>>> ")
335-
n_lines += 1
336-
else:
337-
outfile.write(" ")
327+
outfile.write(">>>>>> ")
328+
n_lines += 1
329+
else:
330+
outfile.write(" ")
338331
outfile.write(line.expandtabs(8))
339332

340333
return n_hits, n_lines
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix output of cover files for ``trace`` module command-line tool.
2+
Previously emitted cover files only when ``--missing`` option was used.
3+
Patch by Michael Selik.

0 commit comments

Comments
 (0)