name: 'cmake-test'
description: 'CMake and CTest reusable component'
inputs:
  cmake-flags:
    description: 'CMake flags to add to build options'
    required: true
  n-chunks:
    chunk: 'Total number of test chunks'
    required: true
  chunk:
    chunk: 'Chunk index to test'
    required: true
runs:
  using: "composite"
  steps:
    - name: checkout
      uses: actions/checkout@v4

    - name: setup-env
      run: |
        JOBS=$(nproc || sysctl -n hw.ncpu || echo 2)
        echo "JOBS=${JOBS}" >> $GITHUB_ENV
        echo "[Debug] Test runner will use ${JOBS} jobs."
      shell: bash

    - name: setup-domain
      run: cmake -S . -B build ${{ inputs.cmake-flags }}
      shell: bash

    - name: make
      run: cmake --build build -j${JOBS}
      shell: bash

    - name: ctest
      run: |
        TESTS_TO_RUN=$(
        echo "$(ctest --test-dir build --show-only=json-v1 | jq '.tests | length') ${{ inputs.n-chunks }} ${{ inputs.chunk }}" |
        sh/chunkify.py
        )
        echo "[Debug] Running tests in range ${TESTS_TO_RUN}."
        ctest --test-dir build -I "${TESTS_TO_RUN}" --output-on-failure --progress -j${JOBS}
      shell: bash
