feat(cli): display custom profile alias names in profile list/show (#40371)

profile list and profile show assumed the wrapper script is always named
after the profile (wrapper_dir / name). When a custom alias exists — e.g.
`hermes profile alias steve --name qiaobusi` creates ~/.local/bin/qiaobusi
pointing at `hermes -p steve` — the display silently showed the profile
name (or nothing) instead of the alias the user actually typed.

The custom-alias *creation* path (create_wrapper_script(name, target)) was
added later; the *display* path was never updated to match.

Add find_alias_for_profile() — a reverse lookup that scans the wrapper dir
for our own wrappers (alias-named file containing 'hermes -p <profile>'),
prefers a custom alias over the profile-named one, strips .bat on Windows,
and sorts for deterministic output. Populate ProfileInfo.alias_name and wire
it into the three display sites (profile describe, list, show).

Credit: salvages the intent of #11506 by wss434631143, reimplemented on
current main against the post-#11506 custom-alias (--name/target) mechanism.

Tests: 6 new (profile-named, custom-name, none, unrelated-file rejection,
windows .bat strip, list_profiles surfacing). All 123 in test_profiles pass.
E2E verified against the real CLI for both custom and profile-named aliases.
This commit is contained in:
kshitij
2026-06-06 08:08:07 +00:00
committed by GitHub
parent c79b6f23e6
commit 5af899c7ca
3 changed files with 123 additions and 7 deletions
+57
View File
@@ -709,6 +709,63 @@ class TestWrapperScript:
assert "#!/bin/sh" not in content
# ===================================================================
# TestFindAliasForProfile — display-side reverse lookup
# ===================================================================
class TestFindAliasForProfile:
"""Tests for find_alias_for_profile() and alias display in list/show."""
def test_profile_named_alias(self, profile_env, monkeypatch):
monkeypatch.setattr("sys.platform", "darwin")
from hermes_cli.profiles import create_wrapper_script, find_alias_for_profile
create_wrapper_script("steve")
assert find_alias_for_profile("steve") == "steve"
def test_custom_alias_name_preferred(self, profile_env, monkeypatch):
# qiaobusi -> steve-jobs: the custom alias name must surface, not the
# profile name, because that's the command the user actually typed.
monkeypatch.setattr("sys.platform", "darwin")
from hermes_cli.profiles import create_wrapper_script, find_alias_for_profile
create_wrapper_script("qiaobusi", target="steve")
assert find_alias_for_profile("steve") == "qiaobusi"
def test_no_alias_returns_none(self, profile_env, monkeypatch):
monkeypatch.setattr("sys.platform", "darwin")
from hermes_cli.profiles import find_alias_for_profile
assert find_alias_for_profile("steve") is None
def test_ignores_unrelated_files(self, profile_env, monkeypatch):
# ~/.local/bin commonly holds unrelated binaries; they must not match.
monkeypatch.setattr("sys.platform", "darwin")
from hermes_cli.profiles import _get_wrapper_dir, find_alias_for_profile
wrapper_dir = _get_wrapper_dir()
wrapper_dir.mkdir(parents=True, exist_ok=True)
(wrapper_dir / "pip").write_text("#!/bin/sh\nexec python -m pip \"$@\"\n")
assert find_alias_for_profile("steve") is None
def test_custom_alias_on_windows(self, profile_env, monkeypatch):
monkeypatch.setattr("sys.platform", "win32")
from hermes_cli.profiles import create_wrapper_script, find_alias_for_profile
create_wrapper_script("qiaobusi", target="steve")
# The .bat extension must be stripped from the returned alias name.
assert find_alias_for_profile("steve") == "qiaobusi"
def test_list_profiles_surfaces_custom_alias(self, profile_env, monkeypatch):
monkeypatch.setattr("sys.platform", "darwin")
from hermes_cli.profiles import (
create_profile,
create_wrapper_script,
list_profiles,
)
create_profile("steve", no_alias=True)
create_wrapper_script("qiaobusi", target="steve")
info = next(p for p in list_profiles() if p.name == "steve")
assert info.alias_name == "qiaobusi"
assert info.alias_path is not None
assert info.alias_path.name == "qiaobusi"
# ===================================================================
# TestRenameProfile
# ===================================================================