name: test

on:
  push:
    branches:
      - master
  pull_request:

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: full

jobs:
  test:
    name: Build & test
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
        rust:
          - stable
          - beta
          - nightly
          # Introduction of self: Arc<..>, needed for the iterator module
          - 1.36.0
          # Introduction of non_exhaustive, used at certain exfiltrators
          - 1.40.0

    runs-on: ${{ matrix.os }}

    steps:
      - name: checkout
        uses: actions/checkout@v2

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.rust }}
          default: true
          profile: minimal

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

      - name: Build & test
        env:
          RUST_VERSION: ${{ matrix.rust }}
          OS: ${{ matrix.os }}
        run: ./ci-check.sh

  rustfmt:
    name: Check formatting
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v2

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

      - run: cargo fmt --all -- --check

  links:
    name: Check documentation links
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v2

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          default: true

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

      - name: Install cargo-deadlinks
        uses: actions-rs/install@v0.1
        with:
          crate: cargo-deadlinks
          use-tool-cache: true

      - name: Check links
        run: |
          for package in $(cargo metadata --no-deps --format-version=1 | jq -r '.packages[] | .name'); do
            cargo rustdoc -p "$package" --all-features -- -D warnings
            dname=$(echo "$package" | tr '-' '_')
            cargo deadlinks --dir "target/doc/$dname" --check-http --ignore-fragments
          done

  ancient-registry:
    name: Check compilation of signal-hook-registry on 1.26.0
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
    runs-on: ${{ matrix.os }}
    steps:
      - name: checkout
        uses: actions/checkout@v2

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

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

      - name: Check compilation
        run: |
          rm Cargo.toml
          cd signal-hook-registry
          sed -i -e '/signal-hook =/d' Cargo.toml
          cargo check

  ancient:
    name: Check compilation on 1.31.0
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
    runs-on: ${{ matrix.os }}
    steps:
      - name: checkout
        uses: actions/checkout@v2

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

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

      - name: Check compilation
        run: |
          rm Cargo.lock
          cargo update
          cargo check --no-default-features


  clippy:
    name: Clippy lints
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

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

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

      - name: Run clippy linter
        run: cargo clippy --all --all-features --tests -- -D clippy::all -D warnings

  # There's bunch of platforms that have some weird quirks (or we don't know
  # that they don't). While fully compiling and testing on them is a bit of a
  # challenge, running cargo check on them should be easy enough and should
  # catch at least some problems (like different sizes of types).
  weird_platforms:
    name: Check weird platforms
    strategy:
      fail-fast: false
      matrix:
        target:
          - x86_64-unknown-linux-musl
          - x86_64-pc-solaris
          - x86_64-linux-android
          - aarch64-linux-android
          - arm-linux-androideabi
          - mips-unknown-linux-musl
          - x86_64-unknown-netbsd
          - x86_64-unknown-freebsd
        extra-args:
          - "--all --all-features"
        include:
          # - wasm32-wasi (not yet? Or not ever?)
          # - x86_64-unknown-redox (Is that platform even usable on stable?)
          - { target: x86_64-fuchsia, extra-args: "--all-features" }
          - { target: asmjs-unknown-emscripten, extra-args: "--all-features" }
          # Seems we have some trouble with properly running C compiler on these. Might as well be problem with cross-compilation setup, but who knows
          # So we avoid the exfiltrator part/signal-hook-sys :-(
          - { target: x86_64-apple-darwin, extra-args: "--features=iterator --all --exclude signal-hook-sys" }
          - { target: x86_64-apple-ios, extra-args: "--features=iterator --all --exclude signal-hook-sys" }

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

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          default: true
          target: ${{ matrix.target }}

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

      - name: Run the check
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: ${{ matrix.extra-args }} --tests --target=${{ matrix.target }}
          use-cross: true
