Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for template generation #9

Merged
merged 12 commits into from
Jun 13, 2024
53 changes: 53 additions & 0 deletions .github/test_on_push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: pybamm_cookiecutter
santacodes marked this conversation as resolved.
Show resolved Hide resolved

on:
workflow_dispatch:
pull_request:
arjxn-py marked this conversation as resolved.
Show resolved Hide resolved

jobs:
style:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: 3.12

- name: Check style
run: |
python -m pip install pre-commit
pre-commit run -a

template_test:
needs: style
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-12, macos-14, windows-latest]
santacodes marked this conversation as resolved.
Show resolved Hide resolved
python-version: ["3.9", "3.10", "3.11", "3.12"]
name:
Template Tests (${{ matrix.os }} / Python ${{ matrix.python-version }})
steps:
- name: Checkout pybamm_cookiecutter
santacodes marked this conversation as resolved.
Show resolved Hide resolved
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
id: setup-python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'

- name: Set up uv
uses: yezz123/setup-uv@v4
with:
uv-venv: ".venv"
agriyakhetarpal marked this conversation as resolved.
Show resolved Hide resolved

- name: Install nox
run: uv pip install nox
arjxn-py marked this conversation as resolved.
Show resolved Hide resolved

- name: Test Template Generation
run: |
nox -s test-generation
10 changes: 10 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import nox

# Options to modify nox behaviour
nox.options.default_venv_backend = "uv|virtualenv"
nox.options.reuse_existing_virtualenvs = True

@nox.session(name="docs")
Expand All @@ -18,3 +19,12 @@ def build_docs(session: nox.Session) -> None:
".",
f"{envbindir}/../tmp/html",
)

@nox.session(name="test-generation")
def run_template_generation(session):
"""Run the tests tests for testing template generation"""
santacodes marked this conversation as resolved.
Show resolved Hide resolved
session.install("setuptools", silent=False)
session.install("pytest", silent=False)
session.install("pytest-cookies", silent=False)
session.install("-e", ".", silent=False)
santacodes marked this conversation as resolved.
Show resolved Hide resolved
session.run("pytest", "tests")
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ classifiers = [
"Typing :: Typed",
]
dynamic = ["version"]
dependencies = ["pybamm"]
dependencies = ["pybamm", "cookiecutter"]

[project.optional-dependencies]
agriyakhetarpal marked this conversation as resolved.
Show resolved Hide resolved
dev = [
"pytest >=6",
"pytest-cov >=3",
"nox",
santacodes marked this conversation as resolved.
Show resolved Hide resolved
"pre-commit",
"pytest-cookies",
]
docs = [
"sphinx",
Expand Down
7 changes: 6 additions & 1 deletion src/pybamm_cookiecutter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"""
from __future__ import annotations

import pybamm

from ._version import version as __version__

__all__ : tuple[str] = ("__version__",)
__all__ : list[str] = [
"__version__",
"pybamm",
santacodes marked this conversation as resolved.
Show resolved Hide resolved
]
5 changes: 0 additions & 5 deletions tests/test_package.py

This file was deleted.

30 changes: 30 additions & 0 deletions tests/test_project_generation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pybamm_cookiecutter as m
import pytest
santacodes marked this conversation as resolved.
Show resolved Hide resolved
from pytest_cookies.plugin import Cookies

def test_version() -> None:
assert m.__version__

@pytest.fixture
def custom_template(tmpdir):
arjxn-py marked this conversation as resolved.
Show resolved Hide resolved
"""
Generating a project using the template into a tempdir
"""
template = tmpdir.ensure("cookiecutter-template", dir=True)
template.join("cookiecutter.json").write('{"project_name": "pybamm_cookie"}')

repo_dir = template.ensure("{{cookiecutter.project_name}}", dir=True)
repo_dir.join("README.rst").write("{{cookiecutter.project_name}}")

return template


def test_bake_custom_project(cookies: Cookies, custom_template):
"""
Testing if the projects exists in the tempdir
"""
result = cookies.bake(template=str(custom_template))
assert result.exit_code == 0, "Exited with code 0"
santacodes marked this conversation as resolved.
Show resolved Hide resolved
assert result.exception is None, result.exception
assert result.project_path.name == "pybamm_cookie"
arjxn-py marked this conversation as resolved.
Show resolved Hide resolved
assert result.project_path.is_dir(), "Project directory not found"
santacodes marked this conversation as resolved.
Show resolved Hide resolved