Skip to content

Commit

Permalink
ci/tests: modernize based on poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
neersighted committed Mar 30, 2024
1 parent cc9df18 commit 5d1b188
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 150 deletions.
5 changes: 1 addition & 4 deletions .github/actions/bootstrap-poetry/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ runs:
allow-prereleases: ${{ inputs.python-prereleases == 'true' }}
update-environment: false

- run: >
pipx install \
${{ inputs.python-version != 'default' && format('--python "{0}"', steps.setup-python.outputs.python-path) || '' }} \
'${{ inputs.poetry-spec }}'
- run: pipx install ${{ inputs.python-version != 'default' && format('--python "{0}"', steps.setup-python.outputs.python-path) || '' }} '${{ inputs.poetry-spec }}'
shell: bash

# Enable handling long path names (+260 char) on the Windows platform
Expand Down
7 changes: 3 additions & 4 deletions .github/actions/poetry-install/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ outputs:
description: Whether an exact cache hit occured
value: ${{ steps.cache.outputs.cache-hit }}

defaults:
run:
working-directory: ${{ inputs.path }}

runs:
using: composite
steps:
Expand Down Expand Up @@ -48,10 +44,13 @@ runs:
enableCrossOsArchive: true

- run: poetry install ${{ inputs.args }}
working-directory: ${{ inputs.path }}
shell: bash

- run: poetry env info
working-directory: ${{ inputs.path }}
shell: bash

- run: poetry show
working-directory: ${{ inputs.path }}
shell: bash
95 changes: 95 additions & 0 deletions .github/workflows/.tests-matrix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Reusable workflow consumed by tests.yaml; used to share a single matrix across jobs.
on:
workflow_call:
inputs:
runner:
required: true
type: string
python-version:
required: true
type: string
run-mypy:
required: true
type: boolean
run-pytest:
required: true
type: boolean
run-pytest-poetry:
required: true
type: boolean

defaults:
run:
shell: bash

jobs:
mypy:
name: mypy
runs-on: ${{ inputs.runner }}
if: inputs.run-mypy
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4

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

- uses: ./.github/actions/poetry-install

- uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4
with:
path: .mypy_cache
key: mypy-${{ runner.os }}-py${{ steps.bootstrap-poetry.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'poetry.lock') }}
restore-keys: |
mypy-${{ runner.os }}-py${{ steps.bootstrap-poetry.outputs.python-version }}-
mypy-${{ runner.os }}-
- run: poetry run mypy

pytest:
name: pytest
runs-on: ${{ inputs.runner }}
if: inputs.run-pytest
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4

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

- uses: ./.github/actions/poetry-install
with:
args: --with github-actions

- run: poetry run pytest --integration -v

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

pytest-poetry:
name: pytest (Poetry)
runs-on: ${{ inputs.runner }}
if: inputs.run-pytest-poetry
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
with:
path: poetry-core

- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
with:
path: poetry
repository: python-poetry/poetry

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

- uses: ./poetry-core/.github/actions/poetry-install
with:
path: ./poetry

- run: poetry add ../poetry-core
working-directory: ./poetry

- run: poetry run pytest -v
working-directory: ./poetry
76 changes: 0 additions & 76 deletions .github/workflows/downstream.yaml

This file was deleted.

153 changes: 87 additions & 66 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,81 +1,102 @@
name: Tests

on:
pull_request: {}
merge_group:
pull_request:
push:
branches: [main]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

defaults:
run:
shell: bash

env:
PYTHONWARNDEFAULTENCODING: 'true'

jobs:
tests:
name: ${{ matrix.os }} / ${{ matrix.python-version }}
runs-on: "${{ matrix.os }}-latest"
changes:
name: Detect changed files
runs-on: ubuntu-latest
outputs:
project: ${{ steps.changes.outputs.project }}
src: ${{ steps.changes.outputs.src }}
tests: ${{ steps.changes.outputs.tests }}
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4

- uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3
id: changes
with:
filters: |
workflow: &workflow
- '.github/actions/**'
- '.github/workflows/tests.yaml'
- '.github/workflows/.tests-matrix.yaml'
project: &project
- *workflow
- 'poetry.lock'
- 'pyproject.toml'
src:
- *project
- 'src/**/*.py'
tests:
- *project
- 'src/**/*.py'
- 'tests/**'
lockfile:
name: Check poetry.lock
runs-on: ubuntu-latest
if: needs.changes.outputs.project == 'true'
needs: changes
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4

- uses: ./.github/actions/bootstrap-poetry

- run: poetry check --lock

tests-matrix:
# Use this matrix with multiple jobs defined in a reusable workflow:
uses: ./.github/workflows/.tests-matrix.yaml
name: ${{ matrix.os.name }} (Python ${{ matrix.python-version }})
if: '!failure() && !cancelled()'
needs:
- lockfile
- changes
with:
runner: ${{ matrix.os.image }}
python-version: ${{ matrix.python-version }}
run-mypy: ${{ needs.changes.outputs.src == 'true' }}
run-pytest: ${{ needs.changes.outputs.tests == 'true' }}
run-pytest-poetry: ${{ needs.changes.outputs.src == 'true' }}
secrets: inherit
strategy:
matrix:
os: [Ubuntu, MacOS, Windows]
os:
- name: Ubuntu
image: ubuntu-22.04
- name: macOS
image: macos-13
- name: Windows
image: windows-2022
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
include:
- os: Ubuntu
python-version: pypy-3.8
- os: {name: Ubuntu, image: ubuntu-22.04}
python-version: pypy3.9
- os: {name: Ubuntu, image: ubuntu-22.04}
python-version: pypy3.10
fail-fast: false
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v4

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

- name: Get full Python version
id: full-python-version
run: echo version=$(python -c "import sys; print('-'.join(str(v) for v in sys.version_info))") >> $GITHUB_OUTPUT

- name: Bootstrap poetry
run: |
curl -sSL https://install.python-poetry.org | python - -y

- name: Update PATH
if: ${{ matrix.os != 'Windows' }}
run: echo "$HOME/.local/bin" >> $GITHUB_PATH

- name: Update Path for Windows
if: ${{ matrix.os == 'Windows' }}
run: echo "$APPDATA\Python\Scripts" >> $GITHUB_PATH

- name: Configure poetry
run: poetry config virtualenvs.in-project true

- name: Set up cache
uses: actions/cache@v4
id: cache
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}

- name: Ensure cache is healthy
if: steps.cache.outputs.cache-hit == 'true'
run: |
# `timeout` is not available on macOS, so we define a custom function.
[ "$(command -v timeout)" ] || function timeout() { perl -e 'alarm shift; exec @ARGV' "$@"; }
# Using `timeout` is a safeguard against the Poetry command hanging for some reason.
timeout 10s poetry run pip --version || rm -rf .venv
- name: Check lock file
run: poetry lock --check

- name: Install dependencies
run: poetry install

- name: Run tests
run: poetry run python -m pytest -p no:sugar -q tests/

- name: Run integration tests
run: poetry run python -m pytest -p no:sugar --integration -q tests/integration

- name: Run mypy
run: poetry run mypy
status:
name: Status
runs-on: ubuntu-latest
if: '!cancelled()'
needs:
- lockfile
- tests-matrix
steps:
- run: ${{ (contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')) && 'false' || 'true' }}

0 comments on commit 5d1b188

Please sign in to comment.