Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/core/test_autoupdate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from unittest.mock import Mock

from titan_cli.utils import autoupdate


def test_update_core_prefers_runtime_version_after_pipx_upgrade(mocker):
"""Report the version from the updated runtime when pipx upgrade succeeds."""
mock_run = mocker.patch("titan_cli.utils.autoupdate.subprocess.run")
mock_run.return_value = Mock(returncode=0, stdout="", stderr="")
mocker.patch(
"titan_cli.utils.autoupdate._get_installed_version_runtime",
return_value="0.3.0",
)
mocker.patch(
"titan_cli.utils.autoupdate._get_installed_version_pipx",
return_value="0.2.0",
)

result = autoupdate.update_core()

assert result["success"] is True
assert result["method"] == "pipx"
assert result["installed_version"] == "0.3.0"
24 changes: 22 additions & 2 deletions titan_cli/utils/autoupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def update_core() -> Dict[str, any]:
)

if proc.returncode == 0:
installed_version = _get_installed_version_pipx()
installed_version = _get_installed_version_runtime() or _get_installed_version_pipx()
if installed_version:
result["success"] = True
result["method"] = "pipx"
Expand All @@ -161,7 +161,7 @@ def update_core() -> Dict[str, any]:
)

if proc.returncode == 0:
installed_version = _get_installed_version_pip()
installed_version = _get_installed_version_runtime() or _get_installed_version_pip()
if installed_version:
result["success"] = True
result["method"] = "pip"
Expand Down Expand Up @@ -227,6 +227,26 @@ def update_plugins() -> Dict[str, any]:
return result


def _get_installed_version_runtime() -> Optional[str]:
"""Get installed version from the current Python runtime."""
try:
proc = subprocess.run(
[
sys.executable,
"-c",
"from importlib.metadata import version; print(version('titan-cli'))",
],
capture_output=True,
text=True,
timeout=10,
)
if proc.returncode == 0:
return proc.stdout.strip() or None
except Exception:
pass
return None


def _get_installed_version_pipx() -> Optional[str]:
"""Get installed version of titan-cli from pipx."""
try:
Expand Down