Skip to content

Commit

Permalink
ci/tests: DRY tests matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
neersighted committed Mar 25, 2024
1 parent aac98a8 commit b683ec7
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 109 deletions.
113 changes: 113 additions & 0 deletions .github/workflows/.tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Reusable workflow consumed by tests.yaml; used to share a single matrix across jobs.
name: .tests

on:
workflow_call:
inputs:
os-name:
required: true
type: string
os-image:
required: true
type: string
python-version:
required: true
type: string
run-mypy:
required: true
type: bool
run-pytest:
required: true
type: bool
run-pytest-export:
required: true
type: bool

jobs:
mypy:
name: mypy [${{ inputs.os-name }} / Python ${{ inputs.python-version }}]
runs-on: ${{ inputs.os-image }}
if: inputs.run-mypy
needs:
- changes
- lockfile
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/bootstrap-poetry
with:
python-version: ${{ matrix.python-version }}

- run: poetry install --sync

- run: poetry env info

- run: poetry show

- uses: actions/cache@v4
with:
path: .mypy_cache
key: mypy-${{ matrix.os.image }}-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml', 'poetry.lock') }}

- run: poetry run mypy

pytest:
name: pytest [${{ inputs.os-name }} / Python ${{ inputs.python-version }}]
runs-on: ${{ inputs.os-image }}
if: inputs.run-pytest
needs:
- changes
- lockfile
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/bootstrap-poetry
with:
python-version: ${{ matrix.python-version }}

- run: poetry install --sync --with github-actions

- run: poetry env info

- run: poetry show

- run: poetry run pytest --integration -v
env:
POETRY_TEST_INTEGRATION_GIT_USERNAME: ${{ github.actor }}
POETRY_TEST_INTEGRATION_GIT_PASSWORD: ${{ github.token }}

- run: git diff --exit-code --stat HEAD

pytest-export:
name: pytest (poetry-plugin-export) [${{ inputs.os-name }} / Python ${{ inputs.python-version }}]
runs-on: ${{ inputs.os-image }}
if: inputs.run-pytest-export
needs:
- changes
- lockfile
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/bootstrap-poetry
with:
python-version: ${{ inputs.python-version }}

- run: poetry install --sync --with github-actions

- run: poetry env info

- run: poetry show

- id: poetry-plugin-export
run: echo version=$(poetry run pip list --format json | jq -r '.[] | select(.name == "poetry-plugin-export").version') >> $GITHUB_OUTPUT

- uses: actions/checkout@v4
with:
path: poetry-plugin-export
repository: python-poetry/poetry-plugin-export
ref: refs/tags/${{ steps.poetry-plugin-export.outputs.version }}

- run: poetry run -C .. pytest -v
working-directory: ./poetry-plugin-export

- run: git -C poetry-plugin-export diff --exit-code --stat HEAD
120 changes: 11 additions & 109 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,7 @@ jobs:

- run: git diff --exit-code --stat HEAD tests/repositories/fixtures/pypi.org

mypy:
name: mypy [${{ matrix.os.name }} / Python ${{ matrix.python-version }}]
runs-on: ${{ matrix.os.image }}
if: needs.changes.outputs.mypy == 'true'
needs:
- changes
- lockfile
tests-matrix:
strategy:
matrix:
os:
Expand All @@ -97,108 +91,16 @@ jobs:
image: windows-2022
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
fail-fast: false
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/bootstrap-poetry
with:
python-version: ${{ matrix.python-version }}

- run: poetry install --sync

- run: poetry env info

- run: poetry show

- uses: actions/cache@v4
with:
path: .mypy_cache
key: mypy-${{ matrix.os.image }}-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml', 'poetry.lock') }}

- run: poetry run mypy

pytest:
name: pytest [${{ matrix.os.name }} / Python ${{ matrix.python-version }}]
runs-on: ${{ matrix.os.image }}
if: needs.changes.outputs.pytest == 'true'
needs:
- changes
- lockfile
strategy:
matrix:
os:
- name: Ubuntu
image: ubuntu-22.04
- name: macOS
image: macos-12
- name: Windows
image: windows-2022
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
fail-fast: false
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/bootstrap-poetry
with:
python-version: ${{ matrix.python-version }}

- run: poetry install --sync --with github-actions

- run: poetry env info

- run: poetry show

- run: poetry run pytest --integration -v
env:
POETRY_TEST_INTEGRATION_GIT_USERNAME: ${{ github.actor }}
POETRY_TEST_INTEGRATION_GIT_PASSWORD: ${{ github.token }}

- run: git diff --exit-code --stat HEAD

pytest-export:
name: pytest (poetry-plugin-export) [${{ matrix.os.name }} / Python ${{ matrix.python-version }}]
runs-on: ${{ matrix.os.image }}
if: needs.changes.outputs.pytest-export == 'true'
needs:
- changes
- lockfile
strategy:
matrix:
os:
- name: Ubuntu
image: ubuntu-22.04
- name: macOS
image: macos-12
- name: Windows
image: windows-2022
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
fail-fast: false
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/bootstrap-poetry
with:
python-version: ${{ matrix.python-version }}

- run: poetry install --sync --with github-actions

- run: poetry env info

- run: poetry show

- id: poetry-plugin-export
run: echo version=$(poetry run pip list --format json | jq -r '.[] | select(.name == "poetry-plugin-export").version') >> $GITHUB_OUTPUT

- uses: actions/checkout@v4
with:
path: poetry-plugin-export
repository: python-poetry/poetry-plugin-export
ref: refs/tags/${{ steps.poetry-plugin-export.outputs.version }}

- run: poetry run -C .. pytest -v
working-directory: ./poetry-plugin-export

- run: git -C poetry-plugin-export diff --exit-code --stat HEAD
# Use this matrix with multiple jobs defined in a reusable workflow:
uses: ./.github/workflows/.tests.yaml
with:
os-name: ${{ matrix.os.name }}
os-image: ${{ matrix.os.image }}
python-version: ${{ matrix.python-version }}
run-mypy: ${{ needs.changes.outputs.mypy == 'true' }}
run-pytest: ${{ needs.changes.outputs.pytest == 'true' }}
run-pytest-export: ${{ needs.changes.outputs.pytest-export == 'true' }}
secrets: inherit

status:
name: Status
Expand Down

0 comments on commit b683ec7

Please sign in to comment.