use presence of activate script rather than sys.prefix to determine if a dir is a virtualenv

This commit is contained in:
John Still 2017-07-11 14:32:09 -05:00
parent 676c4f970d
commit b32cfc88da
1 changed files with 14 additions and 4 deletions

View File

@ -169,6 +169,17 @@ def pytest_runtestloop(session):
return True return True
def _in_venv(path):
"""Attempts to detect if ``path`` is the root of a Virtual Environment by
checking for the existence of the appropriate activate script"""
bindir = path.join('Scripts' if sys.platform.startswith('win') else 'bin')
if not bindir.exists():
return False
activates = ('activate', 'activate.csh', 'activate.fish',
'Activate', 'Activate.bat', 'Activate.ps1')
return any([fname.basename in activates for fname in bindir.listdir()])
def pytest_ignore_collect(path, config): def pytest_ignore_collect(path, config):
ignore_paths = config._getconftest_pathlist("collect_ignore", path=path.dirpath()) ignore_paths = config._getconftest_pathlist("collect_ignore", path=path.dirpath())
ignore_paths = ignore_paths or [] ignore_paths = ignore_paths or []
@ -179,11 +190,10 @@ def pytest_ignore_collect(path, config):
if py.path.local(path) in ignore_paths: if py.path.local(path) in ignore_paths:
return True return True
invenv = py.path.local(sys.prefix) == path allow_in_venv = config.getoption("collect_in_virtualenv")
allow_invenv = config.getoption("collect_in_virtualenv") if _in_venv(path) and not allow_in_venv:
if invenv and not allow_invenv:
config.warn(RuntimeWarning, config.warn(RuntimeWarning,
'Path "%s" appears to be a Python installation; skipping\n' 'Path "%s" appears to be a Python virtual installation; skipping\n'
'Pass --collect-in-virtualenv to force collection of tests in "%s"\n' 'Pass --collect-in-virtualenv to force collection of tests in "%s"\n'
'Use --ignore="%s" to silence this warning' % (path, path, path) 'Use --ignore="%s" to silence this warning' % (path, path, path)
) )