name: CI
on:
  push:
    branches: master
  pull_request:
jobs:
  tap_syntax:
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/homebrew/ubuntu22.04:master
    env:
      HOMEBREW_SIMULATE_MACOS_ON_LINUX: 1
    steps:
      - name: Set up Homebrew
        id: set-up-homebrew
        uses: Homebrew/actions/setup-homebrew@master

      - run: brew test-bot --only-tap-syntax
  rspec_tests:
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
    runs-on: ${{ matrix.os }}
    steps:
    - name: Set up Homebrew
      id: set-up-homebrew
      uses: Homebrew/actions/setup-homebrew@master

    - name: Set up Ruby
      uses: ruby/setup-ruby@3783f195e29b74ae398d7caca108814bbafde90e # v1.180.1
      with:
        bundler-cache: true

    - name: Run Homebrew/homebrew-services RSpec tests
      run: bundle exec rspec

  functional_tests:
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
    runs-on: ${{ matrix.os }}
    env:
      HOMEBREW_NO_INSTALL_FROM_API: 1
    steps:
    - name: Enable linger
      if: matrix.os == 'ubuntu-latest'
      run: |
        loginctl enable-linger "$(whoami)"
        echo "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus" >> "$GITHUB_ENV"

    - name: Set up Homebrew
      id: set-up-homebrew
      uses: Homebrew/actions/setup-homebrew@master

    - name: Set up Ruby
      uses: ruby/setup-ruby@3783f195e29b74ae398d7caca108814bbafde90e # v1.180.1
      with:
        bundler-cache: true

    - name: Unlink services repo
      run: |
        rm "$(brew --repo homebrew/services)"
        cp -a "$PWD" "$(brew --repo homebrew/services)"

    - name: Test start command
      run: |
        brew install consul
        brew services start consul
        brew services list | grep consul
        sleep 5
        consul kv get -recurse

    - name: Test restart command
      run: |
        brew services stop consul
        sleep 5
        brew services run consul
        brew services restart consul
        brew services list | grep consul
        sleep 5
        consul kv get -recurse

    - name: Test stop command
      run: |
        brew services stop consul
        sleep 5
        brew services list | grep consul | grep none

    - name: Test run command
      run: |
        brew services run consul
        sleep 5
        consul kv get -recurse
        brew services stop consul

    - name: Test list command
      run: |
        brew services | grep consul

    - name: Test info command
      run: |
        brew services info consul | grep consul
        brew services info consul --verbose | grep consul
        brew services info consul --json | ruby -e "require 'json'" -e "puts JSON.parse(ARGF.read)"

    - name: Link services repo
      run: |
        rm -rf "$(brew --repo homebrew/services)"
        ln -s "$PWD" "$(brew --repo homebrew/services)"
