name: Build and Test

on: push

jobs:
  regression-check:
    strategy:
      matrix:
        # If we want to duplicate this job for different
        # Rust toolchains (e.g. nightly or 1.37.0), add them here.
        rust_toolchain: [stable]
        os: [ubuntu-latest, macos-latest]
    timeout-minutes: 30
    name: run regression test suite
    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          submodules: true
          fetch-depth: 2

      - name: install rust toolchain ${{ matrix.rust_toolchain }}
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: ${{ matrix.rust_toolchain }}
          override: true

      - name: Install Ubuntu postgres dependencies
        if: matrix.os == 'ubuntu-latest'
        run: |
          sudo apt update
          sudo apt install build-essential libreadline-dev zlib1g-dev flex bison libseccomp-dev

      - name: Install macOs postgres dependencies
        if: matrix.os == 'macos-latest'
        run: |
          brew install flex bison

      - name: Set pg revision for caching
        id: pg_ver
        run: echo ::set-output name=pg_rev::$(git rev-parse HEAD:vendor/postgres)

      - name: Cache postgres build
        id: cache_pg
        uses: actions/cache@v2
        with:
          path: |
            tmp_install/
          key: ${{ runner.os }}-pg-${{ steps.pg_ver.outputs.pg_rev }}

      - name: Build postgres
        if: steps.cache_pg.outputs.cache-hit != 'true'
        run: |
          make postgres

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

      # Use `env CARGO_INCREMENTAL=0` to mitigate https://github.com/rust-lang/rust/issues/91696 for rustc 1.57.0
      - name: Run cargo build
        run: |
          env CARGO_INCREMENTAL=0 cargo build --workspace --bins --examples --tests

      - name: Run cargo test
        run: |
          env CARGO_INCREMENTAL=0 cargo test -- --nocapture --test-threads=1
