|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (c) 2025 Fayaz Yusuf Khan <fayaz.yusuf.khan@gmail.com> |
| 4 | +# |
| 5 | +# Distributed under terms of the MIT license. |
| 6 | +# |
| 7 | +# /// script |
| 8 | +# requires-python = ">=3.9" |
| 9 | +# dependencies = [ |
| 10 | +# "nox", |
| 11 | +# "nox-uv", |
| 12 | +# ] |
| 13 | +# /// |
| 14 | +""" Entry point script for testing, linting, and development of the package. |
| 15 | +
|
| 16 | + This project uses Nox to create isolated environments. |
| 17 | +
|
| 18 | + Requirements: |
| 19 | + - uv |
| 20 | +
|
| 21 | + Usage: |
| 22 | +
|
| 23 | + Run all tests and linting: |
| 24 | + $ uv run noxfile.py |
| 25 | + Run all tests with coverage and linting: |
| 26 | + $ uv run noxfile.py -- --coverage |
| 27 | +
|
| 28 | + Set up a development environment with the default Python version (3.8): |
| 29 | + $ uv run noxfile.py dev |
| 30 | + Set up a development environment with a specific Python version: |
| 31 | + $ uv run noxfile.py dev -P 3.X |
| 32 | + $ uv run noxfile.py dev -P pypy-3.X # For PyPy |
| 33 | +""" |
| 34 | +import nox |
| 35 | + |
| 36 | + |
| 37 | +# Python versions supported and tested against: 3.8, 3.9, 3.10, 3.11 |
| 38 | +PYTHON_MINOR_VERSION_MIN = 8 |
| 39 | +PYTHON_MINOR_VERSION_MAX = 11 |
| 40 | +# SQLAlchemy versions supported and tested against: 1.0, 1.1, 1.2, 1.3 |
| 41 | +SQLALCHEMY_VERSIONS = ["1.0", "1.1", "1.2", "1.3"] |
| 42 | + |
| 43 | +nox.options.default_venv_backend = "uv" |
| 44 | + |
| 45 | + |
| 46 | +@nox.session() |
| 47 | +def lint(session): |
| 48 | + """Run flake8.""" |
| 49 | + session.install("flake8") |
| 50 | + # stop the linter if there are Python syntax errors or undefined names |
| 51 | + session.run( |
| 52 | + "flake8", ".", "--count", "--select=E9,F63,F7,F82", "--show-source", "--statistics", "--extend-exclude", ".venv") |
| 53 | + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide |
| 54 | + session.run( |
| 55 | + "flake8", ".", "--count", "--exit-zero", "--max-complexity=10", |
| 56 | + "--max-line-length=127", "--statistics", "--extend-exclude", ".venv") |
| 57 | + |
| 58 | + |
| 59 | +@nox.session() |
| 60 | +@nox.parametrize("python,sqlalchemy", |
| 61 | + [(f"{interpreter}3.{python_minor}", sqlalchemy_version) |
| 62 | + for interpreter in ("", "pypy-") |
| 63 | + for python_minor in range(PYTHON_MINOR_VERSION_MIN, PYTHON_MINOR_VERSION_MAX + 1) |
| 64 | + for sqlalchemy_version in SQLALCHEMY_VERSIONS |
| 65 | + # SQLA 1.1 or below doesn't seem to support Python 3.10+ |
| 66 | + if sqlalchemy_version >= "1.2" or python_minor <= 9]) |
| 67 | +def test(session, sqlalchemy): |
| 68 | + """Run tests with pytest. |
| 69 | +
|
| 70 | + Use the --coverage option to run tests with coverage. |
| 71 | + For fine-grained control over running the tests, refer the nox documentation: https://nox.thea.codes/en/stable/usage.html |
| 72 | + """ |
| 73 | + session.install("-r", "requirements-test.txt") |
| 74 | + session.install(f"sqlalchemy~={sqlalchemy}.0") |
| 75 | + if "--coverage" in session.posargs: |
| 76 | + session.run("coverage", "run", "--source=sqlalchemy_mptt", "-m", "pytest", "sqlalchemy_mptt/") |
| 77 | + session.run("coverage", "xml") |
| 78 | + else: |
| 79 | + session.run("pytest", "sqlalchemy_mptt/") |
| 80 | + |
| 81 | + |
| 82 | +@nox.session(default=False) |
| 83 | +def dev(session): |
| 84 | + """Set up a development environment. |
| 85 | + This will create a virtual environment and install the package in editable mode in .venv. |
| 86 | +
|
| 87 | + To use a specific Python version, use the -P option: |
| 88 | + $ uv run noxfile.py dev -P 3.X |
| 89 | + $ uv run noxfile.py dev -P pypy-3.X # For PyPy |
| 90 | + """ |
| 91 | + session.run("uv", "venv", "--python", session.python or f"3.{PYTHON_MINOR_VERSION_MIN}", "--seed") |
| 92 | + session.run(".venv/bin/pip", "install", "-r", "requirements-test.txt", external=True) |
| 93 | + session.run(".venv/bin/pip", "install", "-e", ".", external=True) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + nox.main() |
0 commit comments