From 403cc2696057dab97bfc7acb868195711858459e Mon Sep 17 00:00:00 2001 From: Jo Basevi Date: Tue, 21 May 2024 11:27:55 +1000 Subject: [PATCH] Update CI: - Add precommit check for code linting/formatting - Upload code coverage reports to codecov - Run tests with python 3.9, 3.10, 3.11 Move schema and general methods to Model class so they can used for different models Move schema and general methods to Model class so they can used for different models --- .github/workflows/CD.yml | 36 +++++++++---------- .github/workflows/CI.yml | 25 +++++++++++--- .gitignore | 3 +- .pre-commit-config.yaml | 12 +++++++ pyproject.toml | 74 +++++++++++++++++++++++++++++++++++----- 5 files changed, 118 insertions(+), 32 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/CD.yml b/.github/workflows/CD.yml index 029c987..8549769 100644 --- a/.github/workflows/CD.yml +++ b/.github/workflows/CD.yml @@ -1,9 +1,9 @@ -name: CI +name: CD on: - pull_request: - branches: - - 'main' + push: + tags: + - '*' jobs: pypi-build: @@ -18,7 +18,6 @@ jobs: uses: actions/setup-python@v5 with: python-version: 3.11 - cache: 'pip' - name: Install build dependencies run: python3 -m pip install --upgrade build @@ -29,23 +28,22 @@ jobs: - name: Upload distribution artifact uses: actions/upload-artifact@v4 with: + name: release path: dist - tests: - name: Tests + pypi-publish: + # Split build and publish to restrict trusted publishing to just this workflow + needs: ['pypi-build'] + name: Publish to PyPI.org runs-on: ubuntu-latest + permissions: + # IMPORTANT: this permission is mandatory for trusted publishing + id-token: write steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Setup Python 3.11 - uses: actions/setup-python@v5 + - uses: actions/download-artifact@v4 with: - python-version: 3.11 - cache: pip - - - name: Install model_config_tests - run: pip install -e '.[test]' + name: release + path: dist - - name: Run tests - run: pytest -s tests + - name: Publish package distributions to PyPI + uses: pypa/gh-action-pypi-publish@2f6f737ca5f74c637829c0f5c3acd0e29ea5e8bf # v1.8.11 diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 029c987..f0fffc2 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -6,6 +6,13 @@ on: - 'main' jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + - uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd #v3.0.1 + pypi-build: name: Build package for PyPI if: github.repository == 'access-nri/model-config-tests' @@ -34,18 +41,28 @@ jobs: tests: name: Tests runs-on: ubuntu-latest + + strategy: + matrix: + python-version: [ "3.9", "3.10", "3.11" ] + steps: - name: Checkout code uses: actions/checkout@v4 - - name: Setup Python 3.11 + - name: Setup Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: - python-version: 3.11 + python-version: ${{ matrix.python-version }} cache: pip - name: Install model_config_tests - run: pip install -e '.[test]' + run: python3 -m pip install -e '.[test]' - name: Run tests - run: pytest -s tests + run: python3 -m pytest -s + + - name: Upload code coverage + uses: codecov/codecov-action@v4 + with: + files: ./coverage.xml diff --git a/.gitignore b/.gitignore index d0f181d..335835e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ .ipynb_checkpoints .vscode .idea -/tests/tmp/ \ No newline at end of file +/tests/tmp/ +coverage.xml \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..92bb50d --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,12 @@ +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.4.4 + hooks: + - id: ruff + args: [ --fix ] + + - repo: https://github.com/psf/black + rev: 24.4.2 + hooks: + - id: black + language_version: python3 \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index c9a7b96..c2344d5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,9 +1,3 @@ -[build-system] -build-backend = "setuptools.build_meta" -requires = [ - "setuptools >= 61.0.0", -] - [project] name = "model-config-tests" version = "0.0.1" @@ -30,7 +24,7 @@ dependencies = [ [project.optional-dependencies] test = [ - "pylint", + "pytest-cov", "jsonschema >=4.21.1" ] @@ -39,4 +33,68 @@ model-config-tests = "model_config_tests.__main__:main" [project.urls] Homepage = "https://github.com/ACCESS-NRI/model-config-tests/" -Issues = "https://github.com/ACCESS-NRI/model-config-tests/issues" \ No newline at end of file +Issues = "https://github.com/ACCESS-NRI/model-config-tests/issues" + +[build-system] +build-backend = "setuptools.build_meta" +requires = [ + "setuptools >= 61.0.0", +] + +[tool.pytest.ini_options] +addopts = "--cov=./src --cov-report=xml" +testpaths = ["tests"] + +[tool.ruff] +target-version = "py39" +exclude = [ + ".bzr", + ".direnv", + ".eggs", + ".git", + ".git-rewrite", + ".hg", + ".ipynb_checkpoints", + ".mypy_cache", + ".nox", + ".pants.d", + ".pyenv", + ".pytest_cache", + ".pytype", + ".ruff_cache", + ".svn", + ".tox", + ".venv", + ".vscode", + "__pypackages__", + "_build", + "buck-out", + "build", + "dist", + "node_modules", + "site-packages", + "venv", +] +# E402: module level import not at top of file +# E501: line too long - let black worry about that +ignore = [ + "E402", + "E501", +] +select = [ + # Pyflakes + "F", + # Pycodestyle + "E", + "W", + # isort + "I", + # Pyupgrade + "UP", +] + +[tool.ruff.mccabe] +max-complexity = 18 + +[tool.ruff.isort] +known-first-party = ["model_config_tests"]