fix(template): replace click.confirm with input() to avoid ^M on Linux terminals

click.confirm() doesn't strip \r — "y\r" doesn't match "y" and the prompt
loops or fails. Replace with raw input().strip().rstrip('\r').lower() which
handles both \n and \r\n line endings.

Also add --recreate-snapshot flag to skip the interactive prompt entirely
(useful for scripted re-provisioning).

Update snapshot_exists_skip/recreate tests to monkeypatch builtins.input
instead of using CliRunner input=.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-24 02:02:22 +02:00
parent 483dd51a3b
commit 258ef3a7e7
2 changed files with 43 additions and 4 deletions
+34 -3
View File
@@ -613,8 +613,8 @@ def test_prepare_win_snapshot_exists_skip(
backend.list_snapshots.return_value = ["BaseClean"]
transport = _make_transport()
_patch_prepare(monkeypatch, backend, transport)
monkeypatch.setattr("builtins.input", lambda _prompt="": "N")
# User declines deletion with "N"
result = CliRunner().invoke(
cli,
[
@@ -624,7 +624,6 @@ def test_prepare_win_snapshot_exists_skip(
"--admin-password", "adminpass",
"--build-password", "buildpass",
],
input="N\n",
)
assert result.exit_code == 0, result.output
assert "Keeping existing snapshot" in result.output
@@ -645,6 +644,7 @@ def test_prepare_win_snapshot_exists_recreate(
monkeypatch.setattr(tmpl_mod, "_wait_tcp", lambda *_: True)
monkeypatch.setattr(tmpl_mod, "_wait_winrm", lambda *_: None)
monkeypatch.setattr(tmpl_mod.time, "sleep", lambda _: None)
monkeypatch.setattr("builtins.input", lambda _prompt="": "Y")
def _sp_run(cmd: list[str], **_: object) -> None:
sp_calls.append(cmd)
@@ -660,13 +660,44 @@ def test_prepare_win_snapshot_exists_recreate(
"--admin-password", "adminpass",
"--build-password", "buildpass",
],
input="Y\n",
)
assert result.exit_code == 0, result.output
assert any("deleteSnapshot" in " ".join(c) for c in sp_calls)
assert any("snapshot" in " ".join(c) and "deleteSnapshot" not in " ".join(c) for c in sp_calls)
def test_prepare_win_recreate_snapshot_flag(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""--recreate-snapshot skips the interactive prompt and deletes+recreates."""
vmx = _setup_prepare_env(tmp_path)
monkeypatch.chdir(tmp_path)
backend = _make_backend([True, False])
backend.list_snapshots.return_value = ["BaseClean"]
transport = _make_transport()
sp_calls: list[list[str]] = []
_patch_prepare(monkeypatch, backend, transport)
def _sp_run(cmd: list[str], **_: object) -> None:
sp_calls.append(cmd)
monkeypatch.setattr(tmpl_mod.subprocess, "run", _sp_run)
result = CliRunner().invoke(
cli,
[
"template", "prepare-win",
"--vmx", str(vmx),
"--ip", "192.168.1.100",
"--admin-password", "adminpass",
"--build-password", "buildpass",
"--recreate-snapshot",
],
)
assert result.exit_code == 0, result.output
assert any("deleteSnapshot" in " ".join(c) for c in sp_calls)
def test_prepare_win_skip_flags(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None: