name: Ruby Tests

on:
  workflow_call:
    inputs:
      safe-checkout:
        required: true
        description: "The SHA key for the commit we want to run over"
        type: string

permissions:
  contents: read

jobs:
  linux:
    strategy:
      fail-fast: false
      matrix:
        include:
          # Test both FFI and Native implementations on the highest and lowest
          # Ruby versions for CRuby and JRuby, but only on Bazel 5.x.
          - { name: Ruby 2.7, ruby: ruby-2.7.0, ffi: NATIVE }
          # TODO Re-enable these once flakes are fixed
          #- { name: Ruby 2.7, ruby: ruby-2.7.0, ffi: FFI }
          - { name: Ruby 3.0, ruby: ruby-3.0.2 }
          - { name: Ruby 3.1, ruby: ruby-3.1.0 }
          - { name: Ruby 3.2, ruby: ruby-3.2.0, ffi: NATIVE }
          # TODO Re-enable these once flakes are fixed
          #- { name: Ruby 3.2, ruby: ruby-3.2.0, ffi: FFI }
          - { name: JRuby 9.4, ruby: jruby-9.4.3.0, ffi: NATIVE }
          - { name: JRuby 9.4, ruby: jruby-9.4.3.0, ffi: FFI }

    name: Linux ${{ matrix.name }}${{ matrix.ffi == 'FFI' && ' FFI' || '' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout pending changes
        uses: protocolbuffers/protobuf-ci/checkout@v2
        with:
          ref: ${{ inputs.safe-checkout }}
      - name: Run tests
        uses: protocolbuffers/protobuf-ci/bazel-docker@v2
        with:
          image: ${{ matrix.image || format('us-docker.pkg.dev/protobuf-build/containers/test/linux/ruby:{0}-6.3.0-66964dc8b07b6d1fc73a5cc14e59e84c1c534cea', matrix.ruby) }}
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          bazel-cache: ruby_linux/${{ matrix.ruby }}_${{ matrix.bazel }}
          bazel: test //ruby/... //ruby/tests:ruby_version --test_env=KOKORO_RUBY_VERSION --test_env=BAZEL=true ${{ matrix.ffi == 'FFI' && '--//ruby:ffi=enabled --test_env=PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI' || '' }}

  linux-32bit:
    name: Linux 32-bit
    runs-on: ubuntu-latest
    steps:
      - name: Checkout pending changes
        uses: protocolbuffers/protobuf-ci/checkout@v2
        with:
          ref: ${{ inputs.safe-checkout }}

      - name: Cross compile protoc for i386
        id: cross-compile
        uses: protocolbuffers/protobuf-ci/cross-compile-protoc@v2
        with:
          image: us-docker.pkg.dev/protobuf-build/containers/common/linux/bazel:6.3.0-91a0ac83e968068672bc6001a4d474cfd9a50f1d
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          architecture: linux-i386

      - name: Run tests
        uses: protocolbuffers/protobuf-ci/docker@v2
        with:
          image: i386/ruby:2.7.3-buster
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          command: >-
            /bin/bash -cex '
            gem install bundler;
            cd /workspace/ruby;
            bundle;
            PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }} rake;
            rake clobber_package gem;
            PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }} rake test'

  linux-aarch64:
    name: Linux aarch64
    runs-on: ubuntu-latest
    steps:
      - name: Checkout pending changes
        uses: protocolbuffers/protobuf-ci/checkout@v2
        with:
          ref: ${{ inputs.safe-checkout }}

      - name: Cross compile protoc for aarch64
        id: cross-compile
        uses: protocolbuffers/protobuf-ci/cross-compile-protoc@v2
        with:
          image: us-docker.pkg.dev/protobuf-build/containers/common/linux/bazel:6.3.0-91a0ac83e968068672bc6001a4d474cfd9a50f1d
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          architecture: linux-aarch64

      - name: Run tests
        uses: protocolbuffers/protobuf-ci/docker@v2
        with:
          image: arm64v8/ruby:2.7.3-buster
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          command: >-
            /bin/bash -cex '
            gem install bundler;
            cd /workspace/ruby;
            bundle;
            PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }} rake;
            rake clobber_package gem;
            PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }} rake test'

  macos:
    strategy:
      fail-fast: false   # Don't cancel all jobs if one fails.
      matrix:
        include:
        # Test both FFI and Native implementations on the highest and lowest
        # Ruby versions for CRuby, but only on Bazel 5.x.
        # Quote versions numbers otherwise 3.0 will render as 3
        - { version: "2.7", ffi: NATIVE }
        # TODO Re-enable these once flakes are fixed
        #- { version: "2.7", ffi: FFI }
        - { version: "3.0" }
        - { version: "3.1" }
        - { version: "3.2", ffi: NATIVE }
        # TODO Re-enable these once flakes are fixed
        #- { version: "3.2", ffi: FFI }

    name: MacOS Ruby ${{ matrix.version }}${{ matrix.ffi == 'FFI' && ' FFI' || '' }}
    runs-on: macos-12
    steps:
      - name: Checkout pending changes
        uses: protocolbuffers/protobuf-ci/checkout@v2
        with:
          ref: ${{ inputs.safe-checkout }}

      - name: Pin Ruby version
        uses: ruby/setup-ruby@ee26e27437bde475b19a6bf8cb73c9fa658876a2 # v1.134.0
        with:
          ruby-version: ${{ matrix.version }}

      - name: Validate version
        run: ruby --version | grep ${{ matrix.version }} || (echo "Invalid Ruby version - $(ruby --version)" && exit 1)

      - name: Run tests
        uses: protocolbuffers/protobuf-ci/bazel@v2
        with:
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          bazel-cache: ruby_macos/${{ matrix.version }}
          bazel: test //ruby/... --test_env=KOKORO_RUBY_VERSION=${{ matrix.version }} --test_env=BAZEL=true ${{ matrix.ffi == 'FFI' && '--//ruby:ffi=enabled --test_env=PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI' || '' }}

  test_ruby_gems:
    strategy:
      fail-fast: false
      matrix:
        include:
          # Test both FFI and Native implementations on the highest and lowest
          # Ruby versions for CRuby and JRuby, but only on Bazel 5.x.
          - { name: Ruby 2.7, ruby: ruby-2.7.0, ffi: NATIVE }
          - { name: Ruby 2.7, ruby: ruby-2.7.0, ffi: FFI }
          - { name: Ruby 3.0, ruby: ruby-3.0.2}
          - { name: Ruby 3.1, ruby: ruby-3.1.0}
          - { name: Ruby 3.2, ruby: ruby-3.2.0, ffi: NATIVE }
          - { name: Ruby 3.2, ruby: ruby-3.2.0, ffi: FFI }
          - { name: JRuby 9.4, ruby: jruby-9.4.3.0, ffi: NATIVE }
          - { name: JRuby 9.4, ruby: jruby-9.4.3.0, ffi: FFI }
    name: Install ${{ matrix.name }}${{ matrix.ffi == 'FFI' && ' FFI' || '' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout pending changes
        uses: protocolbuffers/protobuf-ci/checkout@v2
        with:
          ref: ${{ inputs.safe-checkout }}
      - name: Run tests
        uses: protocolbuffers/protobuf-ci/bazel-docker@v2
        with:
          image: us-docker.pkg.dev/protobuf-build/containers/test/linux/ruby:${{ matrix.ruby }}-6.3.0-66964dc8b07b6d1fc73a5cc14e59e84c1c534cea
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          bazel-cache: ruby_install/${{ matrix.ruby }}_${{ matrix.bazel }}
          bash: >
            bazel --version;
            ruby --version;
            ./regenerate_stale_files.sh $BAZEL_FLAGS;
            bazel build //ruby:release //:protoc ${{ matrix.ffi == 'FFI' && '--//ruby:ffi=enabled' || '' }} $BAZEL_FLAGS;
            gem install bazel-bin/ruby/google-protobuf-*;
            bazel-bin/protoc --proto_path=src --proto_path=ruby/tests --proto_path=ruby --ruby_out=ruby tests/test_import_proto2.proto;
            bazel-bin/protoc --proto_path=src --proto_path=ruby/tests --proto_path=ruby --ruby_out=ruby tests/basic_test.proto;
            ${{ matrix.ffi == 'FFI' && 'PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI' || '' }} ruby ruby/tests/basic.rb;
            ${{ matrix.ffi == 'FFI' && 'PROTOCOL_BUFFERS_RUBY_IMPLEMENTATION=FFI' || '' }} ruby ruby/tests/implementation.rb
