on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - "*"    

name: Code coverage
permissions:
  contents: read

jobs:
  check:
    name: Code coverage
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
        with:
          persist-credentials: false
      
      - name: Install toolchain
        uses: dtolnay/rust-toolchain@b3b07ba8b418998c39fb20f53e8b695cdcc8de1b
        with:
          toolchain: stable
          components: llvm-tools-preview

      - name: Install grcov
        run: cargo install grcov
      
      - name: Generate code coverage
        env:
          CARGO_INCREMENTAL: "0"
          RUSTFLAGS: "-Cinstrument-coverage"
          LLVM_PROFILE_FILE: "cargo-test-%p-%m.profraw"
        run: cargo test --all-features
      
      - name: Create coverage file
        run: |
          grcov . --binary-path ./target/debug/deps/ -s . -t lcov --branch --ignore-not-existing --ignore '../*' --ignore "/*" -o ./target/coverage.lcov

      - name: Upload to codecov.io
        uses: codecov/codecov-action@18283e04ce6e62d37312384ff67231eb8fd56d24
        with:
          fail_ci_if_error: true
          files: ./target/*.lcov
        env:
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
