test_debug.py 4.5 KB

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