Repository: aj-white/piplexed

Issue:
Pipx metada
Should probably have a check some where that check is pipx metada file is the version we expect ?

Discussion:

Solution patch:
diff --git a/src/piplexed/pipx_venvs.py b/src/piplexed/pipx_venvs.py
index 06ba5ed..5670da3 100644
--- a/src/piplexed/pipx_venvs.py
+++ b/src/piplexed/pipx_venvs.py
@@ -2,6 +2,7 @@
 
 import json
 import platform
+import warnings
 from dataclasses import dataclass
 from pathlib import Path
 
@@ -10,6 +11,7 @@
 from packaging.version import Version
 from platformdirs import user_data_path
 
+PIPX_METADATA_VERSIONS = ["0.1", "0.2", "0.3", "0.4", "0.5"]
 OS_PLATFORM = platform.system()
 
 
@@ -52,6 +54,10 @@ class PackageInfo:
     python: str | None = None
 
 
+def is_metadata_version_valid(metadata_version: str, pipx_metadata_vsn: list[str] = PIPX_METADATA_VERSIONS) -> bool:
+    return metadata_version in pipx_metadata_vsn
+
+
 def get_pipx_metadata(venv_dir: Path | None = PIPX_LOCAL_VENVS) -> list[PackageInfo]:
     venvs = []
     if venv_dir is None or not venv_dir.exists():
@@ -62,6 +68,14 @@ def get_pipx_metadata(venv_dir: Path | None = PIPX_LOCAL_VENVS) -> list[PackageI
             if item.name == "pipx_metadata.json":  # pragma: no branch
                 with open(item) as f:
                     data = json.load(f)
+                    metadata_version = data["pipx_metadata_version"]
+                    if not is_metadata_version_valid(metadata_version):
+                        warnings.warn(
+                            f"{metadata_version} is an unknown (and untested) pipx metadata version,"
+                            "results may be inaccurate",
+                            stacklevel=2,
+                            category=UserWarning,
+                        )
                     # packages installed from pypi have the same package and package_or_url
                     if data["main_package"]["package"] == data["main_package"]["package_or_url"]:
                         pkg_data = PackageInfo(


Test patch:
diff --git a/tests/test_pipx_venvs.py b/tests/test_pipx_venvs.py
index 9a069b2..e0ec669 100644
--- a/tests/test_pipx_venvs.py
+++ b/tests/test_pipx_venvs.py
@@ -9,13 +9,13 @@
 import pytest
 from packaging.version import Version
 
+from piplexed.pipx_venvs import PIPX_METADATA_VERSIONS
 from piplexed.pipx_venvs import PackageInfo
 from piplexed.pipx_venvs import get_local_venv
 from piplexed.pipx_venvs import get_pipx_metadata
+from piplexed.pipx_venvs import is_metadata_version_valid
 from piplexed.pipx_venvs import pipx_home_paths_for_os
 
-PIPX_METADATA_VERSIONS = [None, "0.1", "0.2", "0.3", "0.4", "0.5"]
-
 # Metadata changes for 0.1 -> 0.3 and 0.4 -> 0.5 were at package level
 MOCK_BASE_PIPX_METADATA: dict[str, Any] = {
     "main_package": None,
@@ -243,3 +243,32 @@ def test_default_pipx_home_not_exists_and_fallback_not_exists(mock_pipx_homes):
     default, fallbacks = mock_pipx_homes
     result = get_local_venv()
     assert result is None
+
+
+@pytest.mark.parametrize(
+    "metadata_version, expected",
+    (
+        ("0.1", True),
+        ("0.2", True),
+        ("0.3", True),
+        ("0.4", True),
+        ("0.5", True),
+        ("0.6", False),
+        ("", False),
+        (None, False),
+    ),
+)
+def test_pipx_metaversion_validation(metadata_version, expected):
+    assert is_metadata_version_valid(metadata_version, PIPX_METADATA_VERSIONS) == expected
+
+
+def test_pipx_metadata_warning(venv_dir_test_setup):
+    pypi_json = venv_dir_test_setup / "pypi_package" / "pipx_metadata.json"
+
+    with open(pypi_json, "w") as f:
+        pipx_metadata = mock_metadata(metadata_version="0.5")
+        pipx_metadata["pipx_metadata_version"] = None
+        json.dump(pipx_metadata, f)
+
+    with pytest.warns(UserWarning):
+        get_pipx_metadata(venv_dir_test_setup)