Skip to content

Commit 43bc593

Browse files
committed
Add testing
1 parent f3fe1bf commit 43bc593

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

tests/unit/ctl/test_repository_app.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"""Integration tests for infrahubctl commands."""
22

3+
import tempfile
4+
from pathlib import Path
35
from unittest import mock
46

57
import pytest
8+
import yaml
69
from typer.testing import CliRunner
710

811
from infrahub_sdk.client import InfrahubClient
@@ -322,3 +325,73 @@ def test_repo_list(self, mock_repositories_list) -> None:
322325
result = runner.invoke(app, ["repository", "list"])
323326
assert result.exit_code == 0
324327
assert strip_color(result.stdout) == read_fixture("output.txt", "integration/test_infrahubctl/repository_list")
328+
329+
def test_repo_init(self) -> None:
330+
"""Test the repository init command."""
331+
with (
332+
tempfile.TemporaryDirectory() as temp_dst,
333+
tempfile.NamedTemporaryFile(mode="w", suffix=".yml", delete=False, encoding="utf-8") as temp_yaml,
334+
):
335+
dst = Path(temp_dst)
336+
yaml_path = Path(temp_yaml.name)
337+
commit = "v0.0.1"
338+
339+
answers = {
340+
"generators": True,
341+
"menus": True,
342+
"project_name": "test",
343+
"queries": True,
344+
"scripts": True,
345+
"tests": True,
346+
"transforms": True,
347+
"package_mode": False,
348+
"_commit": commit,
349+
}
350+
351+
yaml.safe_dump(answers, temp_yaml)
352+
temp_yaml.close()
353+
runner.invoke(app, ["repository", "init", str(dst), "--data", str(yaml_path), "--vcs-ref", commit])
354+
coppied_answers = yaml.safe_load((dst / ".copier-answers.yml").read_text())
355+
coppied_answers.pop("_src_path")
356+
357+
assert coppied_answers == answers
358+
assert (dst / "generators").is_dir()
359+
assert (dst / "queries").is_dir()
360+
assert (dst / "scripts").is_dir()
361+
assert (dst / "pyproject.toml").is_file()
362+
363+
def test_repo_init_local_template(self) -> None:
364+
"""Test the repository init command with a local template."""
365+
with (
366+
tempfile.TemporaryDirectory() as temp_src,
367+
tempfile.TemporaryDirectory() as temp_dst,
368+
tempfile.NamedTemporaryFile(mode="w", suffix=".yml", delete=False, encoding="utf-8") as temp_yaml,
369+
):
370+
src = Path(temp_src)
371+
dst = Path(temp_dst)
372+
373+
# Create a simple copier template
374+
(src / "copier.yml").write_text("project_name:\n type: str")
375+
template_dir = src / "{{project_name}}"
376+
template_dir.mkdir()
377+
(template_dir / "file.txt.jinja").write_text("Hello {{ project_name }}")
378+
379+
# Create answers file
380+
yaml_path = Path(temp_yaml.name)
381+
answers = {"project_name": "local-test"}
382+
yaml.safe_dump(answers, temp_yaml)
383+
temp_yaml.close()
384+
385+
# Run the command
386+
result = runner.invoke(
387+
app, ["repository", "init", str(dst), "--template", str(src), "--data", str(yaml_path)]
388+
)
389+
390+
assert result.exit_code == 0, result.stdout
391+
392+
# Check the output
393+
project_dir = dst / "local-test"
394+
assert project_dir.is_dir()
395+
output_file = project_dir / "file.txt"
396+
assert output_file.is_file()
397+
assert output_file.read_text() == "Hello local-test"

0 commit comments

Comments
 (0)