|
| 1 | +import re |
| 2 | +import shutil |
| 3 | +import subprocess |
| 4 | +import tempfile |
| 5 | +from collections.abc import Iterator |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +commit_pattern = re.compile(r"_commit: .*") |
| 12 | +src_path_pattern = re.compile(r"_src_path: .*") |
| 13 | +tests_path = Path(__file__).parent |
| 14 | +root = tests_path.parent |
| 15 | +generated_folder = "tests_generated" |
| 16 | +generated_root = root / generated_folder |
| 17 | +DEFAULT_PARAMETERS = { |
| 18 | + "author_name": "None", |
| 19 | + "author_email": "None", |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(autouse=True, scope="module") |
| 24 | +def working_dir() -> Iterator[Path]: |
| 25 | + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as temp: |
| 26 | + working_dir = Path(temp) / "template" |
| 27 | + working_generated_root = working_dir / generated_folder |
| 28 | + shutil.copytree(root, working_dir) |
| 29 | + subprocess.run(["git", "add", "-A"], cwd=working_dir) |
| 30 | + subprocess.run(["git", "commit", "-m", "draft changes", "--no-verify"], cwd=working_dir) |
| 31 | + |
| 32 | + yield working_dir |
| 33 | + |
| 34 | + for src_dir in working_generated_root.iterdir(): |
| 35 | + dest_dir = generated_root / src_dir.stem |
| 36 | + shutil.rmtree(dest_dir, ignore_errors=True) |
| 37 | + shutil.copytree(src_dir, dest_dir, dirs_exist_ok=True) |
| 38 | + |
| 39 | + |
| 40 | +def run_init( |
| 41 | + working_dir: Path, |
| 42 | + test_name: str, |
| 43 | + *args: str, |
| 44 | + template_url: str | None = None, |
| 45 | + template_branch: str | None = None, |
| 46 | + answers: dict[str, str] | None = None, |
| 47 | +) -> subprocess.CompletedProcess: |
| 48 | + copy_to = working_dir / generated_folder / test_name |
| 49 | + shutil.rmtree(copy_to, ignore_errors=True) |
| 50 | + if template_url is None: |
| 51 | + template_url = str(working_dir) |
| 52 | + |
| 53 | + if template_branch is None: |
| 54 | + git_output = subprocess.run( |
| 55 | + ["git", "rev-parse", "--abbrev-ref", "HEAD"], |
| 56 | + cwd=working_dir, |
| 57 | + stdout=subprocess.PIPE, |
| 58 | + ) |
| 59 | + template_branch = git_output.stdout.decode("utf-8").strip() |
| 60 | + |
| 61 | + init_args = [ |
| 62 | + "algokit", |
| 63 | + "init", |
| 64 | + "--name", |
| 65 | + str(copy_to.stem), |
| 66 | + "--template-url", |
| 67 | + template_url, |
| 68 | + "--UNSAFE-SECURITY-accept-template-url", |
| 69 | + "--defaults", |
| 70 | + "--no-ide", |
| 71 | + "--no-git", |
| 72 | + "--no-bootstrap", |
| 73 | + ] |
| 74 | + answers = {**DEFAULT_PARAMETERS, **(answers or {})} |
| 75 | + |
| 76 | + for question, answer in answers.items(): |
| 77 | + init_args.extend(["-a", question, answer]) |
| 78 | + if template_branch: |
| 79 | + init_args.extend(["--template-url-ref", template_branch]) |
| 80 | + init_args.extend(args) |
| 81 | + |
| 82 | + result = subprocess.run( |
| 83 | + init_args, |
| 84 | + input="y", # acknowledge that input is not a tty |
| 85 | + stdout=subprocess.PIPE, |
| 86 | + stderr=subprocess.STDOUT, |
| 87 | + text=True, |
| 88 | + cwd=copy_to.parent, |
| 89 | + ) |
| 90 | + |
| 91 | + if result.returncode == 0: # if successful, normalize .copier-answers.yml to make observing diffs easier |
| 92 | + copier_answers = Path(copy_to / ".copier-answers.yml") |
| 93 | + content = copier_answers.read_text("utf-8") |
| 94 | + content = commit_pattern.sub("_commit: <commit>", content) |
| 95 | + content = src_path_pattern.sub("_src_path: <src>", content) |
| 96 | + copier_answers.write_text(content, "utf-8") |
| 97 | + |
| 98 | + return result |
| 99 | + |
| 100 | + |
| 101 | +def test_default_parameters(working_dir: Path) -> None: |
| 102 | + response = run_init(working_dir, "test_default_parameters") |
| 103 | + |
| 104 | + assert response.returncode == 0 |
0 commit comments