name: Test & lint
on: [push, pull_request]

env:
  RAILS_ENV: test
  PGHOST: localhost
  PGUSER: postgres

jobs:
  tests:
    name: Test
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        ruby: ['2.7', '3.0', '3.1', '3.2', '3.3']
        gemfile: ['gemfiles/activerecord_5.2.2.gemfile', 'gemfiles/activerecord_6.0.0.gemfile', 'gemfiles/activerecord_6.1.0.gemfile', 'gemfiles/activerecord_7.0.0.gemfile', 'gemfiles/activerecord_7.1.0.gemfile', 'gemfiles/activerecord_main.gemfile']
        include:
          - gemfile: 'gemfiles/activerecord_7.1.0.gemfile'
            ruby: '3.3'
          - gemfile: 'gemfiles/activerecord_7.1.0.gemfile'
            ruby: '3.0'
          - gemfile: 'gemfiles/activerecord_7.0.0.gemfile'
            ruby: '3.3'
          - gemfile: 'gemfiles/activerecord_7.0.0.gemfile'
            ruby: '3.0'
          - gemfile: 'gemfiles/activerecord_6.1.0.gemfile'
            ruby: '3.3'
          - gemfile: 'gemfiles/activerecord_6.1.0.gemfile'
            ruby: '3.0'
        exclude:
          - gemfile: 'gemfiles/activerecord_5.2.2.gemfile'
            ruby: '3.0'
          - gemfile: 'gemfiles/activerecord_5.2.2.gemfile'
            ruby: '3.1'
          - gemfile: 'gemfiles/activerecord_5.2.2.gemfile'
            ruby: '3.2'
          - gemfile: 'gemfiles/activerecord_5.2.2.gemfile'
            ruby: '3.3'
          - gemfile: 'gemfiles/activerecord_7.0.0.gemfile'
            ruby: '2.7'
          - gemfile: 'gemfiles/activerecord_7.1.0.gemfile'
            ruby: '2.7'
          - gemfile: 'gemfiles/activerecord_main.gemfile'
            ruby: '2.7'
          - gemfile: 'gemfiles/activerecord_main.gemfile'
            ruby: '3.0'
    env:
      BUNDLE_GEMFILE: ${{ matrix.gemfile }}

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_USER: postgres
          POSTGRES_DB: cancan_postgresql_spec
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports: ["5432:5432"]

    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: '20'

      # https://github.com/CanCanCommunity/cancancan/pull/669#issuecomment-748019539
      - name: Nokogiri support for Truffleruby
        run: sudo apt-get -yqq install libxml2-dev libxslt-dev
        if: ${{ matrix.ruby == 'truffleruby' }}

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

      - name: Run tests on sqlite
        run: DB=sqlite bundle exec rspec

      - name: Run tests on postgres
        run: DB=postgres bundle exec rspec

  lint:
    name: Lint
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: '20'

      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 2.7
          bundler-cache: true

      - name: Run linter
        run: bundle exec rubocop
