def test_get_sandbox_python_path(self, mock_get_python_lib): # No inheritance python_path = get_sandbox_python_path(inherit_from_parent=False, inherit_parent_virtualenv=False) self.assertEqual(python_path, ':') # Inherit python path from current process # Mock the current process python path os.environ['PYTHONPATH'] = ':/data/test1:/data/test2' python_path = get_sandbox_python_path(inherit_from_parent=True, inherit_parent_virtualenv=False) self.assertEqual(python_path, ':/data/test1:/data/test2') # Inherit from current process and from virtualenv (not running inside virtualenv) clear_virtualenv_prefix() python_path = get_sandbox_python_path(inherit_from_parent=True, inherit_parent_virtualenv=False) self.assertEqual(python_path, ':/data/test1:/data/test2') # Inherit from current process and from virtualenv (running inside virtualenv) sys.real_prefix = '/usr' mock_get_python_lib.return_value = sys.prefix + '/virtualenvtest' python_path = get_sandbox_python_path(inherit_from_parent=True, inherit_parent_virtualenv=True) self.assertEqual(python_path, ':/data/test1:/data/test2:%s/virtualenvtest' % (sys.prefix))
def test_get_sandbox_python_path(self, mock_get_python_lib): # No inheritance python_path = get_sandbox_python_path(inherit_from_parent=False, inherit_parent_virtualenv=False) self.assertEqual(python_path, ":") # Inherit python path from current process # Mock the current process python path with mock.patch.dict(os.environ, {"PYTHONPATH": ":/data/test1:/data/test2"}): python_path = get_sandbox_python_path( inherit_from_parent=True, inherit_parent_virtualenv=False) self.assertEqual(python_path, ":/data/test1:/data/test2") # Inherit from current process and from virtualenv (not running inside virtualenv) clear_virtualenv_prefix() with mock.patch.dict(os.environ, {"PYTHONPATH": ":/data/test1:/data/test2"}): python_path = get_sandbox_python_path( inherit_from_parent=True, inherit_parent_virtualenv=False) self.assertEqual(python_path, ":/data/test1:/data/test2") # Inherit from current process and from virtualenv (running inside virtualenv) sys.real_prefix = "/usr" mock_get_python_lib.return_value = f"{sys.prefix}/virtualenvtest" with mock.patch.dict(os.environ, {"PYTHONPATH": ":/data/test1:/data/test2"}): python_path = get_sandbox_python_path( inherit_from_parent=True, inherit_parent_virtualenv=True) self.assertEqual( python_path, f":/data/test1:/data/test2:{sys.prefix}/virtualenvtest")
def test_get_sandbox_python_path_for_python_action_inherit_from_parent_process_and_venv( self, mock_get_python_lib): # Inherit from current process and from virtualenv (not running inside virtualenv) clear_virtualenv_prefix() # Inherit python path from current process # Mock the current process python path with mock.patch.dict(os.environ, {"PYTHONPATH": ":/data/test1:/data/test2"}): python_path = get_sandbox_python_path( inherit_from_parent=True, inherit_parent_virtualenv=False) self.assertEqual(python_path, ":/data/test1:/data/test2") python_path = get_sandbox_python_path_for_python_action( pack="dummy_pack", inherit_from_parent=True, inherit_parent_virtualenv=True, ) actual_path = python_path.strip(":").split(":") self.assertEqual(len(actual_path), 6) # First entry should be lib/python3 dir from venv self.assertEndsWith(actual_path[0], "virtualenvs/dummy_pack/lib/python3.6") # Second entry should be python3 site-packages dir from venv self.assertEndsWith( actual_path[1], "virtualenvs/dummy_pack/lib/python3.6/site-packages") # Third entry should be actions/lib dir from pack root directory self.assertEndsWith(actual_path[2], "packs/dummy_pack/actions/lib") # And the rest of the paths from get_sandbox_python_path self.assertEqual(actual_path[3], "") self.assertEqual(actual_path[4], "/data/test1") self.assertEqual(actual_path[5], "/data/test2") # Inherit from current process and from virtualenv (running inside virtualenv) sys.real_prefix = "/usr" mock_get_python_lib.return_value = f"{sys.prefix}/virtualenvtest" # Inherit python path from current process # Mock the current process python path with mock.patch.dict(os.environ, {"PYTHONPATH": ":/data/test1:/data/test2"}): python_path = get_sandbox_python_path_for_python_action( pack="dummy_pack", inherit_from_parent=True, inherit_parent_virtualenv=True, ) actual_path = python_path.strip(":").split(":") self.assertEqual(len(actual_path), 7) # First entry should be lib/python3 dir from venv self.assertEndsWith(actual_path[0], "virtualenvs/dummy_pack/lib/python3.6") # Second entry should be python3 site-packages dir from venv self.assertEndsWith( actual_path[1], "virtualenvs/dummy_pack/lib/python3.6/site-packages") # Third entry should be actions/lib dir from pack root directory self.assertEndsWith(actual_path[2], "packs/dummy_pack/actions/lib") # The paths from get_sandbox_python_path self.assertEqual(actual_path[3], "") self.assertEqual(actual_path[4], "/data/test1") self.assertEqual(actual_path[5], "/data/test2") # And the parent virtualenv self.assertEqual(actual_path[6], f"{sys.prefix}/virtualenvtest")