|
1 |
| -"""Coverage Config reload Plugin""" |
| 1 | +"""Coverage Config reload Plugin |
| 2 | +
|
| 3 | +config.from_file was broken in coveragepy 4.4.1 |
| 4 | +https://bitbucket.org/ned/coveragepy/issues/616 |
| 5 | +
|
| 6 | +add_configurer was added in coveragepy 4.5, but also included |
| 7 | +a few easter eggs to make this plugin a tiny bit harder. |
| 8 | +""" |
2 | 9 | import sys
|
3 | 10 |
|
4 |
| -__version__ = '0.2.0' |
| 11 | +import coverage |
| 12 | +from coverage.backward import configparser |
| 13 | + |
| 14 | +__version__ = '0.3.0' |
| 15 | + |
| 16 | + |
| 17 | +class PluginBase(coverage.CoveragePlugin): |
| 18 | + |
| 19 | + @classmethod |
| 20 | + def _register(cls, reg, options, now=True): |
| 21 | + config = get_coverage_config() |
| 22 | + plugin = cls(reg, options, config, now) |
| 23 | + |
| 24 | + # Try using coverage 4.5 |
| 25 | + try: |
| 26 | + reg.add_configurer(plugin) |
| 27 | + return plugin |
| 28 | + except AttributeError: |
| 29 | + pass |
| 30 | + |
| 31 | + # Fallback to using noop added v4.0 |
| 32 | + try: |
| 33 | + reg.add_noop(plugin) |
| 34 | + except Exception: |
| 35 | + reg._add_plugin(plugin, None) |
| 36 | + return plugin |
| 37 | + |
| 38 | + def __init__(self, reg, options, config=None, now=False): |
| 39 | + self._name = self.__class__.__name__.lower().replace('plugin', '') |
| 40 | + self.reg = reg |
| 41 | + self.options = options |
| 42 | + self.config = config |
| 43 | + self.done = False |
| 44 | + self.status = None |
| 45 | + if now: |
| 46 | + self.do() |
| 47 | + |
| 48 | + def configure(self, config): |
| 49 | + if isinstance(config, coverage.Coverage): |
| 50 | + config = config.config |
| 51 | + self.config = config |
| 52 | + |
| 53 | + self.do() |
| 54 | + |
| 55 | + def do(self): |
| 56 | + if not self.done: |
| 57 | + self._do() |
| 58 | + self.done = True |
| 59 | + |
| 60 | + def sys_info(self): |
| 61 | + if not self.done: |
| 62 | + try: |
| 63 | + self.do() |
| 64 | + except: |
| 65 | + pass |
| 66 | + return [(self._name, |
| 67 | + str(self.status) if self.status else str(self.done))] |
| 68 | + |
| 69 | + |
| 70 | +class ConfigReloadPlugin(PluginBase): |
| 71 | + |
| 72 | + def _do(self): |
| 73 | + read_config_files(self.config) |
5 | 74 |
|
6 | 75 |
|
7 | 76 | def get_coverage_config():
|
8 | 77 | """Get coverage config from stack."""
|
9 | 78 | # Stack
|
10 | 79 | # 1. get_coverage_config (i.e. this function)
|
11 |
| - # 2. coverage_init |
12 |
| - # 3. load_plugins |
13 |
| - frame = sys._getframe(2) |
| 80 | + # 2. PluginBase._register |
| 81 | + # 3. coverage_init |
| 82 | + # 4. load_plugins |
| 83 | + frame = sys._getframe(3) |
14 | 84 | config = frame.f_locals['config']
|
15 | 85 | return config
|
16 | 86 |
|
17 | 87 |
|
| 88 | +def read_config_file(config, filename): |
| 89 | + if filename == '.coveragerc': |
| 90 | + rc_file = True |
| 91 | + elif filename in ('tox.ini', 'setup.cfg'): |
| 92 | + rc_file = False |
| 93 | + else: |
| 94 | + # Very likely all other config files are 'own rc' files. |
| 95 | + # However the cost here is minimal for correctness and this |
| 96 | + # reduces the chance it can be broken by future childishness :P |
| 97 | + parser = configparser.RawConfigParser() |
| 98 | + parser.read(filename) |
| 99 | + sections = parser.sections() |
| 100 | + rc_file = not any(section.startswith('coverage:') |
| 101 | + for section in sections) |
| 102 | + |
| 103 | + # Try the old pre 4.4.1 invocation |
| 104 | + try: |
| 105 | + return config.from_file( |
| 106 | + filename, |
| 107 | + section_prefix='' if rc_file else 'coverage:' |
| 108 | + ) |
| 109 | + except TypeError: |
| 110 | + pass |
| 111 | + |
| 112 | + # coverage 5+ |
| 113 | + if hasattr(config, 'config_file'): |
| 114 | + config.config_file = filename |
| 115 | + |
| 116 | + # coverage 4.4.1+ |
| 117 | + return config.from_file(filename, our_file=rc_file) |
| 118 | + |
| 119 | + |
18 | 120 | def read_config_files(config):
|
| 121 | + if not hasattr(config, 'config_files'): |
| 122 | + if config.config_file: |
| 123 | + rv = read_config_file(config, config.config_file) |
| 124 | + assert rv is True |
| 125 | + return |
| 126 | + |
19 | 127 | config_filenames = config.config_files[:]
|
20 | 128 | for filename in config_filenames:
|
21 |
| - prefix = '' if filename == '.coveragerc' else 'coverage:' |
22 |
| - config.from_file(filename, section_prefix=prefix) |
| 129 | + rv = read_config_file(config, filename) |
| 130 | + assert rv is True |
23 | 131 |
|
24 | 132 | # restore original as from_file appends to the config_files list
|
25 | 133 | config.config_files = config_filenames
|
26 | 134 |
|
27 | 135 |
|
28 | 136 | def coverage_init(reg, options):
|
29 |
| - config = get_coverage_config() |
30 |
| - read_config_files(config) |
| 137 | + ConfigReloadPlugin._register(reg, options) |
0 commit comments