name: CI

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

env:
  BUNDLE_JOBS: 4

# jobs defined in the order we want them listed in the Actions UI
jobs:
  coverage:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        ruby: [2.7]
        os: [ubuntu-22.04]
    env:
      COVERALLS_SERVICE_NAME: github
      COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      COVERALLS_DEBUG: true
      CI_BUILD_NUMBER: ${{ github.run_id }}
    steps:
      - uses: actions/checkout@v4

      - name: Setup ruby
        uses: ruby/setup-ruby@v1
        with:
          bundler-cache: false
          ruby-version: ${{ matrix.ruby }}

      - name: Install gems
        run: bundle install

      - name: Run specs and report coverage
        run: bundle exec rake

  test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        # the job name is composed by these attributes
        ruby:
          - 2.3
          - 2.4
          - 2.5
          - 2.6
          - 2.7
          # quotes because of YAML gotcha: https://github.com/actions/runner/issues/849
          - '3.0'
          - 3.1
          - 3.2
          - 3.3
          - 3.4
          - jruby-9.1
          - jruby-9.2
          - jruby-9.3
          - jruby-9.4
          - truffleruby-22.2
          - truffleruby-22.3
          - truffleruby-24.1
        os:
          - ubuntu-22.04
        gemfile:
          - Gemfile
        include:
          # Ubuntu
          - { os: ubuntu-22.04, ruby: 3.4 }
          # macOS
          - { os: macos-13,     ruby: 3.4 }
          # Windows
          - { os: windows-2019, ruby: 3.4 }
          - { os: windows-2022, ruby: 3.4 }
          - { os: windows-2022, ruby: jruby-9.4 }
          # allowed to fail
          - { os: ubuntu-22.04, ruby: head, gemfile: Gemfile, allow-failure: true }
          - { os: ubuntu-22.04, ruby: jruby-head, gemfile: Gemfile, allow-failure: true }
          - { os: ubuntu-22.04, ruby: truffleruby-head, gemfile: Gemfile, allow-failure: true }
    env:
      BUNDLE_GEMFILE: ${{ matrix.gemfile }}
      # Workaround for Windows JRuby JDK issue
      # https://github.com/ruby/setup-ruby/issues/339
      # https://github.com/jruby/jruby/issues/7182#issuecomment-1112953015
      JAVA_OPTS: -Djdk.io.File.enableADS=true
    steps:
      - uses: actions/checkout@v4

      - name: Install libidn (Ubuntu)
        if: startsWith(matrix.os, 'ubuntu')
        run: sudo apt-get install libidn11-dev

      - name: Install libidn (macOS)
        if: startsWith(matrix.os, 'macos')
        run: brew install libidn

      - name: Setup ruby
        continue-on-error: ${{ matrix.allow-failure || false }}
        id: setupruby
        uses: ruby/setup-ruby@v1
        with:
          bundler-cache: false
          ruby-version: ${{ matrix.ruby }}

      - name: Install gems
        continue-on-error: ${{ matrix.allow-failure || false }}
        id: bundle
        run: bundle install

      - name: Run specs
        continue-on-error: ${{ matrix.allow-failure || false }}
        id: specs
        run: bundle exec rake spec

      # because continue-on-error marks the steps as pass if they fail
      - name: >-
          Setup ruby outcome: ${{ steps.setupruby.outcome }}
        run: echo NOOP
      - name: >-
          Install gems outcome: ${{ steps.bundle.outcome }}
        run: echo NOOP
      - name: >-
          Run specs outcome: ${{ steps.specs.outcome }}
        run: echo NOOP
