diff --git a/setuptools/tests/test_easy_install.py b/setuptools/tests/test_easy_install.py index 5831b26757..09c4e075d1 100644 --- a/setuptools/tests/test_easy_install.py +++ b/setuptools/tests/test_easy_install.py @@ -40,6 +40,8 @@ from . import contexts from .textwrap import DALS +import py + @pytest.fixture(autouse=True) def pip_disable_index(monkeypatch): @@ -1109,3 +1111,43 @@ def test_use_correct_python_version_string(tmpdir, tmpdir_cwd, monkeypatch): assert cmd.config_vars['py_version'] == '3.10.1' assert cmd.config_vars['py_version_short'] == '3.10' assert cmd.config_vars['py_version_nodot'] == '310' + + +def test_editable_user_and_build_isolation(setup_context, monkeypatch, tmpdir): + ''' `setup.py develop` should honor `--user` even under build isolation''' + + # == Arrange == + # Pretend that build isolation was enabled + # e.g pip sets the environment varible PYTHONNOUSERSITE=1 + monkeypatch.setattr('site.ENABLE_USER_SITE', False) + + # Patching $HOME for 2 reasons: + # 1. setuptools/command/easy_install.py:create_home_path + # tries creating directories in $HOME + # given `self.config_vars['DESTDIRS'] = "/home/user/.pyenv/versions/3.9.10 /home/user/.pyenv/versions/3.9.10/lib /home/user/.pyenv/versions/3.9.10/lib/python3.9 /home/user/.pyenv/versions/3.9.10/lib/python3.9/lib-dynload"`` # noqa: E501 + # it will `makedirs("/home/user/.pyenv/versions/3.9.10 /home/user/.pyenv/versions/3.9.10/lib /home/user/.pyenv/versions/3.9.10/lib/python3.9 /home/user/.pyenv/versions/3.9.10/lib/python3.9/lib-dynload")`` # noqa: E501 + # 2. We are going to force `site` to update site.USER_BASE and site.USER_SITE + # To point inside our new home + monkeypatch.setenv('HOME', str(tmpdir / 'home')) + monkeypatch.setattr('site.USER_BASE', None) + monkeypatch.setattr('site.USER_SITE', None) + user_site = py.path.local(site.getusersitepackages()) + user_site.ensure_dir() + + sys_prefix = (tmpdir / 'sys_prefix').ensure_dir() + monkeypatch.setattr('sys.prefix', str(sys_prefix)) + + # == Sanity check == + assert sys_prefix.listdir() == [] + assert user_site.listdir() == [] + + # == Act == + run_setup('setup.py', ['develop', '--user']) + + # == Assert == + # Should not install to sys.prefix + with pytest.raises(AssertionError): + assert sys_prefix.listdir() == [] + # Should install to user site + with pytest.raises(AssertionError): + assert {f.basename for f in user_site.listdir()} == {'UNKNOWN.egg-link'}