| 1 |
# Copyright 2015-2026 Facundo Batista, Nicolás Demarchi |
| 2 |
# |
| 3 |
# This program is free software: you can redistribute it and/or modify it |
| 4 |
# under the terms of the GNU General Public License version 3, as published |
| 5 |
# by the Free Software Foundation. |
| 6 |
# |
| 7 |
# This program is distributed in the hope that it will be useful, but |
| 8 |
# WITHOUT ANY WARRANTY; without even the implied warranties of |
| 9 |
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 10 |
# PURPOSE. See the GNU General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU General Public License along |
| 13 |
# with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 |
# |
| 15 |
# For further info, check https://github.com/PyAr/fades |
| 16 |
|
| 17 |
"""Tests for pip related code.""" |
| 18 |
|
| 19 |
import os |
| 20 |
import io |
| 21 |
import pytest |
| 22 |
from pathlib import Path |
| 23 |
from unittest.mock import patch, call |
| 24 |
|
| 25 |
from fades.pipmanager import PipManager |
| 26 |
from fades import pipmanager |
| 27 |
from fades import helpers |
| 28 |
|
| 29 |
BIN_PATH = Path("somepath") |
| 30 |
|
| 31 |
|
| 32 |
def test_get_parsing_ok_pytest(): |
| 33 |
mocked_stdout = [ |
| 34 |
"Name: foo", |
| 35 |
"Version: 2.0.0", |
| 36 |
"Location: ~/.local/share/fades/86cc492/lib/python3.4/site-packages", |
| 37 |
"Requires: ", |
| 38 |
] |
| 39 |
mgr = PipManager(BIN_PATH, pip_installed=True) |
| 40 |
with patch.object(helpers, "logged_exec", return_value=mocked_stdout): |
| 41 |
version = mgr.get_version("foo") |
| 42 |
assert version, "2.0.0" |
| 43 |
|
| 44 |
|
| 45 |
def test_get_parsing_error(logs): |
| 46 |
mocked_stdout = [ |
| 47 |
"Name: foo", |
| 48 |
"Release: 2.0.0", |
| 49 |
"Location: ~/.local/share/fades/86cc492/lib/python3.4/site-packages", |
| 50 |
"Requires: ", |
| 51 |
] |
| 52 |
mgr = PipManager(BIN_PATH, pip_installed=True) |
| 53 |
with patch.object(helpers, "logged_exec", return_value=mocked_stdout): |
| 54 |
version = mgr.get_version("foo") |
| 55 |
|
| 56 |
assert version == "" |
| 57 |
assert ( |
| 58 |
'Fades is having problems getting the installed version. ' |
| 59 |
'Run with -v or check the logs for details' |
| 60 |
) in logs.error |
| 61 |
|
| 62 |
|
| 63 |
def test_real_case_levenshtein(): |
| 64 |
mocked_stdout = [ |
| 65 |
"Metadata-Version: 1.1", |
| 66 |
"Name: python-Levenshtein", |
| 67 |
"Version: 0.12.0", |
| 68 |
"License: GPL", |
| 69 |
] |
| 70 |
mgr = PipManager(BIN_PATH, pip_installed=True) |
| 71 |
with patch.object(helpers, "logged_exec", return_value=mocked_stdout): |
| 72 |
version = mgr.get_version("foo") |
| 73 |
assert version == "0.12.0" |
| 74 |
|
| 75 |
|
| 76 |
def test_install(): |
| 77 |
mgr = PipManager(BIN_PATH, pip_installed=True) |
| 78 |
pip_path = Path(BIN_PATH) / "pip" |
| 79 |
with patch.object(helpers, "logged_exec") as mock: |
| 80 |
mgr.install("foo") |
| 81 |
|
| 82 |
# check it always upgrades pip, and then the proper install |
| 83 |
python_path = Path(BIN_PATH) / "python" |
| 84 |
c1 = call([python_path, "-m", "pip", "install", "pip", "--upgrade"]) |
| 85 |
c2 = call([pip_path, "install", "foo"]) |
| 86 |
assert mock.call_args_list == [c1, c2] |
| 87 |
|
| 88 |
|
| 89 |
def test_install_without_pip_upgrade(): |
| 90 |
mgr = PipManager(BIN_PATH, pip_installed=True, avoid_pip_upgrade=True) |
| 91 |
pip_path = Path(BIN_PATH) / "pip" |
| 92 |
with patch.object(helpers, "logged_exec") as mock: |
| 93 |
mgr.install("foo") |
| 94 |
mock.assert_called_with([pip_path, "install", "foo"]) |
| 95 |
|
| 96 |
|
| 97 |
def test_install_multiword_dependency(): |
| 98 |
mgr = PipManager(BIN_PATH, pip_installed=True) |
| 99 |
pip_path = Path(BIN_PATH) / "pip" |
| 100 |
with patch.object(helpers, "logged_exec") as mock: |
| 101 |
mgr.install("foo bar") |
| 102 |
mock.assert_called_with([pip_path, "install", "foo", "bar"]) |
| 103 |
|
| 104 |
|
| 105 |
def test_install_with_options(): |
| 106 |
mgr = PipManager(BIN_PATH, pip_installed=True, options=["--bar baz"]) |
| 107 |
pip_path = Path(BIN_PATH) / "pip" |
| 108 |
with patch.object(helpers, "logged_exec") as mock: |
| 109 |
mgr.install("foo") |
| 110 |
mock.assert_called_with([pip_path, "install", "foo", "--bar", "baz"]) |
| 111 |
|
| 112 |
|
| 113 |
def test_install_with_options_using_equal(): |
| 114 |
mgr = PipManager(BIN_PATH, pip_installed=True, options=["--bar=baz"]) |
| 115 |
pip_path = Path(BIN_PATH) / "pip" |
| 116 |
with patch.object(helpers, "logged_exec") as mock: |
| 117 |
mgr.install("foo") |
| 118 |
mock.assert_called_with([pip_path, "install", "foo", "--bar=baz"]) |
| 119 |
|
| 120 |
|
| 121 |
def test_install_raise_error(logs): |
| 122 |
mgr = PipManager(BIN_PATH, pip_installed=True) |
| 123 |
with patch.object(helpers, "logged_exec", side_effect=['ok', Exception("Kapow!")]): |
| 124 |
with pytest.raises(Exception): |
| 125 |
mgr.install("foo") |
| 126 |
|
| 127 |
assert "Error installing foo: Kapow!" in logs.error |
| 128 |
|
| 129 |
|
| 130 |
def test_install_without_pip(): |
| 131 |
mgr = PipManager(BIN_PATH, pip_installed=False) |
| 132 |
pip_path = Path(BIN_PATH) / "pip" |
| 133 |
with patch.object(helpers, "logged_exec") as mocked_exec: |
| 134 |
with patch.object(mgr, "_brute_force_install_pip") as mocked_install_pip: |
| 135 |
mgr.install("foo") |
| 136 |
assert mocked_install_pip.call_count == 1 |
| 137 |
mocked_exec.assert_called_with([pip_path, "install", "foo"]) |
| 138 |
|
| 139 |
|
| 140 |
def test_brute_force_install_pip_installer_exists(tmp_path): |
| 141 |
tmp_file = tmp_path / "hello.txt" |
| 142 |
mgr = PipManager(BIN_PATH, pip_installed=False) |
| 143 |
python_path = Path(BIN_PATH) / "python" |
| 144 |
|
| 145 |
# get the tempfile but leave it there to be found |
| 146 |
open(tmp_file, 'wt', encoding='utf8').close() |
| 147 |
mgr.pip_installer_fname = Path(tmp_file) |
| 148 |
|
| 149 |
with patch.object(helpers, "logged_exec") as mocked_exec: |
| 150 |
with patch.object(mgr, "_download_pip_installer") as download_installer: |
| 151 |
mgr._brute_force_install_pip() |
| 152 |
|
| 153 |
assert not download_installer.called |
| 154 |
mocked_exec.assert_called_with([python_path, mgr.pip_installer_fname, "-I"]) |
| 155 |
assert mgr.pip_installed |
| 156 |
|
| 157 |
|
| 158 |
def test_brute_force_install_pip_no_installer(tmp_path): |
| 159 |
tmp_file = tmp_path / "hello.txt" |
| 160 |
mgr = PipManager(BIN_PATH, pip_installed=False) |
| 161 |
python_path = Path(BIN_PATH) / "python" |
| 162 |
|
| 163 |
mgr.pip_installer_fname = Path(tmp_file) |
| 164 |
with patch.object(helpers, "logged_exec") as mocked_exec: |
| 165 |
with patch.object(mgr, "_download_pip_installer") as download_installer: |
| 166 |
mgr._brute_force_install_pip() |
| 167 |
|
| 168 |
download_installer.assert_called_once_with() |
| 169 |
mocked_exec.assert_called_with([python_path, mgr.pip_installer_fname, "-I"]) |
| 170 |
assert mgr.pip_installed |
| 171 |
|
| 172 |
|
| 173 |
def test_download_pip_installer(tmp_path): |
| 174 |
tmp_file = tmp_path / "hello.txt" |
| 175 |
mgr = PipManager(BIN_PATH, pip_installed=False) |
| 176 |
|
| 177 |
mgr.pip_installer_fname = tmp_file |
| 178 |
with patch("fades.pipmanager.request.urlopen", return_value=io.BytesIO(b"hola")) as urlopen: |
| 179 |
mgr._download_pip_installer() |
| 180 |
assert os.path.exists(mgr.pip_installer_fname) |
| 181 |
urlopen.assert_called_once_with(pipmanager.PIP_INSTALLER) |
| 182 |
|
| 183 |
|
| 184 |
def test_freeze(tmp_path): |
| 185 |
tmp_file = tmp_path / "reqtest.txt" |
| 186 |
|
| 187 |
# call and check pip was executed ok |
| 188 |
mgr = PipManager(BIN_PATH) |
| 189 |
with patch.object(helpers, "logged_exec") as mock: |
| 190 |
mock.return_value = ['moño>11', 'foo==1.2'] # "bad" order, on purpose |
| 191 |
mgr.freeze(tmp_file) |
| 192 |
|
| 193 |
pip_path = Path(BIN_PATH) / "pip" |
| 194 |
mock.assert_called_with([pip_path, "freeze", "--all", "--local"]) |
| 195 |
|
| 196 |
# check results were stored properly |
| 197 |
with open(tmp_file, 'rt', encoding='utf8') as fh: |
| 198 |
stored = fh.read() |
| 199 |
assert stored == 'foo==1.2\nmoño>11\n' |