From b32cfc88daad55f6518fc828db7aa770d4e4c80a Mon Sep 17 00:00:00 2001 From: John Still Date: Tue, 11 Jul 2017 14:32:09 -0500 Subject: [PATCH] use presence of activate script rather than sys.prefix to determine if a dir is a virtualenv --- _pytest/main.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/_pytest/main.py b/_pytest/main.py index bd33ab95f..caf2ca813 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -169,6 +169,17 @@ def pytest_runtestloop(session): 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): ignore_paths = config._getconftest_pathlist("collect_ignore", path=path.dirpath()) ignore_paths = ignore_paths or [] @@ -179,11 +190,10 @@ def pytest_ignore_collect(path, config): if py.path.local(path) in ignore_paths: return True - invenv = py.path.local(sys.prefix) == path - allow_invenv = config.getoption("collect_in_virtualenv") - if invenv and not allow_invenv: + allow_in_venv = config.getoption("collect_in_virtualenv") + if _in_venv(path) and not allow_in_venv: 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' 'Use --ignore="%s" to silence this warning' % (path, path, path) )