name: Build & Test

on:
  workflow_dispatch:
  workflow_call:
    inputs:
      plan:
        required: false
        type: string
  # pull_request: # No need to trigger on PR, cargo-dist already does
  push:
    branches:
    - master

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  nextest:
    runs-on: ${{matrix.os}}
    strategy:
      matrix:
        build: [linux, macos]
        include:
        - build: linux
          os: ubuntu-latest
          target: x86_64-unknown-linux-musl
        - build: macos
          os: macos-latest
          target: x86_64-apple-darwin
    steps:
      - name: "[linux] Install dependencies"
        run: |
          sudo apt-get install tmux
          tmux -V
          locale
        if: runner.os == 'Linux'
      - name: "[macos] Install dependencies"
        run: |
          brew install tmux
          tmux -V
          locale
        if: runner.os == 'macOS'
        env:
          HOMEBREW_NO_AUTO_UPDATE: 1
      - name: Checkout repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 1
      - run: rustup toolchain install
      - uses: taiki-e/install-action@v2
        with:
          tool: nextest@0.9
      - name: Cache
        uses: Swatinem/rust-cache@v2
      - name: Run tests
        run: cargo nextest run --release --all-targets --features test-utils
        env:
          LC_ALL: en_US.UTF-8
          TERM: xterm-256color

  clippy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 1
      - run: rustup toolchain install
      - name: Cache
        uses: Swatinem/rust-cache@v2
      - name: Clippy
        run: cargo clippy

  rustfmt:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
      - run: rustup toolchain install
      - name: Check formatting
        run: |
          cargo fmt --all -- --check

  build-no-default-features:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - run: rustup toolchain install
      - name: Cache
        uses: Swatinem/rust-cache@v2
      - name: Build without any feature
        run: |
          cargo +stable build --no-default-features
      - name: Build with cli feature
        run: |
          cargo +stable build --no-default-features --features cli
      - name: Build with frizbee feature
        run: |
          cargo +nightly build --no-default-features --features nightly-frizbee
