---
name: Test

on:
  pull_request: {}
  push:
    branches: master

env:
  BUNDLE_WITHOUT: development:release

permissions:
  contents: read

jobs:
  rubocop_and_matrix:
    env:
      BUNDLE_WITHOUT: release
    runs-on: ubuntu-24.04
    outputs:
      ruby: ${{ steps.ruby.outputs.versions }}
    steps:
      - uses: actions/checkout@v6
      - name: Install Ruby ${{ matrix.ruby }}
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: "3.4"
          bundler-cache: true
      - name: Run Rubocop
        run: bundle exec rake rubocop
      - id: ruby
        uses: voxpupuli/ruby-version@v2

  test:
    runs-on: ubuntu-24.04
    needs: rubocop_and_matrix
    strategy:
      fail-fast: false
      matrix:
        ruby: ${{ fromJSON(needs.rubocop_and_matrix.outputs.ruby) }}
        puppet:
          - "~> 8.0"
          - "https://github.com/OpenVoxProject/puppet.git#main"
    name: "Ruby ${{ matrix.ruby }} - OpenVox ${{ matrix.puppet }}"
    env:
      PUPPET_VERSION: ${{ matrix.puppet }}
    steps:
      - uses: actions/checkout@v6
      - name: Install Ruby ${{ matrix.ruby }}
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{ matrix.ruby }}
          bundler-cache: true
      - name: Output Ruby environment
        run: bundle env
      - name: Run tests
        run: bundle exec rake
      - name: Build gem
        run: gem build --strict --verbose *.gemspec

  tests:
    if: always()
    needs:
      - rubocop_and_matrix
      - test
    runs-on: ubuntu-24.04
    name: Test suite
    steps:
      - name: Decide whether the needed jobs succeeded or failed
        uses: re-actors/alls-green@release/v1
        with:
          jobs: ${{ toJSON(needs) }}
