name: CI

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

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  test:
    name: Test Suite
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        rust: [stable]
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust }}

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

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
        with:
          shared-key: "test-${{ matrix.os }}"

      - name: Run tests (includes proptest suite)
        run: cargo nextest run --all-features --verbose

      - name: Run doc tests
        run: cargo test --doc --verbose

  lint:
    name: Linting
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
        with:
          shared-key: "lint"

      - name: Check formatting
        run: cargo fmt --all -- --check

      - name: Run clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

  benchmark:
    name: Benchmarks
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
        with:
          shared-key: "bench"

      - name: Run benchmarks
        run: cargo bench --bench lint_bench -- --save-baseline pr

      - name: Checkout base branch
        run: git checkout ${{ github.base_ref }}

      - name: Run baseline benchmarks
        run: cargo bench --bench lint_bench -- --save-baseline base

      - name: Compare benchmarks
        run: |
          echo "## Benchmark Results" >> $GITHUB_STEP_SUMMARY
          echo "Comparison between base branch and PR:" >> $GITHUB_STEP_SUMMARY
          echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
          cargo bench --bench lint_bench -- --baseline base pr 2>&1 | tee -a $GITHUB_STEP_SUMMARY
          echo "\`\`\`" >> $GITHUB_STEP_SUMMARY

  coverage:
    name: Code Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview

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

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
        with:
          shared-key: "coverage"

      - name: Generate coverage
        run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v5
        with:
          files: ./lcov.info
          fail_ci_if_error: false
          token: ${{ secrets.CODECOV_TOKEN }}
          slug: 192d-Wing/mkdlint
