name: CI
on: [push, pull_request]

env:
  CARGO_INCREMENTAL: false
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: full
  MSRV: 1.65

jobs:
  check-msrv:
    name: Check MSRV and minimal-versions
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Install MSRV toolchain
        run: rustup toolchain install $MSRV --no-self-update --profile=minimal --component clippy

      - name: Install nightly toolchain
        run: rustup toolchain install nightly --no-self-update --profile=minimal

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

      # -Z avoid-dev-deps doesn't work
      - run: cargo +nightly hack generate-lockfile --remove-dev-deps -Z minimal-versions

      - name: Test all features
        run: cargo +$MSRV clippy --all-features --workspace -- -D warnings

  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Install cargo-nextest and cargo-llvm-cov
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-nextest,cargo-llvm-cov

      - name: Default test
        # Our intention here is to test `naga` with no features enabled. But
        # since `cli` is the default package, a plain `cargo test` will build
        # `naga` with the features requested in `cli/Cargo.toml`. Passing
        # `--package naga` causes us to use the default features in the
        # top-level `Cargo.toml` instead.
        run: cargo nextest run --package naga

      - name: Test all features
        run: cargo llvm-cov --lcov --output-path lcov.info nextest --all-features --workspace

      - name: Upload coverage report to codecov
        uses: codecov/codecov-action@v3
        with:
          files: lcov.info

      - name: Check snapshots
        run: git diff --exit-code -- tests/out

  check:
    name: Check benchmarks and naga-fuzz
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Check benchmarks
        run: cargo check --benches

      - name: Check naga-fuzz
        run: |
          cd fuzz
          cargo check

  documentation:
    name: Documentation
    runs-on: ubuntu-latest
    env:
      RUSTDOCFLAGS: -Dwarnings
    steps:
      - uses: actions/checkout@v3

      - run: cargo doc -p naga --all-features --document-private-items

  fmt:
    name: Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - run: cargo fmt -- --check
