fix(webapp): keep -p <profile> in the --no-open hint after a private launch

-p re-homes only the process it is given to, so after `hermes -p work webapp` the hint's bare `hermes webapp --no-open` opened the sticky profile's Webapp instead. The hint now names the profile when -p chose one a bare rerun would not pick.
This commit is contained in:
unsupportedpastels authored and Mark S. committed 2026-10-25 23:32:04 -04:00
1 parent 395c673739
commit c2c5eef149
2 files changed
+29 -1

No files matched your search

+12 -1
View File
@@ -145,6 +145,17 @@ def _profile_choice(home: Path, target: Path) -> dict:
return {"profile": name} if name and name != "default" else {}
def _rerun_command(*flags: str) -> str:
"""``hermes webapp *flags*`` for the profile this run used: ``-p`` re-homed only this process, so a
bare rerun gets the sticky profile, and the command keeps ``-p <name>`` when that names another one."""
import shlex
from hermes_cli.main import explicit_cli_profile
from hermes_cli.profiles import get_active_profile
name = explicit_cli_profile()
profile = ["-p", shlex.quote(name)] if name and name != get_active_profile() else []
return " ".join(["hermes", *profile, "webapp", *flags])
def older_gateway(response: dict) -> bool:
"""Whether *response* is a gateway's answer to a verb it does not know: it predates the Webapp."""
return not response.get("ok") and str(response.get("error") or "").startswith("unknown verb:")
@@ -347,7 +358,7 @@ def cmd_webapp(args) -> int:
# carries it to the browser; failing that, the user opens it by hand.
if not args.no_open and _open_privately(target, link["url"]):
print("Opened the Webapp in your browser.")
print("If no browser opened, run `hermes webapp --no-open` for a link.")
print(f"If no browser opened, run `{_rerun_command('--no-open')}` for a link.")
return 0
print(f"Open this link in your browser within {link['expires_in_seconds']} seconds. It works "
"once and signs the browser in as you, so keep it private:")
@@ -16,6 +16,7 @@ from urllib.parse import unquote, urlsplit
import pytest
from hermes_cli import main as cli_main
from hermes_cli import webapp_launcher as launcher
from tests.hermes_cli import test_webapp_launcher as launcher_tests
@@ -42,6 +43,7 @@ def _page_path(url: str) -> Path:
@pytest.mark.platforms("posix")
def test_a_private_link_reaches_the_browser_in_a_page_only_the_user_can_read(private, capsys):
private["monkeypatch"].setattr(cli_main, "_explicit_cli_profile", None)
assert launcher.cmd_webapp(_args()) == 0
[opened] = private["calls"]["opened"]
assert "ticket" not in opened
@@ -57,6 +59,21 @@ def test_a_private_link_reaches_the_browser_in_a_page_only_the_user_can_read(pri
assert "If no browser opened, run `hermes webapp --no-open` for a link." in out
@pytest.mark.platforms("posix")
@pytest.mark.parametrize(("flag", "sticky", "command"), [
("work", None, "hermes -p work webapp --no-open"),
("default", "work", "hermes -p default webapp --no-open"),
("work", "work", "hermes webapp --no-open"),
])
def test_the_no_open_hint_keeps_the_profile_a_bare_rerun_would_not_pick(private, capsys, flag, sticky, command):
"""``-p`` re-homes only this process: a bare ``hermes webapp --no-open`` gets the sticky profile."""
private["monkeypatch"].setattr(cli_main, "_explicit_cli_profile", flag)
if sticky:
(private["root"] / "active_profile").write_text(sticky + "\n", encoding="utf-8")
assert launcher.cmd_webapp(_args()) == 0
assert f"If no browser opened, run `{command}` for a link." in capsys.readouterr().out
@pytest.mark.platforms("posix")
def test_a_folder_other_users_could_read_is_closed_before_the_page_is_written(private):
folder = private["root"] / "webapp"