def test_install_raise_error(self): mgr = PipManager(BIN_PATH, pip_installed=True) with patch.object(helpers, 'logged_exec') as mock: mock.side_effect = Exception("Kapow!") with self.assertRaises(Exception): mgr.install('foo') self.assertLoggedError("Error installing foo: Kapow!")
def test_install_with_options(self): mgr = PipManager(BIN_PATH, pip_installed=True, options=['--bar baz']) pip_path = os.path.join(BIN_PATH, 'pip') with patch.object(helpers, 'logged_exec') as mock: mgr.install('foo') mock.assert_called_with( [pip_path, 'install', 'foo', '--bar', 'baz'])
def test_install_without_pip(self): mgr = PipManager('/usr/bin', pip_installed=False) with patch.object(helpers, 'logged_exec') as mocked_exec: with patch.object(mgr, '_brute_force_install_pip') as mocked_install_pip: mgr.install('foo') self.assertEqual(mocked_install_pip.call_count, 1) mocked_exec.assert_called_with(['/usr/bin/pip', 'install', 'foo'])
def create_venv(requested_deps, interpreter, is_current): """Create a new virtualvenv with the requirements of this script.""" # create virtualenv env = FadesEnvBuilder() env_path, env_bin_path, pip_installed = env.create_env(interpreter, is_current) venv_data = {} venv_data['env_path'] = env_path venv_data['env_bin_path'] = env_bin_path venv_data['pip_installed'] = pip_installed # install deps installed = {} for repo in requested_deps.keys(): if repo == REPO_PYPI: mgr = PipManager(env_bin_path, pip_installed=pip_installed) else: logger.warning("Install from %r not implemented", repo) continue installed[repo] = {} repo_requested = requested_deps[repo] logger.debug("Installing dependencies for repo %r: requested=%s", repo, repo_requested) for dependency in repo_requested: mgr.install(dependency) # always store the installed dependency, as in the future we'll select the venv # based on what is installed, not what used requested (remember that user may # request >, >=, etc!) project = dependency.project_name installed[repo][project] = mgr.get_version(project) logger.debug("Installed dependencies: %s", installed) return venv_data, installed
def test_install_without_pip(self): mgr = PipManager(BIN_PATH, pip_installed=False) pip_path = os.path.join(BIN_PATH, 'pip') with patch.object(helpers, 'logged_exec') as mocked_exec: with patch.object(mgr, '_brute_force_install_pip') as mocked_install_pip: mgr.install('foo') self.assertEqual(mocked_install_pip.call_count, 1) mocked_exec.assert_called_with([pip_path, 'install', 'foo'])
def test_install_without_pip(mocker): mgr = PipManager(BIN_PATH, pip_installed=False) pip_path = os.path.join(BIN_PATH, "pip") mocked_exec = mocker.patch.object(helpers, "logged_exec") mocked_install_pip = mocker.patch.object(mgr, "_brute_force_install_pip") mgr.install("foo") assert mocked_install_pip.call_count == 1 mocked_exec.assert_called_with([pip_path, "install", "foo"])
def test_say_hi_on_first_install(self): mgr = PipManager(BIN_PATH, pip_installed=True, options=['--bar=baz']) with patch.object(helpers, 'logged_exec'): mgr.install('foo') self.assertLoggedInfo("Hi! This is fades") logassert.setup(self, 'fades.pipmanager') mgr.install('bar') self.assertNotLoggedInfo("Hi! This is fades")
def test_install_without_pip(self): mgr = PipManager('/usr/bin', pip_installed=False) with patch.object(helpers, 'logged_exec') as mocked_exec: with patch.object( mgr, '_brute_force_install_pip') as mocked_install_pip: mgr.install('foo') self.assertEqual(mocked_install_pip.call_count, 1) mocked_exec.assert_called_with(['/usr/bin/pip', 'install', 'foo'])
def test_install_raise_error(mocker, logged): mgr = PipManager(BIN_PATH, pip_installed=True) mocker.patch.object(helpers, "logged_exec", side_effect=Exception("Kapow!")) with pytest.raises(Exception): mgr.install("foo") logged.assert_error("Error installing foo: Kapow!")
def test_download_pip_installer(self): mgr = PipManager('/usr/bin', pip_installed=False) with tempfile.NamedTemporaryFile() as temp_file: mgr.pip_installer_fname = temp_file.name with patch('fades.pipmanager.request.urlopen') as urlopen: urlopen.return_value = io.BytesIO(b'hola') mgr._download_pip_installer() self.assertTrue(os.path.exists(mgr.pip_installer_fname)) urlopen.assert_called_once_with(pipmanager.PIP_INSTALLER)
def test_download_pip_installer(mocker, tmp_path): tmp_file = str(tmp_path / "hello.txt") mgr = PipManager(BIN_PATH, pip_installed=False) mgr.pip_installer_fname = tmp_file urlopen = mocker.patch("fades.pipmanager.request.urlopen", return_value=io.BytesIO(b"hola")) mgr._download_pip_installer() assert os.path.exists(mgr.pip_installer_fname) urlopen.assert_called_once_with(pipmanager.PIP_INSTALLER)
def test_get_parsing_ok(self): mocked_stdout = ['Name: foo', 'Version: 2.0.0', 'Location: ~/.local/share/fades/86cc492/lib/python3.4/site-packages', 'Requires: '] mgr = PipManager(BIN_PATH, pip_installed=True) with patch.object(helpers, 'logged_exec') as mock: mock.return_value = mocked_stdout version = mgr.get_version('foo') self.assertEqual(version, '2.0.0')
def test_install(mocker): mgr = PipManager(BIN_PATH, pip_installed=True) pip_path = os.path.join(BIN_PATH, "pip") mock = mocker.patch.object(helpers, "logged_exec") mgr.install("foo") # check it always upgrades pip, and then the proper install python_path = os.path.join(BIN_PATH, "python") c1 = mocker.call([python_path, "-m", "pip", "install", "pip", "--upgrade"]) c2 = mocker.call([pip_path, "install", "foo"]) assert mock.call_args_list == [c1, c2]
def test_get_parsing_ok_pytest(mocker): mocked_stdout = [ "Name: foo", "Version: 2.0.0", "Location: ~/.local/share/fades/86cc492/lib/python3.4/site-packages", "Requires: ", ] mgr = PipManager(BIN_PATH, pip_installed=True) mocker.patch.object(helpers, "logged_exec", return_value=mocked_stdout) version = mgr.get_version("foo") assert version, "2.0.0"
def test_real_case_levenshtein(mocker): mocked_stdout = [ "Metadata-Version: 1.1", "Name: python-Levenshtein", "Version: 0.12.0", "License: GPL", ] mgr = PipManager(BIN_PATH, pip_installed=True) mocker.patch.object(helpers, "logged_exec", return_value=mocked_stdout) version = mgr.get_version("foo") assert version == "0.12.0"
def test_real_case_levenshtein(self): mocked_stdout = [ 'Metadata-Version: 1.1', 'Name: python-Levenshtein', 'Version: 0.12.0', 'License: GPL', ] mgr = PipManager('/usr/bin', pip_installed=True) with patch.object(helpers, 'logged_exec') as mock: mock.return_value = mocked_stdout version = mgr.get_version('foo') self.assertEqual(version, '0.12.0')
def test_get_parsing_error(self): mocked_stdout = ['Name: foo', 'Release: 2.0.0', 'Location: ~/.local/share/fades/86cc492/lib/python3.4/site-packages', 'Requires: '] mgr = PipManager(BIN_PATH, pip_installed=True) with patch.object(helpers, 'logged_exec') as mock: version = mgr.get_version('foo') mock.return_value = mocked_stdout self.assertEqual(version, '') self.assertLoggedError('Fades is having problems getting the installed version. ' 'Run with -v or check the logs for details')
def test_real_case_levenshtein(self): mocked_stdout = [ 'Metadata-Version: 1.1', 'Name: python-Levenshtein', 'Version: 0.12.0', 'License: GPL', ] mgr = PipManager(BIN_PATH, pip_installed=True) with patch.object(helpers, 'logged_exec') as mock: mock.return_value = mocked_stdout version = mgr.get_version('foo') self.assertEqual(version, '0.12.0')
def test_download_pip_installer(self): mgr = PipManager(BIN_PATH, pip_installed=False) # get a tempfile and remove it, so later the installer is downloaded there tempfile = get_tempfile(self) os.remove(tempfile) mgr.pip_installer_fname = tempfile with patch('fades.pipmanager.request.urlopen') as urlopen: urlopen.return_value = io.BytesIO(b'hola') mgr._download_pip_installer() self.assertTrue(os.path.exists(mgr.pip_installer_fname)) urlopen.assert_called_once_with(pipmanager.PIP_INSTALLER)
def test_brute_force_install_pip_installer_exists(self): mgr = PipManager(BIN_PATH, pip_installed=False) python_path = os.path.join(BIN_PATH, 'python') with patch.object(helpers, 'logged_exec') as mocked_exec, \ patch.object(mgr, '_download_pip_installer') as download_installer: # get the tempfile but leave it there to be found mgr.pip_installer_fname = get_tempfile(self) mgr._brute_force_install_pip() self.assertEqual(download_installer.call_count, 0) mocked_exec.assert_called_with([python_path, mgr.pip_installer_fname, '-I']) self.assertTrue(mgr.pip_installed)
def create_venv(requested_deps, interpreter, is_current, options, pip_options, avoid_pip_upgrade): """Create a new virtualvenv with the requirements of this script.""" # create virtualenv env = _FadesEnvBuilder() env_path, env_bin_path, pip_installed = env.create_env( interpreter, is_current, options) venv_data = {} venv_data['env_path'] = env_path venv_data['env_bin_path'] = env_bin_path venv_data['pip_installed'] = pip_installed # install deps installed = {} for repo in requested_deps.keys(): if repo in (REPO_PYPI, REPO_VCS): mgr = PipManager(env_bin_path, pip_installed=pip_installed, options=pip_options, avoid_pip_upgrade=avoid_pip_upgrade) else: logger.warning("Install from %r not implemented", repo) continue installed[repo] = {} repo_requested = requested_deps[repo] logger.debug("Installing dependencies for repo %r: requested=%s", repo, repo_requested) for dependency in repo_requested: try: mgr.install(dependency) except Exception: logger.debug("Installation Step failed, removing virtualenv") destroy_venv(env_path) raise FadesError('Dependency installation failed') if repo == REPO_VCS: # no need to request the installed version, as we'll always compare # to the url itself project = dependency.url version = None else: # always store the installed dependency, as in the future we'll select the venv # based on what is installed, not what used requested (remember that user may # request >, >=, etc!) project = dependency.project_name version = mgr.get_version(project) installed[repo][project] = version logger.debug("Installed dependencies: %s", installed) return venv_data, installed
def test_brute_force_install_pip_no_installer(mocker, tmp_path): tmp_file = str(tmp_path / "hello.txt") mgr = PipManager(BIN_PATH, pip_installed=False) python_path = os.path.join(BIN_PATH, "python") mocked_exec = mocker.patch.object(helpers, "logged_exec") download_installer = mocker.patch.object(mgr, "_download_pip_installer") mgr.pip_installer_fname = tmp_file mgr._brute_force_install_pip() download_installer.assert_called_once_with() mocked_exec.assert_called_with( [python_path, mgr.pip_installer_fname, "-I"]) assert mgr.pip_installed
def test_get_parsing_error(mocker, logs): mocked_stdout = [ "Name: foo", "Release: 2.0.0", "Location: ~/.local/share/fades/86cc492/lib/python3.4/site-packages", "Requires: ", ] mgr = PipManager(BIN_PATH, pip_installed=True) mocker.patch.object(helpers, "logged_exec", return_value=mocked_stdout) version = mgr.get_version("foo") assert version == "" assert ('Fades is having problems getting the installed version. ' 'Run with -v or check the logs for details') in logs.error
def test_brute_force_install_pip_no_installer(self): mgr = PipManager('/usr/bin', pip_installed=False) with patch.object(helpers, 'logged_exec') as mocked_exec, \ patch.object(mgr, '_download_pip_installer') as download_installer: with tempfile.NamedTemporaryFile() as temp_file: mgr.pip_installer_fname = temp_file.name temp_file.close( ) # Without this the file does not gets created mgr._brute_force_install_pip() download_installer.assert_called_once_with() mocked_exec.assert_called_with( ['/usr/bin/python', mgr.pip_installer_fname, '-I']) self.assertTrue(mgr.pip_installed)
def test_brute_force_install_pip_no_installer(self): mgr = PipManager(BIN_PATH, pip_installed=False) python_path = os.path.join(BIN_PATH, 'python') with patch.object(helpers, 'logged_exec') as mocked_exec, \ patch.object(mgr, '_download_pip_installer') as download_installer: # get the tempfile and remove it so then it's not found tempfile = get_tempfile(self) os.remove(tempfile) mgr.pip_installer_fname = tempfile mgr._brute_force_install_pip() download_installer.assert_called_once_with() mocked_exec.assert_called_with([python_path, mgr.pip_installer_fname, '-I']) self.assertTrue(mgr.pip_installed)
def test_freeze(mocker, tmp_path): tmp_file = str(tmp_path / "reqtest.txt") mock = mocker.patch.object(helpers, "logged_exec") mock.return_value = ['moño>11', 'foo==1.2'] # "bad" order, on purpose # call and check pip was executed ok mgr = PipManager(BIN_PATH) mgr.freeze(tmp_file) pip_path = os.path.join(BIN_PATH, "pip") mock.assert_called_with([pip_path, "freeze", "--all", "--local"]) # check results were stored properly with open(tmp_file, 'rt', encoding='utf8') as fh: stored = fh.read() assert stored == 'foo==1.2\nmoño>11\n'
def test_brute_force_install_pip_installer_exists(mocker, tmp_path): tmp_file = str(tmp_path / "hello.txt") mgr = PipManager(BIN_PATH, pip_installed=False) python_path = os.path.join(BIN_PATH, "python") mocked_exec = mocker.patch.object(helpers, "logged_exec") download_installer = mocker.patch.object(mgr, "_download_pip_installer") # get the tempfile but leave it there to be found open(tmp_file, 'wt', encoding='utf8').close() mgr.pip_installer_fname = tmp_file mgr._brute_force_install_pip() assert not download_installer.called mocked_exec.assert_called_with( [python_path, mgr.pip_installer_fname, "-I"]) assert mgr.pip_installed
def test_brute_force_install_pip_installer_exists(self): mgr = PipManager('/usr/bin', pip_installed=False) with patch.object(helpers, 'logged_exec') as mocked_exec, \ patch.object(mgr, '_download_pip_installer') as download_installer: # Create empty file with tempfile.NamedTemporaryFile(delete=False) as f: mgr.pip_installer_fname = f.name try: mgr._brute_force_install_pip() finally: os.remove(mgr.pip_installer_fname) self.assertEqual(download_installer.call_count, 0) mocked_exec.assert_called_with( ['/usr/bin/python', mgr.pip_installer_fname, '-I']) self.assertTrue(mgr.pip_installed)
def create_venv(requested_deps, interpreter, is_current, options, pip_options): """Create a new virtualvenv with the requirements of this script.""" # create virtualenv env = _FadesEnvBuilder() env_path, env_bin_path, pip_installed = env.create_env(interpreter, is_current, options) venv_data = {} venv_data['env_path'] = env_path venv_data['env_bin_path'] = env_bin_path venv_data['pip_installed'] = pip_installed # install deps installed = {} for repo in requested_deps.keys(): if repo in (REPO_PYPI, REPO_VCS): mgr = PipManager(env_bin_path, pip_installed=pip_installed, options=pip_options) else: logger.warning("Install from %r not implemented", repo) continue installed[repo] = {} repo_requested = requested_deps[repo] logger.debug("Installing dependencies for repo %r: requested=%s", repo, repo_requested) for dependency in repo_requested: try: mgr.install(dependency) except Exception: logger.debug("Installation Step failed, removing virtualenv") destroy_venv(env_path) raise FadesError('Dependency installation failed') if repo == REPO_VCS: # no need to request the installed version, as we'll always compare # to the url itself project = dependency.url version = None else: # always store the installed dependency, as in the future we'll select the venv # based on what is installed, not what used requested (remember that user may # request >, >=, etc!) project = dependency.project_name version = mgr.get_version(project) installed[repo][project] = version logger.debug("Installed dependencies: %s", installed) return venv_data, installed
def test_install_with_options_using_equal(self): mgr = PipManager('/usr/bin', pip_installed=True, options=['--bar=baz']) with patch.object(helpers, 'logged_exec') as mock: mgr.install('foo') mock.assert_called_with( ['/usr/bin/pip', 'install', 'foo', '--bar=baz'])
def test_install_with_options_using_equal(mocker): mgr = PipManager(BIN_PATH, pip_installed=True, options=["--bar=baz"]) pip_path = os.path.join(BIN_PATH, "pip") mock = mocker.patch.object(helpers, "logged_exec") mgr.install("foo") mock.assert_called_with([pip_path, "install", "foo", "--bar=baz"])
def test_install_multiword_dependency(mocker): mgr = PipManager(BIN_PATH, pip_installed=True) pip_path = os.path.join(BIN_PATH, "pip") mock = mocker.patch.object(helpers, "logged_exec") mgr.install("foo bar") mock.assert_called_with([pip_path, "install", "foo", "bar"])
def test_install_multiword_dependency(self): mgr = PipManager(BIN_PATH, pip_installed=True) pip_path = os.path.join(BIN_PATH, 'pip') with patch.object(helpers, 'logged_exec') as mock: mgr.install('foo bar') mock.assert_called_with([pip_path, 'install', 'foo', 'bar'])
def test_install(self): mgr = PipManager('/usr/bin', pip_installed=True) with patch.object(helpers, 'logged_exec') as mock: mgr.install('foo') mock.assert_called_with(['/usr/bin/pip', 'install', 'foo'])
def test_install_with_options_using_equal(self): mgr = PipManager('/usr/bin', pip_installed=True, options=['--bar=baz']) with patch.object(helpers, 'logged_exec') as mock: mgr.install('foo') mock.assert_called_with(['/usr/bin/pip', 'install', 'foo', '--bar=baz'])
def test_install_with_options_using_equal(self): mgr = PipManager(BIN_PATH, pip_installed=True, options=['--bar=baz']) pip_path = os.path.join(BIN_PATH, 'pip') with patch.object(helpers, 'logged_exec') as mock: mgr.install('foo') mock.assert_called_with([pip_path, 'install', 'foo', '--bar=baz'])