Open ichard26 opened 1 month ago
For the erroring case, could we avoid the monkey patching and just define a custom command and 'pretend' we're not in a virtualenv, something like (the attribute setting/restoring magic should probably be in a fixture/context manager):
def test_require_virtualenv_errors_when_not_in_venv(self) -> None:
class Cmd(Command):
ignore_require_venv: bool = False
# edit variable set by PEP-405 compliant virtualenvs
# to pretend we're not in a venv even if we actually are
has_base_prefix = hasattr(sys, "base_prefix")
old_base_prefix = getattr(sys, "base_prefix", None)
if has_base_prefix:
delattr(sys, "base_prefix")
try:
with pytest.raises(SystemExit) as excinfo:
Cmd(name="fake", summary="").main(["--require-virtualenv"])
assert excinfo.value.code == VIRTUALENV_NOT_FOUND
finally:
# restore our modifications
if has_base_prefix:
sys.base_prefix = old_base_prefix
As far as I know, the require-virtualenv functionality is essentially entirely untested. This is the main reason why it is not documented (see also https://github.com/pypa/pip/issues/2429).
It would be good to see tests for
--require-virtualenv
--require-virtualenv
featureBelow, I've attached some old discussion on this topic.
I started taking a look at the tests- as far as I can tell
require_venv
isn't actually tested anywhere and it's not clear to me that there would be any benefit to testing the behavior ofignore_require_venv
for this particular command.This is the only reference I can find (
tests/unit/test_options.py
) and it only seems to be asserting that the order (command, option) doesn't matter.my first thought is to implement something like:
running_under_virtualenv()
incli/base_command.py
(with return value of has_venv column in table below)assert_option_error(msg)
however... that test condition looks an awful lot like a reimplementation of the original implementation...
error condition truth matrix:
so I guess I'm a little lost on what a test for this should look like...
Originally posted by @justin-f-perez in https://github.com/pypa/pip/issues/10658#issuecomment-968286761