|
| 1 | +""" |
| 2 | +Test the Cookiecutter template. |
| 3 | +""" |
| 4 | +from cookiecutter.generate import generate_context |
| 5 | +from cookiecutter.main import cookiecutter |
| 6 | +from pathlib import Path |
| 7 | +from shlex import split |
| 8 | +from subprocess import run |
| 9 | +from venv import create |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(scope="session") |
| 15 | +def template() -> Path: |
| 16 | + """ The template under test. |
| 17 | +
|
| 18 | + """ |
| 19 | + return Path(__file__).resolve().parents[1] |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(scope="module") |
| 23 | +def tmpdir(tmp_path_factory) -> Path: |
| 24 | + """ Test directory. |
| 25 | +
|
| 26 | + """ |
| 27 | + return tmp_path_factory.mktemp("test_template") |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(scope="module") |
| 31 | +def context(template) -> dict: |
| 32 | + """ Template context for testing. |
| 33 | +
|
| 34 | + """ |
| 35 | + context = generate_context(template.joinpath("cookiecutter.json")) |
| 36 | + context["cookiecutter"].update({ |
| 37 | + "project_slug": "slugify" |
| 38 | + }) |
| 39 | + return context["cookiecutter"] |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture(scope="module") |
| 43 | +def project(tmpdir, template, context) -> Path: |
| 44 | + """ Create a test project from the Cookiecutter template. |
| 45 | +
|
| 46 | + """ |
| 47 | + cookiecutter(str(template), no_input=True, output_dir=tmpdir, extra_context=context) |
| 48 | + return tmpdir / context["project_slug"] |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture |
| 52 | +def python(tmp_path): |
| 53 | + """ Create a Python virtual environment for testing. |
| 54 | +
|
| 55 | + """ |
| 56 | + venv = tmp_path / ".venv" |
| 57 | + create(venv, with_pip=True) |
| 58 | + return venv / "bin" / "python" |
| 59 | + |
| 60 | + |
| 61 | +def test_project(project): |
| 62 | + """ Verify that the project was created correctly. |
| 63 | +
|
| 64 | + """ |
| 65 | + # Just a basic sanity test. |
| 66 | + assert len(list(project.iterdir())) == 4 |
| 67 | + return |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture |
| 71 | +def install_render_engine_cli(python): |
| 72 | + install_cli = "pip install render_engine[cli]" |
| 73 | + install_args = split(f"{python} -m {install_cli}") |
| 74 | + install_process = run(install_args) |
| 75 | + assert install_process.returncode == 0 |
| 76 | + |
| 77 | + |
| 78 | +def test_site_generation(context, project, python, install_render_engine_cli): |
| 79 | + generate_site = "render_engine build app:app" |
| 80 | + generate_args = split(f"{python} -m {generate_site}") |
| 81 | + generate_process = run(generate_args, cwd=project) |
| 82 | + assert generate_process.returncode == 0 |
| 83 | + |
| 84 | + |
| 85 | +# Make the script executable. |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + raise SystemExit(pytest.main([__file__])) |
0 commit comments