name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref_name }}-${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: true

env:
  CARGO_INCREMENTAL: 0
  CARGO_NET_RETRY: 10
  RUSTUP_MAX_RETRIES: 10

jobs:
  cargo-build:
    name: "cargo build"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly-2022-11-01
          override: true
      - uses: Swatinem/rust-cache@v1
      - run: cargo build --all
      - run: ./target/debug/ruff_dev generate-all
      - run: git diff --quiet README.md || echo "::error file=README.md::This file is outdated. Run 'cargo +nightly dev generate-all'."
      - run: git diff --quiet ruff.schema.json || echo "::error file=ruff.schema.json::This file is outdated. Run 'cargo +nightly dev generate-all'."
      - run: git diff --exit-code -- README.md ruff.schema.json

  cargo-fmt:
    name: "cargo fmt"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly-2022-11-01
          override: true
          components: rustfmt
      - run: cargo fmt --all --check

  cargo_clippy:
    name: "cargo clippy"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly-2022-11-01
          override: true
          components: clippy
          target: wasm32-unknown-unknown
      - uses: Swatinem/rust-cache@v1
      - run: cargo clippy --workspace --all-targets --all-features -- -D warnings -W clippy::pedantic
      - run: cargo clippy -p ruff --target wasm32-unknown-unknown --all-features -- -D warnings -W clippy::pedantic

  cargo-test:
    name: "cargo test"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly-2022-11-01
          override: true
      - uses: Swatinem/rust-cache@v1
      - run: cargo install cargo-insta
      - run: pip install black[d]==22.12.0
      - name: Run tests
        run: |
          cargo insta test --all --delete-unreferenced-snapshots
          git diff --exit-code
      - run: cargo test --package ruff_cli --test black_compatibility_test -- --ignored
      # Check for broken links in the documentation.
      # Setting RUSTDOCFLAGS because `cargo doc --check` isn't yet implemented (https://github.com/rust-lang/cargo/issues/10025).
      - run: RUSTDOCFLAGS="-D warnings" cargo doc --all --no-deps

# TODO(charlie): Re-enable the `wasm-pack` tests.
# See: https://github.com/charliermarsh/ruff/issues/1425
#  wasm-pack-test:
#    name: "wasm-pack test"
#    runs-on: ubuntu-latest
#    env:
#      WASM_BINDGEN_TEST_TIMEOUT: 60
#    steps:
#      - uses: actions/checkout@v3
#      - uses: actions-rs/toolchain@v1
#        with:
#          profile: minimal
#          toolchain: nightly-2022-11-01
#          override: true
#      - uses: actions/cache@v3
#        env:
#          cache-name: cache-cargo
#        with:
#          path: |
#            ~/.cargo/registry
#            ~/.cargo/git
#          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/Cargo.lock') }}
#          restore-keys: |
#            ${{ runner.os }}-build-${{ env.cache-name }}-
#            ${{ runner.os }}-build-
#            ${{ runner.os }}-
#      - uses: jetli/wasm-pack-action@v0.4.0
#      - uses: jetli/wasm-bindgen-action@v0.2.0
#      - run: wasm-pack test --node

  maturin-build:
    name: "maturin build"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly-2022-11-01
          override: true
      - uses: Swatinem/rust-cache@v1
      - uses: actions/setup-python@v4
        with:
          python-version: "3.11"
      - run: pip install maturin
      - run: maturin build -b bin

  typos:
    name: Spell Check with Typos
    runs-on: ubuntu-latest
    steps:
    - name: Checkout Actions Repository
      uses: actions/checkout@v2

    - name: Check spelling of file.txt
      uses: crate-ci/typos@master
      with:
        files: .
