name: test

on:
  push:
    branches:
      - main
      - release/**
      - "*_dev"
  pull_request:
    branches:
      - main
      - release/**
  schedule:
    - cron: "0 3 * * 4"

env:
  CARGO_WS_VERSION: "0.2.44"

jobs:
  ## Run all default oriented feature sets across all platforms.
  platform-matrix:
    name: platform
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        #os: [ubuntu-latest, macos-latest, windows-latest]
        os: [ubuntu-latest, macos-latest]
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable

      - uses: extractions/setup-just@v2

      - name: cargo install cargo-workspaces
        uses: baptiste0928/cargo-install@v3
        with:
          crate: cargo-workspaces
          version: ${{ env.CARGO_WS_VERSION }}

      - name: just all-features
        run: just all-features

  ## Run all default oriented feature sets across all platforms.
  code-coverage:
    name: coverage
    needs: platform-matrix
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@nightly
        with:
          components: llvm-tools-preview

      - uses: extractions/setup-just@v2

      - name: cargo install cargo-llvm-cov
        uses: baptiste0928/cargo-install@v3
        with:
          crate: cargo-llvm-cov

      - name: just coverage
        run: just coverage

      - name: upload coverage
        uses: codecov/codecov-action@v4
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          verbose: true
          #files: target/llvm-cov-target/hickory-dns-coverage.json

  ## Work through all of the variations of the different features, only on linux to save concurrent resources
  exhaustive-features-matrix:
    name: exhaustive
    # Let's wait for the all-features cache
    needs: platform-matrix
    runs-on: ubuntu-latest
    strategy:
      matrix:
        #   feature: [default-features, no-default-features, dns-over-rustls, dns-over-https-rustls, dns-over-native-tls, dns-over-openssl, dnssec-openssl, dnssec-ring, mdns, async-std]
        feature:
          [
            default,
            no-default-features,
            dns-over-rustls,
            dns-over-https-rustls,
            dns-over-quic,
            dns-over-h3,
            dns-over-native-tls,
            dnssec-openssl,
            dnssec-ring,
            doc,
          ]
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable

      - uses: extractions/setup-just@v2

      - name: cargo install cargo-workspaces
        uses: baptiste0928/cargo-install@v3
        with:
          crate: cargo-workspaces
          version: ${{ env.CARGO_WS_VERSION }}

      - name: just
        run: just ${{ matrix.feature }}

  ## Check past and future versions
  ##   this enforces the minimum version of rust this project works with
  past-future-matrix:
    name: past-future
    # Let's wait for the all-features cache
    needs: platform-matrix
    runs-on: ubuntu-latest
    strategy:
      matrix:
        version: ["1.67.0", beta, nightly]
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.version }}

      - uses: extractions/setup-just@v2

      - name: cargo install cargo-workspaces
        uses: baptiste0928/cargo-install@v3
        with:
          crate: cargo-workspaces
          version: ${{ env.CARGO_WS_VERSION }}

      - name: just no-default-features
        run: just no-default-features

      - name: just build-bench
        if: matrix.version == 'nightly'
        run: just build-bench

  ## Execute the clippy checks
  cleanliness:
    name: cleanliness
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      # not using the cargo cache here, since this differs significantly
      - name: cargo-all cache
        uses: actions/cache@v4
        with:
          path: ~/.cargo
          key: ${{ runner.os }}-cargo-all-${{ hashFiles('**/Cargo.toml') }}-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-all-${{ hashFiles('**/Cargo.toml') }}
            ${{ runner.os }}-cargo-all
            ${{ runner.os }}-cargo

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

      - uses: extractions/setup-just@v2

      - name: cargo install cargo-workspaces
        uses: baptiste0928/cargo-install@v3
        with:
          crate: cargo-workspaces
          version: ${{ env.CARGO_WS_VERSION }}

      # Clippy
      - name: just clippy
        run: just clippy
      # Rustfmt
      - name: just fmt
        run: just fmt
      # Audit
      - name: cargo audit
        run: just audit

  # Build and run bind to test our compatibility with standard name servers
  compatibility:
    name: compatibility
    # wait for the cache from all-features
    needs: platform-matrix
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable

      - uses: extractions/setup-just@v2

      - name: target/bind cache
        uses: actions/cache@v4
        with:
          path: target/bind
          key: ${{ runner.os }}-bind-${{ hashFiles('**/justfile') }}
          restore-keys: |
            ${{ runner.os }}-bind-${{ hashFiles('**/justfile') }}
            ${{ runner.os }}-bind

      - name: just compatibility
        run: just compatibility
