diff --git a/tests/remote/unit/test_remote_command_unknown_tag.py b/tests/remote/unit/test_remote_command_unknown_tag.py index 3f4c5034..546fdec3 100644 --- a/tests/remote/unit/test_remote_command_unknown_tag.py +++ b/tests/remote/unit/test_remote_command_unknown_tag.py @@ -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 @@ -10,7 +10,17 @@ import pytest from unshackle.core.services import Services 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", "url": "https://example.com", @@ -55,3 +65,10 @@ def test_fetch_failure_keeps_stub_instead_of_raising() -> None: cmd = _make("EXAMPLE") assert cmd.name == "EXAMPLE" 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} diff --git a/tests/remote/unit/test_session_log.py b/tests/remote/unit/test_session_log.py index d175494d..1fcde984 100644 --- a/tests/remote/unit/test_session_log.py +++ b/tests/remote/unit/test_session_log.py @@ -146,6 +146,24 @@ def test_service_params_are_the_only_service_options_when_present() -> None: 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: """RemoteClient raises SystemExit on transport errors; the drain must swallow it.""" from unshackle.core.remote_service import RemoteService diff --git a/unshackle/core/api/handlers.py b/unshackle/core/api/handlers.py index d26462b7..1595c6b8 100644 --- a/unshackle/core/api/handlers.py +++ b/unshackle/core/api/handlers.py @@ -252,6 +252,11 @@ def instantiate_service( 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"} 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)) if data and data.get("service_params") is None: for k in names & transport_keys & data.keys(): diff --git a/unshackle/core/services.py b/unshackle/core/services.py index 44f97352..589bb7ab 100644 --- a/unshackle/core/services.py +++ b/unshackle/core/services.py @@ -572,7 +572,7 @@ class Services(click.Group): kwargs["multiple"] = True if param.get("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