Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock PATH for problematic interpreter selection test in CI #474

Merged
merged 17 commits into from
May 14, 2018
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ dist: precise

# TRAVIS_PYTHON_VERSION

env:
global:
# This is necessary to have consistent testing of methods that rely on falling back to PATH for
# selecting a python interpreter.
- PATH=$PWD/.tox/py36/bin:$PWD/.tox/py27/bin:$PATH

cache:
directories:
- .pyenv_test
Expand Down
130 changes: 67 additions & 63 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,18 @@ def test_interpreter_constraints_to_pex_info_py2():
assert set(['>=2.7', '<3']) == set(pex_info.interpreter_constraints)


@pytest.mark.skip('https://github.com/pantsbuild/pex/issues/470')
def test_interpreter_constraints_to_pex_info_py3():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_pex_python in test_integration.py needs the same treatment as this test - both were disabled.

with temporary_dir() as output_dir:
# target python 3
pex_out_path = os.path.join(output_dir, 'pex_py3.pex')
res = run_pex_command(['--disable-cache',
'--interpreter-constraint=>3',
'-o', pex_out_path])
res.assert_success()
pex_info = get_pex_info(pex_out_path)
assert ['>3'] == pex_info.interpreter_constraints
py3_interpreter = ensure_python_interpreter('3.6.3')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than mocking out os.getenv here, how about just temporarily setting the PATH env var with twitter.common.contextutil.environment_as for the scope of this test?

with environment_as(PATH=os.path.dirname(py3_interpreter)):
with temporary_dir() as output_dir:
# target python 3
pex_out_path = os.path.join(output_dir, 'pex_py3.pex')
res = run_pex_command(['--disable-cache',
'--interpreter-constraint=>3',
'-o', pex_out_path])
res.assert_success()
pex_info = get_pex_info(pex_out_path)
assert ['>3'] == pex_info.interpreter_constraints


def test_interpreter_resolution_with_constraint_option():
Expand Down Expand Up @@ -497,61 +498,64 @@ def test_pex_exec_with_pex_python_path_and_pex_python_but_no_constraints():
assert str(pex_python_path.split(':')[0]).encode() in stdout


@pytest.mark.skip('https://github.com/pantsbuild/pex/issues/470')
def test_pex_python():
with temporary_dir() as td:
pexrc_path = os.path.join(td, '.pexrc')
with open(pexrc_path, 'w') as pexrc:
pex_python = ensure_python_interpreter('3.6.3')
pexrc.write("PEX_PYTHON=%s" % pex_python)

# test PEX_PYTHON with valid constraints
pex_out_path = os.path.join(td, 'pex.pex')
res = run_pex_command(['--disable-cache',
'--rcfile=%s' % pexrc_path,
'--interpreter-constraint=>3',
'--interpreter-constraint=<3.8',
'-o', pex_out_path])
res.assert_success()

stdin_payload = b'import sys; print(sys.executable); sys.exit(0)'
stdout, rc = run_simple_pex(pex_out_path, stdin=stdin_payload)
assert rc == 0
correct_interpreter_path = pex_python.encode()
assert correct_interpreter_path in stdout

# test PEX_PYTHON with incompatible constraints
pexrc_path = os.path.join(td, '.pexrc')
with open(pexrc_path, 'w') as pexrc:
pex_python = ensure_python_interpreter('2.7.10')
pexrc.write("PEX_PYTHON=%s" % pex_python)

pex_out_path = os.path.join(td, 'pex2.pex')
res = run_pex_command(['--disable-cache',
'--rcfile=%s' % pexrc_path,
'--interpreter-constraint=>3',
'--interpreter-constraint=<3.8',
'-o', pex_out_path])
res.assert_success()

stdin_payload = b'import sys; print(sys.executable); sys.exit(0)'
stdout, rc = run_simple_pex(pex_out_path, stdin=stdin_payload)
assert rc == 1
fail_str = 'not compatible with specified interpreter constraints'.encode()
assert fail_str in stdout

# test PEX_PYTHON with no constraints
pex_out_path = os.path.join(td, 'pex3.pex')
res = run_pex_command(['--disable-cache',
'--rcfile=%s' % pexrc_path,
'-o', pex_out_path])
res.assert_success()
py2_path_interpreter = ensure_python_interpreter('2.7.10')
py3_path_interpreter = ensure_python_interpreter('3.6.3')
path = ':'.join([os.path.dirname(py2_path_interpreter), os.path.dirname(py3_path_interpreter)])
with environment_as(PATH=path):
with temporary_dir() as td:
pexrc_path = os.path.join(td, '.pexrc')
with open(pexrc_path, 'w') as pexrc:
pex_python = ensure_python_interpreter('3.6.3')
pexrc.write("PEX_PYTHON=%s" % pex_python)

# test PEX_PYTHON with valid constraints
pex_out_path = os.path.join(td, 'pex.pex')
res = run_pex_command(['--disable-cache',
'--rcfile=%s' % pexrc_path,
'--interpreter-constraint=>3',
'--interpreter-constraint=<3.8',
'-o', pex_out_path])
res.assert_success()

stdin_payload = b'import sys; print(sys.executable); sys.exit(0)'
stdout, rc = run_simple_pex(pex_out_path, stdin=stdin_payload)
assert rc == 0
correct_interpreter_path = pex_python.encode()
assert correct_interpreter_path in stdout

stdin_payload = b'import sys; print(sys.executable); sys.exit(0)'
stdout, rc = run_simple_pex(pex_out_path, stdin=stdin_payload)
assert rc == 0
correct_interpreter_path = pex_python.encode()
assert correct_interpreter_path in stdout
# test PEX_PYTHON with incompatible constraints
pexrc_path = os.path.join(td, '.pexrc')
with open(pexrc_path, 'w') as pexrc:
pex_python = ensure_python_interpreter('2.7.10')
pexrc.write("PEX_PYTHON=%s" % pex_python)

pex_out_path = os.path.join(td, 'pex2.pex')
res = run_pex_command(['--disable-cache',
'--rcfile=%s' % pexrc_path,
'--interpreter-constraint=>3',
'--interpreter-constraint=<3.8',
'-o', pex_out_path])
res.assert_success()

stdin_payload = b'import sys; print(sys.executable); sys.exit(0)'
stdout, rc = run_simple_pex(pex_out_path, stdin=stdin_payload)
assert rc == 1
fail_str = 'not compatible with specified interpreter constraints'.encode()
assert fail_str in stdout

# test PEX_PYTHON with no constraints
pex_out_path = os.path.join(td, 'pex3.pex')
res = run_pex_command(['--disable-cache',
'--rcfile=%s' % pexrc_path,
'-o', pex_out_path])
res.assert_success()

stdin_payload = b'import sys; print(sys.executable); sys.exit(0)'
stdout, rc = run_simple_pex(pex_out_path, stdin=stdin_payload)
assert rc == 0
correct_interpreter_path = pex_python.encode()
assert correct_interpreter_path in stdout


def test_entry_point_targeting():
Expand Down