name: Run Tests (mySQL)

on: [pull_request]

jobs:
  mysql:
    runs-on: ubuntu-latest

    env:
      DB_ADAPTER: mysql2
      MYSQL_PWD: root
      RAILS_ENV: test
      WICKED_PDF_PATH: vendor/bundle/bin/wkhtmltopdf

    steps:
    # Checkout the repo
    - uses: actions/checkout@v1
      with:
        fetch-depth: 1

    # Install the necessary MySQL dev packages
    - name: 'Install Mysql Packages'
      run: |
        sudo apt-get update
        sudo apt-get install -y mysql-client libmysqlclient-dev

    # Extract the Ruby version from the Gemfile.lock
    - name: 'Determine Ruby Version'
      run: echo ::set-env name=RUBY_VERSION::$(echo `cat ./Gemfile.lock | grep -A 1 'RUBY VERSION' | grep 'ruby' | grep -oE '[0-9]\.[0-9]'`)

    # Install Ruby - using the version found in the Gemfile.lock
    - name: 'Install Ruby'
      uses: actions/setup-ruby@v1
      with:
        ruby-version: ${{ env.RUBY_VERSION }}

    # Copy all of the example configs over
    - name: 'Setup Default Configuration'
      run: |
        # Make copies of all the example config files
        cp config/branding.yml.sample config/branding.yml
        cp config/database.yml.sample config/database.yml
        cp config/secrets.yml.sample config/secrets.yml
        cp config/initializers/contact_us.rb.example config/initializers/contact_us.rb
        cp config/initializers/devise.rb.example config/initializers/devise.rb
        cp config/initializers/recaptcha.rb.example config/initializers/recaptcha.rb
        cp config/initializers/wicked_pdf.rb.example config/initializers/wicked_pdf.rb

    # Try to retrieve the gems from the cache
    - name: 'Cache Gems'
      uses: actions/cache@v1
      with:
        path: vendor/bundle
        key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }}
        restore-keys: |
          ${{ runner.os }}-gem-

    # Install bundler and run bundle install
    - name: 'Bundle Install'
      run: |
        gem install bundler -v 1.17.2
        bundle config path vendor/bundle
        bundle install --jobs 4 --retry 3 --without pgsql rollbar aws

    # Try to retrieve the yarn JS dependencies from the cache
    - name: 'Cache Yarn Packages'
      uses: actions/cache@v1
      with:
        path: node_modules/
        key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-yarn-
          ${{ runner.os }}-

    # Install the JS dependencies
    - name: 'Yarn Install'
      run: |
        yarn install

    # Setup the database
    - name: 'Setup Test DB'
      run: bundle exec rake db:setup RAILS_ENV=test

    # Compile the assets
    - name: 'Compile Assets'
      run: |
        bundle exec rake webpacker:compile RAILS_ENV=test
        bundle exec rake assets:precompile RAILS_ENV=test

    # Run the JS tests
    - name: 'Run Karma Tests'
      run: |
        yarn add karma
        yarn run test

    # Run the Rspec tests
    - name: 'Run Rspec Tests'
      run: bundle exec rspec spec/
