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 new test infrastructure #29

Merged
merged 23 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7ad77d5
Seperated nox sessions for templates and tests and, new testing infra…
santacodes Jul 21, 2024
0c8c51a
Merge branch 'pybamm-team:main' into newtestinfra
santacodes Jul 22, 2024
f8b7f04
Merge branch 'main' into newtestinfra
arjxn-py Jul 22, 2024
4146ac9
Merge branch 'main' into newtestinfra
santacodes Jul 23, 2024
607ffaf
made hatch vcs optional
santacodes Jul 23, 2024
62f8713
Apply suggestions from code review
santacodes Jul 23, 2024
572c190
fixed version modules
santacodes Jul 24, 2024
6a7eb84
added _version.pyi with annotations
santacodes Jul 24, 2024
65a7eb4
Update {{cookiecutter.project_name}}/src/{{cookiecutter.__project_slu…
santacodes Jul 24, 2024
cebcd8a
fixed hatch vcs and versioning
santacodes Jul 24, 2024
5afccee
Apply suggestions from code review
santacodes Jul 24, 2024
9571750
code review suggestion in the parent yml workflow
santacodes Jul 24, 2024
8bc8a67
restored git history for doctests
santacodes Jul 24, 2024
d6fd552
changed project name to project slug in module imports
santacodes Jul 24, 2024
621b05b
changed project name in workflow
santacodes Jul 24, 2024
ef3ef2f
changed vcs configuration
santacodes Jul 26, 2024
fd76ebc
Apply suggestions from code review
santacodes Jul 27, 2024
30cd4b1
made the workflow uniform and fixed workflow for generated project tests
santacodes Jul 27, 2024
a9aa352
Merge branch 'pybamm-team:main' into newtestinfra
santacodes Jul 30, 2024
fd6f428
Apply suggestions from code review
santacodes Jul 31, 2024
4ced431
added session setup function in noxfile
santacodes Jul 31, 2024
518428f
Apply suggestions from code review
santacodes Jul 31, 2024
bd4bb98
Update .github/workflows/test_on_push.yml
santacodes Jul 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 59 additions & 11 deletions .github/workflows/test_on_push.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test template generation
name: Test template and generated project

on:
workflow_dispatch:
Expand All @@ -7,7 +7,6 @@ on:
branches:
- main


jobs:
style:
runs-on: ubuntu-latest
Expand All @@ -18,9 +17,14 @@ jobs:
with:
python-version: 3.12

- name: Set up uv
uses: yezz123/setup-uv@v4
with:
uv-venv: ".venv"

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

template_test:
Expand All @@ -32,7 +36,7 @@ jobs:
os: [ubuntu-latest, macos-13, macos-14, windows-latest]
python-version: ["3.9", "3.10", "3.11", "3.12"]
name:
Template Tests (${{ matrix.os }} / Python ${{ matrix.python-version }})
Template Generation/Project Tests (${{ matrix.os }} / Python ${{ matrix.python-version }})
steps:
- name: Checkout pybamm-cookiecutter
uses: actions/checkout@v4
Expand All @@ -51,13 +55,15 @@ jobs:
- name: Install nox
run: uv pip install nox[uv]

- name: Test Template Generation
run: |
nox -s test-generation
- name: Test template generation
run: nox -s template-tests

- name: Test project
run: nox -s project-tests

- name: Run coverage tests
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'
run: python -m nox -s coverage
run: nox -s coverage

- name: Upload coverage report
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'
Expand All @@ -80,10 +86,52 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: 3.12
cache: 'pip'

- name: Set up uv
uses: yezz123/setup-uv@v4
with:
uv-venv: ".venv"

- name: Install nox
run: python -m pip install nox
run: uv pip install nox[uv]

- name: Check if the documentation can be built
run: python -m nox -s docs
run: nox -s docs

santacodes marked this conversation as resolved.
Show resolved Hide resolved
generated_project_tests:
needs: [template_test]

Saransh-cpp marked this conversation as resolved.
Show resolved Hide resolved
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-13, macos-14, windows-latest]
python-version: ["3.9", "3.10", "3.11", "3.12"]
name:
Generated Project Tests (${{ matrix.os }} / Python ${{ matrix.python-version }})
steps:
- name: Checkout pybamm-cookiecutter
uses: actions/checkout@v4

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

- name: Install cookiecutter and generate a project
run: |
python -m pip install cookiecutter
cookiecutter . --no-input

- name: Set up uv
uses: yezz123/setup-uv@v4
with:
uv-venv: ".venv"

- name: Install nox
uses: wntrblm/[email protected]

- name: Test the generated project
working-directory: ./pybamm-example-project
run: |
nox -s generated-project-tests
5 changes: 4 additions & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"backend": [
"hatch"
],
"vcs": false,
"mypy": false,
"__year": "{% now 'utc', '%Y' %}",
"__project_slug": "{{ cookiecutter.project_name | lower | replace('-', '_') | replace('.', '_') }}",
"_copy_without_render": ["*.yml"],
"__type": "{{ 'pure' }}",
"__answers": "",
"__ci": "{{ cookiecutter.platform }}",
Expand All @@ -40,7 +42,8 @@
"license": "Select a license",
"backend": {
"__prompt__": "Choose a build backend",
"hatch": "Hatchling (recommended for pure Python projects)"
"hatch": "Hatchling (recommended for pure Python projects)",
"vcs": "Let hatch get the version from your version control system (Git/Mercurial/SVN)?"
santacodes marked this conversation as resolved.
Show resolved Hide resolved
},
"mypy": "Use mypy and static types"
}
Expand Down
1 change: 0 additions & 1 deletion hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ def prepare_git() -> None:
)

