def test_with_extra_cppflags(self): """Verify that existing CPPFLAGS are preserved""" expected_python = self._create_python_binary('install_dir') self.useFixture( fixtures.EnvironmentVariable('CPPFLAGS', '-I/opt/include')) _pip.Pip(python_major_version='test', part_dir='part_dir', install_dir='install_dir', stage_dir='stage_dir') class check_env(): def __init__(self, test): self.test = test def __eq__(self, env): self.test.assertThat(env, Contains('CPPFLAGS')) self.test.assertThat(env['CPPFLAGS'], Contains('-I/opt/include')) return True self.mock_run_output.assert_has_calls([ mock.call([expected_python, '-m', 'pip'], env=check_env(self), stderr=subprocess.STDOUT) ])
def test_with_extra_cppflags(self): """Verify that existing CPPFLAGS are preserved""" expected_python = self._create_python_binary("install_dir") self.useFixture( fixtures.EnvironmentVariable("CPPFLAGS", "-I/opt/include")) _pip.Pip( python_major_version="test", part_dir="part_dir", install_dir="install_dir", stage_dir="stage_dir", ).setup() class check_env: def __init__(self, test): self.test = test def __eq__(self, env): self.test.assertThat(env, Contains("CPPFLAGS")) self.test.assertThat(env["CPPFLAGS"], Contains("-I/opt/include")) return True self.mock_run_output.assert_has_calls([ mock.call( [expected_python, "-m", "pip"], env=check_env(self), stderr=subprocess.STDOUT, ) ])
def setUp(self): super().setUp() self.pip = _pip.Pip(python_major_version='test', part_dir='part_dir', install_dir='install_dir', stage_dir='stage_dir') # We don't care about anything init did to the mock here: reset it self.mock_run.reset_mock()
def _assert_expected_enviroment(self, expected_python, headers_path): _pip.Pip( python_major_version="test", part_dir="part_dir", install_dir="install_dir", stage_dir="stage_dir", ).setup() class check_env: def __init__(self, test): self.test = test def __eq__(self, env): self.test.assertThat(env, Contains("PYTHONUSERBASE")) self.test.assertThat(env["PYTHONUSERBASE"], Equals("install_dir")) self.test.assertThat(env, Contains("PYTHONHOME")) if expected_python.startswith("install_dir"): self.test.assertThat( env["PYTHONHOME"], Equals(os.path.join("install_dir", "usr"))) else: self.test.assertThat( env["PYTHONHOME"], Equals(os.path.join("stage_dir", "usr"))) self.test.assertThat(env, Contains("PATH")) self.test.assertThat( env["PATH"], Contains(os.path.join("install_dir", "usr", "bin"))) if headers_path: self.test.assertThat(env, Contains("CPPFLAGS")) self.test.assertThat(env["CPPFLAGS"], Contains("-I{}".format(headers_path))) return True self.mock_run_output.assert_has_calls([ mock.call( [expected_python, "-m", "pip"], stderr=subprocess.STDOUT, env=check_env(self), ), mock.call( [expected_python, "-m", "pip", "list", "--format=json"], env=mock.ANY, ), mock.call( [expected_python, "-m", "pip", "list", "--format=json"], env=mock.ANY, ), ])
def setUp(self): super().setUp() self.pip = _pip.Pip( python_major_version="test", part_dir="part_dir", install_dir="install_dir", stage_dir="stage_dir", ) # We don't care about anything init did to the mock here: reset it self.mock_run.reset_mock()
def test_clean_packages(self): pip = _pip.Pip(python_major_version='test', part_dir='part_dir', install_dir='install_dir', stage_dir='stage_dir') packages_dir = os.path.join('part_dir', 'python-packages') self.assertTrue(os.path.exists(packages_dir)) # Now verify that asking pip to clean removes its packages pip.clean_packages() self.assertFalse(os.path.exists(packages_dir))
def test_clean_packages(self): pip = _pip.Pip( python_major_version="test", part_dir="part_dir", install_dir="install_dir", stage_dir="stage_dir", ) packages_dir = os.path.join("part_dir", "python-packages") self.assertTrue(os.path.exists(packages_dir)) # Now verify that asking pip to clean removes its packages pip.clean_packages() self.assertFalse(os.path.exists(packages_dir))
def test_setup_unexpected_error(self): """Test that pip initialization doesn't eat legit errors""" # Raises an exception indicating something bad happened self.mock_run_output.side_effect = subprocess.CalledProcessError( 1, 'foo', b'no good, very bad') pip = _pip.Pip(python_major_version='test', part_dir='part_dir', install_dir='install_dir', stage_dir='stage_dir') # Verify that pip lets that exception through self.assertRaises(subprocess.CalledProcessError, pip.setup)
def test_setup_with_pip_installed(self): """Test that no attempt is made to reinstall pip""" # Since _run doesn't raise an exception indicating pip isn't installed, # it must be installed. # Verify that no attempt is made to reinstall pip _pip.Pip(python_major_version='test', part_dir='part_dir', install_dir='install_dir', stage_dir='stage_dir').setup() self._assert_check_for_pip() self.mock_run.assert_not_called()
def test_init_with_pip_installed(self): """Test that no attempt is made to reinstall pip""" # Since _run doesn't raise an exception indicating pip isn't installed, # it must be installed. # Verify that no attempt is made to reinstall pip _pip.Pip(python_major_version='test', part_dir='part_dir', install_dir='install_dir', stage_dir='stage_dir') self.mock_run_output.assert_called_once_with(self.command, stderr=subprocess.STDOUT, env=mock.ANY)
def _assert_expected_enviroment(self, expected_python, headers_path): _pip.Pip(python_major_version='test', part_dir='part_dir', install_dir='install_dir', stage_dir='stage_dir').setup() class check_env(): def __init__(self, test): self.test = test def __eq__(self, env): self.test.assertThat(env, Contains('PYTHONUSERBASE')) self.test.assertThat(env['PYTHONUSERBASE'], Equals('install_dir')) self.test.assertThat(env, Contains('PYTHONHOME')) if expected_python.startswith('install_dir'): self.test.assertThat( env['PYTHONHOME'], Equals(os.path.join('install_dir', 'usr'))) else: self.test.assertThat( env['PYTHONHOME'], Equals(os.path.join('stage_dir', 'usr'))) self.test.assertThat(env, Contains('PATH')) self.test.assertThat( env['PATH'], Contains(os.path.join('install_dir', 'usr', 'bin'))) if headers_path: self.test.assertThat(env, Contains('CPPFLAGS')) self.test.assertThat(env['CPPFLAGS'], Contains('-I{}'.format(headers_path))) return True self.mock_run_output.assert_has_calls([ mock.call([expected_python, '-m', 'pip'], stderr=subprocess.STDOUT, env=check_env(self)), mock.call([expected_python, '-m', 'pip', 'list', '--format=json'], env=mock.ANY), mock.call([expected_python, '-m', 'pip', 'list', '--format=json'], env=mock.ANY), ])
def test_init_without_pip_installed(self): """Test that the system pip is used to install our own pip""" # Raise an exception indicating that pip isn't installed def fake_run(command, **kwargs): if command == self.command: raise subprocess.CalledProcessError(1, 'foo', b'no module named pip') self.mock_run_output.side_effect = fake_run # Verify that pip is then installed _pip.Pip(python_major_version='test', part_dir='part_dir', install_dir='install_dir', stage_dir='stage_dir') part_pythonhome = os.path.join('install_dir', 'usr') host_pythonhome = os.path.join(os.path.sep, 'usr') # What we're asserting here: # 1. That we test for the installed pip # 2. That we then download pip (and associated tools) using host pip # 3. That we then install pip (and associated tools) using host pip self.assertThat(self.mock_run_output.mock_calls, HasLength(3)) self.mock_run_output.assert_has_calls([ mock.call(self.command, env=_CheckPythonhomeEnv(self, part_pythonhome), stderr=subprocess.STDOUT), mock.call(_CheckCommand(self, 'download', ['pip', 'setuptools', 'wheel'], []), env=_CheckPythonhomeEnv(self, host_pythonhome), cwd=None), mock.call(_CheckCommand(self, 'install', ['pip', 'setuptools', 'wheel'], ['--ignore-installed']), env=_CheckPythonhomeEnv(self, host_pythonhome), cwd=None), ])
def test_setup_without_pip_installed(self): """Test that the system pip is used to install our own pip""" # Raise an exception indicating that pip isn't installed def fake_run(command, **kwargs): if command == self.command: raise subprocess.CalledProcessError(1, "foo", b"no module named pip") return "[]" self.mock_run_output.side_effect = fake_run # Verify that pip is then installed _pip.Pip( python_major_version="test", part_dir="part_dir", install_dir="install_dir", stage_dir="stage_dir", ).setup() part_pythonhome = os.path.join("install_dir", "usr") host_pythonhome = os.path.join(os.path.sep, "usr") # What we're asserting here: # 1. That we test for the installed pip # 2. That we then download pip (and associated tools) using host pip # 3. That we then install pip (and associated tools) using host pip # Check that we correctly test for the installed pip self.mock_run_output.assert_has_calls([ mock.call( self.command, stderr=subprocess.STDOUT, env=_CheckPythonhomeEnv(self, part_pythonhome), ), mock.call(self.command + ["list", "--format=json"], env=mock.ANY), mock.call(self.command + ["list", "--format=json"], env=mock.ANY), ]) # Now test that we download and install pip using the host's pip, and # then install the associated tools using the pip we pulled. self.assertThat(self.mock_run.mock_calls, HasLength(6)) self.mock_run.assert_has_calls([ mock.call( _CheckCommand(self, "download", ["pip"], []), env=_CheckPythonhomeEnv(self, host_pythonhome), cwd=None, ), mock.call( _CheckCommand(self, "install", ["pip"], ["--ignore-installed"]), env=_CheckPythonhomeEnv(self, host_pythonhome), cwd=None, ), mock.call( _CheckCommand(self, "download", ["wheel"], []), env=_CheckPythonhomeEnv(self, part_pythonhome), cwd=None, ), mock.call( _CheckCommand(self, "install", ["wheel"], ["--ignore-installed"]), env=_CheckPythonhomeEnv(self, part_pythonhome), cwd=None, ), mock.call( _CheckCommand(self, "download", ["setuptools"], []), env=_CheckPythonhomeEnv(self, part_pythonhome), cwd=None, ), mock.call( _CheckCommand(self, "install", ["setuptools"], ["--ignore-installed"]), env=_CheckPythonhomeEnv(self, part_pythonhome), cwd=None, ), ])