name: Unit tests

on:
  push:
    branches:
      - main
  pull_request:

permissions: {}

jobs:
  test-ubuntu-macos:
    strategy:
      matrix:
        platform:
          - macos-latest
          - ubuntu-latest
        python:
          - "3.9"
          - "3.10"
          - "3.11"
          - "3.12"
          - "3.13"
          - "pypy"

    runs-on: ${{ matrix.platform }}
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Install the latest version of uv and set the python version
        uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
        with:
          enable-cache: true
          cache-dependency-glob: pyproject.toml

      - name: Install python
        run: uv python install ${{ matrix.python }}

      - name: test
        run: make test

  test-windows:
    strategy:
      matrix:
        windows:
          - 'win64'
        python:
          - "3.9"
          - "3.10"
          - "3.11"
          - "3.12"
          - "3.13"

    runs-on: windows-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Install the latest version of uv and set the python version
        uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
        with:
          enable-cache: true
          cache-dependency-glob: pyproject.toml

      # Split Python installation from virtual environment creation.
      # setup-uv@v5's python-version parameter automatically creates and activates a venv, which conflicts with our
      # Makefile's venv management.
      # https://github.com/astral-sh/setup-uv/pull/194
      - name: Install python
        run: uv python install ${{ matrix.python }}

      - uses: dawidd6/action-download-artifact@b6e2e70617bc3265edd6dab6c906732b2f1ae151 # v21
        with:
          repo: pyca/infra
          workflow: build-windows-openssl.yml
          branch: main
          workflow_conclusion: success
          name: "openssl-${{ matrix.windows }}"
          path: "C:/openssl-${{ matrix.windows }}/"
          github_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Configure
        shell: bash
        run: |
          echo "OPENSSL_DIR=C:/openssl-${{ matrix.windows }}" >> $GITHUB_ENV
          echo "OPENSSL_NO_VENDOR=1" >> $GITHUB_ENV

      - name: test
        run: make test
        shell: bash

  all-tests-pass:
    if: always()

    needs: [test-ubuntu-macos, test-windows]

    runs-on: ubuntu-latest

    steps:
      - name: check test jobs
        uses: re-actors/alls-green@05ac9388f0aebcb5727afa17fcccfecd6f8ec5fe # v1.2.2
        with:
          jobs: ${{ toJSON(needs) }}
