-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
automate site generation and testing
- Loading branch information
1 parent
a57be5c
commit c393f4c
Showing
6 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
pip-tools | ||
pytest | ||
|
||
-r requirements.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__])) |