def test_pants_test_interpreter_selection_with_pexrc(self): """Test the pants test goal with intepreters selected from a PEX_PYTHON_PATH defined in a pexrc file on disk. """ py27 = '2.7' py3 = '3' if self.has_python_version(py27) and self.has_python_version(py3): print('Found both python {} and python {}. Running test.'.format(py27, py3)) py27_path, py3_path = self.python_interpreter_path(py27), self.python_interpreter_path(py3) with setup_pexrc_with_pex_python_path(os.path.join(os.path.dirname(sys.argv[0]), '.pexrc'), [py27_path, py3_path]): with temporary_dir() as interpreters_cache: pants_ini_config = {'python-setup': {'interpreter_cache_dir': interpreters_cache}} pants_run_27 = self.run_pants( command=['test', '{}:test_py2'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_27) pants_run_3 = self.run_pants( command=['test', '{}:test_py3'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_3) else: print('Could not find both python {} and python {} on system. Skipping.'.format(py27, py3)) self.skipTest('Missing neccesary Python interpreters on system.')
def test_interpereter_cache_setup_using_pex_python_paths(self): """Test cache setup using interpreters from a mocked PEX_PYTHON_PATH.""" py27 = '2' py3 = '3' if PantsRunIntegrationTest.has_python_version(py27) and PantsRunIntegrationTest.has_python_version(py3): print('Found both python {} and python {}. Running test.'.format(py27, py3)) py27_path = PantsRunIntegrationTest.python_interpreter_path(py27) py3_path = PantsRunIntegrationTest.python_interpreter_path(py3) with setup_pexrc_with_pex_python_path('~/.pexrc', [py27_path, py3_path]): # Target python 2 for interpreter cache. with self._setup_test(constraints=['CPython>=2.7'], mock_setup_paths_interpreters=(py27_path, py3_path)) as (cache, cache_path): interpreters = cache.setup() # The majority of systems are able to satisfy the above constraint without needing to use setup paths, # so checking for presence of compatible interpreters is sufficient. Constraints are typcially met # by a system interpreter and as a result, the Pants venv interpreter from `py27_path` does not get # added to the interpreter cache. self.assertGreater(len(interpreters), 0) # Target python 3 for interpreter cache. with self._setup_test(constraints=['CPython>=3'], mock_setup_paths_interpreters=(py27_path, py3_path)) as (cache, cache_path): interpreters = cache.setup() self.assertGreater(len(interpreters), 0) # Travis CI does not support `python3.6` from the command line, so we just have to check # that the interpreter path of `py3_path` lines up with a cached interpreter. For this # case,`py3_path` is just missing '.6' at the end of the binary's path basename. # Ex: # p36_path = /path/to/python3 # pi.binary = /path/to/python3.6 assert any([py3_path in pi.binary for pi in interpreters]) else: print('Could not find both python {} and python {} on system. Skipping.'.format(py27, py3)) self.skipTest('Missing neccesary Python interpreters on system.')
def test_pants_test_interpreter_selection_with_pexrc(self): """Test the pants test goal with intepreters selected from a PEX_PYTHON_PATH defined in a pexrc file on disk. """ py27_path, py3_path = python_interpreter_path(PY_27), python_interpreter_path(PY_3) with setup_pexrc_with_pex_python_path([py27_path, py3_path]): with temporary_dir() as interpreters_cache: pants_ini_config = { 'python-setup': { 'interpreter_cache_dir': interpreters_cache, 'interpreter_search_paths': ['<PEXRC>'], } } pants_run_27 = self.run_pants( command=['test', '{}:test_py2'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_27) pants_run_3 = self.run_pants( command=['test', '{}:test_py3'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_3)
def test_pants_run_interpreter_selection_with_pexrc(self): py27 = '2.7' py3 = '3' if self.skip_if_no_python(py27) or self.skip_if_no_python(py3): return py27_path, py3_path = self.python_interpreter_path(py27), self.python_interpreter_path(py3) with setup_pexrc_with_pex_python_path(os.path.join(os.path.dirname(sys.argv[0]), '.pexrc'), [py27_path, py3_path]): with temporary_dir() as interpreters_cache: pants_ini_config = {'python-setup': {'interpreter_cache_dir': interpreters_cache}} pants_run_27 = self.run_pants( command=['run', '{}:main_py2'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_27) # Interpreter selection for Python 2 is problematic in CI due to multiple virtualenvs in play. if not os.getenv('CI'): self.assertIn(py27_path.split(py27)[0], pants_run_27.stdout_data) pants_run_3 = self.run_pants( command=['run', '{}:main_py3'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_3) # Protection for when the sys.executable path underlies a symlink pointing to 'python' without '3' # at the end of the basename. self.assertIn(py3_path.split(py3)[0], pants_run_3.stdout_data)
def test_pants_binary_interpreter_selection_with_pexrc(self): py27_path, py3_path = python_interpreter_path(PY_27), python_interpreter_path(PY_3) with setup_pexrc_with_pex_python_path([py27_path, py3_path]): with temporary_dir() as interpreters_cache: pants_ini_config = {'python-setup': {'interpreter_cache_dir': interpreters_cache}} pants_run_27 = self.run_pants( command=['binary', '{}:main_py2'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_27) pants_run_3 = self.run_pants( command=['binary', '{}:main_py3'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_3) # Ensure proper interpreter constraints were passed to built pexes. py2_pex = os.path.join(os.getcwd(), 'dist', 'main_py2.pex') py3_pex = os.path.join(os.getcwd(), 'dist', 'main_py3.pex') py2_info = PexInfo.from_pex(py2_pex) py3_info = PexInfo.from_pex(py3_pex) self.assertIn('CPython>2.7.6,<3', py2_info.interpreter_constraints) self.assertIn('CPython>3', py3_info.interpreter_constraints) # Cleanup created pexes. os.remove(py2_pex) os.remove(py3_pex)
def test_pants_test_interpreter_selection_with_pexrc(self): """Test the pants test goal with intepreters selected from a PEX_PYTHON_PATH defined in a pexrc file on disk. """ py27 = '2.7' py3 = '3' if self.has_python_version(py27) and self.has_python_version(py3): print('Found both python {} and python {}. Running test.'.format(py27, py3)) py27_path, py3_path = self.python_interpreter_path(py27), self.python_interpreter_path(py3) with setup_pexrc_with_pex_python_path(os.path.join(os.path.dirname(sys.argv[0]), '.pexrc'), [py27_path, py3_path]): with temporary_dir() as interpreters_cache: pants_ini_config = {'python-setup': {'interpreter_cache_dir': interpreters_cache}} pants_run_27 = self.run_pants( command=['test', '{}:test_py2'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_27) pants_run_3 = self.run_pants( command=['test', '{}:test_py3'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_3) else: print('Could not find both python {} and python {} on system. Skipping.'.format(py27, py3)) self.skipTest('Missing neccesary Python interpreters on system.')
def test_pex_python_paths(self): """Test pex python path helper method of PythonInterpreterCache.""" py27_path, py36_path = python_interpreter_path(PY_27), python_interpreter_path(PY_36) with setup_pexrc_with_pex_python_path([py27_path, py36_path]): with self._setup_cache() as (cache, _): pex_python_paths = cache.pex_python_paths() self.assertEqual(pex_python_paths, [py27_path, py36_path])
def test_pants_binary_interpreter_selection_with_pexrc(self): py27 = '2.7' py3 = '3' if self.skip_if_no_python(py27) or self.skip_if_no_python(py3): return py27_path, py3_path = self.python_interpreter_path(py27), self.python_interpreter_path(py3) with setup_pexrc_with_pex_python_path(os.path.join(os.path.dirname(sys.argv[0]), '.pexrc') , [py27_path, py3_path]): with temporary_dir() as interpreters_cache: pants_ini_config = {'python-setup': {'interpreter_cache_dir': interpreters_cache}} pants_run_27 = self.run_pants( command=['binary', '{}:main_py2'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_27) pants_run_3 = self.run_pants( command=['binary', '{}:main_py3'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_3) # Ensure proper interpreter constraints were passed to built pexes. py2_pex = os.path.join(os.getcwd(), 'dist', 'main_py2.pex') py3_pex = os.path.join(os.getcwd(), 'dist', 'main_py3.pex') py2_info = get_pex_info(py2_pex) py3_info = get_pex_info(py3_pex) self.assertIn('CPython>2.7.6,<3', py2_info.interpreter_constraints) self.assertIn('CPython>3', py3_info.interpreter_constraints) # Cleanup created pexes. os.remove(py2_pex) os.remove(py3_pex)
def test_pants_run_interpreter_selection_with_pexrc(self): py27_path, py3_path = python_interpreter_path( PY_27), python_interpreter_path(PY_3) with setup_pexrc_with_pex_python_path([py27_path, py3_path]): with temporary_dir() as interpreters_cache: pants_ini_config = { 'python-setup': { 'interpreter_cache_dir': interpreters_cache } } pants_run_27 = self.run_pants(command=[ 'run', '{}:main_py2'.format( os.path.join(self.testproject, 'python_3_selection_testing')) ], config=pants_ini_config) self.assert_success(pants_run_27) self.assertIn(py27_path, pants_run_27.stdout_data) pants_run_3 = self.run_pants(command=[ 'run', '{}:main_py3'.format( os.path.join(self.testproject, 'python_3_selection_testing')) ], config=pants_ini_config) self.assert_success(pants_run_3) self.assertIn(py3_path, pants_run_3.stdout_data)
def test_pants_test_interpreter_selection_with_pexrc(self): """Test the pants test goal with intepreters selected from a PEX_PYTHON_PATH defined in a pexrc file on disk. """ py27_path, py3_path = python_interpreter_path(PY_27), python_interpreter_path(PY_3) with setup_pexrc_with_pex_python_path([py27_path, py3_path]): with temporary_dir() as interpreters_cache: pants_ini_config = { 'python-setup': { 'interpreter_cache_dir': interpreters_cache, 'interpreter_search_paths': ['<PEXRC>'], } } pants_run_27 = self.run_pants( command=['test', '{}:test_py2'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_27) pants_run_3 = self.run_pants( command=['test', '{}:test_py3'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_3)
def test_interpereter_cache_setup_using_pex_python_paths(self): """Test cache setup using interpreters from a mocked PEX_PYTHON_PATH.""" py27_path, py36_path = python_interpreter_path(PY_27), python_interpreter_path(PY_36) with setup_pexrc_with_pex_python_path([py27_path, py36_path]): with self._setup_cache(constraints=['CPython>=2.7,<3']) as (cache, _): self.assertIn(py27_path, {pi.binary for pi in cache.setup()}) with self._setup_cache(constraints=['CPython>=3.6,<4']) as (cache, _): self.assertIn(py36_path, {pi.binary for pi in cache.setup()})
def test_interpereter_cache_setup_using_pex_python_paths(self): """Test cache setup using interpreters from a mocked PEX_PYTHON_PATH.""" py27_path, py36_path = python_interpreter_path(PY_27), python_interpreter_path(PY_36) with setup_pexrc_with_pex_python_path([py27_path, py36_path]): with self._setup_cache(constraints=['CPython>=2.7,<3'], search_paths=['<PEXRC>']) as (cache, _): self.assertIn(py27_path, {pi.binary for pi in cache.setup()}) with self._setup_cache(constraints=['CPython>=3.6,<4'], search_paths=['<PEXRC>']) as (cache, _): self.assertIn(py36_path, {pi.binary for pi in cache.setup()})
def test_expand_interpreter_search_paths(self): with environment_as(PATH='/env/path1:/env/path2'): with setup_pexrc_with_pex_python_path(['/pexrc/path1:/pexrc/path2']): with fake_pyenv_root(['2.7.14', '3.5.5']) as (pyenv_root, expected_pyenv_paths): paths = ['/foo', '<PATH>', '/bar', '<PEXRC>', '/baz', '<PYENV>', '/qux'] expanded_paths = PythonSetup.expand_interpreter_search_paths( paths, pyenv_root_func=lambda: pyenv_root) expected = ['/foo', '/env/path1', '/env/path2', '/bar', '/pexrc/path1', '/pexrc/path2', '/baz'] + expected_pyenv_paths + ['/qux'] self.assertListEqual(expected, expanded_paths)
def test_pex_python_paths(self): """Test pex python path helper method of PythonInterpreterCache.""" py27 = '2' py3 = '3' if PantsRunIntegrationTest.has_python_version(py27) and PantsRunIntegrationTest.has_python_version(py3): print('Found both python {} and python {}. Running test.'.format(py27, py3)) py27_path = PantsRunIntegrationTest.python_interpreter_path(py27) py3_path = PantsRunIntegrationTest.python_interpreter_path(py3) with self._setup_test() as (cache, cache_path): with setup_pexrc_with_pex_python_path('~/.pexrc', [py27_path, py3_path]): pex_python_paths = cache.pex_python_paths() self.assertEqual(pex_python_paths, [py27_path, py3_path])
def test_pex_python_paths(self): """Test pex python path helper method of PythonInterpreterCache.""" py27 = '2' py3 = '3' if PantsRunIntegrationTest.has_python_version( py27) and PantsRunIntegrationTest.has_python_version(py3): print('Found both python {} and python {}. Running test.'.format( py27, py3)) py27_path = PantsRunIntegrationTest.python_interpreter_path(py27) py3_path = PantsRunIntegrationTest.python_interpreter_path(py3) with self._setup_test() as (cache, cache_path): with setup_pexrc_with_pex_python_path('~/.pexrc', [py27_path, py3_path]): pex_python_paths = cache.pex_python_paths() self.assertEqual(pex_python_paths, [py27_path, py3_path])
def test_expand_interpreter_search_paths(self): with environment_as(PATH='/env/path1:/env/path2'): with setup_pexrc_with_pex_python_path( ['/pexrc/path1:/pexrc/path2']): with fake_pyenv_root(['2.7.14', '3.5.5' ]) as (pyenv_root, expected_pyenv_paths): paths = [ '/foo', '<PATH>', '/bar', '<PEXRC>', '/baz', '<PYENV>', '/qux' ] expanded_paths = PythonSetup.expand_interpreter_search_paths( paths, pyenv_root_func=lambda: pyenv_root) expected = [ '/foo', '/env/path1', '/env/path2', '/bar', '/pexrc/path1', '/pexrc/path2', '/baz' ] + expected_pyenv_paths + ['/qux'] self.assertListEqual(expected, expanded_paths)
def test_pants_run_interpreter_selection_with_pexrc(self): py27_path, py3_path = python_interpreter_path(PY_27), python_interpreter_path(PY_3) with setup_pexrc_with_pex_python_path([py27_path, py3_path]): with temporary_dir() as interpreters_cache: pants_ini_config = {'python-setup': {'interpreter_cache_dir': interpreters_cache}} pants_run_27 = self.run_pants( command=['run', '{}:main_py2'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_27) self.assertIn(py27_path, pants_run_27.stdout_data) pants_run_3 = self.run_pants( command=['run', '{}:main_py3'.format(os.path.join(self.testproject, 'python_3_selection_testing'))], config=pants_ini_config ) self.assert_success(pants_run_3) self.assertIn(py3_path, pants_run_3.stdout_data)
def test_interpereter_cache_setup_using_pex_python_paths(self): """Test cache setup using interpreters from a mocked PEX_PYTHON_PATH.""" py27 = '2' py3 = '3' if PantsRunIntegrationTest.has_python_version( py27) and PantsRunIntegrationTest.has_python_version(py3): print('Found both python {} and python {}. Running test.'.format( py27, py3)) py27_path = PantsRunIntegrationTest.python_interpreter_path(py27) py3_path = PantsRunIntegrationTest.python_interpreter_path(py3) with setup_pexrc_with_pex_python_path('~/.pexrc', [py27_path, py3_path]): # Target python 2 for interpreter cache. with self._setup_test(constraints=['CPython>=2.7'], mock_setup_paths_interpreters=( py27_path, py3_path)) as (cache, cache_path): interpreters = cache.setup() # The majority of systems are able to satisfy the above constraint without needing to use setup paths, # so checking for presence of compatible interpreters is sufficient. Constraints are typcially met # by a system interpreter and as a result, the Pants venv interpreter from `py27_path` does not get # added to the interpreter cache. self.assertGreater(len(interpreters), 0) # Target python 3 for interpreter cache. with self._setup_test(constraints=['CPython>=3'], mock_setup_paths_interpreters=( py27_path, py3_path)) as (cache, cache_path): interpreters = cache.setup() self.assertGreater(len(interpreters), 0) # Travis CI does not support `python3.6` from the command line, so we just have to check # that the interpreter path of `py3_path` lines up with a cached interpreter. For this # case,`py3_path` is just missing '.6' at the end of the binary's path basename. # Ex: # p36_path = /path/to/python3 # pi.binary = /path/to/python3.6 assert any([py3_path in pi.binary for pi in interpreters]) else: print( 'Could not find both python {} and python {} on system. Skipping.' .format(py27, py3)) self.skipTest('Missing neccesary Python interpreters on system.')
def test_pants_run_interpreter_selection_with_pexrc(self): py27 = '2.7' py3 = '3' if self.skip_if_no_python(py27) or self.skip_if_no_python(py3): return py27_path, py3_path = self.python_interpreter_path( py27), self.python_interpreter_path(py3) with setup_pexrc_with_pex_python_path( os.path.join(os.path.dirname(sys.argv[0]), '.pexrc'), [py27_path, py3_path]): with temporary_dir() as interpreters_cache: pants_ini_config = { 'python-setup': { 'interpreter_cache_dir': interpreters_cache } } pants_run_27 = self.run_pants(command=[ 'run', '{}:main_py2'.format( os.path.join(self.testproject, 'python_3_selection_testing')) ], config=pants_ini_config) self.assert_success(pants_run_27) # Interpreter selection for Python 2 is problematic in CI due to multiple virtualenvs in play. if not os.getenv('CI'): self.assertIn( py27_path.split(py27)[0], pants_run_27.stdout_data) pants_run_3 = self.run_pants(command=[ 'run', '{}:main_py3'.format( os.path.join(self.testproject, 'python_3_selection_testing')) ], config=pants_ini_config) self.assert_success(pants_run_3) # Protection for when the sys.executable path underlies a symlink pointing to 'python' without '3' # at the end of the basename. self.assertIn(py3_path.split(py3)[0], pants_run_3.stdout_data)
def test_get_pex_python_paths(self): with setup_pexrc_with_pex_python_path(['foo/bar', 'baz', '/qux/quux']): paths = PythonSetup.get_pex_python_paths() self.assertListEqual(['foo/bar', 'baz', '/qux/quux'], paths)
def test_get_pex_python_paths(self): with setup_pexrc_with_pex_python_path(['foo/bar', 'baz', '/qux/quux']): paths = PythonSetup.get_pex_python_paths() self.assertListEqual(['foo/bar', 'baz', '/qux/quux'], paths)