File size: 8,497 Bytes
d086025 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test_pip_lock.py b/tests/test_pip_lock.py
new file mode 100644
--- /dev/null
+++ b/tests/test_pip_lock.py
@@ -0,0 +1,249 @@
+from __future__ import annotations
+
+from collections.abc import Generator
+from contextlib import contextmanager
+from email.message import EmailMessage
+from types import SimpleNamespace
+from unittest import mock
+
+import pytest
+
+from pip_lock import (
+ check_requirements,
+ get_installed,
+ get_mismatches,
+ parse_pip,
+ print_errors,
+ read_pip,
+)
+
+
+@contextmanager
+def mock_get_distributions(versions: dict[str, str]) -> Generator[None]:
+ def fake_get_distributions():
+ dists = []
+ for name, version in versions.items():
+ metadata = EmailMessage()
+ metadata["Name"] = name
+ dists.append(SimpleNamespace(metadata=metadata, version=version))
+ return iter(dists)
+
+ with mock.patch("pip_lock.get_distributions", fake_get_distributions):
+ yield
+
+
+class TestReadPip:
+ def test_read(self, tmp_path):
+ requirements = tmp_path / "requirements.txt"
+ requirements.write_text("package1==1.0\npackage2==1.1\n")
+ assert read_pip(str(requirements)) == ["package1==1.0", "package2==1.1"]
+
+ def test_include(self, tmp_path):
+ requirements_inc = tmp_path / "requirements_inc.txt"
+ requirements_inc.write_text("other-package==1.0\n")
+ requirements = tmp_path / "requirements.txt"
+ requirements.write_text(f"-r {requirements_inc}\n")
+
+ assert read_pip(str(requirements)) == [
+ f"-r {requirements_inc}",
+ "other-package==1.0",
+ ]
+
+ def test_empty(self, tmp_path):
+ requirements = tmp_path / "requirements.txt"
+ requirements.write_text("\n")
+ assert read_pip(str(requirements)) == [""]
+
+
+class TestParsePip:
+ def test_version(self):
+ assert parse_pip(["package1==1.0", "package2==1.1"]) == {
+ "package1": "1.0",
+ "package2": "1.1",
+ }
+
+ def test_normalize_dashes(self):
+ assert parse_pip(["package_one==1.0"]) == {
+ "package-one": "1.0",
+ }
+
+ def test_normalize_dots(self):
+ assert parse_pip(["foo.bar==1.0"]) == {
+ "foo-bar": "1.0",
+ }
+
+ def test_ignore_empty(self):
+ assert parse_pip([""]) == {}
+
+ def test_ignore_comments(self):
+ assert parse_pip(["# Comment"]) == {}
+
+ def test_ignore_includes(self):
+ assert parse_pip(["-r example.txt"]) == {}
+
+ def test_ignore_arguments(self):
+ assert parse_pip(["--find-links file:./wheels"]) == {}
+
+ def test_ignore_http_urls(self):
+ assert parse_pip(["http://www.google.com"]) == {}
+
+ def test_ignore_https_urls(self):
+ assert parse_pip(["https://www.google.com"]) == {}
+
+ def test_ignore_bzr_http_urls(self):
+ url = "bzr+http://bzr.example.com"
+ assert parse_pip([url]) == {}
+
+ def test_ignore_git_https_urls(self):
+ url = "git+https://git@github.com/adamchainz/pip-lock.git@80361b8#egg=pip-lock"
+ assert parse_pip([url]) == {}
+
+ def test_ignore_at_git_ssh_urls(self):
+ assert parse_pip(["foo @ git+ssh://example.com"]) == {}
+
+ def test_ignore_at_https_urls(self):
+ assert parse_pip(["foo @ https://example.com"]) == {}
+
+
+class TestGetInstalled:
+ def test_single(self):
+ with mock_get_distributions({"package": "1.0.0"}):
+ result = get_installed()
+
+ assert result == {"package": "1.0.0"}
+
+ def test_several(self):
+ versions = {"package-one": "1.0.0", "package-two": "2.0.0"}
+ with mock_get_distributions(versions):
+ result = get_installed()
+
+ assert result == {"package-one": "1.0.0", "package-two": "2.0.0"}
+
+ def test_normalize_case(self):
+ # importlib.metadata should already lowercase names, but we do so to
+ # be sure
+ with mock_get_distributions({"Package": "1.0.0"}):
+ result = get_installed()
+
+ assert result == {"package": "1.0.0"}
+
+ def test_normalize_underscore(self):
+ with mock_get_distributions({"package_one": "1.0.0"}):
+ result = get_installed()
+
+ assert result == {"package-one": "1.0.0"}
+
+ def test_normalize_dots(self):
+ with mock_get_distributions({"package.one": "1.0.0"}):
+ result = get_installed()
+
+ assert result == {"package-one": "1.0.0"}
+
+
+class TestGetMismatches:
+ def test_relative_requirements_file(self, monkeypatch, tmp_path):
+ monkeypatch.chdir(tmp_path)
+ requirements = tmp_path / "requirements.txt"
+ requirements.write_text("package==1.2\n")
+
+ with mock_get_distributions({"package": "1.1"}):
+ result = get_mismatches("requirements.txt")
+
+ assert result == {"package": ("1.2", "1.1")}
+
+ def test_version_mismatch(self, monkeypatch, tmp_path):
+ monkeypatch.chdir(tmp_path)
+ requirements = tmp_path / "requirements.txt"
+ requirements.write_text("package==1.2\n")
+
+ with mock_get_distributions({"package": "1.1"}):
+ result = get_mismatches("requirements.txt")
+
+ assert result == {"package": ("1.2", "1.1")}
+
+ def test_missing(self, tmp_path):
+ requirements = tmp_path / "requirements.txt"
+ requirements.write_text("package==1.1\n")
+
+ with mock_get_distributions({}):
+ result = get_mismatches(str(requirements))
+
+ assert result == {"package": ("1.1", None)}
+
+ def test_no_mismatches(self, tmp_path):
+ requirements = tmp_path / "requirements.txt"
+ requirements.write_text("package==1.1\n")
+
+ with mock_get_distributions({"package": "1.1"}):
+ result = get_mismatches(str(requirements))
+
+ assert result == {}
+
+ def test_no_mismatches_case_insensitive(self, tmp_path):
+ requirements = tmp_path / "requirements.txt"
+ requirements.write_text("package==1.1\n")
+
+ with mock_get_distributions({"Package": "1.1"}):
+ result = get_mismatches(str(requirements))
+
+ assert result == {}
+
+ def test_empty(self, tmp_path):
+ requirements = tmp_path / "requirements.txt"
+ requirements.write_text("\n")
+
+ with mock_get_distributions({}):
+ result = get_mismatches(str(requirements))
+
+ assert result == {}
+
+ def test_package_with_extra(self, tmp_path):
+ requirements = tmp_path / "requirements.txt"
+ requirements.write_text("package[anextra]==1.1\n")
+
+ with mock_get_distributions({"package": "1.1"}):
+ result = get_mismatches(str(requirements))
+
+ assert result == {}
+
+
+class TestPrintErrors:
+ def test_errors(self, capsys):
+ print_errors(["error message 1", "error message 2"])
+ _, err = capsys.readouterr()
+ assert "error message 1" in err
+ assert "error message 2" in err
+
+ def test_pre_post_text(self, capsys):
+ print_errors(["error message"], "pre text", "post text")
+ _, err = capsys.readouterr()
+ assert "error message" in err
+ assert "pre text" in err
+ assert "post text" in err
+
+
+class TestCheckRequirements:
+ @mock.patch("pip_lock.get_mismatches")
+ def test_no_mismatches(self, get_mismatches):
+ get_mismatches.return_value = {}
+ check_requirements("requirements.txt")
+
+ @mock.patch("pip_lock.get_mismatches")
+ def test_mismatches(self, get_mismatches, capsys):
+ get_mismatches.return_value = {
+ "package1": ("1.1", "1.0"),
+ "package2": ("1.0", None),
+ }
+ with pytest.raises(SystemExit):
+ check_requirements("requirements.txt")
+
+ _, err = capsys.readouterr()
+ assert "package1 has version 1.1 but you have version 1.0 installed" in err
+ assert "package2 is in requirements.txt but not in virtualenv" in err
+
+ def test_relative_requirements_file(self, monkeypatch, tmp_path):
+ monkeypatch.chdir(tmp_path)
+ requirements = tmp_path / "requirements.txt"
+ requirements.write_text("package==1.2\n")
+ with mock_get_distributions({"package": "1.2"}):
+ check_requirements("requirements.txt")
|