Skip to content

Commit

Permalink
automate site generation and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
john0isaac committed Dec 8, 2024
1 parent a57be5c commit c393f4c
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"charliermarsh.ruff",
"GitHub.vscode-github-actions",
"yzhang.markdown-all-in-one",
"DavidAnson.vscode-markdownlint"
"DavidAnson.vscode-markdownlint",
"ms-vscode.makefile-tools"
]
}
}
Expand Down
6 changes: 4 additions & 2 deletions .devcontainer/setup.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
python -m pip install --user --upgrade pip
python -m pip install --user pip-tools
python -m venv .venv
source .venv/bin/activate
python -m pip install pip-tools
python -m piptools compile --upgrade -o requirements.txt requirements.in
python -m pip install --user -r requirements.txt
python -m pip install -r requirements-dev.txt
28 changes: 28 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: PyTest
on:
workflow_call:
workflow_dispatch:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
fail-fast: true
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: python -m pip install --upgrade pip -r requirements-dev.txt
- name: Run tests
run: python tests/test_template.py
3 changes: 3 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pip-tools
pytest

-r requirements.txt
4 changes: 4 additions & 0 deletions tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
python_files = test_*.py
python_classes = [A-Z]*Test
python_functions = test_*
92 changes: 92 additions & 0 deletions tests/test_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
""" Test the Cookiecutter template.
A template project is created in a temporary directory, the application is
installed into a self-contained venv environment, and the application test
suite is run.
"""
from cookiecutter.generate import generate_context
from cookiecutter.main import cookiecutter
from pathlib import Path
from shlex import split
from subprocess import run
from venv import create

import pytest


@pytest.fixture(scope="session")
def template() -> Path:
""" The template under test.
"""
return Path(__file__).resolve().parents[1]


@pytest.fixture(scope="module")
def tmpdir(tmp_path_factory) -> Path:
""" Test directory.
"""
return tmp_path_factory.mktemp("test_template")


@pytest.fixture(scope="module")
def context(template) -> dict:
""" Template context for testing.
"""
context = generate_context(template.joinpath("cookiecutter.json"))
context["cookiecutter"].update({
"project_slug": "slugify"
})
return context["cookiecutter"]


@pytest.fixture(scope="module")
def project(tmpdir, template, context) -> Path:
""" Create a test project from the Cookiecutter template.
"""
cookiecutter(str(template), no_input=True, output_dir=tmpdir, extra_context=context)
return tmpdir / context["project_slug"]


@pytest.fixture
def python(tmp_path):
""" Create a Python virtual environment for testing.
"""
venv = tmp_path / ".venv"
create(venv, with_pip=True)
return venv / "bin" / "python"


def test_project(project):
""" Verify that the project was created correctly.
"""
# Just a basic sanity test.
assert len(list(project.iterdir())) == 4
return


@pytest.fixture
def install_render_engine_cli(python):
install_cli = "pip install render_engine[cli]"
install_args = split(f"{python} -m {install_cli}")
install_process = run(install_args)
assert install_process.returncode == 0


def test_site_generation(context, project, python, install_render_engine_cli):
generate_site = "render-engine build app:app"
generate_args = split(generate_site)
generate_process = run(generate_args, cwd=project)
assert generate_process.returncode == 0


# Make the script executable.

if __name__ == "__main__":
raise SystemExit(pytest.main([__file__]))

0 comments on commit c393f4c

Please sign in to comment.