Skip to content

Commit

Permalink
Convert bdist_pex tests to explicit cmdclass. (#897)
Browse files Browse the repository at this point in the history
Previously the test relied upon the tox venv interpreter (which
implicitly builds and installs the pex wheel) being the interpreter that
executed the `python setup.py bdist_pex ...` command. This assumption
could be violated when running under `pyenv` which OSX CI shards do.
Instead, explicitly add pex to the `PYTHONPATH` of the interpreter and
let distutils know about `bdist_pex` via `--command-packages`.

Fixes #896
  • Loading branch information
jsirois committed Feb 20, 2020
1 parent eebb8dd commit 97849d4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
17 changes: 14 additions & 3 deletions pex/interpreter.py
Expand Up @@ -549,7 +549,14 @@ def __repr__(self):
)


def spawn_python_job(args, env=None, interpreter=None, expose=None, **subprocess_kwargs):
def spawn_python_job(
args,
env=None,
interpreter=None,
expose=None,
pythonpath=None,
**subprocess_kwargs
):
"""Spawns a python job.
:param args: The arguments to pass to the python interpreter.
Expand All @@ -561,21 +568,25 @@ def spawn_python_job(args, env=None, interpreter=None, expose=None, **subprocess
interpreter.
:type interpreter: :class:`PythonInterpreter`
:param expose: The names of any vendored distributions to expose to the spawned python process.
These will be appended to `pythonpath` if passed.
:type expose: list of str
:param pythonpath: The PYTHONPATH to expose to the spawned python process. These will be
pre-pended to the `expose` path if passed.
:type pythonpath: list of str
:param subprocess_kwargs: Any additional :class:`subprocess.Popen` kwargs to pass through.
:returns: A job handle to the spawned python process.
:rtype: :class:`Job`
"""
pythonpath = list(pythonpath or ())
if expose:
subprocess_env = (env or os.environ).copy()
# In order to expose vendored distributions with their un-vendored import paths in-tact, we
# need to set `__PEX_UNVENDORED__`. See: vendor.__main__.ImportRewriter._modify_import.
subprocess_env['__PEX_UNVENDORED__'] = '1'

pythonpath = third_party.expose(expose)
pythonpath.extend(third_party.expose(expose))
else:
subprocess_env = env
pythonpath = None

interpreter = interpreter or PythonInterpreter.get()
cmd, process = interpreter.open_process(
Expand Down
12 changes: 10 additions & 2 deletions tests/test_bdist_pex.py
Expand Up @@ -11,14 +11,22 @@
from pex.testing import WheelBuilder, make_project, temporary_content


def pex_project_dir():
return subprocess.check_output(['git', 'rev-parse', '--show-toplevel']).decode('utf-8').strip()


@contextmanager
def bdist_pex(project_dir, bdist_args=None):
with temporary_dir() as dist_dir:
cmd = ['setup.py', 'bdist_pex', '--bdist-dir={}'.format(dist_dir)]
cmd = [
'setup.py',
'--command-packages', 'pex.commands',
'bdist_pex', '--bdist-dir={}'.format(dist_dir)
]
if bdist_args:
cmd.extend(bdist_args)

spawn_python_job(args=cmd, cwd=project_dir).wait()
spawn_python_job(args=cmd, cwd=project_dir, pythonpath=[pex_project_dir()]).wait()
dists = os.listdir(dist_dir)
assert len(dists) == 1
yield os.path.join(dist_dir, dists[0])
Expand Down

0 comments on commit 97849d4

Please sign in to comment.