-
Notifications
You must be signed in to change notification settings - Fork 0
/
tox_ipdb.py
68 lines (55 loc) · 2.68 KB
/
tox_ipdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Tox plugin which installs ipdb in tox environments."""
import logging
import tox
__version__ = '1.0'
_LOGGER = logging.getLogger(__name__)
if tox.__version__ < '4':
# TOX 3
from tox import hookimpl # type: ignore[attr-defined]
from tox.action import Action
from tox.config import Config, DepConfig # type: ignore[attr-defined]
from tox.venv import VirtualEnv
@hookimpl
def tox_configure(config: Config) -> None:
"""Add ipdb to dependencies of every tox environment."""
for envconfig in config.envconfigs.values():
envconfig.deps.append(DepConfig('ipdb'))
@hookimpl
def tox_testenv_create(venv: VirtualEnv, action: Action) -> None:
"""Add tox-ipdb to provision venv."""
if venv.name == venv.envconfig.config.provision_tox_env:
# Add tox-ipdb-plugin itself into provision tox environment.
venv.envconfig.deps.append(DepConfig('tox-ipdb-plugin'))
else:
# TOX 4
from tox.config.loader.api import Override
from tox.config.of_type import _PLACE_HOLDER
from tox.config.sets import EnvConfigSet
from tox.plugin import impl
from tox.session.state import State
@impl
def tox_add_env_config(env_conf: EnvConfigSet, state: State) -> None:
"""Ensure ipdb is in every tox environment."""
if 'package_env' in state.conf.core and env_conf.name == state.conf.core['package_env']:
# Skip package env
_LOGGER.debug("tox-ipdb-plugin[%s]: Ignoring package environment.", env_conf.name)
return
old_deps = env_conf['deps'].lines()
if env_conf.name == state.conf.core['provision_tox_env']:
# It's a provision env, add the plugin itself.
new_deps = old_deps + ['tox-ipdb-plugin']
_LOGGER.debug("tox-ipdb-plugin[%s]: Appending tox-ipdb-plugin to provision environment dependencies.",
env_conf.name)
else:
# It's not a provision env, add ipdb.
new_deps = old_deps + ['ipdb']
_LOGGER.debug("tox-ipdb-plugin[%s]: Appending ipdb to environment dependencies.", env_conf.name)
_LOGGER.debug("tox-ipdb-plugin[%s]: New deps: %s", env_conf.name, new_deps)
override = Override('{}.deps={}'.format(env_conf.name, '\n'.join(new_deps)))
# API changed in tox 4.15.0
if tox.__version__ < '4.15':
env_conf.loaders[0].overrides[override.key] = override # type: ignore[assignment]
else:
env_conf.loaders[0].overrides.setdefault(override.key, []).append(override)
# Clear cache
env_conf._defined['deps']._cache = _PLACE_HOLDER # type: ignore[attr-defined] # _cache is not public API