on:
  push:
    branches:
      - master
      - dev
  pull_request:
    branches:
      - master
      - dev

name: Code Coverage
jobs:
  coverage:
    name: Run test coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Cache cargo registry
        uses: actions/cache@v1
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
      - name: Cache cargo index
        uses: actions/cache@v1
        with:
          path: ~/.cargo/git
          key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
      - name: Cache cargo binaries
        uses: actions/cache@v1
        with:
          path: ~/.cargo/bin
          key: ${{ runner.os }}-cargo-bin-${{ hashFiles('**/Cargo.lock') }}
      - name: Toolchain setup
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          override: true
          components: llvm-tools-preview
      - name: Install grcov
        run: if [[ ! -e ~/.cargo/bin/grcov ]]; then cargo install grcov; fi
      - name: Updating package repo
        run: sudo apt-get update
      - name: Installing dependencies
        run: sudo apt-get install libasound2-dev
      - name: Run tests
        run: cargo test
        env:
          RUSTFLAGS: '-Zinstrument-coverage'
          LLVM_PROFILE_FILE: 'report-%p-%m.profraw'
      - name: Run grcov
        run: grcov . --binary-path target/debug/deps/ -s . -t lcov --branch --ignore-not-existing --ignore '../**' --ignore '/*' -o coverage.lcov
      - name: Coveralls upload
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          path-to-lcov: coverage.lcov
