OLD | NEW |
(Empty) | |
| 1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 |
| 2 # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt |
| 3 |
| 4 """Tests of coverage/debug.py""" |
| 5 |
| 6 import os |
| 7 import re |
| 8 |
| 9 import coverage |
| 10 from coverage.backward import StringIO |
| 11 from coverage.debug import info_formatter, info_header |
| 12 from tests.coveragetest import CoverageTest |
| 13 |
| 14 |
| 15 class InfoFormatterTest(CoverageTest): |
| 16 """Tests of misc.info_formatter.""" |
| 17 |
| 18 run_in_temp_dir = False |
| 19 |
| 20 def test_info_formatter(self): |
| 21 lines = list(info_formatter([ |
| 22 ('x', 'hello there'), |
| 23 ('very long label', ['one element']), |
| 24 ('regular', ['abc', 'def', 'ghi', 'jkl']), |
| 25 ('nothing', []), |
| 26 ])) |
| 27 self.assertEqual(lines, [ |
| 28 ' x: hello there', |
| 29 'very long label: one element', |
| 30 ' regular: abc', |
| 31 ' def', |
| 32 ' ghi', |
| 33 ' jkl', |
| 34 ' nothing: -none-', |
| 35 ]) |
| 36 |
| 37 def test_info_formatter_with_generator(self): |
| 38 lines = list(info_formatter(('info%d' % i, i) for i in range(3))) |
| 39 self.assertEqual(lines, ['info0: 0', 'info1: 1', 'info2: 2']) |
| 40 |
| 41 def test_info_header(self): |
| 42 self.assertEqual( |
| 43 info_header("x"), |
| 44 "-- x ---------------------------------------------------------" |
| 45 ) |
| 46 self.assertEqual( |
| 47 info_header("hello there"), |
| 48 "-- hello there -----------------------------------------------" |
| 49 ) |
| 50 |
| 51 |
| 52 class DebugTraceTest(CoverageTest): |
| 53 """Tests of debug output.""" |
| 54 |
| 55 def f1_debug_output(self, debug): |
| 56 """Runs some code with `debug` option, returns the debug output.""" |
| 57 # Make code to run. |
| 58 self.make_file("f1.py", """\ |
| 59 def f1(x): |
| 60 return x+1 |
| 61 |
| 62 for i in range(5): |
| 63 f1(i) |
| 64 """) |
| 65 |
| 66 debug_out = StringIO() |
| 67 cov = coverage.Coverage(debug=debug) |
| 68 cov._debug_file = debug_out |
| 69 self.start_import_stop(cov, "f1") |
| 70 |
| 71 out_lines = debug_out.getvalue().splitlines() |
| 72 return out_lines |
| 73 |
| 74 def test_debug_no_trace(self): |
| 75 out_lines = self.f1_debug_output([]) |
| 76 |
| 77 # We should have no output at all. |
| 78 self.assertFalse(out_lines) |
| 79 |
| 80 def test_debug_trace(self): |
| 81 out_lines = self.f1_debug_output(["trace"]) |
| 82 |
| 83 # We should have a line like "Tracing 'f1.py'" |
| 84 self.assertIn("Tracing 'f1.py'", out_lines) |
| 85 |
| 86 # We should have lines like "Not tracing 'collector.py'..." |
| 87 coverage_lines = lines_matching( |
| 88 out_lines, |
| 89 r"^Not tracing .*: is part of coverage.py$" |
| 90 ) |
| 91 self.assertTrue(coverage_lines) |
| 92 |
| 93 def test_debug_trace_pid(self): |
| 94 out_lines = self.f1_debug_output(["trace", "pid"]) |
| 95 |
| 96 # Now our lines are always prefixed with the process id. |
| 97 pid_prefix = "^pid %5d: " % os.getpid() |
| 98 pid_lines = lines_matching(out_lines, pid_prefix) |
| 99 self.assertEqual(pid_lines, out_lines) |
| 100 |
| 101 # We still have some tracing, and some not tracing. |
| 102 self.assertTrue(lines_matching(out_lines, pid_prefix + "Tracing ")) |
| 103 self.assertTrue(lines_matching(out_lines, pid_prefix + "Not tracing ")) |
| 104 |
| 105 def test_debug_config(self): |
| 106 out_lines = self.f1_debug_output(["config"]) |
| 107 |
| 108 labels = """ |
| 109 attempted_config_files branch config_files cover_pylib data_file |
| 110 debug exclude_list extra_css html_dir html_title ignore_errors |
| 111 include omit parallel partial_always_list partial_list paths |
| 112 precision show_missing source timid xml_output |
| 113 """.split() |
| 114 for label in labels: |
| 115 label_pat = r"^\s*%s: " % label |
| 116 self.assertEqual(len(lines_matching(out_lines, label_pat)), 1) |
| 117 |
| 118 def test_debug_sys(self): |
| 119 out_lines = self.f1_debug_output(["sys"]) |
| 120 |
| 121 labels = """ |
| 122 version coverage cover_dirs pylib_dirs tracer config_files |
| 123 configs_read data_path python platform implementation executable |
| 124 cwd path environment command_line cover_match pylib_match |
| 125 """.split() |
| 126 for label in labels: |
| 127 label_pat = r"^\s*%s: " % label |
| 128 self.assertEqual( |
| 129 len(lines_matching(out_lines, label_pat)), |
| 130 1, |
| 131 msg="Incorrect lines for %r" % label, |
| 132 ) |
| 133 |
| 134 |
| 135 def lines_matching(lines, pat): |
| 136 """Gives the list of lines from `lines` that match `pat`.""" |
| 137 return [l for l in lines if re.search(pat, l)] |
OLD | NEW |