name: brew doctor
on:
  pull_request:
    paths:
      - .github/workflows/doctor.yml
      - Library/Homebrew/cmd/doctor.rb
      - Library/Homebrew/diagnostic.rb
      - Library/Homebrew/extend/os/diagnostic.rb
      - Library/Homebrew/extend/os/mac/diagnostic.rb
      - Library/Homebrew/os/mac/xcode.rb

permissions:
  contents: read

env:
  HOMEBREW_DEVELOPER: 1
  HOMEBREW_NO_AUTO_UPDATE: 1

defaults:
  run:
    shell: bash -xeuo pipefail {0}

jobs:
  determine-runners:
    runs-on: ubuntu-latest
    outputs:
      runners: ${{ steps.determine-runners.outputs.runners }}
    steps:
      - name: Set up Homebrew
        id: set-up-homebrew
        uses: Homebrew/actions/setup-homebrew@main
        with:
          core: false
          cask: false
          test-bot: false

      - name: Determine runners to use for this job
        id: determine-runners
        env:
          HOMEBREW_MACOS_TIMEOUT: 30
        run: brew determine-test-runners --all-supported

  tests:
    needs: determine-runners
    strategy:
      matrix:
        include: ${{ fromJson(needs.determine-runners.outputs.runners) }}
      fail-fast: false
    name: ${{ matrix.name }}
    runs-on: ${{ matrix.runner }}
    timeout-minutes: ${{ matrix.timeout }}
    defaults:
      run:
        working-directory: /tmp
    steps:
      - name: Set up Homebrew
        id: set-up-homebrew
        uses: Homebrew/actions/setup-homebrew@main
        with:
          core: false
          cask: false
          test-bot: true

      - run: brew test-bot --only-cleanup-before
        if: matrix.cleanup

      - run: brew doctor

      - run: brew test-bot --only-cleanup-after
        if: always() && matrix.cleanup
