name: Tests

on: [push, pull_request]

jobs:
  Tests:
    name: Test ${{ matrix.os }} with ${{ matrix.python-version }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      max-parallel: 18
      matrix:
        os: ["ubuntu-latest", "macos-latest", "windows-latest"]
        python-version: ["3.9", "3.10", "3.11", "3.12"]
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # fetch the entire repo history, required to guarantee versioneer will pick up the tags
      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - run: python -m venv pycalphad-dev
      - name: "Activate virtual environment (Linux/Mac)"
        run: source pycalphad-dev/bin/activate
        if: ${{ runner.os != 'Windows' }}
      - name: "Activate virtual environment (Windows)"
        run: pycalphad-dev\Scripts\Activate.ps1
        if: ${{ runner.os == 'Windows' }}
      - run: pip install build
      - run: python -m build --wheel
        env:
          CYTHON_COVERAGE: 1
      - run: pip install dist/*.whl
        shell: bash  # support cross-platform paths
      - run: pip check
      - run: pip list
      # pytest:
      # - The `--import-mode=append` and `--pyargs pycalphad` flags test the installed package over the local one
      # - The `--cov` flag is required to turn on coverage
      - run: pytest -v --import-mode=append --cov --cov-config=pyproject.toml --pyargs pycalphad
      - run: coverage xml
      - uses: actions/upload-artifact@v4
        with:
          name: coverage-${{ matrix.os }}-${{ matrix.python-version }}
          path: coverage.xml

  Upload-Coverage:
    runs-on: ubuntu-latest
    needs: [Tests]
    steps:
      # The source code _must_ be checked out for coverage to be processed at Codecov.
      - uses: actions/checkout@v4
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: coverage-*
      - name: Upload to Codecov
        uses: codecov/codecov-action@v4
        env:
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
        with:
          fail_ci_if_error: true
