plugin2.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 for testing."""
  8. def file_tracer(self, filename):
  9. if "render.py" in filename:
  10. return RenderFileTracer()
  11. def file_reporter(self, filename):
  12. return FileReporter(filename)
  13. class RenderFileTracer(coverage.FileTracer):
  14. """A FileTracer using information from the caller."""
  15. def has_dynamic_source_filename(self):
  16. return True
  17. def dynamic_source_filename(self, filename, frame):
  18. if frame.f_code.co_name != "render":
  19. return None
  20. source_filename = os.path.abspath(frame.f_locals['filename'])
  21. return source_filename
  22. def line_number_range(self, frame):
  23. lineno = frame.f_locals['linenum']
  24. return lineno, lineno+1
  25. class FileReporter(coverage.FileReporter):
  26. """A goofy file reporter."""
  27. def lines(self):
  28. # Goofy test arrangement: claim that the file has as many lines as the
  29. # number in its name.
  30. num = os.path.basename(self.filename).split(".")[0].split("_")[1]
  31. return set(range(1, int(num)+1))
  32. def coverage_init(reg, options): # pylint: disable=unused-argument
  33. """Called by coverage to initialize the plugins here."""
  34. reg.add_file_tracer(Plugin())