Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
test: use copier.yaml to parametrize tests (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-makerx authored Apr 19, 2023
1 parent 8cb24d9 commit 326ea7f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 42 deletions.
18 changes: 15 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ flake8 = "^6.0.0"
pytest = "^7.2.2"
mypy = "^1.1.1"
pre-commit = "^3.2.1"
types-pyyaml = "^6.0.12.9"

[build-system]
requires = ["poetry-core"]
Expand Down
83 changes: 44 additions & 39 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
from pathlib import Path

import pytest
import yaml

commit_pattern = re.compile(r"_commit: .*")
src_path_pattern = re.compile(r"_src_path: .*")
tests_path = Path(__file__).parent
root = tests_path.parent
generated_folder = "tests_generated"
generated_root = root / generated_folder
DEFAULT_PARAMETERS = {
"author_name": "None",
"author_email": "None",
}


@pytest.fixture(autouse=True, scope="module")
Expand Down Expand Up @@ -68,9 +73,7 @@ def run_init(
"--no-git",
"--no-bootstrap",
]
answers = dict(answers or {})
answers.setdefault("author_name", "None")
answers.setdefault("author_email", "None")
answers = {**DEFAULT_PARAMETERS, **(answers or {})}

for question, answer in answers.items():
init_args.extend(["-a", question, answer])
Expand Down Expand Up @@ -99,12 +102,6 @@ def run_init(
return result


def test_default_parameters(working_dir: Path) -> None:
response = run_init(working_dir, "test_default_parameters")

assert response.returncode == 0


def run_init_kwargs(working_dir: Path, **kwargs: str | bool) -> None:
answers = {k: str(v) for k, v in kwargs.items()}
name_suffix = "_".join(f"{k}-{v}" for k, v in answers.items())
Expand All @@ -113,36 +110,44 @@ def run_init_kwargs(working_dir: Path, **kwargs: str | bool) -> None:
assert response.returncode == 0


@pytest.mark.parametrize("ide_vscode", [True, False])
def test_ide_vscode(working_dir: Path, *, ide_vscode: bool) -> None:
run_init_kwargs(working_dir, ide_vscode=ide_vscode)


@pytest.mark.parametrize("use_python_black", [True, False])
def test_use_python_black(working_dir: Path, *, use_python_black: bool) -> None:
run_init_kwargs(working_dir, use_python_black=use_python_black)

def get_questions_from_copier_yaml() -> Iterator[tuple[str, str | bool]]:
copier_yaml = root / "copier.yaml"
ignored_keys = {
"_subdirectory", # copier setting
# the following are ignored as they are passed automatically by algokit
"project_name",
"algod_token",
"algod_server",
"algod_port",
"indexer_token",
"indexer_server",
"indexer_port",
}
ignored_keys.update(DEFAULT_PARAMETERS)

with copier_yaml.open("r", encoding="utf-8") as stream:
questions = yaml.safe_load(stream)
for question_name, details in questions.items():
if question_name in ignored_keys:
continue
match details["type"]:
case "str":
if "choices" not in details:
continue

for choice in details["choices"].values():
yield question_name, choice
case "bool":
yield question_name, False
yield question_name, True


@pytest.mark.parametrize(("question_name", "answer"), get_questions_from_copier_yaml())
def test_parameters(working_dir: Path, question_name: str, answer: str | bool) -> None:
run_init_kwargs(working_dir, **{question_name: answer})

@pytest.mark.parametrize("use_python_mypy", [True, False])
def test_use_python_mypy(working_dir: Path, *, use_python_mypy: bool) -> None:
run_init_kwargs(working_dir, use_python_mypy=use_python_mypy)


@pytest.mark.parametrize("use_python_pytest", [True, False])
def test_use_python_pytest(working_dir: Path, *, use_python_pytest: bool) -> None:
run_init_kwargs(working_dir, use_python_pytest=use_python_pytest)


@pytest.mark.parametrize("use_python_pip_audit", [True, False])
def test_use_python_pip_audit(working_dir: Path, *, use_python_pip_audit: bool) -> None:
run_init_kwargs(working_dir, use_python_pip_audit=use_python_pip_audit)


@pytest.mark.parametrize("deployment_language", ["python", "typescript"])
def test_deployment_language(working_dir: Path, *, deployment_language: str) -> None:
run_init_kwargs(working_dir, deployment_language=deployment_language)

def test_default_parameters(working_dir: Path) -> None:
response = run_init(working_dir, "test_default_parameters")

@pytest.mark.parametrize("python_linter", ["ruff", "flake8", "none"])
def test_python_linter(working_dir: Path, *, python_linter: str) -> None:
run_init_kwargs(working_dir, python_linter=python_linter)
assert response.returncode == 0

0 comments on commit 326ea7f

Please sign in to comment.