name: Codecov

on:
  push:
    branches:
      - main
    paths-ignore:
      - README.md
      - CONTRIBUTING.md
      - CITATION.cff
      - LICENSE
      - .readthedocs.yml
      - docs-img/**
      - docs/**
      - awkward-cpp/docs/**
      - studies/**

  workflow_dispatch:

concurrency:
  group: 'coverage-${{ github.head_ref || github.run_id }}'
  cancel-in-progress: true

env:
  # Leverage reproducible builds by setting a constant SOURCE_DATE_EPOCH
  # This will ensure that the hash of the awkward-cpp directory remains
  # constant for unchanged files, meaning that it can be used for caching
  SOURCE_DATE_EPOCH: "1668811211"

jobs:
  coverage:
    runs-on: ubuntu-latest
    name: Run Codecov

    env:
      PIP_ONLY_BINARY: cmake
      PYTHON_VERSION: "3.10"

    steps:
      - uses: actions/checkout@v6
        with:
          submodules: true

      - name: 'Python ${{ env.PYTHON_VERSION }}'
        uses: actions/setup-python@v6
        with:
          python-version: '${{ env.PYTHON_VERSION }}'

      - name: Generate build files
        run: pipx run nox -s prepare -- --headers --signatures --tests

      - name: Cache awkward-cpp wheel
        id: cache-awkward-cpp-wheel
        uses: actions/cache@v5
        with:
          path: ./awkward-cpp/dist
          key: ${{ github.job }}-${{ env.PYTHON_VERSION }}-${{ hashFiles('awkward-cpp/**') }}

      - name: Build awkward-cpp wheel
        if: steps.cache-awkward-cpp-wheel.outputs.cache-hit != 'true'
        run: |
          python -m pip install build
          python -m build -w ./awkward-cpp
          ls ./awkward-cpp/dist

      - name: Install awkward-cpp
        run: python -m pip install -v ./awkward-cpp/dist/*.whl

      - name: Build & install awkward
        run: python -m pip install -v .

      - name: Print versions
        run: python -m pip list

      - name: Install test requirements
        run: python -m pip install -v -r requirements-test-full.txt

      - name: Test
        run: >-
          python -m pytest -vv -rs tests --cov=awkward --cov-report=term
          --cov-report=xml

      - name: Upload Codecov results
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
