Skip to content

Commit bab0ea7

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 d5402d3 commit bab0ea7

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

.tox_helpers/runpip.py

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