# PR workflow.
#
# Runs full suite of checks, with warnings treated as errors.
# Gather code coverage stats and publish them on coveralls.io.

name: PR

on: pull_request

env:
  # Run all cargo commands with --verbose.
  CARGO_TERM_VERBOSE: true
  RUST_BACKTRACE: 1
  # Deny all compiler warnings.
  RUSTFLAGS: "-D warnings"

jobs:
  checks:
    name: Rustfmt & Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      # Install Rust and required components
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          components: rustfmt, clippy

      # Generate Cargo.lock, needed for the cache.
      - name: Generate Cargo.lock
        run: cargo generate-lockfile

      # Cache.
      - name: Cargo cache registry, index and build
        uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-cache-${{ hashFiles('**/Cargo.lock') }}

      # Check if the code is formatted correctly.
      - name: Check formatting
        run: cargo fmt --all -- --check

      # Run Clippy.
      - shell: bash
        run: ./scripts/clippy

  coverage:
    name: Code coverage check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      # Install Rust
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      # Generate Cargo.lock, needed for the cache.
      - name: Generate Cargo.lock
        run: cargo generate-lockfile

      # Cache.
      - name: Cargo cache registry, index and build
        uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-cache-${{ hashFiles('**/Cargo.lock') }}

      # Run cargo tarpaulin & push result to coveralls.io
      - name: rust-tarpaulin code coverage check
        uses: actions-rs/tarpaulin@master
        with:
          args: '-v --release --out Lcov'
      - name: Push code coverage results to coveralls.io
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          parallel: true
          path-to-lcov: ./lcov.info
      - name: Coveralls Finished
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          parallel-finished: true

  test:
    name: Test
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macOS-latest]
    steps:
      - uses: actions/checkout@v2
      
      # Install Rust
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      # Generate Cargo.lock, needed for the cache.
      - name: Generate Cargo.lock
        run: cargo generate-lockfile

      # Cache.
      - name: Cargo cache registry, index and build
        uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-cache-${{ hashFiles('**/Cargo.lock') }}

      # Run tests.
      - shell: bash
        run: ./scripts/tests

  # Test publish using --dry-run.
  test-publish:
    name: Test Publish
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      # Install Rust
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Cargo publish dry run
        run: cargo publish --dry-run
