Skip to content

Commit dca6946

Browse files
santacodesarjxn-pySaransh-cppagriyakhetarpal
authored
Added tests for template generation (#9)
# Description - Added basic tests for template generation using `pytest-cookies`. - Added `cookiecutter` as a dependency and setup `___init.py__` for the src layout. --------- Co-authored-by: Arjun Verma <[email protected]> Co-authored-by: Saransh Chopra <[email protected]> Co-authored-by: Agriya Khetarpal <[email protected]>
1 parent 6ea83fd commit dca6946

File tree

6 files changed

+108
-8
lines changed

6 files changed

+108
-8
lines changed

.github/test_on_push.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Test template generation
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
push:
7+
branches:
8+
- main
9+
jobs:
10+
style:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Setup Python
15+
uses: actions/setup-python@v5
16+
with:
17+
python-version: 3.12
18+
19+
- name: Check style
20+
run: |
21+
python -m pip install pre-commit
22+
pre-commit run -a
23+
24+
template_test:
25+
needs: style
26+
runs-on: ${{ matrix.os }}
27+
strategy:
28+
fail-fast: false
29+
matrix:
30+
os: [ubuntu-latest, macos-13, macos-14, windows-latest]
31+
python-version: ["3.9", "3.10", "3.11", "3.12"]
32+
name:
33+
Template Tests (${{ matrix.os }} / Python ${{ matrix.python-version }})
34+
steps:
35+
- name: Checkout pybamm-cookiecutter
36+
uses: actions/checkout@v4
37+
38+
- name: Set up Python ${{ matrix.python-version }}
39+
id: setup-python
40+
uses: actions/setup-python@v5
41+
with:
42+
python-version: ${{ matrix.python-version }}
43+
cache: 'pip'
44+
45+
- name: Set up uv
46+
uses: yezz123/setup-uv@v4
47+
with:
48+
uv-venv: ".venv"
49+
50+
- name: Install nox
51+
run: uv pip install nox[uv]
52+
53+
- name: Test Template Generation
54+
run: |
55+
nox -s test-generation

noxfile.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import nox
22

33
# Options to modify nox behaviour
4+
nox.options.default_venv_backend = "uv|virtualenv"
45
nox.options.reuse_existing_virtualenvs = True
56

67
@nox.session(name="docs")
@@ -18,3 +19,10 @@ def build_docs(session: nox.Session) -> None:
1819
".",
1920
f"{envbindir}/../tmp/html",
2021
)
22+
23+
@nox.session(name="test-generation")
24+
def run_template_generation(session):
25+
"""Run the tests for testing template generation"""
26+
session.install("setuptools", silent=False)
27+
session.install("-e", ".[dev]", silent=False)
28+
session.run("pytest", "tests")

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ classifiers = [
3030
"Typing :: Typed",
3131
]
3232
dynamic = ["version"]
33-
dependencies = ["pybamm"]
33+
dependencies = ["pybamm", "cookiecutter"]
3434

3535
[project.optional-dependencies]
3636
dev = [
3737
"pytest >=6",
3838
"pytest-cov >=3",
39-
"nox",
39+
"nox[uv]",
4040
"pre-commit",
41+
"pytest-cookies",
4142
]
4243
docs = [
4344
"sphinx",

src/pybamm_cookiecutter/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
"""
66
from __future__ import annotations
77

8+
import pybamm
9+
810
from ._version import version as __version__
911

10-
__all__ : tuple[str] = ("__version__",)
12+
__all__ : list[str] = [
13+
"__version__",
14+
]

tests/test_package.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/test_project_generation.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pybamm_cookiecutter as m
2+
import pytest
3+
from pytest_cookies.plugin import Cookies
4+
5+
def test_version() -> None:
6+
assert m.__version__
7+
8+
def test_bake_project(cookies: Cookies):
9+
"""
10+
Testing the template generation with default values
11+
"""
12+
result = cookies.bake()
13+
assert result.exit_code == 0, f"Exited with code {result.exit_code}, expected 0"
14+
assert result.exception is None, result.exception
15+
assert result.project_path.name == "pybamm-example-project"
16+
assert result.project_path.is_dir(), f"Project directory {result.project_path} not found"
17+
18+
19+
def test_bake_custom_project(cookies: Cookies):
20+
"""
21+
Testing the template generation with custom template and checking if the projects exists in the tmp_path
22+
"""
23+
result = cookies.bake(extra_context={
24+
"author_full_name": "pybamm_user",
25+
"author_email": "[email protected]",
26+
"project_name": "pybamm_cookie",
27+
"project_slug": "example",
28+
"project_short_description": "This is an example pybamm cookiecutter template",
29+
"project_url": "pybamm.org",
30+
"project_version": "0.1.0",
31+
"documentation_engine": "sphinx(rst)",
32+
})
33+
34+
assert result.exit_code == 0, f"Exited with code {result.exit_code}, expected 0"
35+
assert result.exception is None, result.exception
36+
assert result.project_path.name == "pybamm_cookie"
37+
assert result.project_path.is_dir(), f"Project directory {result.project_path} not found"

0 commit comments

Comments
 (0)