name: CI

on:
- push
- pull_request

env:
  JRUBY_OPTS: "-X+C" # temporarily force JRuby to compile, so Java exception trace will contain .rb lines

jobs:
  ruby-versions:
    uses: ruby/actions/.github/workflows/ruby_versions.yml@master
    with:
      min_version: 2.4

  host:
    needs: ruby-versions
    name: ${{ matrix.os }} ${{ matrix.ruby }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        ruby: ${{ fromJson(needs.ruby-versions.outputs.versions) }}
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
        include:
          - { os: windows-latest , ruby: mingw }
          - { os: windows-latest , ruby: mswin }
        exclude:
          - { os: macos-latest , ruby: 2.4 }
          - { os: macos-latest , ruby: 2.5 }
          - { os: windows-latest , ruby: head }
          - { os: ubuntu-latest , ruby: truffleruby }
          - { os: macos-latest , ruby: truffleruby }
          - { os: windows-latest , ruby: truffleruby }
          - { os: windows-latest , ruby: truffleruby-head }

    steps:
      - uses: actions/checkout@v6

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{ matrix.ruby }}
          bundler-cache: true

      - run: bundle exec rake compile

      # If strscan in Ruby's master has the same version of this strscan,
      # "gem install pkg/*.gem" fails.
      - run: bundle exec rake version:bump
        if: >-
          matrix.ruby == 'head' ||
          matrix.ruby == 'mingw' ||
          matrix.ruby == 'mswin'

      - run: bundle exec rake build

      - uses: actions/upload-artifact@v6
        if: >-
          matrix.os == 'ubuntu-latest' &&
            (matrix.ruby == '3.3' || matrix.ruby == 'jruby')
        with:
          name: gem-${{ matrix.os }}-${{ matrix.ruby }}
          path: pkg/

      - run: echo 'TESTOPTS=-v --ignore-name=/unscan/' >> $GITHUB_ENV
        if: ${{ startsWith(matrix.ruby, 'truffle') }}

      - run: bundle exec rake test

      - run: gem install --verbose --backtrace pkg/*.gem

      - run: gem install test-unit-ruby-core test-unit

      - name: Run tests on the installed gem
        run: ruby run-test.rb ${TESTOPTS}
        if: matrix.ruby != '2.4' # strscan is a default gem from 2.5
