Merge pull request 'test_vars: mock ask function instead of sys.stdin' (#2493) from stdin into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/2493
This commit is contained in:
Mic92
2024-11-26 11:56:38 +00:00
2 changed files with 12 additions and 4 deletions

View File

@@ -6,8 +6,13 @@ from clan_cli.errors import ClanError
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
# This is for simulating user input in tests.
MOCK_PROMPT_RESPONSE = None
def ask(description: str, input_type: str) -> str: def ask(description: str, input_type: str) -> str:
if MOCK_PROMPT_RESPONSE:
return next(MOCK_PROMPT_RESPONSE)
if input_type == "line": if input_type == "line":
result = input(f"Enter the value for {description}: ") result = input(f"Enter the value for {description}: ")
elif input_type == "multiline": elif input_type == "multiline":

View File

@@ -1,7 +1,6 @@
import json import json
import shutil import shutil
from dataclasses import dataclass from dataclasses import dataclass
from io import StringIO
from pathlib import Path from pathlib import Path
import pytest import pytest
@@ -389,7 +388,9 @@ def test_prompt(
my_generator["script"] = "cat $prompts/prompt1 > $out/my_value" my_generator["script"] = "cat $prompts/prompt1 > $out/my_value"
flake.refresh() flake.refresh()
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
monkeypatch.setattr("sys.stdin", StringIO(input_value)) monkeypatch.setattr(
"clan_cli.vars.prompt.MOCK_PROMPT_RESPONSE", iter([input_value])
)
cli.run(["vars", "generate", "--flake", str(flake.path), "my_machine"]) cli.run(["vars", "generate", "--flake", str(flake.path), "my_machine"])
in_repo_store = in_repo.FactStore( in_repo_store = in_repo.FactStore(
Machine(name="my_machine", flake=FlakeId(str(flake.path))) Machine(name="my_machine", flake=FlakeId(str(flake.path)))
@@ -497,7 +498,9 @@ def test_prompt_create_file(
flake.refresh() flake.refresh()
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
sops_setup.init() sops_setup.init()
monkeypatch.setattr("sys.stdin", StringIO("input1\ninput2\n")) monkeypatch.setattr(
"clan_cli.vars.prompt.MOCK_PROMPT_RESPONSE", iter(["input1", "input2"])
)
cli.run(["vars", "generate", "--flake", str(flake.path), "my_machine"]) cli.run(["vars", "generate", "--flake", str(flake.path), "my_machine"])
sops_store = sops.SecretStore( sops_store = sops.SecretStore(
Machine(name="my_machine", flake=FlakeId(str(flake.path))) Machine(name="my_machine", flake=FlakeId(str(flake.path)))
@@ -520,7 +523,7 @@ def test_api_get_prompts(
my_generator["files"]["prompt1"]["secret"] = False my_generator["files"]["prompt1"]["secret"] = False
flake.refresh() flake.refresh()
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
monkeypatch.setattr("sys.stdin", StringIO("input1")) monkeypatch.setattr("clan_cli.vars.prompt.MOCK_PROMPT_RESPONSE", iter(["input1"]))
cli.run(["vars", "generate", "--flake", str(flake.path), "my_machine"]) cli.run(["vars", "generate", "--flake", str(flake.path), "my_machine"])
machine = Machine(name="my_machine", flake=FlakeId(str(flake.path))) machine = Machine(name="my_machine", flake=FlakeId(str(flake.path)))
api_prompts = get_prompts(machine) api_prompts = get_prompts(machine)