| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | import sys | |
| 3 | 3 | from test.support import TESTFN, rmtree, unlink, captured_stdout | |
| 4 | 4 | from test.support.script_helper import assert_python_ok, assert_python_failure | |
| 5 | + import textwrap | ||
| 5 | 6 | import unittest | |
| 6 | 7 | ||
| 7 | 8 | import trace | |
@@ -365,6 +366,46 @@ def test_ignored(self): | |||
| 365 | 366 | # Matched before. | |
| 366 | 367 | self.assertTrue(ignore.names(jn('bar', 'baz.py'), 'baz')) | |
| 367 | 368 | ||
| 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 | + | ||
| 368 | 409 | class TestCommandLine(unittest.TestCase): | |
| 369 | 410 | ||
| 370 | 411 | def test_failures(self): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,9 +73,6 @@ def _unsettrace(): | |||
| 73 | 73 | ||
| 74 | 74 | PRAGMA_NOCOVER = "#pragma NO COVER" | |
| 75 | 75 | ||
| 76 | - # Simple rx to find lines with no code. | ||
| 77 | - rx_blank = re.compile(r'^\s*(#.*)?$') | ||
| 78 | - | ||
| 79 | 76 | class _Ignore: | |
| 80 | 77 | def __init__(self, modules=None, dirs=None): | |
| 81 | 78 | self._mods = set() if not modules else set(modules) | |
@@ -278,16 +275,15 @@ def write_results(self, show_missing=True, summary=False, coverdir=None): | |||
| 278 | 275 | lnotab = _find_executable_linenos(filename) | |
| 279 | 276 | else: | |
| 280 | 277 | 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 | ||
| 291 | 287 | ||
| 292 | 288 | ||
| 293 | 289 | if summary and sums: | |
@@ -306,6 +302,7 @@ def write_results(self, show_missing=True, summary=False, coverdir=None): | |||
| 306 | 302 | ||
| 307 | 303 | def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None): | |
| 308 | 304 | """Return a coverage results file in path.""" | |
| 305 | + # ``lnotab`` is a dict of executable lines, or a line number "table" | ||
| 309 | 306 | ||
| 310 | 307 | try: | |
| 311 | 308 | outfile = open(path, "w", encoding=encoding) | |
@@ -324,17 +321,13 @@ def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None): | |||
| 324 | 321 | outfile.write("%5d: " % lines_hit[lineno]) | |
| 325 | 322 | n_hits += 1 | |
| 326 | 323 | 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 | ||
| 332 | 326 | # #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(" ") | ||
| 338 | 331 | outfile.write(line.expandtabs(8)) | |
| 339 | 332 | ||
| 340 | 333 | return n_hits, n_lines | |
| Original file line number | Diff line number | Diff 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. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments