mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-25 10:25:09 +00:00
fix(remote): send a service option under its served name
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
"""make_remote_command resolves names against the remote server's list and reports the failures."""
|
"""make_remote_command resolves names against the remote server's list, reports the failures and keeps served option names."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -10,7 +10,17 @@ import pytest
|
|||||||
from unshackle.core.services import Services
|
from unshackle.core.services import Services
|
||||||
|
|
||||||
SERVED = [
|
SERVED = [
|
||||||
{"tag": "EXAMPLE", "url": "https://example.com", "help": "Example service.", "cli_params": [], "aliases": []},
|
{
|
||||||
|
"tag": "EXAMPLE",
|
||||||
|
"url": "https://example.com",
|
||||||
|
"help": "Example service.",
|
||||||
|
"cli_params": [
|
||||||
|
{"kind": "option", "name": "zone_code", "opts": ["--zone"], "is_flag": False, "default": None},
|
||||||
|
{"kind": "option", "name": "flavour_name", "opts": ["--flavour"], "is_flag": False, "choices": ["a", "b"]},
|
||||||
|
{"kind": "option", "name": "loud_mode", "opts": ["--loud"], "is_flag": True, "default": False},
|
||||||
|
],
|
||||||
|
"aliases": [],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"tag": "DEMO",
|
"tag": "DEMO",
|
||||||
"url": "https://example.com",
|
"url": "https://example.com",
|
||||||
@@ -55,3 +65,10 @@ def test_fetch_failure_keeps_stub_instead_of_raising() -> None:
|
|||||||
cmd = _make("EXAMPLE")
|
cmd = _make("EXAMPLE")
|
||||||
assert cmd.name == "EXAMPLE"
|
assert cmd.name == "EXAMPLE"
|
||||||
assert cmd.help is None
|
assert cmd.help is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_options_keep_the_served_name() -> None:
|
||||||
|
with patch.object(Services, "fetch_remote_services", return_value=SERVED):
|
||||||
|
cmd = _make("EXAMPLE")
|
||||||
|
ctx = cmd.make_context("EXAMPLE", ["--zone", "eu", "--flavour", "b", "--loud", "an-id"])
|
||||||
|
assert ctx.params == {"title": "an-id", "zone_code": "eu", "flavour_name": "b", "loud_mode": True}
|
||||||
|
|||||||
@@ -146,6 +146,24 @@ def test_service_params_are_the_only_service_options_when_present() -> None:
|
|||||||
build({"service_params": {"count": "x"}})
|
build({"service_params": {"count": "x"}})
|
||||||
|
|
||||||
|
|
||||||
|
def test_service_params_keyed_by_flag_reach_an_option_with_another_name() -> None:
|
||||||
|
import click
|
||||||
|
|
||||||
|
from unshackle.core.api.handlers import SESSION_TRANSPORT_KEYS, instantiate_service
|
||||||
|
|
||||||
|
@click.command()
|
||||||
|
@click.argument("title")
|
||||||
|
@click.option("--zone", "zone_code", default=None)
|
||||||
|
@click.option("--lang", "svc_lang", default=None)
|
||||||
|
def cli(title: str, **kwargs: object) -> dict:
|
||||||
|
return kwargs
|
||||||
|
|
||||||
|
module = type("Svc", (), {"cli": cli})
|
||||||
|
data = {"service_params": {"zone": "eu", "svc_lang": "de", "lang": "fr"}}
|
||||||
|
built = instantiate_service(click.Context(click.Command("dl")), module, "t", data, SESSION_TRANSPORT_KEYS)
|
||||||
|
assert built == {"zone_code": "eu", "svc_lang": "de"}
|
||||||
|
|
||||||
|
|
||||||
def test_drain_server_logs_survives_client_systemexit() -> None:
|
def test_drain_server_logs_survives_client_systemexit() -> None:
|
||||||
"""RemoteClient raises SystemExit on transport errors; the drain must swallow it."""
|
"""RemoteClient raises SystemExit on transport errors; the drain must swallow it."""
|
||||||
from unshackle.core.remote_service import RemoteService
|
from unshackle.core.remote_service import RemoteService
|
||||||
|
|||||||
@@ -252,6 +252,11 @@ def instantiate_service(
|
|||||||
cli_params = getattr(getattr(service_module, "cli", None), "params", []) or []
|
cli_params = getattr(getattr(service_module, "cli", None), "params", []) or []
|
||||||
names = {p.name for p in cli_params if getattr(p, "name", None) and p.name != "title"}
|
names = {p.name for p in cli_params if getattr(p, "name", None) and p.name != "title"}
|
||||||
data = data or {}
|
data = data or {}
|
||||||
|
nested = data.get("service_params")
|
||||||
|
if isinstance(nested, dict):
|
||||||
|
flagged = {click.Option(p.opts).name: p.name for p in cli_params if p.param_type_name == "option" and p.opts}
|
||||||
|
renamed = {flagged.get(k, k): v for k, v in nested.items() if k not in names}
|
||||||
|
data = {**data, "service_params": {**renamed, **nested}}
|
||||||
extras = cast_or_refuse(cli_params, parent_ctx, service_options(data, names, transport_keys))
|
extras = cast_or_refuse(cli_params, parent_ctx, service_options(data, names, transport_keys))
|
||||||
if data and data.get("service_params") is None:
|
if data and data.get("service_params") is None:
|
||||||
for k in names & transport_keys & data.keys():
|
for k in names & transport_keys & data.keys():
|
||||||
|
|||||||
@@ -572,7 +572,7 @@ class Services(click.Group):
|
|||||||
kwargs["multiple"] = True
|
kwargs["multiple"] = True
|
||||||
if param.get("help"):
|
if param.get("help"):
|
||||||
kwargs["help"] = param["help"]
|
kwargs["help"] = param["help"]
|
||||||
remote_cli = click.option(*opts, **kwargs)(remote_cli)
|
remote_cli = click.option(param["name"], *opts, **kwargs)(remote_cli)
|
||||||
|
|
||||||
return remote_cli
|
return remote_cli
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user