def test_pex_script(project_name, zip_safe): with built_wheel(name=project_name, zip_safe=zip_safe) as bdist_path: env_copy = os.environ.copy() env_copy["PEX_SCRIPT"] = "hello_world" so, rc = run_simple_pex_test("", env=env_copy) assert rc == 1, so.decode("utf-8") assert b"Could not find script 'hello_world'" in so so, rc = run_simple_pex_test("", env=env_copy, dists=[bdist_path]) assert rc == 0, so.decode("utf-8") assert b"hello world" in so env_copy["PEX_SCRIPT"] = "shell_script" so, rc = run_simple_pex_test("", env=env_copy, dists=[bdist_path]) assert rc == 1, so.decode("utf-8") assert b"Unable to parse" in so
def test_entry_point_exit_code(): setup_py = dedent(""" from setuptools import setup setup( name='my_app', version='0.0.0', zip_safe=True, packages=[''], entry_points={'console_scripts': ['my_app = my_app:do_something']}, ) """) error_msg = 'setuptools expects this to exit non-zero' my_app = dedent(""" def do_something(): return '%s' """ % error_msg) with temporary_content({ 'setup.py': setup_py, 'my_app.py': my_app }) as project_dir: installer = EggInstaller(project_dir) dist = DistributionHelper.distribution_from_path(installer.bdist()) so, rc = run_simple_pex_test('', env=make_env(PEX_SCRIPT='my_app'), dists=[dist]) assert so.decode('utf-8').strip() == error_msg assert rc == 1
def test_pex_script(project_name, zip_safe): with built_wheel(name=project_name, zip_safe=zip_safe) as bdist_path: env_copy = os.environ.copy() env_copy['PEX_SCRIPT'] = 'hello_world' so, rc = run_simple_pex_test('', env=env_copy) assert rc == 1, so.decode('utf-8') assert b"Could not find script 'hello_world'" in so so, rc = run_simple_pex_test('', env=env_copy, dists=[bdist_path]) assert rc == 0, so.decode('utf-8') assert b'hello world' in so env_copy['PEX_SCRIPT'] = 'shell_script' so, rc = run_simple_pex_test('', env=env_copy, dists=[bdist_path]) assert rc == 1, so.decode('utf-8') assert b'Unable to parse' in so
def test_entry_point_exit_code(): setup_py = dedent(""" from setuptools import setup setup( name='my_app', version='0.0.0', zip_safe=True, packages=[''], entry_points={'console_scripts': ['my_app = my_app:do_something']}, ) """) error_msg = 'setuptools expects this to exit non-zero' my_app = dedent(""" def do_something(): return '%s' """ % error_msg) with temporary_content({'setup.py': setup_py, 'my_app.py': my_app}) as project_dir: installer = EggInstaller(project_dir) dist = DistributionHelper.distribution_from_path(installer.bdist()) so, rc = run_simple_pex_test('', env={'PEX_SCRIPT': 'my_app'}, dists=[dist]) assert so.decode('utf-8').strip() == error_msg assert rc == 1
def test_pex_script(installer_impl, project_name): with make_installer(name=project_name, installer_impl=installer_impl) as installer: bdist = DistributionHelper.distribution_from_path(installer.bdist()) env_copy = os.environ.copy() env_copy['PEX_SCRIPT'] = 'hello_world' so, rc = run_simple_pex_test('', env=env_copy) assert rc == 1, so.decode('utf-8') assert b'Could not find' in so so, rc = run_simple_pex_test('', env=env_copy, dists=[bdist]) assert rc == 0, so.decode('utf-8') assert b'hello world' in so env_copy['PEX_SCRIPT'] = 'shell_script' so, rc = run_simple_pex_test('', env=env_copy, dists=[bdist]) assert rc == 1, so.decode('utf-8') assert b'Unable to parse' in so
def test_pex_script(installer_impl, project_name, zip_safe): kw = dict(name=project_name, installer_impl=installer_impl, zip_safe=zip_safe) with make_installer(**kw) as installer: bdist = DistributionHelper.distribution_from_path(installer.bdist()) env_copy = os.environ.copy() env_copy['PEX_SCRIPT'] = 'hello_world' so, rc = run_simple_pex_test('', env=env_copy) assert rc == 1, so.decode('utf-8') assert b'Could not find script hello_world' in so so, rc = run_simple_pex_test('', env=env_copy, dists=[bdist]) assert rc == 0, so.decode('utf-8') assert b'hello world' in so env_copy['PEX_SCRIPT'] = 'shell_script' so, rc = run_simple_pex_test('', env=env_copy, dists=[bdist]) assert rc == 1, so.decode('utf-8') assert b'Unable to parse' in so
def test_pex_atexit_swallowing(): body = textwrap.dedent(""" import atexit def raise_on_exit(): raise Exception('This is an exception') atexit.register(raise_on_exit) """) so, rc = run_simple_pex_test(body) assert so == b'' assert rc == 0 env_copy = os.environ.copy() env_copy.update(PEX_TEARDOWN_VERBOSE='1') so, rc = run_simple_pex_test(body, env=env_copy) assert b'This is an exception' in so assert rc == 0
def test_pex_interpreter(): with named_temporary_file() as fp: fp.write(b"print('Hello world')") fp.flush() env = make_env(PEX_INTERPRETER=1) so, rc = run_simple_pex_test("", args=(fp.name,), coverage=True, env=env) assert so == b'Hello world\n' assert rc == 0
def test_pex_python_symlink(): with temporary_dir() as td: symlink_path = os.path.join(td, 'python-symlink') os.symlink(sys.executable, symlink_path) pexrc_path = os.path.join(td, '.pexrc') with open(pexrc_path, 'w') as pexrc: pexrc.write("PEX_PYTHON=%s" % symlink_path) body = "print('Hello')" _, rc = run_simple_pex_test(body, coverage=True, env=make_env(HOME=td)) assert rc == 0
def test_pex_interpreter(): with named_temporary_file() as fp: fp.write(b"print('Hello world')") fp.flush() env = os.environ.copy() env['PEX_INTERPRETER'] = '1' so, rc = run_simple_pex_test("", args=(fp.name,), coverage=True, env=env) assert so == b'Hello world\n' assert rc == 0
def test_pex_python_symlink(): with temporary_dir() as td: with environment_as(HOME=td): symlink_path = os.path.join(td, 'python-symlink') os.symlink(sys.executable, symlink_path) pexrc_path = os.path.join(td, '.pexrc') with open(pexrc_path, 'w') as pexrc: pexrc.write("PEX_PYTHON=%s" % symlink_path) body = "print('Hello')" _, rc = run_simple_pex_test(body, coverage=True) assert rc == 0
def test_pex_interpreter(): with temporary_file() as fp: fp.write(b"print('Hello world')") fp.flush() env = os.environ.copy() env['PEX_INTERPRETER'] = '1' so, rc = run_simple_pex_test("", args=(fp.name, ), coverage=True, env=env) assert so == b'Hello world\n' assert rc == 0
def test_excepthook_honored(): body = textwrap.dedent(""" import sys def excepthook(ex_type, ex, tb): print('Custom hook called with: {0}'.format(ex)) sys.exit(42) sys.excepthook = excepthook raise Exception('This is an exception') """) so, rc = run_simple_pex_test(body) assert so == b'Custom hook called with: This is an exception\n', 'Standard out was: %s' % so assert rc == 42
def test_pex_sys_exit_does_not_raise(): body = "import sys; sys.exit(2)" so, rc = run_simple_pex_test(body) assert so == b'', 'Should not print SystemExit exception.' assert rc == 2
def test_pex_uncaught_exceptions(): body = "raise Exception('This is an exception')" so, rc = run_simple_pex_test(body) assert b'This is an exception' in so, 'Standard out was: %s' % so assert rc == 1
def test_pex_execute(): body = "print('Hello')" _, rc = run_simple_pex_test(body, coverage=True) assert rc == 0
def test_pex_raise(): body = "raise Exception('This will improve coverage.')" run_simple_pex_test(body, coverage=True)
def _test_sys_exit(arg, expected_output, expected_rc): body = "import sys; sys.exit({arg})".format(arg=arg) so, rc = run_simple_pex_test(body) assert so == expected_output, 'Should not print SystemExit traceback.' assert rc == expected_rc
def _test_sys_exit(arg, expected_output, expected_rc): # type: (Union[str, int], bytes, int) -> None body = "import sys; sys.exit({arg})".format(arg=arg) so, rc = run_simple_pex_test(body) assert so == expected_output, "Should not print SystemExit traceback." assert rc == expected_rc
def test_pex_uncaught_exceptions(): # type: () -> None body = "raise Exception('This is an exception')" so, rc = run_simple_pex_test(body) assert b"This is an exception" in so, "Standard out was: %r" % so assert rc == 1