Skip to content

Commit

Permalink
Remove PIP_REQUIRE_VIRTUALENV from environment when invoking pip
Browse files Browse the repository at this point in the history
Fixes pypa#90.
  • Loading branch information
adamchainz committed Sep 10, 2020
1 parent 10bf02e commit d1e9df0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
21 changes: 15 additions & 6 deletions src/build/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,18 @@ def install(self, requirements): # type: (Iterable[str]) -> None
with tempfile.NamedTemporaryFile('w+', prefix='build-reqs-', suffix='.txt', delete=False) as req_file:
req_file.write(os.linesep.join(requirements))
req_file.close()
cmd = [
sys.executable, '-m', 'pip', 'install', '--prefix',
self.path, '-r', os.path.abspath(req_file.name)
]
subprocess.check_call(cmd)
os.unlink(req_file.name)

try:
cmd = [
sys.executable, '-m', 'pip', 'install', '--prefix',
self.path, '-r', os.path.abspath(req_file.name)
]
env = os.environ.copy()
# Allow pip to operate without an active virtualenv
env.pop("PIP_REQUIRE_VIRTUALENV", None)

retcode = subprocess.Popen(cmd, env=env).wait()
if retcode != 0:
raise subprocess.CalledProcessError(retcode, cmd)
finally:
os.unlink(req_file.name)
24 changes: 20 additions & 4 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import build.env


def test_isolated_environment_setup(mocker):
def test_isolated_environment_setup():
old_path = os.environ['PATH']
with build.env.IsolatedEnvironment.for_current() as env:
if os.name != 'nt':
Expand Down Expand Up @@ -56,19 +56,35 @@ def test_isolated_environment_setup(mocker):
def test_isolated_environment_install(mocker):
with build.env.IsolatedEnvironment.for_current() as env:
mocker.patch('subprocess.check_call')
mocker.patch('subprocess.Popen')
subprocess.Popen.return_value.wait.return_value = 0

env.install([])
subprocess.check_call.assert_not_called()
subprocess.Popen.assert_not_called()

env.install(['some', 'requirements'])
if sys.version_info[:2] != (3, 5):
subprocess.check_call.assert_called()
args = subprocess.check_call.call_args[0][0]
subprocess.Popen.assert_called()
args = subprocess.Popen.call_args[0][0]
assert args[:7] == [
sys.executable, '-m', 'pip', 'install', '--prefix', env.path, '-r'
]


def test_isolated_environment_install_no_require_virtualenv(mocker):
with build.env.IsolatedEnvironment.for_current() as env:
mocker.patch('subprocess.check_call')
mocker.patch('subprocess.Popen')
subprocess.Popen.return_value.wait.return_value = 0
mocker.patch.dict(os.environ, {"PIP_REQUIRE_VIRTUALENV": "true"})

env.install(['some', 'requirements'])
if sys.version_info[:2] != (3, 5):
subprocess.Popen.assert_called()
env = subprocess.Popen.call_args[1]
assert "PIP_REQUIRE_VIRTUALENV" not in env


def test_uninitialised_isolated_environment():
env = build.env.IsolatedEnvironment.for_current()

Expand Down

0 comments on commit d1e9df0

Please sign in to comment.