name: Run RSpec
on:
  workflow_call:
    inputs:
      # This tells the build which library to run
      library:
        required: true
        type: string

      # This tells rspec-rails what branch to run in ci it is passed in to allow the @main
      # version of this workflow to be shared amongst branches
      rspec_version:
        required: true
        type: string

permissions:
  contents: read

env:
  DIFF_LCS_VERSION: "> 1.4.3"
  RSPEC_CI: true
  RSPEC_VERSION: ${{ inputs.rspec_version }}

jobs:
  rspec:
    name: Ruby ${{ matrix.ruby }} ${{ matrix.name_extra || '' }}
    runs-on: ${{ matrix.os || 'ubuntu-latest' }}
    strategy:
      matrix:
        ruby:
          - '3.4'
          - '3.3'
          - '3.2'
          - '3.1'
          - '3.0'
          - 2.7
          - 2.6
          - 2.5
          - 2.4
          - 2.3
          - 2.2
        env:
          -
            DIFF_LCS_VERSION: "> 1.6.0"
        include:
          - ruby: 2.7
            name_extra: "with diff-lcs 1.3"
            env:
              DIFF_LCS_VERSION: "~> 1.3.0"
          - ruby: 2.7
            name_extra: "with diff-lcs 1.4.3"
            env:
              DIFF_LCS_VERSION: "1.4.3"
          - ruby: 2.7
            name_extra: "with diff-lcs 1.4.x > 3"
            env:
              DIFF_LCS_VERSION: "~> 1.4.4"
      fail-fast: false

    continue-on-error: ${{ matrix.allow_failure || endsWith(matrix.ruby, 'head') }}
    defaults:
      run:
        working-directory: ${{ inputs.library }}
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{ matrix.ruby }}
      - run: ../script/update_rubygems_and_install_bundler
      - run: bundle install --standalone
      - run: bundle binstubs --all
      - run: ../script/run_rspec
      - run: ../script/run_rspec_one_by_one
      - run: ../script/run_cucumber

  legacy:
    name: Legacy Ruby Builds (${{ matrix.container.version }})
    runs-on: ubuntu-latest
    container:
      image: ${{ matrix.container.tag }}
      options: ${{ matrix.container.options || '--add-host github-complains-if-this-is-empty.com:127.0.0.1' }}
    strategy:
      fail-fast: false
      matrix:
        container:
          - version: "2.1.9"
            tag: ghcr.io/rspec/docker-ci:2.1.9
            post: git config --global --add safe.directory `pwd`
          - version: "2.0"
            tag: ghcr.io/rspec/docker-ci:2.0.0
          - version: "1.9.3"
            tag: ghcr.io/rspec/docker-ci:1.9.3
          - version: "1.9.2"
            tag: ghcr.io/rspec/docker-ci:1.9.2
            options: "--add-host rubygems.org:151.101.129.227 --add-host api.rubygems.org:151.101.129.227"
          - version: "1.8.7"
            tag: ghcr.io/rspec/docker-ci:1.8.7
            options: "--add-host rubygems.org:151.101.129.227 --add-host api.rubygems.org:151.101.129.227"
          - version: "REE"
            tag: ghcr.io/rspec/docker-ci:ree
            options: "--add-host rubygems.org:151.101.129.227 --add-host api.rubygems.org:151.101.129.227"
          - version: "JRuby 1.7"
            tag: ghcr.io/rspec/docker-ci:jruby-1.7
            env:
              JAVA_OPTS: '-client -XX:+TieredCompilation -XX:TieredStopAtLevel=1'
          - version: "JRuby 1.7 1.8 mode"
            tag: ghcr.io/rspec/docker-ci:jruby-1.7
            jruby_opts: '--dev --1.8'
            pre: gem uninstall jruby-openssl
            options: "--add-host rubygems.org:151.101.129.227 --add-host api.rubygems.org:151.101.129.227"
            env:
              JAVA_OPTS: '-client -XX:+TieredCompilation -XX:TieredStopAtLevel=1'
          - version: "JRuby 9.1.17.0"
            tag: ghcr.io/rspec/docker-ci:jruby-9.1.17.0
            options: "--add-host rubygems.org:151.101.129.227 --add-host api.rubygems.org:151.101.129.227"
            env:
              JAVA_OPTS: '-client -XX:+TieredCompilation -XX:TieredStopAtLevel=1'
    env:
      LEGACY_CI: true
      JRUBY_OPTS: ${{ matrix.container.jruby_opts || '--dev' }}
      NO_COVERAGE: true
    steps:
      - run: git config --global --add safe.directory $GITHUB_WORKSPACE
      - run: git init $GITHUB_WORKSPACE
      - run: git remote add origin https://github.com/rspec/rspec
      - run: git config --local gc.auto 0
      - run: git fetch --no-tags --prune --no-recurse-submodules --depth=1 origin +$GITHUB_SHA:$GITHUB_REF
      - run: git checkout --progress --force $GITHUB_REF
      - run: ${{ matrix.container.pre }}
      - run: script/legacy_setup.sh
      - run: ${{ matrix.container.post }}
      - run: bundle exec rspec
        working-directory: ${{ inputs.library }}
      - run: ../script/run_cucumber_legacy
        working-directory: ${{ inputs.library }}

  windows:
    name: Ruby ${{ matrix.ruby }} (Windows)
    runs-on: windows-latest
    strategy:
      matrix:
        ruby:
          - 2.7
          - 2.6
          - 2.5
          - 2.4
          - 2.3
          - 2.2
      fail-fast: false
    defaults:
      run:
        working-directory: ${{ inputs.library }}
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          bundler: '2.2.22'
          ruby-version: ${{ matrix.ruby }}
          bundler-cache: true
      - run: choco install ansicon
      - run: bundle exec rspec --backtrace
