How to use the pex.testing.run_pex_command function in pex

To help you get started, we’ve selected a few pex examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pantsbuild / pex / tests / test_integration.py View on Github external
def do_resolve(req_name, req_version, platform, extra_flags=None):
      extra_flags = extra_flags or ''
      pex_path = os.path.join(output_dir, 'test.pex')
      results = run_pex_command(['--disable-cache',
                                 '--no-build',
                                 '%s==%s' % (req_name, req_version),
                                 '--platform=%s' % (platform),
                                 '-o', pex_path] + extra_flags.split())
      return pex_path, results
github pantsbuild / pex / tests / test_integration.py View on Github external
def test_pex_repl_built():
  """Tests the REPL in the context of a built pex."""
  stdin_payload = b'import requests; import sys; sys.exit(3)'

  with temporary_dir() as output_dir:
    # Create a temporary pex containing just `requests` with no entrypoint.
    pex_path = os.path.join(output_dir, 'requests.pex')
    results = run_pex_command(['--disable-cache', 'requests', '-o', pex_path])
    results.assert_success()

    # Test that the REPL is functional.
    stdout, rc = run_simple_pex(pex_path, stdin=stdin_payload)
    assert rc == 3
    assert b'>>>' in stdout
github pantsbuild / pex / tests / test_integration.py View on Github external
def test_interpreter_resolution_pex_python_path_precedence_over_pex_python():
  with temporary_dir() as td:
    pexrc_path = os.path.join(td, '.pexrc')
    with open(pexrc_path, 'w') as pexrc:
      # set both PPP and PP
      pex_python_path = ':'.join([
        ensure_python_interpreter(PY27),
        ensure_python_interpreter(PY36)
      ])
      pexrc.write("PEX_PYTHON_PATH=%s\n" % pex_python_path)
      pex_python = '/path/to/some/python'
      pexrc.write("PEX_PYTHON=%s" % pex_python)

    pex_out_path = os.path.join(td, 'pex.pex')
    res = run_pex_command(['--disable-cache',
      '--rcfile=%s' % pexrc_path,
      '--interpreter-constraint=>3,<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_path.split(':')[1].encode()
    assert correct_interpreter_path in stdout
github pantsbuild / pex / tests / test_integration.py View on Github external
def test_interpreter_constraints_honored_without_ppp_or_pp():
  # Create a pex with interpreter constraints, but for not the default interpreter in the path.
  with temporary_dir() as td:
    py36_path = ensure_python_interpreter(PY36)
    py35_path = ensure_python_interpreter(PY35)

    pex_out_path = os.path.join(td, 'pex.pex')
    env = make_env(
      PEX_IGNORE_RCFILES="1",
      PATH=os.pathsep.join([
        os.path.dirname(py35_path),
        os.path.dirname(py36_path),
      ])
    )
    res = run_pex_command(['--disable-cache',
      '--interpreter-constraint===%s' % PY36,
      '-o', pex_out_path],
      env=env
    )
    res.assert_success()

    # We want to try to run that pex with no environment variables set
    stdin_payload = b'import sys; print(sys.executable); sys.exit(0)'

    stdout, rc = run_simple_pex(pex_out_path, stdin=stdin_payload, env=env)
    assert rc == 0

    # If the constraints are honored, it will have run python3.6 and not python3.5
    # Without constraints, we would expect it to use python3.5 as it is the minimum interpreter
    # in the PATH.
    assert str(py36_path).encode() in stdout
github pantsbuild / pex / tests / test_integration.py View on Github external
test_file_path = os.path.join(td, 'build_and_run_child_pex.py')
            with open(test_file_path, 'w') as fh:
              fh.write(dedent("""
                import sys
                print(sys.executable)
                """))
            pex_out_path = os.path.join(td, 'child.pex')
            res = run_pex_command(['--disable-cache',
              '-o', pex_out_path])
            stdin_payload = b'import sys; print(sys.executable); sys.exit(0)'
            stdout, rc = run_simple_pex(pex_out_path, stdin=stdin_payload)
            print(stdout)
        '''.format(ensure_python_interpreter(child_pex_interpreter_version))))

    pex_out_path = os.path.join(td, 'parent.pex')
    res = run_pex_command(['--disable-cache',
      'pex',
      '{}'.format(td),
      '-e', 'testing:tester',
      '-o', pex_out_path])
    res.assert_success()

    stdout, rc = run_simple_pex(pex_out_path)
    assert rc == 0
    # Ensure that child pex used the proper interpreter as specified by its pexrc.
    correct_interpreter_path = ensure_python_interpreter(child_pex_interpreter_version)
    correct_interpreter_path = correct_interpreter_path.encode()  # Py 2/3 compatibility
    assert correct_interpreter_path in stdout
github pantsbuild / pex / tests / test_integration.py View on Github external
def test_pex_repl_cli():
  """Tests the REPL in the context of the pex cli itself."""
  stdin_payload = b'import sys; sys.exit(3)'

  with temporary_dir() as output_dir:
    # Create a temporary pex containing just `requests` with no entrypoint.
    pex_path = os.path.join(output_dir, 'pex.pex')
    results = run_pex_command(['--disable-cache',
                               'requests',
                               './',
                               '-e', 'pex.bin.pex:main',
                               '-o', pex_path])
    results.assert_success()

    # Test that the REPL is functional.
    stdout, rc = run_simple_pex(pex_path, stdin=stdin_payload)
    assert rc == 3
    assert b'>>>' in stdout
github pantsbuild / pex / tests / test_integration.py View on Github external
def test_pex_root():
  with nested(temporary_dir(), temporary_dir(), temporary_dir()) as (td, output_dir, tmp_home):
    output_path = os.path.join(output_dir, 'pex.pex')
    args = ['pex', '-o', output_path, '--not-zip-safe', '--pex-root={0}'.format(td)]
    results = run_pex_command(args=args, env=make_env(HOME=tmp_home, PEX_INTERPRETER='1'))
    results.assert_success()
    assert ['pex.pex'] == os.listdir(output_dir), 'Expected built pex file.'
    assert [] == os.listdir(tmp_home), 'Expected empty temp home dir.'
github pantsbuild / pex / tests / test_integration.py View on Github external
def pex_with_no_entrypoints():
  with temporary_dir() as out:
    pex = os.path.join(out, 'pex.pex')
    run_pex_command(['setuptools==36.2.7', '-o', pex])
    test_script = b'from setuptools.sandbox import run_setup; print(str(run_setup))'
    yield pex, test_script, out
github pantsbuild / pex / tests / test_integration.py View on Github external
def test_pex_re_exec_failure():
  with temporary_dir() as output_dir:

    # create 2 pex files for PEX_PATH
    pex1_path = os.path.join(output_dir, 'pex1.pex')
    res1 = run_pex_command(['--disable-cache', 'requests', '-o', pex1_path])
    res1.assert_success()
    pex2_path = os.path.join(output_dir, 'pex2.pex')
    res2 = run_pex_command(['--disable-cache', 'flask', '-o', pex2_path])
    res2.assert_success()
    pex_path = ':'.join(os.path.join(output_dir, name) for name in ('pex1.pex', 'pex2.pex'))

    # create test file test.py that attmepts to import modules from pex1/pex2
    test_file_path = os.path.join(output_dir, 'test.py')
    with open(test_file_path, 'w') as fh:
      fh.write(dedent('''
        import requests
        import flask
        import sys
        import os
        import subprocess
        if 'RAN_ONCE' in os.environ::
github pantsbuild / pex / tests / test_integration.py View on Github external
run_pex_command(['six', '-o', six_pex]).assert_success()

    src_dir = os.path.join(td, 'src')
    os.mkdir(src_dir)

    src_file = os.path.join(src_dir, 'execute_import.py')
    with open(src_file, 'w') as fp:
      fp.write(dedent("""\
        import pkg_resources
        import sys

        pkg_resources.get_distribution('six')
        """))

    setuptools_pex = os.path.join(td, 'autopep8.pex')
    run_pex_command(['autopep8', 'setuptools', '-D', src_dir, '--entry-point',
      'execute_import', '-o', setuptools_pex]).assert_success()
    _, return_code = run_simple_pex(setuptools_pex, env=make_env(PEX_PATH=six_pex))
    assert return_code == 0