redditraffler/redditraffler

View on GitHub
.github/workflows/run_tests.yml

Summary

Maintainability
Test Coverage
name: Run Tests

env:
  PYTHON_VERSION: 3.6 # Match whatever's running in Heroku
  NODE_VERSION: 12.18 # Match whatever's running in Heroku

on:
  push:
    branches:
      - staging
  pull_request:
    # Intentionally blank; no config

jobs:
  lint-and-test-backend:
    name: Test Python app
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:12.3-alpine
        ports:
          - 5432:5432
        env:
          POSTGRES_USER: ci_test
          POSTGRES_PASSWORD: ci
          POSTGRES_DB: redditraffler_ci_test
    env:
      PIPENV_VENV_IN_PROJECT: true
      PIP_CACHE_DIR: /tmp/pip_cache
      PIPENV_CACHE_DIR: /tmp/pipenv_cache
      TEST_DATABASE_URL: postgres://ci_test:ci@localhost:5432/redditraffler_ci_test
    steps:
      - uses: actions/checkout@v2

      - uses: actions/setup-python@v2
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Install Pipenv dependencies
        run: |
          pip install pipenv
          pipenv install --dev --deploy

      - name: Run linter
        run: pipenv run lint

      - name: Install Code Climate test reporter
        run: |
          curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
          chmod +x ./cc-test-reporter

      - name: Run tests
        run: |
          ./cc-test-reporter before-build
          pipenv run coverage run -m pytest -vv tests
          pipenv run coverage xml
          ./cc-test-reporter format-coverage --output /tmp/codeclimate_backend_coverage.json

      - name: Store Backend Coverage Results
        uses: actions/upload-artifact@v2
        with:
          name: codeclimate_backend_coverage
          path: /tmp/codeclimate_backend_coverage.json

  lint-and-test-frontend:
    name: Test JavaScript components
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - uses: actions/setup-node@v1
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: Check node_modules cache
        uses: actions/cache@v2
        with:
          path: ./node_modules
          key: node-deps-${{ hashFiles('yarn.lock', 'package.json') }}

      - name: Install Yarn dependencies
        run: yarn install --dev

      - name: Run linter
        run: yarn lint

      - name: Install Code Climate test reporter
        run: |
          curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
          chmod +x ./cc-test-reporter

      - name: Run tests
        run: |
          ./cc-test-reporter before-build
          yarn test --coverage
          ./cc-test-reporter format-coverage --output /tmp/codeclimate_frontend_coverage.json

      - name: Store Frontend Coverage Results
        uses: actions/upload-artifact@v2
        with:
          name: codeclimate_frontend_coverage
          path: /tmp/codeclimate_frontend_coverage.json

  build-frontend:
    name: Test Webpack build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - uses: actions/setup-node@v1
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: Check node_modules cache
        uses: actions/cache@v2
        with:
          path: ./node_modules
          key: node-deps-${{ hashFiles('yarn.lock', 'package.json') }}

      - name: Install Yarn dependencies
        run: yarn install --dev

      - name: Build project
        run: yarn build

  aggregate-and-report-coverage:
    name: Upload coverage reports
    runs-on: ubuntu-latest
    needs: [lint-and-test-backend, lint-and-test-frontend]
    env:
      # NOTE 2021-04-24 Dependabot cannot access secrets due to a recent change, so we hardcode the reporter ID.
      # https://github.community/t/dependabot-doesnt-see-github-actions-secrets/167104
      # There should be no security issue with exposing the ID here: https://docs.codeclimate.com/docs/finding-your-test-coverage-token#should-i-keep-my-test-reporter-id-secret
      CC_TEST_REPORTER_ID: ff76aa89df1bdfcc6bb22ff067a277ab631af944d2540aa4b716a43651a54119
    steps:
      - name: Download Coverage Result Artifacts
        uses: actions/download-artifact@v2
        with:
          path: /tmp

      - name: Install Code Climate test reporter
        run: |
          curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
          chmod +x ./cc-test-reporter

      - name: Sum coverage
        run: ./cc-test-reporter sum-coverage --parts 2 /tmp/codeclimate_backend_coverage/codeclimate_backend_coverage.json /tmp/codeclimate_frontend_coverage/codeclimate_frontend_coverage.json

      - name: Upload coverage
        run: ./cc-test-reporter upload-coverage --id $CC_TEST_REPORTER_ID