diff --git a/README.md b/README.md index 6cc2240..d66afd2 100644 --- a/README.md +++ b/README.md @@ -381,6 +381,7 @@ See also git tags: https://github.com/jedie/manageprojects/tags [comment]: <> (✂✂✂ auto generated history start ✂✂✂) * [v0.23.0](https://github.com/jedie/manageprojects/compare/v0.22.1...v0.23.0) + * 2025-09-04 - Bugfix type error in publish version check * 2025-09-04 - ruff: remove "force-sort-within-sections = true" (Just use the default) * 2024-11-21 - Switch from click to tyro * [v0.22.1](https://github.com/jedie/manageprojects/compare/v0.22.0...v0.22.1) diff --git a/manageprojects/tests/test_utilities_publish.py b/manageprojects/tests/test_utilities_publish.py index bbbada5..1a8f917 100644 --- a/manageprojects/tests/test_utilities_publish.py +++ b/manageprojects/tests/test_utilities_publish.py @@ -2,21 +2,23 @@ import sys import tomllib from pathlib import Path -from unittest import TestCase from unittest.mock import patch +from bx_py_utils.test_utils.redirect import RedirectOut from cli_base.cli_tools import subprocess_utils from cli_base.cli_tools.test_utils.git_utils import init_git from cli_base.cli_tools.test_utils.logs import AssertLogs +from cli_base.cli_tools.test_utils.rich_test_utils import NoColorEnvRich from packaging.version import Version import manageprojects from manageprojects.cli_dev import PACKAGE_ROOT from manageprojects.test_utils.subprocess import FakeStdout, SubprocessCallMock -from manageprojects.tests.base import GIT_BIN_PARENT +from manageprojects.tests.base import GIT_BIN_PARENT, BaseTestCase from manageprojects.utilities.publish import ( PublisherGit, build, + check_version, clean_version, get_pyproject_toml_version, hatchling_dynamic_version, @@ -25,7 +27,7 @@ from manageprojects.utilities.temp_path import TemporaryDirectory -class PublishTestCase(TestCase): +class PublishTestCase(BaseTestCase): def test_build(self): def return_callback(popenargs, args, kwargs): return FakeStdout(stdout='Mocked run output') @@ -216,3 +218,33 @@ def test_hatchling_dynamic_version(self): ) self.assertIsInstance(version, Version) self.assertEqual(version, Version(manageprojects.__version__)) + + def test_check_version(self): + # Enforce matching versions: + with patch('manageprojects.utilities.publish.version', return_value=manageprojects.__version__): + module_version = check_version( + module=manageprojects, + package_path=PACKAGE_ROOT, + ) + self.assertEqual(module_version, Version(manageprojects.__version__)) + + # Error case: + with ( + NoColorEnvRich(), + RedirectOut() as buffer, + self.assertRaises(SystemExit), + patch('manageprojects.utilities.publish.version', return_value='0.1.2'), + ): + module_version = check_version( + module=manageprojects, + package_path=PACKAGE_ROOT, + ) + self.assertEqual(module_version, Version(manageprojects.__version__)) + self.assertEqual(buffer.stderr, '') + self.assert_in_content( + got=buffer.stdout, + parts=( + f'Version mismatch: current version {manageprojects.__version__} is not the installed one: 0.1.2', + '(Hint: Install package and run publish again)', + ), + ) diff --git a/manageprojects/utilities/publish.py b/manageprojects/utilities/publish.py index fef73c8..d6145c5 100644 --- a/manageprojects/utilities/publish.py +++ b/manageprojects/utilities/publish.py @@ -204,8 +204,12 @@ def check_version(*, module, package_path: Path, distribution_name: str | None = if not distribution_name: distribution_name = module.__name__ - module_version = clean_version(module.__version__) - installed_version = clean_version(version(distribution_name)) + if module_version := module.__version__: + module_version = clean_version(module_version) + + if installed_version := version(distribution_name): + installed_version = clean_version(installed_version) + if module_version != installed_version: exit_with_error( f'Version mismatch: current version {module_version} is not the installed one: {installed_version}',