name: Celix Coverage

on: [push, pull_request]

env:
  CONAN_USER_HOME: "${{ github.workspace }}/release/"
  CONAN_USER_HOME_SHORT: "${{ github.workspace }}/release/short"
  CONAN_HOME: "${{ github.workspace }}/release/"
  CCACHE_DIR: "${{ github.workspace }}/.ccache"
  CCACHE_SLOPPINESS: include_file_ctime,include_file_mtime

jobs:
  coverage:
    runs-on: ubuntu-20.04
    timeout-minutes: 120
    steps:
      - name: Checkout source code
        uses: actions/checkout@v3.3.0
      - name: Install conan and lcov
        run: |
          sudo apt-get install -yq --no-install-recommends lcov
          sudo pip install conan==1.59.0
      - name: Setup Conan Profile
        run: |
          conan profile new default --detect
          conan profile update settings.build_type=Debug default
          #Note no backwards compatiblity for gcc5 needed, setting libcxx to c++11.
          conan profile update settings.compiler.libcxx=libstdc++11 default
      - name: Conan Cache
        id: cache-conan
        uses: actions/cache@v3
        env:
          cache-name: cache-conan2-modules
        with:
          path: ${{ env.CONAN_HOME }}
          key: ${{ runner.os }}-gcov-builder-${{ env.cache-name }}-${{ hashFiles('conanfile.py') }}
          restore-keys: ${{ runner.os }}-gcov-builder-${{ env.cache-name }}-
      - name: Prepare ccache timestamp
        id: ccache_cache_timestamp
        run: |
          echo timestamp=`date +"%Y-%m-%d-%H;%M;%S"` >> $GITHUB_OUTPUT
      - name: ccache Cache
        uses: actions/cache@v3
        with:
          path: ${{ env.CCACHE_DIR }}
          key: ${{ runner.os }}-gcov-ccache-${{ steps.ccache_cache_timestamp.outputs.timestamp }}
          restore-keys: |
            ${{ runner.os }}-gcov-ccache-
      - name: Install Dependencies
        env:
          CONAN_BUILD_OPTIONS: |
            -o celix:enable_testing=True
            -o celix:build_all=True
            -o celix:enable_code_coverage=True
            -o celix:enable_testing_on_ci=True
            -o celix:enable_ccache=True
        run: |
          #force require libcurl 7.64.1, due to a sha256 verify issue in libcurl/7.87.0
          conan install . celix/ci -pr:b default -pr:h default -if build ${CONAN_BUILD_OPTIONS} -b missing -b cpputest --require-override=libcurl/7.64.1 --require-override=openssl/1.1.1s
      - name: Build
        run: |
          conan build . -bf build
      - name: Test with coverage
        run: |
          cd build
          source conanrun.sh
          make coverage
          source deactivate_conanrun.sh
          lcx="lcov --output-file=coverage.info " && for i in `find . -name "*.info.cleaned"`; do lcx+=" --add-tracefile=$i"; done && $lcx
      - name: Codecov
        uses: codecov/codecov-action@e156083f13aff6830c92fc5faa23505779fbf649
        with:
          file: build/coverage.info
          name: codecov-celix