From 5f907d192196373b7bcc6542d676afe87f78af08 Mon Sep 17 00:00:00 2001 From: "Mark A. Tsuchida" Date: Mon, 19 Jan 2026 18:04:17 -0600 Subject: [PATCH] ls-vendors/list_vendors() filter by OS/arch A breaking change, but probably almost always what is wanted. (Assisted by Claude Code; any errors are mine.) --- docs/api.md | 3 +++ docs/changelog.md | 5 ++++ docs/cli.md | 3 +++ src/cjdk/_api.py | 12 ++++++--- src/cjdk/_jdk.py | 13 ++++----- tests/test_api.py | 55 +++++++++++++++++++++++++++++++++++++++ tests/test_integration.py | 2 +- 7 files changed, 82 insertions(+), 11 deletions(-) diff --git a/docs/api.md b/docs/api.md index 96d0ef8..3cd79f8 100644 --- a/docs/api.md +++ b/docs/api.md @@ -11,6 +11,9 @@ SPDX-License-Identifier: MIT ```{eval-rst} .. autofunction:: cjdk.list_vendors .. versionadded:: 0.4.0 +.. versionchanged:: 0.6.0 + Now filters vendors by the given os/arch, which default to the current + platform. ``` ```{eval-rst} diff --git a/docs/changelog.md b/docs/changelog.md index 07c66d0..6ed522d 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -10,6 +10,11 @@ See also the section on [versioning](versioning-scheme). ## [Unreleased] +### Changed + +- `list_vendors()` and `ls-vendors` now filter vendors by OS and architecture, + defaulting to the current platform. + ## [0.5.0] - 2026-01-07 ### Added diff --git a/docs/cli.md b/docs/cli.md index b2634fa..1460ad9 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -52,6 +52,9 @@ successful, returns the exit code of the launched program. ```{eval-rst} .. versionadded:: 0.4.0 +.. versionchanged:: 0.6.0 + Now filters vendors by the ``--os`` and ``--arch`` options, which default to + the current platform. ``` ## Working with cached JDKs diff --git a/src/cjdk/_api.py b/src/cjdk/_api.py index f4b07b0..34f456e 100644 --- a/src/cjdk/_api.py +++ b/src/cjdk/_api.py @@ -49,16 +49,16 @@ def list_vendors(**kwargs: Unpack[ConfigKwargs]) -> list[str]: """ Return the list of available JDK vendors. - Parameters - ---------- - None - Other Parameters ---------------- cache_dir : pathlib.Path or str, optional Override the root cache directory. index_url : str, optional Alternative URL for the JDK index. + os : str, optional + Operating system for the JDK (default: current operating system). + arch : str, optional + CPU architecture for the JDK (default: current architecture). Returns ------- @@ -71,6 +71,10 @@ def list_vendors(**kwargs: Unpack[ConfigKwargs]) -> list[str]: If configuration is invalid. InstallError If fetching the index fails. + + .. versionchanged:: 0.6.0 + Now filters vendors by the given os/arch, which default to the current + platform. """ conf = _conf.configure(**kwargs) return sorted(_jdk.available_vendors(conf)) diff --git a/src/cjdk/_jdk.py b/src/cjdk/_jdk.py index d79c340..b2c940e 100644 --- a/src/cjdk/_jdk.py +++ b/src/cjdk/_jdk.py @@ -29,18 +29,19 @@ def available_vendors(conf: Configuration) -> set[str]: """ - Return the set of available JDK vendor names. + Return the set of available JDK vendor names for the configured OS and arch. Arguments: - conf -- Configuration (currently unused, for future os/arch filtering) + conf -- Configuration (uses os and arch for filtering) """ - _ = conf index = _index.jdk_index(conf) + try: + vendors = index[conf.os][conf.arch] + except KeyError: + return set() return { vendor.removeprefix("jdk@") - for os in index - for arch in index[os] - for vendor in index[os][arch] + for vendor in vendors if vendor.startswith("jdk@") } diff --git a/tests/test_api.py b/tests/test_api.py index c32d860..af2aea9 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -154,3 +154,58 @@ def test_env_var_set(): with f("CJDK_TEST_ENV_VAR", "testvalue"): assert os.environ["CJDK_TEST_ENV_VAR"] == "testvalue" assert "CJDK_TEST_ENV_VAR" not in os.environ + + +def test_list_vendors(tmp_path): + with mock_server.start( + endpoint="/index.json", + data={ + "linux": { + "amd64": { + "jdk@adoptium": {"17": "zip+http://example.com/a.zip"}, + "jdk@zulu": {"17": "zip+http://example.com/z.zip"}, + }, + "arm64": { + "jdk@adoptium": {"17": "zip+http://example.com/a.zip"}, + }, + }, + "darwin": { + "amd64": { + "jdk@graalvm": {"17": "zip+http://example.com/g.zip"}, + }, + }, + }, + ) as server: + vendors_linux_amd64 = _api.list_vendors( + os="linux", + arch="amd64", + cache_dir=tmp_path / "cache", + index_url=server.url("/index.json"), + _allow_insecure_for_testing=True, + ) + vendors_linux_arm64 = _api.list_vendors( + os="linux", + arch="arm64", + cache_dir=tmp_path / "cache", + index_url=server.url("/index.json"), + _allow_insecure_for_testing=True, + ) + vendors_darwin_amd64 = _api.list_vendors( + os="darwin", + arch="amd64", + cache_dir=tmp_path / "cache", + index_url=server.url("/index.json"), + _allow_insecure_for_testing=True, + ) + vendors_nonexistent = _api.list_vendors( + os="windows", + arch="amd64", + cache_dir=tmp_path / "cache", + index_url=server.url("/index.json"), + _allow_insecure_for_testing=True, + ) + + assert vendors_linux_amd64 == ["adoptium", "zulu"] + assert vendors_linux_arm64 == ["adoptium"] + assert vendors_darwin_amd64 == ["graalvm"] + assert vendors_nonexistent == [] diff --git a/tests/test_integration.py b/tests/test_integration.py index ae21b19..8584570 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -6,7 +6,7 @@ def test_list_vendors(): - vendors = _api.list_vendors() + vendors = _api.list_vendors(os="linux", arch="amd64") assert vendors is not None assert "adoptium" in vendors assert "corretto" in vendors