defaults: &defaults
  working_directory: ~/ruby-dns-mock
  docker:
    - image: circleci/ruby:2.5.0-node
  environment:
    CC_TEST_REPORTER_ID: 18fba9e7d6885c48453a80a0f019a60d9ffa3fd80467652a6b3c95fef5ea9a50

references:
  restore_bundle_cache: &restore_bundle_cache
    restore_cache:
      keys:
        - ruby-dns-mock-{{ checksum "dns_mock.gemspec" }}

  bundle_install: &bundle_install
    run:
      name: Installing gems
      command: bundle install --path vendor/bundle

  save_bundle_cache: &save_bundle_cache
    save_cache:
      key: ruby-dns-mock-{{ checksum "dns_mock.gemspec" }}
      paths:
        - vendor/bundle

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

version: 2
jobs:
  linters:
    <<: *defaults

    steps:
      - checkout

      - <<: *restore_bundle_cache
      - <<: *bundle_install
      - <<: *save_bundle_cache

      - run:
          name: Running overcommit
          command: |
            bundle exec overcommit -s
            SKIP=AuthorEmail,AuthorName bundle exec overcommit -r

  tests:
    <<: *defaults

    steps:
      - checkout

      - <<: *restore_bundle_cache
      - <<: *bundle_install
      - <<: *save_bundle_cache
      - <<: *install_codeclimate_reporter

      - run:
          name: Running tests
          command: |
            mkdir /tmp/test-results
            ./cc-test-reporter before-build
            bundle exec rspec

      - run:
          name: Code Climate Test Coverage
          command: |
            ./cc-test-reporter format-coverage -t simplecov -o "coverage/codeclimate.$CIRCLE_NODE_INDEX.json"

      - store_test_results:
          path: /tmp/test-results

      - store_artifacts:
          path: /tmp/test-results
          destination: test-results

      - deploy:
          command: |
            ./cc-test-reporter sum-coverage --output - --parts $CIRCLE_NODE_TOTAL coverage/codeclimate.*.json | ./cc-test-reporter upload-coverage --debug --input -

workflows:
  version: 2
  build:
    jobs:
      - linters
      - tests