name: Code Coverage

on:
  push:
    branches:
      - main
      - feat/*
  pull_request:
    branches:
      - main
      - feat/*
    paths-ignore:
      - "docs/**"
      - "**.md"
  merge_group:
    types: [checks_requested]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

defaults:
  run:
    # micromamba activation
    shell: bash -l -eo pipefail {0}

jobs:
  coverage:
    name: Code Coverage
    runs-on: ubuntu-latest
    steps:
      - name: Checkout mamba repository
        uses: actions/checkout@v4
      - name: Create build environment
        uses: mamba-org/setup-micromamba@v2
        with:
          environment-file: ./dev/environment-dev.yml
          environment-name: build_env
          cache-environment: true
      - name: Install dev-extra environment
        run: micromamba install -n build_env -y -f ./dev/environment-dev-extra.yml
      - name: Install micromamba-static environment
        run: micromamba install -n build_env -y -f ./dev/environment-micromamba-static.yml
      - uses: hendrikmuhs/ccache-action@main
        with:
          variant: sccache
          key: ${{ github.job }}-${{ github.runner.os }}
          restore-keys: |
            ccache-libmamba-${{ github.runner.os }}
      - name: Build mamba with coverage
        run: |
          cmake -B build/ -G Ninja \
            --preset mamba-unix-shared-debug \
            -D CMAKE_CXX_COMPILER_LAUNCHER=sccache \
            -D CMAKE_C_COMPILER_LAUNCHER=sccache \
            -D MAMBA_WARNING_AS_ERROR=ON \
            -D BUILD_LIBMAMBAPY=ON \
            -D BUILD_MICROMAMBA=ON \
            -D ENABLE_MAMBA_ROOT_PREFIX_FALLBACK=OFF \
            -D CMAKE_BUILD_TYPE=Debug \
            -D CMAKE_CXX_FLAGS_DEBUG="-g -O0 -fno-omit-frame-pointer --coverage -fprofile-update=atomic" \
            -D CMAKE_C_FLAGS_DEBUG="-g -O0 -fno-omit-frame-pointer --coverage -fprofile-update=atomic"
          cmake --build build/ --parallel
      - name: Show build cache statistics
        run: sccache --show-stats
      - name: Run C++ tests with coverage
        continue-on-error: true
        run: |
          unset CONDARC  # Interferes with tests
          ./build/libmamba/ext/solv-cpp/tests/test_solv_cpp
          ./build/libmamba/tests/test_libmamba
      - name: Install lcov
        run: |
          sudo apt-get update
          sudo apt-get install -y lcov
      - name: Generate C++ coverage report
        run: |
          lcov --directory . --capture --output-file cpp_coverage.info
          lcov --remove cpp_coverage.info '/usr/*' '*/tests/*' '*/build/*' --output-file cpp_coverage.info --ignore-errors unused
          lcov --summary cpp_coverage.info --ignore-errors mismatch --rc geninfo_unexecuted_blocks=1

      # TODO: Those steps need adaptations so that the coverage reports from the C++ and the python test suites are consolidated.
      # - name: Install libmambapy
      #   run: |
      #     cmake --install build/ --prefix "${CONDA_PREFIX}"
      #     python -m pip install --no-deps --no-build-isolation ./libmambapy
      # - name: Run Python tests with coverage
      #   continue-on-error: true
      #   run: |
      #     # Run libmambapy tests with coverage
      #     python -m pytest libmambapy/tests/ --cov=libmambapy --cov-report=xml --cov-report=term-missing
      #     # Run micromamba tests with coverage
      #     export TEST_MAMBA_EXE=$(pwd)/build/micromamba/mamba
      #     python -m pytest micromamba/tests/ --cov=micromamba --cov-report=xml --cov-report=term-missing

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v5
        with:
          files: |
            ./cpp_coverage.info
            ./coverage.xml
          fail_ci_if_error: true
          token: ${{ secrets.CODECOV_TOKEN }}
