Skip to content

Commit 4f50cfd

Browse files
authored
Merge pull request #43 from cachedjdk/ls-vendors-os-arch
ls-vendors/list_vendors() filter by OS/arch
2 parents 882ad8d + 5f907d1 commit 4f50cfd

File tree

7 files changed

+82
-11
lines changed

7 files changed

+82
-11
lines changed

docs/api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ SPDX-License-Identifier: MIT
1111
```{eval-rst}
1212
.. autofunction:: cjdk.list_vendors
1313
.. versionadded:: 0.4.0
14+
.. versionchanged:: 0.6.0
15+
Now filters vendors by the given os/arch, which default to the current
16+
platform.
1417
```
1518

1619
```{eval-rst}

docs/changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ See also the section on [versioning](versioning-scheme).
1010

1111
## [Unreleased]
1212

13+
### Changed
14+
15+
- `list_vendors()` and `ls-vendors` now filter vendors by OS and architecture,
16+
defaulting to the current platform.
17+
1318
## [0.5.0] - 2026-01-07
1419

1520
### Added

docs/cli.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ successful, returns the exit code of the launched program.
5252

5353
```{eval-rst}
5454
.. versionadded:: 0.4.0
55+
.. versionchanged:: 0.6.0
56+
Now filters vendors by the ``--os`` and ``--arch`` options, which default to
57+
the current platform.
5558
```
5659

5760
## Working with cached JDKs

src/cjdk/_api.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ def list_vendors(**kwargs: Unpack[ConfigKwargs]) -> list[str]:
4949
"""
5050
Return the list of available JDK vendors.
5151
52-
Parameters
53-
----------
54-
None
55-
5652
Other Parameters
5753
----------------
5854
cache_dir : pathlib.Path or str, optional
5955
Override the root cache directory.
6056
index_url : str, optional
6157
Alternative URL for the JDK index.
58+
os : str, optional
59+
Operating system for the JDK (default: current operating system).
60+
arch : str, optional
61+
CPU architecture for the JDK (default: current architecture).
6262
6363
Returns
6464
-------
@@ -71,6 +71,10 @@ def list_vendors(**kwargs: Unpack[ConfigKwargs]) -> list[str]:
7171
If configuration is invalid.
7272
InstallError
7373
If fetching the index fails.
74+
75+
.. versionchanged:: 0.6.0
76+
Now filters vendors by the given os/arch, which default to the current
77+
platform.
7478
"""
7579
conf = _conf.configure(**kwargs)
7680
return sorted(_jdk.available_vendors(conf))

src/cjdk/_jdk.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,19 @@
2929

3030
def available_vendors(conf: Configuration) -> set[str]:
3131
"""
32-
Return the set of available JDK vendor names.
32+
Return the set of available JDK vendor names for the configured OS and arch.
3333
3434
Arguments:
35-
conf -- Configuration (currently unused, for future os/arch filtering)
35+
conf -- Configuration (uses os and arch for filtering)
3636
"""
37-
_ = conf
3837
index = _index.jdk_index(conf)
38+
try:
39+
vendors = index[conf.os][conf.arch]
40+
except KeyError:
41+
return set()
3942
return {
4043
vendor.removeprefix("jdk@")
41-
for os in index
42-
for arch in index[os]
43-
for vendor in index[os][arch]
44+
for vendor in vendors
4445
if vendor.startswith("jdk@")
4546
}
4647

tests/test_api.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,58 @@ def test_env_var_set():
154154
with f("CJDK_TEST_ENV_VAR", "testvalue"):
155155
assert os.environ["CJDK_TEST_ENV_VAR"] == "testvalue"
156156
assert "CJDK_TEST_ENV_VAR" not in os.environ
157+
158+
159+
def test_list_vendors(tmp_path):
160+
with mock_server.start(
161+
endpoint="/index.json",
162+
data={
163+
"linux": {
164+
"amd64": {
165+
"jdk@adoptium": {"17": "zip+http://example.com/a.zip"},
166+
"jdk@zulu": {"17": "zip+http://example.com/z.zip"},
167+
},
168+
"arm64": {
169+
"jdk@adoptium": {"17": "zip+http://example.com/a.zip"},
170+
},
171+
},
172+
"darwin": {
173+
"amd64": {
174+
"jdk@graalvm": {"17": "zip+http://example.com/g.zip"},
175+
},
176+
},
177+
},
178+
) as server:
179+
vendors_linux_amd64 = _api.list_vendors(
180+
os="linux",
181+
arch="amd64",
182+
cache_dir=tmp_path / "cache",
183+
index_url=server.url("/index.json"),
184+
_allow_insecure_for_testing=True,
185+
)
186+
vendors_linux_arm64 = _api.list_vendors(
187+
os="linux",
188+
arch="arm64",
189+
cache_dir=tmp_path / "cache",
190+
index_url=server.url("/index.json"),
191+
_allow_insecure_for_testing=True,
192+
)
193+
vendors_darwin_amd64 = _api.list_vendors(
194+
os="darwin",
195+
arch="amd64",
196+
cache_dir=tmp_path / "cache",
197+
index_url=server.url("/index.json"),
198+
_allow_insecure_for_testing=True,
199+
)
200+
vendors_nonexistent = _api.list_vendors(
201+
os="windows",
202+
arch="amd64",
203+
cache_dir=tmp_path / "cache",
204+
index_url=server.url("/index.json"),
205+
_allow_insecure_for_testing=True,
206+
)
207+
208+
assert vendors_linux_amd64 == ["adoptium", "zulu"]
209+
assert vendors_linux_arm64 == ["adoptium"]
210+
assert vendors_darwin_amd64 == ["graalvm"]
211+
assert vendors_nonexistent == []

tests/test_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
def test_list_vendors():
9-
vendors = _api.list_vendors()
9+
vendors = _api.list_vendors(os="linux", arch="amd64")
1010
assert vendors is not None
1111
assert "adoptium" in vendors
1212
assert "corretto" in vendors

0 commit comments

Comments
 (0)