Advertisement
mwchase

LC:Nobilis Update 3 - plugin

May 14th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. import importlib
  2. import re
  3. import sys
  4.  
  5.  
  6. TEST_MODULE = re.compile(r'tests\.((?:\w.+\.)*)test_(\w+)')
  7. FIDDLE_WITH_COVERAGE = False
  8.  
  9.  
  10. def module_path(test_module):
  11.     match = TEST_MODULE.fullmatch(test_module)
  12.     if match:
  13.         yield ''.join(match.groups())
  14.  
  15.  
  16. def pytest_addoption(parser):
  17.     parser.addoption('--divide-and-cover', action='store_true',
  18.                      help='activate divide and cover tracing')
  19.  
  20.  
  21. def pytest_configure(config):
  22.     if config.option.divide_and_cover:
  23.         from coverage_handler import UNDER_WRAPPER
  24.         if UNDER_WRAPPER:
  25.             global FIDDLE_WITH_COVERAGE
  26.             FIDDLE_WITH_COVERAGE = True
  27.         else:
  28.             print('Warning: called with --divide-and-cover, but not running '
  29.                   'under divide_and_cover. Not activating plugin.')
  30.  
  31.  
  32. # This is probably wrong
  33. def pytest_collection_modifyitems(session, config, items):
  34.     if FIDDLE_WITH_COVERAGE:
  35.         from .coverage_handler import coverage_script
  36.         modules = sys.modules.copy()
  37.         paths = []
  38.         for test_path in modules:
  39.             for module_path_ in module_path(test_path):
  40.                 paths.append(module_path_)
  41.                 coverage_script.new_coverage(test_path, module_path_)
  42.         # This next bit needs to be run under a coverage tracer, with source
  43.         # based on paths. Specifically, it should use the root package from
  44.         # each path
  45.         roots = sorted({path.split('.', 1)[0] for path in paths})
  46.         coverage_script.make_import_coverage(roots)
  47.         print('Covering packages under: {}'.format(roots))
  48.         coverage_script.switch_coverage(coverage_script.import_coverage)
  49.         try:
  50.             for path in paths:
  51.                 try:
  52.                     importlib.import_module(path)
  53.                 except ImportError:
  54.                     print('Could not import {}'.format(path))
  55.         finally:
  56.             coverage_script.deactivate_coverage()
  57.  
  58.  
  59. def pytest_runtest_setup(item):
  60.     if FIDDLE_WITH_COVERAGE:
  61.         from .coverage_handler import coverage_script
  62.         coverage_script.activate_coverage(item.obj.__module__)
  63.  
  64.  
  65. def pytest_runtest_teardown(item, nextitem):
  66.     if FIDDLE_WITH_COVERAGE:
  67.         from .coverage_handler import coverage_script
  68.         coverage_script.deactivate_coverage()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement