Skip to content

Commit a514354

Browse files
committed
tox: fix environment setup
Use the virtualenv's pip for installing dependencies, not the code that is about to be tested. commit 922f9a3f6dc4ac642492204808b4d5cc995aca66 Author: Benoit Pierre <[email protected]> Date: Wed Aug 9 02:47:39 2017 +0200 tox/travis: fix and rework handling of un-vendored jobs
1 parent a9d56c7 commit a514354

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

.tox_helpers/runpip.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from glob import glob
2+
import distutils.sysconfig
3+
import os
4+
import shutil
5+
import subprocess
6+
import sys
7+
import textwrap
8+
9+
10+
VIRTUAL_ENV = os.environ['VIRTUAL_ENV']
11+
TOX_PIP_DIR = os.path.join(VIRTUAL_ENV, 'pip')
12+
SITE_PACKAGES = distutils.sysconfig.get_python_lib()
13+
14+
15+
def pip(args):
16+
# First things first, safeguard the environment
17+
# original pip so it can be used for all calls.
18+
if not os.path.exists(TOX_PIP_DIR):
19+
os.mkdir(TOX_PIP_DIR)
20+
# Remove executable/launchers.
21+
for entry in glob(os.path.join(VIRTUAL_ENV, 'bin', 'pip*')):
22+
os.unlink(entry)
23+
# Relocate package and distribution info.
24+
for src in (
25+
os.path.join(SITE_PACKAGES, 'pip'),
26+
glob(os.path.join(SITE_PACKAGES, 'pip-*.dist-info'))[0],
27+
):
28+
shutil.move(src, TOX_PIP_DIR)
29+
# Create a very simple launcher that
30+
# can be used for Linux and Windows.
31+
with open(os.path.join(TOX_PIP_DIR, 'pip.py'), 'w') as fp:
32+
fp.write(textwrap.dedent(
33+
'''
34+
import sys
35+
from pip._vendor import pkg_resources
36+
sys.exit(pkg_resources.load_entry_point(
37+
'pip', 'console_scripts', 'pip'
38+
)())
39+
'''
40+
).lstrip())
41+
# And use a temporary copy of that version
42+
# so it can uninstall itself if needed.
43+
temp_pip = TOX_PIP_DIR + '.tmp'
44+
try:
45+
shutil.copytree(TOX_PIP_DIR, temp_pip)
46+
cmd = [sys.executable, os.path.join(temp_pip, 'pip.py')]
47+
cmd.extend(args)
48+
subprocess.check_call(cmd)
49+
finally:
50+
shutil.rmtree(temp_pip)
51+
52+
53+
if __name__ == '__main__':
54+
pip(sys.argv[1:])

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ recursive-include src/pip/_vendor *.pem
1919
recursive-include docs Makefile *.rst *.py *.bat
2020

2121
prune .github
22+
prune .tox_helpers
2223
prune .travis
2324
prune docs/_build
2425
prune news

tox.ini

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ envlist =
33
docs, packaging, lint-py2, lint-py3,
44
py27, py33, py34, py35, py36, py37, pypy
55

6+
[helpers]
7+
# Wrapper for calls to pip that make sure the version being used is the
8+
# original virtualenv (stable) version, and not the code being tested.
9+
pip = python {toxinidir}/.tox_helpers/runpip.py
10+
611
[testenv]
712
setenv =
813
# This is required in order to get UTF-8 output inside of the subprocesses
914
# that our tests use.
1015
LC_CTYPE = en_US.UTF-8
1116
deps = -r{toxinidir}/dev-requirements.txt
1217
commands = py.test --timeout 300 []
13-
install_command = python -m pip install {opts} {packages}
18+
install_command = {[helpers]pip} install {opts} {packages}
19+
list_dependencies_command = {[helpers]pip} freeze
1420

1521
[testenv:docs]
1622
deps = sphinx == 1.6.1

0 commit comments

Comments
 (0)