diff --git a/tests/test_cli_cd.py b/tests/test_cli_cd.py index 4b39cab..edf6e39 100644 --- a/tests/test_cli_cd.py +++ b/tests/test_cli_cd.py @@ -25,3 +25,29 @@ def fake_run_external_command(command: str, shell: bool = True, check: bool = Fa assert Path.cwd() == target assert not called assert str(target) in capsys.readouterr().out + + +def test_handle_cd_command_expands_environment_variables(tmp_path, monkeypatch, capsys): + monkeypatch.chdir(tmp_path) + target = tmp_path / "env-target" + target.mkdir() + monkeypatch.setenv("TRUSHELL_CD_TARGET", str(target)) + + result = cli._handle_cd_command("cd $TRUSHELL_CD_TARGET") + + assert result is True + assert Path.cwd() == target + assert str(target) in capsys.readouterr().out + + +def test_handle_cd_command_strips_trailing_whitespace(tmp_path, monkeypatch, capsys): + monkeypatch.chdir(tmp_path) + target = tmp_path / "space-target" + target.mkdir() + monkeypatch.setenv("TRUSHELL_CD_TARGET", str(target)) + + result = cli._handle_cd_command("cd $TRUSHELL_CD_TARGET ") + + assert result is True + assert Path.cwd() == target + assert str(target) in capsys.readouterr().out diff --git a/trushell/cli.py b/trushell/cli.py index ff10ff7..820aee7 100644 --- a/trushell/cli.py +++ b/trushell/cli.py @@ -274,6 +274,7 @@ def _handle_chronoterm_command(raw_command: str, normalized_command: str) -> boo return False +# TODO: Deprecate this CLI cd stub in favor of kernel dispatch to avoid duplicate path handling logic. def _handle_cd_command(raw_command: str) -> bool: command, argument = _split_command(raw_command) if command != "cd": @@ -283,7 +284,7 @@ def _handle_cd_command(raw_command: str) -> bool: typer.secho("Syntax: cd ", fg=typer.colors.YELLOW) return True - target = os.path.expanduser(argument) + target = os.path.expandvars(os.path.expanduser(argument.strip())) try: os.chdir(target)