Skip to content

Commit

Permalink
refactor package to use nox for testing (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuadavidthomas authored Mar 23, 2024
1 parent 9d6a7d4 commit a5ee6d2
Show file tree
Hide file tree
Showing 6 changed files with 419 additions and 80 deletions.
98 changes: 82 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: test

on:
push:
branches: main
pull_request_target:
branches: [main]
pull_request:

concurrency:
group: test-${{ github.head_ref }}
Expand All @@ -14,24 +14,90 @@ env:
FORCE_COLOR: "1"

jobs:
run:
name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }}
runs-on: ${{ matrix.os }}
generate-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false

- uses: westerveltco/setup-ci-action@v0
with:
python-version: 3.8
extra-python-dependencies: nox
use-uv: true

- id: set-matrix
run: |
echo "matrix=$(python -m nox -l --json | jq -c '[.[] | select(.name == "tests") | {"python-version": .python, "django-version": .call_spec.django, "wagtail-version": .call_spec.wagtail}] | {include: .}')" >> $GITHUB_OUTPUT
test:
name: Python ${{ matrix.python-version }}, Django ${{ matrix.django-version }}, Wagtail ${{ matrix.wagtail-version }}
runs-on: ubuntu-latest
needs: generate-matrix
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12-dev"]
matrix: ${{ fromJSON(needs.generate-matrix.outputs.matrix) }}
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
- uses: actions/checkout@v4
with:
persist-credentials: false

- uses: westerveltco/setup-ci-action@v0
with:
python-version: ${{ matrix.python-version }}
- name: Install Hatch
run: |
python -m pip install --upgrade pip
python -m pip install hatch
extra-python-dependencies: nox
use-uv: true

- name: Run tests
run: hatch run cov
run: |
python -m nox --session "tests(python='${{ matrix.python-version }}', django='${{ matrix.django-version }}', wagtail='${{ matrix.wagtail-version }}')"
tests:
runs-on: ubuntu-latest
needs: test
if: always()
steps:
- name: OK
if: ${{ !(contains(needs.*.result, 'failure')) }}
run: exit 0
- name: Fail
if: ${{ contains(needs.*.result, 'failure') }}
run: exit 1

types:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false

- uses: westerveltco/setup-ci-action@v0
with:
python-version: 3.8
extra-python-dependencies: nox
use-uv: true

- name: Run mypy
run: |
python -m nox --session "mypy"
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false

- uses: westerveltco/setup-ci-action@v0
with:
python-version: 3.8
extra-python-dependencies: nox
use-uv: true

- name: Run coverage
run: |
python -m nox --session "coverage"
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,23 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

<!--
## [${version}]
### Added - for new features
### Changed - for changes in existing functionality
### Deprecated - for soon-to-be removed features
### Removed - for now removed features
### Fixed - for any bug fixes
### Security - in case of vulnerabilities
[${version}]: https://github.com/westerveltco/wagtail-heroicons/releases/tag/v${version}
-->

## [Unreleased]

### Changed

- Now using `nox` for testing.

### Removed

- Dropped support for Python 3.7.
Expand Down
43 changes: 43 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
set dotenv-load := true

@_default:
just --list

# ----------------------------------------------------------------------
# DEPENDENCIES
# ----------------------------------------------------------------------

bootstrap:
@just pup
python -m uv pip install --editable '.[dev]'

pup:
python -m pip install --upgrade pip uv

# ----------------------------------------------------------------------
# TESTING/TYPES
# ----------------------------------------------------------------------

test *ARGS:
python -m nox --session "test" -- "{{ ARGS }}"

testall *ARGS:
python -m nox --session "tests" -- "{{ ARGS }}"

coverage:
python -m nox --session "coverage"

types:
python -m nox --session "mypy"

# ----------------------------------------------------------------------
# UTILS
# ----------------------------------------------------------------------

# format justfile
fmt:
just --fmt --unstable

# run pre-commit on all files
lint:
python -m nox --session "lint"
150 changes: 150 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
from __future__ import annotations

import os
from pathlib import Path

import nox

nox.options.default_venv_backend = "uv|virtualenv"
nox.options.reuse_existing_virtualenvs = True

