name: Coverage

on:
  push:
    branches:
      - master
  pull_request:

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: full

jobs:
  coverage:
    name: Coverage
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          profile: minimal
          default: true

      - name: Pre-installing grcov
        uses: actions-rs/install@v0.1
        with:
          crate: grcov
          use-tool-cache: true

      - name: Restore cache
        uses: Swatinem/rust-cache@v1

      - name: Run tests
        run: cargo test --all --all-features
        env:
          CARGO_INCREMENTAL: '0'
          RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests'
          RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests'
          # This seems not to work, the C code can't seem to find the gcov functions. By not providing this, we probably give up coverage from these :-(
          #CFLAGS: --coverage
          #CXXFLAGS: --coverage

      - name: Generate coverage
        uses: actions-rs/grcov@v0.1

      - name: Upload to codecov.io
        uses: codecov/codecov-action@5a8bb4701eca7ba3673f21664b887f652c58d0a3
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
