-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect and warn/ignore local python installations #2566
Conversation
hi @jmsdvl - first thanks for this piece of work, while it makes the situation better, its however not addressing #2518 since it does not detect virtualenvs and instead takes a look at the current virtualenv only second there should be a way to unittest however it seems rather tricky with the current infrastucture - i#ll try to take a deeper look this week |
@RonnyPfannschmidt is there a bulletproof way to detect a virtualenv that isn't active? I guess I could just have the code sniff around the directory structure looking for the python executable and / or a site-packages directory. Might get expensive if its run in |
@jmsdvl i would propose to check for the platform specific virtualenv activation scripts, so it would be one check per directory @nicoddemus any oppinion? |
…f a dir is a virtualenv
Sounds like a good idea to me. 👍 Is there legitimate cases you guys can think of for searching for tests inside a virtual environment? |
@nicoddemus not really, unless you're working on the virtual environment itself or tools that heavily interact with it. But then that's why there's a CLI override option. I implemented the check for a relevant activate script. If that, by whatever slim chance, gives false positives, it would be fairly trivial to add an extra check for another file indicative of a virtual env (say, a python binary) in the same directory as the activate script. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome @jmsdvl, thanks a ton for the PR.
changelog/2518.feature
Outdated
@@ -0,0 +1 @@ | |||
Collection ignores the currently active Python installation by default; `--collect-in-virtualenv` overrides this behavior. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should update the note to mention that now we detect virtual environments.
changelog/2518.feature
Outdated
@@ -0,0 +1 @@ | |||
Collection ignores the currently active Python installation by default; `--collect-in-virtualenv` overrides this behavior. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I suggest we add a blurb next to norecursedirs
explaining that by default pytest will also skip any virtual environments, detected by their activation script, and that can be bypassed with --collect-in-virtualenv
.
@@ -70,6 +70,8 @@ def pytest_addoption(parser): | |||
group.addoption('--keepduplicates', '--keep-duplicates', action="store_true", | |||
dest="keepduplicates", default=False, | |||
help="Keep duplicate tests.") | |||
group.addoption('--collect-in-virtualenv', action='store_true', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a default=False
here to be explicit, otherwise I believe the default is None
.
@@ -167,6 +169,17 @@ def pytest_runtestloop(session): | |||
return True | |||
|
|||
|
|||
def _in_venv(path): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice going making this a separate function. I suggest we unittest it directly by creating directories with fake activation scripts and calling it accordingly; we want to exercise it on CI to avoid someone touching it and breaking it by accident.
@@ -177,6 +190,15 @@ def pytest_ignore_collect(path, config): | |||
if py.path.local(path) in ignore_paths: | |||
return True | |||
|
|||
allow_in_venv = config.getoption("collect_in_virtualenv") | |||
if _in_venv(path) and not allow_in_venv: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we should emit this warning; the common case is for users to have some virtual environment locally, so we would generate needless warnings all the time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably want to make this check only if the user did not explicitly passed the path into the command line, like what we do to while collecting python test files in python.py
.
IOW, pytest .env
should collect everything under .env
even if .env
is a virtual environment directory.
Oh sorry I did not look at the code until now. Just playing devil's advocate there. 😁 |
Btw,
Awesome, we can always use more help. Feel free to dig around, triage issues and participate on PR reviews. 👍 |
Requested changes, from the top:
So the overall behavior at this point is that
And its all tested and in the docs. 💥 |
Great, thanks a lot! 🙌 |
This should address / close ticket #2518. The behaviour implemented is as follows:
sys.prefix
, the directory and any tests it contains will be ignored and a helpful warning will be issued.--collect-in-virtualenv
(a new CLI flag) is passed, pytest will collect anyways.--ignore=./.env
or what have you, the warning is silenced.I tried to write a test but couldn't quite figure out how to effectively mock
sys.prefix
; I'd appreciate some help there. Other feedback is also totally welcome.