-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tox plugin to handle --testmon (#107)
- sets TESTMON_DATAFILE to use a data file per tox venv - installs pytest-testmon into a tox venv when --testmon gets used
- Loading branch information
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
A tox plugin to automatically install and use pytest-testmon with tox. | ||
It installs pytest-testmon once `--testmon` is used in one of the commands | ||
(starting with "pytest" or "py.test"). | ||
It also sets the TESTMON_DATAFILE environment variable (always) to use a | ||
datafile in the venv's directory. | ||
It uses the tox_runenvreport hook instead of tox_testenv_install_deps (where | ||
it could just add the dep unconditionally) to only install pytest-testmon on | ||
demand. Changing envconfig.deps on demand would re-create the venv. | ||
""" | ||
import os | ||
|
||
import pluggy | ||
from tox.config import DepConfig | ||
|
||
hookimpl = pluggy.HookimplMarker("tox") | ||
|
||
|
||
def _uses_testmon(envconfig): | ||
"""Test if an envconfig uses testmon by looking at the command(s). | ||
The command will include resolved {posargs} already, i.e. arguments passed | ||
on the command line. | ||
""" | ||
for command in envconfig.commands: | ||
if command[0] == 'pytest' or command[0] == 'py.test': | ||
if '--testmon' in command: | ||
return True | ||
return False | ||
|
||
|
||
def touch_stampfile(venv): | ||
open(venv.path.join('.testmon_installed'), 'a').close() | ||
|
||
|
||
def installed_testmon(venv): | ||
return os.path.exists(venv.path.join('.testmon_installed')) | ||
|
||
|
||
@hookimpl | ||
def tox_runenvreport(venv, action): | ||
datafile = str(venv.path.join('.testmondata')) | ||
action.setactivity('testmon', 'setting TESTMON_DATAFILE=%s' % datafile) | ||
venv.envconfig.setenv['TESTMON_DATAFILE'] = datafile | ||
|
||
if (_uses_testmon(venv.envconfig) | ||
and 'pytest-testmon' not in (x.name for x in venv.envconfig.deps)): | ||
if not installed_testmon(venv): | ||
action.setactivity('testmon', 'installing pytest-testmon') | ||
# Uses _install for handling configured indexservers. | ||
# venv.run_install_command(['pytest-testmon'], action=action) | ||
venv._install([DepConfig('pytest-testmon')], action=action) | ||
|
||
touch_stampfile(venv) |