name: Run Tests (postgreSQL)

on: [pull_request]

jobs:
  postgresql:
    runs-on: ubuntu-latest

    services:
      # Postgres installation
      db:
        image: postgres
        env:
          # Latest version of Postgres has increased security. We can use the default
          # user/password in this testing scenario though so use the following env
          # variable to bypass this changes:
          # https://github.com/docker-library/postgres/issues/681
          POSTGRES_HOST_AUTH_METHOD: trust
        ports: ['5432:5432']
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    env:
      RAILS_ENV: test
      DATABASE_URL: postgres://postgres:@localhost:5432/roadmap_test

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

    # Install the necessary Postgres dev packages
    - name: 'Install Postgresql Packages'
      run: |
        sudo apt-get update
        sudo apt-get install libpq-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 mysql 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 }}-

    # Figure out where wkhtmltopdf is installed
    - name: 'Determine wkhtmltopdf location'
      run: echo ::set-env name=WICKED_PDF_PATH::$(echo `bundle exec which wkhtmltopdf`)

    # 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
        bundle exec rake assets:precompile

    # 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/
