annotate.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. """Source file annotation for coverage.py."""
  4. import io
  5. import os
  6. import re
  7. from coverage.files import flat_rootname
  8. from coverage.misc import isolate_module
  9. from coverage.report import Reporter
  10. os = isolate_module(os)
  11. class AnnotateReporter(Reporter):
  12. """Generate annotated source files showing line coverage.
  13. This reporter creates annotated copies of the measured source files. Each
  14. .py file is copied as a .py,cover file, with a left-hand margin annotating
  15. each line::
  16. > def h(x):
  17. - if 0: #pragma: no cover
  18. - pass
  19. > if x == 1:
  20. ! a = 1
  21. > else:
  22. > a = 2
  23. > h(2)
  24. Executed lines use '>', lines not executed use '!', lines excluded from
  25. consideration use '-'.
  26. """
  27. def __init__(self, coverage, config):
  28. super(AnnotateReporter, self).__init__(coverage, config)
  29. self.directory = None
  30. blank_re = re.compile(r"\s*(#|$)")
  31. else_re = re.compile(r"\s*else\s*:\s*(#|$)")
  32. def report(self, morfs, directory=None):
  33. """Run the report.
  34. See `coverage.report()` for arguments.
  35. """
  36. self.report_files(self.annotate_file, morfs, directory)
  37. def annotate_file(self, fr, analysis):
  38. """Annotate a single file.
  39. `fr` is the FileReporter for the file to annotate.
  40. """
  41. statements = sorted(analysis.statements)
  42. missing = sorted(analysis.missing)
  43. excluded = sorted(analysis.excluded)
  44. if self.directory:
  45. dest_file = os.path.join(self.directory, flat_rootname(fr.relative_filename()))
  46. if dest_file.endswith("_py"):
  47. dest_file = dest_file[:-3] + ".py"
  48. dest_file += ",cover"
  49. else:
  50. dest_file = fr.filename + ",cover"
  51. with io.open(dest_file, 'w', encoding='utf8') as dest:
  52. i = 0
  53. j = 0
  54. covered = True
  55. source = fr.source()
  56. for lineno, line in enumerate(source.splitlines(True), start=1):
  57. while i < len(statements) and statements[i] < lineno:
  58. i += 1
  59. while j < len(missing) and missing[j] < lineno:
  60. j += 1
  61. if i < len(statements) and statements[i] == lineno:
  62. covered = j >= len(missing) or missing[j] > lineno
  63. if self.blank_re.match(line):
  64. dest.write(u' ')
  65. elif self.else_re.match(line):
  66. # Special logic for lines containing only 'else:'.
  67. if i >= len(statements) and j >= len(missing):
  68. dest.write(u'! ')
  69. elif i >= len(statements) or j >= len(missing):
  70. dest.write(u'> ')
  71. elif statements[i] == missing[j]:
  72. dest.write(u'! ')
  73. else:
  74. dest.write(u'> ')
  75. elif lineno in excluded:
  76. dest.write(u'- ')
  77. elif covered:
  78. dest.write(u'> ')
  79. else:
  80. dest.write(u'! ')
  81. dest.write(line)