if __name__ == "__main__":
print("Executing script!!")
prepare_git()
arjxn-py marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 14 additions & 6 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,28 @@ def build_docs(session: nox.Session) -> None:
".",
"build/html/",
)

@nox.session(name="test-generation")
def run_template_generation(session):
"""Run the tests for testing template generation"""
def install_and_run_tests(session, test_dir):
"""Install dependencies and run tests in the specified directory."""
session.install("setuptools", silent=False)
session.install("-e", ".[dev]", silent=False)
session.run("pytest", "tests")
session.run("pytest", test_dir)

@nox.session(name="template-tests")
def run_template_generation(session):
"""Run tests for the template generation."""
install_and_run_tests(session, "tests/template_tests")

@nox.session(name="project-tests")
def run_project_tests(session):
"""Run the tests for testing project units"""
install_and_run_tests(session, "tests/project_tests")

@nox.session(name="coverage")
def run_coverage(session):
"""Run the coverage tests and generate an XML report."""
session.install("setuptools", silent=False)
session.install("coverage", silent=False)
session.install("-e", ".[all,dev,jax]", silent=False)
session.install("-e", ".[dev]", silent=False)
session.run("pytest", "--cov=src/pybamm_cookiecutter", "--cov-report=xml", "tests/")

@nox.session(name="dev")
Expand Down
File renamed without changes.
94 changes: 94 additions & 0 deletions {{cookiecutter.project_name}}/.github/workflows/test_on_push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Project Tests
santacodes marked this conversation as resolved.
Show resolved Hide resolved

on:
workflow_dispatch:
pull_request:
push:
branches:
- main

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

test_project:
needs: style
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-13, macos-14, windows-latest]
python-version: ["3.9", "3.10", "3.11", "3.12"]
name:
Template Generation/Project Tests (${{ matrix.os }} / Python ${{ matrix.python-version }})
steps:
- name: Checkout pybamm-cookiecutter
uses: actions/checkout@v4
with:
fetch-depth: 0

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

- name: Set up uv
uses: yezz123/setup-uv@v4
with:
uv-venv: ".venv"

- name: Install nox
run: uv pip install nox[uv]

- name: Test project units
run: nox -s generated-project-tests

- name: Test user tests
run: nox -s user-tests

- name: Run coverage tests
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'
run: nox -s coverage

- name: Upload coverage report
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}

