|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Tests for check_version_uniqueness.py.""" |
| 3 | + |
| 4 | +import os |
| 5 | +import urllib.error |
| 6 | +from unittest import mock |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from check_version_uniqueness import ( |
| 11 | + get_package_info, |
| 12 | + main, |
| 13 | + version_exists_on_pypi, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class TestVersionExistsOnPypi: |
| 18 | + def test_returns_true_when_version_exists(self): |
| 19 | + mock_response = mock.MagicMock() |
| 20 | + mock_response.__enter__ = mock.MagicMock(return_value=mock_response) |
| 21 | + mock_response.__exit__ = mock.MagicMock(return_value=False) |
| 22 | + |
| 23 | + with mock.patch("urllib.request.urlopen", return_value=mock_response): |
| 24 | + assert version_exists_on_pypi("some-package", "1.0.0") is True |
| 25 | + |
| 26 | + def test_returns_false_when_404(self): |
| 27 | + error = urllib.error.HTTPError( |
| 28 | + url="", code=404, msg="Not Found", hdrs=None, fp=None # type: ignore[arg-type] |
| 29 | + ) |
| 30 | + with mock.patch("urllib.request.urlopen", side_effect=error): |
| 31 | + assert version_exists_on_pypi("some-package", "9.9.9") is False |
| 32 | + |
| 33 | + def test_returns_none_on_server_error(self): |
| 34 | + error = urllib.error.HTTPError( |
| 35 | + url="", code=500, msg="Server Error", hdrs=None, fp=None # type: ignore[arg-type] |
| 36 | + ) |
| 37 | + with mock.patch("urllib.request.urlopen", side_effect=error): |
| 38 | + assert version_exists_on_pypi("some-package", "1.0.0") is None |
| 39 | + |
| 40 | + def test_returns_none_on_network_error(self): |
| 41 | + with mock.patch( |
| 42 | + "urllib.request.urlopen", side_effect=ConnectionError("no network") |
| 43 | + ): |
| 44 | + assert version_exists_on_pypi("some-package", "1.0.0") is None |
| 45 | + |
| 46 | + |
| 47 | +class TestGetPackageInfo: |
| 48 | + def test_reads_name_and_version(self, tmp_path): |
| 49 | + pkg = tmp_path / "packages" / "my-pkg" |
| 50 | + pkg.mkdir(parents=True) |
| 51 | + (pkg / "pyproject.toml").write_text( |
| 52 | + '[project]\nname = "my-pkg"\nversion = "1.2.3"\n' |
| 53 | + ) |
| 54 | + with mock.patch( |
| 55 | + "check_version_uniqueness.Path", |
| 56 | + side_effect=lambda p: tmp_path / p if p == "packages" else __import__("pathlib").Path(p), |
| 57 | + ): |
| 58 | + assert get_package_info("my-pkg") == ("my-pkg", "1.2.3") |
| 59 | + |
| 60 | + def test_returns_none_for_missing_file(self): |
| 61 | + assert get_package_info("nonexistent-package-xyz") is None |
| 62 | + |
| 63 | + |
| 64 | +class TestMain: |
| 65 | + def _run(self, changed, package_info, pypi_result): |
| 66 | + patches = [ |
| 67 | + mock.patch("check_version_uniqueness.get_changed_packages", return_value=changed), |
| 68 | + mock.patch("check_version_uniqueness.get_package_info", side_effect=lambda d: package_info.get(d)), |
| 69 | + mock.patch("check_version_uniqueness.version_exists_on_pypi", return_value=pypi_result), |
| 70 | + ] |
| 71 | + with patches[0], patches[1], patches[2]: |
| 72 | + os.environ.pop("BASE_SHA", None) |
| 73 | + os.environ.pop("HEAD_SHA", None) |
| 74 | + return main() |
| 75 | + |
| 76 | + def test_passes_when_version_not_on_pypi(self): |
| 77 | + assert self._run(["my-pkg"], {"my-pkg": ("my-pkg", "2.0.0")}, False) == 0 |
| 78 | + |
| 79 | + def test_fails_when_version_exists_on_pypi(self): |
| 80 | + assert self._run(["my-pkg"], {"my-pkg": ("my-pkg", "2.0.0")}, True) == 1 |
| 81 | + |
| 82 | + def test_fails_on_network_error(self): |
| 83 | + assert self._run(["my-pkg"], {"my-pkg": ("my-pkg", "2.0.0")}, None) == 1 |
| 84 | + |
| 85 | + def test_no_changed_packages(self): |
| 86 | + assert self._run([], {}, False) == 0 |
| 87 | + |
| 88 | + def test_fails_when_version_unchanged_but_on_pypi(self): |
| 89 | + """The key scenario: code changed, version not bumped, already on PyPI.""" |
| 90 | + assert self._run(["my-pkg"], {"my-pkg": ("my-pkg", "1.0.0")}, True) == 1 |
| 91 | + |
| 92 | + def test_multiple_packages_one_conflict(self): |
| 93 | + def pypi_check(name, version): |
| 94 | + return name == "pkg-a" |
| 95 | + |
| 96 | + with ( |
| 97 | + mock.patch("check_version_uniqueness.get_changed_packages", return_value=["pkg-a", "pkg-b"]), |
| 98 | + mock.patch( |
| 99 | + "check_version_uniqueness.get_package_info", |
| 100 | + side_effect=lambda d: {"pkg-a": ("pkg-a", "1.0.0"), "pkg-b": ("pkg-b", "2.0.0")}.get(d), |
| 101 | + ), |
| 102 | + mock.patch("check_version_uniqueness.version_exists_on_pypi", side_effect=pypi_check), |
| 103 | + ): |
| 104 | + os.environ.pop("BASE_SHA", None) |
| 105 | + os.environ.pop("HEAD_SHA", None) |
| 106 | + assert main() == 1 |
0 commit comments