def test_pex_paths(): # Tests that PEX_PATH allows importing sources from the referenced pex. with named_temporary_file() as fake_stdout: with temporary_dir() as temp_dir: pex1_path = os.path.join(temp_dir, "pex1") write_simple_pex( pex1_path, sources=[ ("foo_pkg/__init__.py", ""), ("foo_pkg/foo_module.py", 'def foo_func():\n return "42"'), ], ) pex2_path = os.path.join(temp_dir, "pex2") pex2 = write_simple_pex( pex2_path, "import sys; from bar_pkg.bar_module import bar_func; " "sys.stdout.write(bar_func()); sys.exit(0)", sources=[ ("bar_pkg/__init__.py", ""), ( "bar_pkg/bar_module.py", "from foo_pkg.foo_module import foo_func\ndef bar_func():\n return foo_func()", ), ], ) rc = PEX(pex2.path()).run(stdin=None, stdout=fake_stdout, env={"PEX_PATH": pex1_path}) assert rc == 0 fake_stdout.seek(0) assert fake_stdout.read() == b"42"
def test_pex_paths(): # Tests that PEX_PATH allows importing sources from the referenced pex. with named_temporary_file() as fake_stdout: with temporary_dir() as temp_dir: pex1_path = os.path.join(temp_dir, 'pex1') write_simple_pex(pex1_path, sources=[('foo_pkg/__init__.py', ''), ('foo_pkg/foo_module.py', 'def foo_func():\n return "42"')]) pex2_path = os.path.join(temp_dir, 'pex2') pex2 = write_simple_pex( pex2_path, 'import sys; from bar_pkg.bar_module import bar_func; ' 'sys.stdout.write(bar_func()); sys.exit(0)', sources= [('bar_pkg/__init__.py', ''), ('bar_pkg/bar_module.py', 'from foo_pkg.foo_module import foo_func\ndef bar_func():\n return foo_func()' )]) rc = PEX(pex2.path()).run(stdin=None, stdout=fake_stdout, env={'PEX_PATH': pex1_path}) assert rc == 0 fake_stdout.seek(0) assert fake_stdout.read() == b'42'
def test_pex_paths(): # Tests that PEX_PATH allows importing sources from the referenced pex. with named_temporary_file() as fake_stdout: with temporary_dir() as temp_dir: pex1_path = os.path.join(temp_dir, 'pex1') write_simple_pex( pex1_path, exe_contents='', sources=[ ('foo_pkg/__init__.py', ''), ('foo_pkg/foo_module.py', 'def foo_func():\n return "42"') ] ) pex2_path = os.path.join(temp_dir, 'pex2') pex2 = write_simple_pex( pex2_path, 'import sys; from bar_pkg.bar_module import bar_func; ' 'sys.stdout.write(bar_func()); sys.exit(0)', sources=[ ('bar_pkg/bar_module.py', 'from foo_pkg.foo_module import foo_func\ndef bar_func():\n return foo_func()') ] ) rc = PEX(pex2.path()).run(stdin=None, stdout=fake_stdout, env={'PEX_PATH': pex1_path}) assert rc == 0 fake_stdout.seek(0) assert fake_stdout.read() == b'42'
def test_pex_run(): with named_temporary_file() as fake_stdout: with temporary_dir() as temp_dir: pex = write_simple_pex( temp_dir, 'import sys; sys.stdout.write("hello"); sys.stderr.write("hello"); sys.exit(0)' ) rc = PEX(pex.path()).run(stdin=None, stdout=fake_stdout, stderr=fake_stdout) assert rc == 0 fake_stdout.seek(0) assert fake_stdout.read() == b'hellohello'
def test_pex_run_extra_sys_path(): with named_temporary_file() as fake_stdout: with temporary_dir() as temp_dir: pex = write_simple_pex( temp_dir, 'import sys; sys.stdout.write(":".join(sys.path)); sys.exit(0)' ) rc = PEX(pex.path()).run( stdin=None, stdout=fake_stdout, stderr=None, env={ 'PEX_EXTRA_SYS_PATH': 'extra/syspath/entry1:extra/syspath/entry2' }) assert rc == 0 fake_stdout.seek(0) syspath = fake_stdout.read().split(b':') assert b'extra/syspath/entry1' in syspath assert b'extra/syspath/entry2' in syspath
def assert_isolation(self, inherit_path, expected_output): env = dict(PYTHONPATH=self.pythonpath) with named_temporary_file() as fake_stdout: with temporary_dir() as temp_dir: pex_builder = write_simple_pex( temp_dir, pex_info=self.pex_info(inherit_path), dists=self.dists, exe_contents=self.exe, ) # Test the PEX.run API. rc = PEX(pex_builder.path()).run(stdout=fake_stdout, env=env) assert rc == 0 fake_stdout.seek(0) assert expected_output == fake_stdout.read().decode('utf-8') # Test direct PEX execution. assert expected_output == subprocess.check_output([sys.executable, pex_builder.path()], env=env).decode('utf-8')