version: 2.1

jobs:
  build:
    parameters:
      docker-image:
        type: string
      gemfile:
        type: string
    docker:
      - image: << parameters.docker-image >>
    environment:
      MOCHA_OPTIONS=debug
    steps:
      - checkout
      - run: ruby --version
      - when:
          condition:
            equal: [ "jruby:latest", << parameters.docker-image >>]
          steps:
            - run: apt-get update
            - run: apt-get install -y git
      - run: gem --version
      - run: bundle --version
      - run: bundle install --gemfile=<< parameters.gemfile >>
      - when:
          condition:
            equal: [ "Gemfile", << parameters.gemfile >>]
          steps:
            - run: bundle exec rake test
      - when:
          condition:
            matches:
              pattern: ".*minitest.*"
              value: << parameters.gemfile >>
          steps:
            - run: MOCHA_RUN_INTEGRATION_TESTS=minitest bundle exec --gemfile=<< parameters.gemfile >> rake test
      - when:
          condition:
            matches:
              pattern: ".*test-unit.*"
              value: << parameters.gemfile >>
          steps:
            - run: MOCHA_RUN_INTEGRATION_TESTS=test-unit bundle exec --gemfile=<< parameters.gemfile >> rake test
      - when:
          condition:
            and:
              - equal: [ "ruby:latest", << parameters.docker-image >> ]
              - equal: [ "Gemfile", << parameters.gemfile >>]
          steps:
            - run: bundle exec rake test:performance
            - run: MOCHA_GENERATE_DOCS=1 bundle install --gemfile=<< parameters.gemfile >>
            - run: MOCHA_GENERATE_DOCS=1 rake yardoc
  lint:
    docker:
      - image: ruby:2.6
    steps:
      - checkout
      - run: ruby --version
      - run: gem --version
      - run: bundle --version
      - run: bundle install --gemfile=Gemfile
      - run: bundle exec rake lint

workflows:
  build-all:
    jobs: &all-jobs
      - lint
      - build:
          matrix:
            parameters:
              docker-image:
                - ruby:2.1
                - ruby:2.2
                - ruby:2.3
                - ruby:2.4
                - ruby:2.5
                - ruby:2.6
                - ruby:2.7
                - ruby:3.0
                - ruby:3.1
                - ruby:3.2
                - ruby:3.3
                - ruby:3.4.0-preview2
                - ruby:latest
                - jruby:latest
              gemfile:
                - Gemfile
      - build:
          matrix:
            parameters:
              docker-image:
                - ruby:latest
              gemfile:
                - gemfiles/Gemfile.minitest.latest
                - gemfiles/Gemfile.test-unit.latest
  weekly:
    triggers:
      - schedule:
          cron: "0 3 * * 0"
          filters:
            branches:
              only:
                - main
    jobs: *all-jobs
