Skip to content

Commit 5f416b1

Browse files
authored
Merge pull request #164 from astrofrog/virtualenv-notox
Run tests in clean virtual environment [version without tox]
2 parents 3302aa1 + 2a24461 commit 5f416b1

File tree

3 files changed

+85
-26
lines changed

3 files changed

+85
-26
lines changed

cibuildwheel/linux.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,36 @@ def build(project_dir, output_dir, test_command, test_requires, test_extras, bef
8585
fi
8686
delocated_wheel=(/tmp/delocated_wheel/*.whl)
8787
88-
# Install the wheel we just built
89-
"$PYBIN/pip" install "$delocated_wheel"{test_extras}
90-
91-
# Install any requirements to run the tests
92-
if [ ! -z "{test_requires}" ]; then
93-
"$PYBIN/pip" install {test_requires}
94-
fi
95-
96-
# Run the tests from a different directory
9788
if [ ! -z {test_command} ]; then
98-
pushd $HOME
99-
PATH="$PYBIN:$PATH" sh -c {test_command}
100-
popd
89+
# Set up a virtual environment to install and test from, to make sure
90+
# there are no dependencies that were pulled in at build time.
91+
"$PYBIN/pip" install virtualenv
92+
venv_dir=`mktemp -d`/venv
93+
"$PYBIN/python" -m virtualenv "$venv_dir"
94+
95+
# run the tests in a subshell to keep that `activate`
96+
# script from polluting the env
97+
(
98+
source "$venv_dir/bin/activate"
99+
100+
echo "Running tests using `which python`"
101+
102+
# Install the wheel we just built
103+
pip install "$delocated_wheel"{test_extras}
104+
105+
# Install any requirements to run the tests
106+
if [ ! -z "{test_requires}" ]; then
107+
pip install {test_requires}
108+
fi
109+
110+
# Run the tests from a different directory
111+
pushd $HOME
112+
sh -c {test_command}
113+
popd
114+
)
115+
116+
# clean up
117+
rm -rf "$venv_dir"
101118
fi
102119
103120
# we're all done here; move it to output

cibuildwheel/macos.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import print_function
2+
import tempfile
23
import os, subprocess, shlex, sys, shutil
34
from collections import namedtuple
45
from glob import glob
@@ -120,18 +121,40 @@ def call(args, env=None, cwd=None, shell=False):
120121
call(['delocate-wheel', '-w', '/tmp/delocated_wheel', built_wheel], env=env)
121122
delocated_wheel = glob('/tmp/delocated_wheel/*.whl')[0]
122123

123-
# install the wheel
124-
call(['pip', 'install', delocated_wheel + test_extras], env=env)
125-
126-
# test the wheel
127-
if test_requires:
128-
call(['pip', 'install'] + test_requires, env=env)
129124
if test_command:
125+
# set up a virtual environment to install and test from, to make sure
126+
# there are no dependencies that were pulled in at build time.
127+
call(['pip', 'install', 'virtualenv'], env=env)
128+
venv_dir = tempfile.mkdtemp()
129+
call(['python', '-m', 'virtualenv', venv_dir], env=env)
130+
131+
virtualenv_env = env.copy()
132+
virtualenv_env['PATH'] = os.pathsep.join([
133+
os.path.join(venv_dir, 'bin'),
134+
virtualenv_env['PATH'],
135+
])
136+
# Fix some weird issue with the shebang of installed scripts
137+
# See https://github.com/theacodes/nox/issues/44 and https://github.com/pypa/virtualenv/issues/620
138+
virtualenv_env.pop('__PYVENV_LAUNCHER__', None)
139+
140+
# check that we are using the Python from the virtual environment
141+
call(['which', 'python'], env=virtualenv_env)
142+
143+
# install the wheel
144+
call(['pip', 'install', delocated_wheel + test_extras], env=virtualenv_env)
145+
146+
# test the wheel
147+
if test_requires:
148+
call(['pip', 'install'] + test_requires, env=virtualenv_env)
149+
130150
# run the tests from $HOME, with an absolute path in the command
131151
# (this ensures that Python runs the tests against the installed wheel
132152
# and not the repo code)
133153
test_command_prepared = prepare_command(test_command, project=abs_project_dir)
134-
call(test_command_prepared, cwd=os.environ['HOME'], env=env, shell=True)
154+
call(test_command_prepared, cwd=os.environ['HOME'], env=virtualenv_env, shell=True)
155+
156+
# clean up
157+
shutil.rmtree(venv_dir)
135158

136159
# we're all done here; move it to output (overwrite existing)
137160
dst = os.path.join(output_dir, os.path.basename(delocated_wheel))

cibuildwheel/windows.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,37 @@ def shell(args, env=None, cwd=None):
121121
shell(['pip', 'wheel', abs_project_dir, '-w', built_wheel_dir, '--no-deps'] + get_build_verbosity_extra_flags(build_verbosity), env=env)
122122
built_wheel = glob(built_wheel_dir+'/*.whl')[0]
123123

124-
# install the wheel
125-
shell(['pip', 'install', built_wheel + test_extras], env=env)
126-
127-
# test the wheel
128-
if test_requires:
129-
shell(['pip', 'install'] + test_requires, env=env)
130124
if test_command:
125+
# set up a virtual environment to install and test from, to make sure
126+
# there are no dependencies that were pulled in at build time.
127+
shell(['pip', 'install', 'virtualenv'], env=env)
128+
venv_dir = tempfile.mkdtemp()
129+
shell(['python', '-m', 'virtualenv', venv_dir], env=env)
130+
131+
virtualenv_env = env.copy()
132+
virtualenv_env['PATH'] = os.pathsep.join([
133+
os.path.join(venv_dir, 'Scripts'),
134+
virtualenv_env['PATH'],
135+
])
136+
137+
# check that we are using the Python from the virtual environment
138+
shell(['which', 'python'], env=virtualenv_env)
139+
140+
# install the wheel
141+
shell(['pip', 'install', built_wheel + test_extras], env=virtualenv_env)
142+
143+
# test the wheel
144+
if test_requires:
145+
shell(['pip', 'install'] + test_requires, env=virtualenv_env)
146+
131147
# run the tests from c:\, with an absolute path in the command
132148
# (this ensures that Python runs the tests against the installed wheel
133149
# and not the repo code)
134150
test_command_prepared = prepare_command(test_command, project=abs_project_dir)
135-
shell([test_command_prepared], cwd='c:\\', env=env)
151+
shell([test_command_prepared], cwd='c:\\', env=virtualenv_env)
152+
153+
# clean up
154+
shutil.rmtree(venv_dir)
136155

137156
# we're all done here; move it to output (remove if already exists)
138157
dst = os.path.join(output_dir, os.path.basename(built_wheel))

0 commit comments

Comments
 (0)