diff --git a/hermes_cli/main.py b/hermes_cli/main.py index 7de68d2cb4..0fb5a637c4 100644 --- a/hermes_cli/main.py +++ b/hermes_cli/main.py @@ -4920,7 +4920,12 @@ def _restore_stashed_changes( if input_fn is not None: response = input_fn("Restore local changes now? [Y/n]", "y") else: - response = input().strip().lower() + try: + response = input().strip().lower() + except (KeyboardInterrupt, EOFError): + print() + print("No interactive input available — skipping automatic restore.") + response = "n" if response not in ("", "y", "yes"): print("Skipped restoring local changes.") print("Your changes are still preserved in git stash.") diff --git a/tests/hermes_cli/test_update_autostash.py b/tests/hermes_cli/test_update_autostash.py index dee8cc1fbd..0487b42519 100644 --- a/tests/hermes_cli/test_update_autostash.py +++ b/tests/hermes_cli/test_update_autostash.py @@ -116,6 +116,30 @@ def test_restore_stashed_changes_can_skip_restore_and_keep_stash(monkeypatch, tm assert "git stash apply abc123" in out +def test_restore_stashed_changes_handles_eof_and_keeps_stash(monkeypatch, tmp_path, capsys): + calls = [] + + def fake_run(cmd, **kwargs): + calls.append((cmd, kwargs)) + raise AssertionError(f"unexpected command: {cmd}") + + monkeypatch.setattr(hermes_main.subprocess, "run", fake_run) + + def _raise_eof(): + raise EOFError + + monkeypatch.setattr("builtins.input", _raise_eof) + + restored = hermes_main._restore_stashed_changes(["git"], tmp_path, "abc123", prompt_user=True) + + assert restored is False + assert calls == [] + out = capsys.readouterr().out + assert "No interactive input available — skipping automatic restore." in out + assert "Your changes are still preserved in git stash." in out + assert "git stash apply abc123" in out + + def test_restore_stashed_changes_applies_without_prompt_when_disabled(monkeypatch, tmp_path, capsys): calls = []