name: CI

on:
  push:
    branches:
      - master
  pull_request: {}
  release:
    types:
      - created

jobs:
  test:
    name: Test on ${{ matrix.os }} with features ${{ matrix.features }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - macOS-latest
          - windows-latest
    runs-on: ${{ matrix.os }}

    steps:
    - uses: actions/checkout@v1

    - name: Install stable toolchain
      uses: actions-rs/toolchain@v1
      with:
        profile: minimal
        toolchain: stable
        override: true
        components: rustfmt, clippy
      if: matrix.os != 'windows-latest'

    - name: Install stable toolchain (windows)
      uses: actions-rs/toolchain@v1
      with:
        profile: minimal
        toolchain: stable
        target: x86_64-pc-windows-gnu
        override: true
      if: matrix.os == 'windows-latest'

    - name: Install `just` command runner
      uses: extractions/setup-just@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Setup (debian)
      run: |
        echo "Install system tools"
        sudo apt-get update -y
        sudo apt-get install -y curl xz-utils build-essential gcc-multilib valgrind
      if: matrix.os == 'ubuntu-latest'

    - name: Setup (windows)
      run: |
        $env:PATH = "C:\msys64\mingw64\bin;C:\msys64\usr\bin;$env:PATH"
        echo "::set-env name=PATH::${env:PATH}"
        echo "::set-env name=CARGO_BUILD_TARGET::x86_64-pc-windows-gnu"
      if: matrix.os == 'windows-latest'

    - name: Build (default features)
      env:
        FEATURES: ""
      run: |
        cargo clean
        just FEATURES="$FEATURES" build

    - name: Test (default features)
      env:
        FEATURES: ""
      run: |
        just FEATURES="$FEATURES" test

    - name: Build (--no-default-features)
      env:
        FEATURES: "--no-default-features"
      run: |
        cargo clean
        just FEATURES="$FEATURES" build

    - name: Test (--no-default-features)
      env:
        FEATURES: "--no-default-features"
      run: |
        just FEATURES="$FEATURES" test

    - name: Build (--all-features)
      env:
        FEATURES: "--all-features"
      run: |
        cargo clean
        just FEATURES="$FEATURES" build

    - name: Test (--all-features)
      env:
        FEATURES: "--all-features"
      run: |
        just FEATURES="$FEATURES" test

    - name: Lint
      run: just lint
      if: matrix.os == 'ubuntu-latest'

    - name: Check for leaks (Valgrind)
      run: just valgrind
      if: matrix.os == 'ubuntu-latest' && matrix.features == '--all-features'
