name: Coverage

on:
  pull_request:
    paths-ignore:
      - 'docs/**'
  push:
    branches:
      - main
    paths-ignore:
      - 'docs/**'

jobs:
  coverage-ubuntu-gcc:
    timeout-minutes: 30
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        backends: [ V8, JavaScriptCore, QuickJs, Lua ]
    steps:
      - uses: actions/checkout@v2
      - uses: actions/cache@v2
        with:
          key: ScriptX-UnitTests-Depedencies-${{ hashFiles('test/cmake/**') }}
          path: |
            build/ScriptXTestLibs
            build/googletest-src
      - name: prepare lcov
        run: |
          sudo apt-get install -y lcov
      - name: Configure cmake
        env:
          SCRIPTX_TEST_FORCE_UPDATE_DEPS: ON
        run: |
          mkdir -p build && cd build
          cmake ../test                                       \
                -DSCRIPTX_BACKEND=${{ matrix.backends }}      \
                -DDEVOPS_ENABLE_COVERAGE=ON                   \
                -DCMAKE_BUILD_TYPE=DEBUG
      - name: Compile UnitTests
        run: |
          cd build
          cmake --build . -j $(nproc) --target UnitTests
      - name: Run UnitTests
        run: |
          cd build && ./UnitTests
      - name: Generate Coverate Data
        # https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
        run: |
          cd build
          lcov -c -d CMakeFiles/ -o all.info
          lcov -e all.info -o coverage.info '*/ScriptX/src/*' '*/ScriptX/backend/*'

      - name: Coveralls GitHub Action
        uses: coverallsapp/github-action@v1.1.2
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          path-to-lcov: './build/coverage.info'
          flag-name: "Backend-${{ matrix.backends }}"
          parallel: true

  coverage-finish:
    timeout-minutes: 30
    needs: coverage-ubuntu-gcc
    runs-on: ubuntu-latest
    steps:
      - name: Coveralls Finished
        uses: coverallsapp/github-action@v1.1.2
        with:
          github-token: ${{ secrets.github_token }}
          parallel-finished: true
