---
name: Cargo build and test
on:
  pull_request:
    paths:
      - .github/workflows/*.yml
      - '**/*.rs'
      - Cargo.toml
      - Cargo.lock
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: "--deny warnings "
jobs:
  build-and-test:
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        rust: [stable, beta]
        include:
          - os: ubuntu-latest
            rust: nightly
          - os: ubuntu-latest
            rust: 1.65.0
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - uses: actions-rs/toolchain@v1.0.6
        with:
          toolchain: ${{ matrix.rust }}
          default: true

      - name: Install cargo-hack
        uses: taiki-e/install-action@cargo-hack

      - name: Build
        shell: bash
        run: cargo build --locked

      # Run through all tests with all combinations of features
      - name: Test
        shell: bash
        run: cargo hack --feature-powerset --exclude-all-features test

      # Run `cargo test` but with artificial delay injected into some code paths. This helps
      # running through some hard-to-time code paths. loom testing is not included since it is not
      # compatible nor make any sense together with sleeping.
      - name: Test with artificial delay
        shell: bash
        run: RUSTFLAGS+="--cfg oneshot_test_delay" cargo hack --feature-powerset --exclude-all-features test

      # Compile the library against loom to do correctness testing.
      # `--features loom` must be given so that only feature powerset combinations including loom are tested.
      - name: Test with loom
        shell: bash
        run: |
          RUSTFLAGS+="--cfg oneshot_loom" LOOM_MAX_BRANCHES=100000 \
            cargo hack --feature-powerset --exclude-all-features --features loom test

      # Check that the documentation builds and has no warnings
      - name: Build documentation
        shell: bash
        run: RUSTDOCFLAGS="--deny warnings" cargo hack --feature-powerset --exclude-all-features doc