run_doctests:
needs: style
runs-on: ubuntu-latest
name: Doctests (ubuntu-latest / Python 3.12)

steps:
- name: Check out pybamm-cookiecutter repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.12

- name: Set up uv
uses: yezz123/setup-uv@v4
with:
uv-venv: ".venv"

- name: Install nox
run: uv pip install nox[uv]

- name: Check if the documentation can be built
run: nox -s docs
71 changes: 60 additions & 11 deletions {{cookiecutter.project_name}}/noxfile.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,69 @@
import nox
from pathlib import Path
import os

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

VENV_DIR = Path("./venv").resolve()

@nox.session(name="docs")
def build_docs(session):
def build_docs(session: nox.Session) -> None:
"""Build the documentation and load it in a browser tab, rebuilding on changes."""
envbindir = session.bin
session.install("-e", ".[docs]")
with session.chdir("docs/"):
session.run(
"sphinx-autobuild",
"-j",
"auto",
"--open-browser",
"-qT",
".",
f"{envbindir}/../tmp/html",
)
# For local development
if session.interactive:
session.run(
"sphinx-autobuild",
"-j",
agriyakhetarpal marked this conversation as resolved.
Show resolved Hide resolved
"auto",
"--open-browser",
"-qT",
".",
"build/html/",
)
# For CI testing if documentation builds
else:
session.run(
"sphinx-build",
"-b",
"html",
"-W",
"--keep-going",
".",
"build/html/",
)

def install_and_run_tests(session, test_dir):
"""Install dependencies and run tests in the specified directory."""
session.install("setuptools", silent=False)
session.install("-e", ".[dev]", silent=False)
session.run("pytest", test_dir)

@nox.session(name="generated-project-tests")
def run_generated_project_tests(session):
"""Run the tests for testing units inside generated project"""
install_and_run_tests(session, "tests/generated_project_tests")

@nox.session(name="user-tests")
def run_user_tests(session):
"""Run user written tests"""
install_and_run_tests(session, "tests/user_tests")

@nox.session(name="coverage")
def run_coverage(session):
"""Run the coverage tests and generate an XML report."""
session.posargs.append("--cov=src/pybamm_cookiecutter")
session.posargs.append("--cov-report=xml")
run_user_tests(session)


@nox.session(name="dev")
def set_dev(session):
"""Install pybamm-cookiecutter in editable mode"""
session.install("virtualenv")
session.run("virtualenv", os.fsdecode(VENV_DIR), silent=True)
python = os.fsdecode(VENV_DIR.joinpath("bin/python"))
session.run(python, "-m", "pip", "install", "-e", ".[dev]")
11 changes: 10 additions & 1 deletion {{cookiecutter.project_name}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
{%- if cookiecutter.backend == "hatch" %}
requires = [
"hatchling",
"hatch-vcs"
{%- if cookiecutter.backend == "hatch" and cookiecutter.vcs == true %}
santacodes marked this conversation as resolved.
Show resolved Hide resolved
"hatch-vcs",
{%- endif %}
]
build-backend = "hatchling.build"
{%- endif %}
Expand Down Expand Up @@ -86,12 +88,19 @@ SPM = "{{ cookiecutter.__project_slug }}.models.input.SPM:SPM"
{# keep this line here for newline #}
{%- if cookiecutter.backend == "hatch" %}
[tool.hatch]
{%- if cookiecutter.backend == "hatch" and cookiecutter.vcs == true %}
santacodes marked this conversation as resolved.
Show resolved Hide resolved
version.source = "vcs"
build.hooks.vcs.version-file = "src/{{ cookiecutter.__project_slug }}/_version.py"
{%- endif %}
envs.default.dependencies = [
"pybamm",
]
{%- endif %}

{%- if cookiecutter.backend == "hatch" and cookiecutter.vcs == false %}
[tool.hatch.version]
path = "src/{{ cookiecutter.__project_slug }}/__init__.py"
{%- endif %}
{# keep this line here for newline #}
{%- if cookiecutter.mypy %}
[tool.mypy]
Expand Down
Loading
Loading