plugin1.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. """A plugin for test_plugins.py to import."""
  4. import os.path
  5. import coverage
  6. class Plugin(coverage.CoveragePlugin):
  7. """A plugin to import, so that it isn't in the test's current directory."""
  8. def file_tracer(self, filename):
  9. """Trace only files named xyz.py"""
  10. if "xyz.py" in filename:
  11. return FileTracer(filename)
  12. def file_reporter(self, filename):
  13. return FileReporter(filename)
  14. class FileTracer(coverage.FileTracer):
  15. """A FileTracer emulating a simple static plugin."""
  16. def __init__(self, filename):
  17. """Claim that xyz.py was actually sourced from ABC.zz"""
  18. self._filename = filename
  19. self._source_filename = os.path.join(
  20. "/src",
  21. os.path.basename(filename.replace("xyz.py", "ABC.zz"))
  22. )
  23. def source_filename(self):
  24. return self._source_filename
  25. def line_number_range(self, frame):
  26. """Map the line number X to X05,X06,X07."""
  27. lineno = frame.f_lineno
  28. return lineno*100+5, lineno*100+7
  29. class FileReporter(coverage.FileReporter):
  30. """Dead-simple FileReporter."""
  31. def lines(self):
  32. return set([105, 106, 107, 205, 206, 207])
  33. def coverage_init(reg, options): # pylint: disable=unused-argument
  34. """Called by coverage to initialize the plugins here."""
  35. reg.add_file_tracer(Plugin())