Skip to content

Commit

Permalink
use presence of activate script rather than sys.prefix to determine i…
Browse files Browse the repository at this point in the history
…f a dir is a virtualenv
  • Loading branch information
John Still committed Jul 11, 2017
1 parent 676c4f9 commit b32cfc8
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions _pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand All @@ -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)
)
Expand Down

0 comments on commit b32cfc8

Please sign in to comment.