PY38 = "3.8"
PY39 = "3.9"
PY310 = "3.10"
PY311 = "3.11"
PY312 = "3.12"
PY_VERSIONS = [PY38, PY39, PY310, PY311, PY312]
PY_DEFAULT = PY_VERSIONS[0]
PY_LATEST = PY_VERSIONS[-1]

DJ32 = "3.2"
DJ42 = "4.2"
DJ50 = "5.0"
DJMAIN = "main"
DJMAIN_MIN_PY = PY310
DJ_VERSIONS = [DJ32, DJ42, DJ50, DJMAIN]
DJ_LTS = [DJ32, DJ42]
DJ_DEFAULT = DJ_LTS[0]
DJ_LATEST = DJ_VERSIONS[-2]

WT52 = "5.2"
WT60 = "6.0"
WTMAIN = "main"
WTMAIN_MIN_PY = PY38
WTMAIN_MIN_DJ = DJ42
WT_VERSIONS = [WT52, WT60, WTMAIN]
WT_LTS = [WT52]
WT_DEFAULT = WT_LTS[0]
WT_LATEST = WT_VERSIONS[-2]


def version(ver: str) -> tuple[int, ...]:
"""Convert a string version to a tuple of ints, e.g. "3.10" -> (3, 10)"""
return tuple(map(int, ver.split(".")))


def should_skip(python: str, django: str, wagtail: str) -> bool:
"""Return True if the test should be skipped"""

if django == DJMAIN and version(python) < version(DJMAIN_MIN_PY):
# Django main requires Python 3.10+
return True

if django == DJ32 and version(python) >= version(PY312):
# Django 3.2 requires Python < 3.12
return True

if django == DJ50 and version(python) < version(PY310):
# Django 5.0 requires Python 3.10+
return True

if wagtail == WTMAIN and version(python) < version(WTMAIN_MIN_PY):
# Wagtail main requires Python 3.8+
return True

if wagtail == WTMAIN and (
django == DJMAIN or version(django) < version(WTMAIN_MIN_DJ)
):
# Wagtail main requires Django 4.2+
return True

return False


@nox.session
def test(session):
session.notify(
f"tests(python='{PY_DEFAULT}', django='{DJ_DEFAULT}', wagtail='{WT_DEFAULT}')"
)


@nox.session
@nox.parametrize(
"python,django,wagtail",
[
(python, django, wagtail)
for python in PY_VERSIONS
for django in DJ_VERSIONS
for wagtail in WT_VERSIONS
if not should_skip(python, django, wagtail)
],
)
def tests(session, django, wagtail):
session.install("wagtail-heroicons[dev] @ .")

if django == DJMAIN:
session.install(
"django @ https://github.com/django/django/archive/refs/heads/main.zip"
)
else:
session.install(f"django=={django}")

if wagtail == WTMAIN:
session.install(
"wagtail @ https://github.com/wagtail/wagtail/archive/refs/heads/main.zip"
)
else:
session.install(f"wagtail=={wagtail}")

session.run("python", "-m", "pytest")


@nox.session
def coverage(session):
session.install("wagtail-heroicons[dev] @ .")
session.run("python", "-m", "pytest", "--cov=wagtail_heroicons")

try:
summary = os.environ["GITHUB_STEP_SUMMARY"]
with Path(summary).open("a") as output_buffer:
output_buffer.write("")
output_buffer.write("### Coverage\n\n")
output_buffer.flush()
session.run(
"python",
"-m",
"coverage",
"report",
"--skip-covered",
"--skip-empty",
"--format=markdown",
stdout=output_buffer,
)
except KeyError:
session.run(
"python", "-m", "coverage", "html", "--skip-covered", "--skip-empty"
)

session.run("python", "-m", "coverage", "report")


@nox.session
def lint(session):
session.install("wagtail-heroicons[lint] @ .")
session.run("python", "-m", "pre_commit", "run", "--all-files")


@nox.session
def mypy(session):
session.install("wagtail-heroicons[dev] @ .")
session.run("python", "-m", "mypy", ".")
Loading

0 comments on commit a5ee6d2

Please sign in to comment.