|
| 1 | +# Copyright (c) 2026 ByteDance Ltd. and/or its affiliates. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import asyncio |
| 7 | +from importlib.metadata import version |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import httpx |
| 11 | +import pytest |
| 12 | + |
| 13 | +from arkruntime import Ark, AsyncArk, _version |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize("custom", [None, "my-app/2.0"]) |
| 17 | +@pytest.mark.parametrize("asynchronous", [False, True]) |
| 18 | +def test_user_agent_on_requests(custom: str | None, asynchronous: bool) -> None: |
| 19 | + requests = [] |
| 20 | + |
| 21 | + def handle(request: httpx.Request) -> httpx.Response: |
| 22 | + requests.append(request) |
| 23 | + return httpx.Response(200, json={"id": "test-file", "object": "file"}) |
| 24 | + |
| 25 | + headers = {"User-Agent": custom} if custom else {} |
| 26 | + transport = httpx.MockTransport(handle) |
| 27 | + if asynchronous: |
| 28 | + |
| 29 | + async def run() -> None: |
| 30 | + async with AsyncArk(api_key="placeholder", http_client=httpx.AsyncClient(transport=transport)) as client: |
| 31 | + await client.files.retrieve("test-file", extra_headers=headers) |
| 32 | + |
| 33 | + asyncio.run(run()) |
| 34 | + else: |
| 35 | + with Ark(api_key="placeholder", http_client=httpx.Client(transport=transport)) as client: |
| 36 | + client.files.retrieve("test-file", extra_headers=headers) |
| 37 | + |
| 38 | + assert len(requests) == 1 |
| 39 | + assert requests[0].headers.get_list("User-Agent") == [custom or "ark-runtime-python/" + version("arkruntime")] |
| 40 | + |
| 41 | + |
| 42 | +def test_checkout_version_takes_precedence_over_installed_metadata( |
| 43 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 44 | +) -> None: |
| 45 | + source = tmp_path / "src" / "arkruntime" / "_version.py" |
| 46 | + monkeypatch.setattr(_version, "__file__", str(source)) |
| 47 | + monkeypatch.setattr(_version, "version", lambda name: "0.0.1") |
| 48 | + (tmp_path / "pyproject.toml").write_text( |
| 49 | + '[tool.example]\nversion = "9.9.9"\n[project]\nname = "arkruntime"\nversion = "2.3.4rc1"\n' |
| 50 | + '[tool.other]\nversion = "8.8.8"\n', |
| 51 | + encoding="utf-8", |
| 52 | + ) |
| 53 | + assert _version._get_version() == "2.3.4rc1" |
| 54 | + |
| 55 | + |
| 56 | +def test_installed_version_uses_distribution_metadata(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 57 | + monkeypatch.setattr(_version, "__file__", str(tmp_path / "site-packages" / "arkruntime" / "_version.py")) |
| 58 | + |
| 59 | + def metadata_version(name: str) -> str: |
| 60 | + assert name == "arkruntime" |
| 61 | + return "3.4.5" |
| 62 | + |
| 63 | + monkeypatch.setattr(_version, "version", metadata_version) |
| 64 | + assert _version._get_version() == "3.4.5" |
| 65 | + |
| 66 | + |
| 67 | +def test_missing_source_version_does_not_use_stale_metadata(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 68 | + monkeypatch.setattr(_version, "__file__", str(tmp_path / "src" / "arkruntime" / "_version.py")) |
| 69 | + (tmp_path / "pyproject.toml").write_text('[project]\nname = "arkruntime"\n', encoding="utf-8") |
| 70 | + with pytest.raises(RuntimeError, match="Expected a static project.version"): |
| 71 | + _version._get_version() |
0 commit comments