Skip to content

Commit b32cfc8

Browse files
author
John Still
committed
use presence of activate script rather than sys.prefix to determine if a dir is a virtualenv
1 parent 676c4f9 commit b32cfc8

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

_pytest/main.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ def pytest_runtestloop(session):
169169
return True
170170

171171

172+
def _in_venv(path):
173+
"""Attempts to detect if ``path`` is the root of a Virtual Environment by
174+
checking for the existence of the appropriate activate script"""
175+
bindir = path.join('Scripts' if sys.platform.startswith('win') else 'bin')
176+
if not bindir.exists():
177+
return False
178+
activates = ('activate', 'activate.csh', 'activate.fish',
179+
'Activate', 'Activate.bat', 'Activate.ps1')
180+
return any([fname.basename in activates for fname in bindir.listdir()])
181+
182+
172183
def pytest_ignore_collect(path, config):
173184
ignore_paths = config._getconftest_pathlist("collect_ignore", path=path.dirpath())
174185
ignore_paths = ignore_paths or []
@@ -179,11 +190,10 @@ def pytest_ignore_collect(path, config):
179190
if py.path.local(path) in ignore_paths:
180191
return True
181192

182-
invenv = py.path.local(sys.prefix) == path
183-
allow_invenv = config.getoption("collect_in_virtualenv")
184-
if invenv and not allow_invenv:
193+
allow_in_venv = config.getoption("collect_in_virtualenv")
194+
if _in_venv(path) and not allow_in_venv:
185195
config.warn(RuntimeWarning,
186-
'Path "%s" appears to be a Python installation; skipping\n'
196+
'Path "%s" appears to be a Python virtual installation; skipping\n'
187197
'Pass --collect-in-virtualenv to force collection of tests in "%s"\n'
188198
'Use --ignore="%s" to silence this warning' % (path, path, path)
189199
)

0 commit comments

Comments
 (0)