Skip to content

Instantly share code, notes, and snippets.

@mmizutani
Forked from trev/config.yml
Created June 7, 2019 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmizutani/d1d8f7154f195f09ddf6f1a8867ae7d5 to your computer and use it in GitHub Desktop.
Save mmizutani/d1d8f7154f195f09ddf6f1a8867ae7d5 to your computer and use it in GitHub Desktop.
CircleCI 2.0 with parallelism & simplecov for Rails
# Ruby CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
defaults: &defaults
working_directory: ~/split_app
parallelism: 2
docker:
- image: circleci/ruby:2.5.0-node-browsers
environment:
PGHOST: localhost
PGUSER: split
RAILS_ENV: test
- image: circleci/postgres:9.6
environment:
POSTGRES_USER: split
POSTGRES_DB: split_test
POSTGRES_PASSWORD: ""
version: 2
jobs:
test:
<<: *defaults
steps:
- checkout
- save_cache:
key: v2-repo-{{ .Environment.CIRCLE_SHA1 }}
paths:
- ~/split_app
- restore_cache:
keys:
- gem-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
- gem-cache-{{ arch }}-{{ .Branch }}
- gem-cache
- run: bundle install --path vendor/bundle
- save_cache:
key: gem-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
paths:
- ~/split_app/vendor/bundle
- restore_cache:
keys:
- yarn-cache-{{ arch }}-{{ .Branch }}-{{ checksum "yarn.lock" }}
- yarn-cache-{{ arch }}-{{ .Branch }}
- yarn-cache
- run:
name: Yarn Install
command: yarn install --cache-folder ~/.cache/yarn
- save_cache:
key: yarn-cache-{{ arch }}-{{ .Branch }}-{{ checksum "yarn.lock" }}
paths:
- ~/split_app/.cache/yarn
- run: cp .env.test .env
- run: bundle exec rubocop
- run: bundle exec brakeman -z
- run: dockerize -wait tcp://localhost:5432 -timeout 1m
- run: bundle exec rake db:schema:load
- run:
name: Parallel Rspec
environment:
- RAILS_ENV: test
- RACK_ENV: test
command: |
mkdir -p /tmp/rspec
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
bundle exec rspec --profile 10 \
--format RspecJunitFormatter \
--out /tmp/rspec/rspec.xml \
--format progress \
-- \
$TEST_FILES
- store_test_results:
path: /tmp/rspec
- run:
name: Stash Coverage Results
command: |
mkdir coverage_results
cp -R coverage/.resultset.json coverage_results/.resultset-${CIRCLE_NODE_INDEX}.json
- persist_to_workspace:
root: .
paths:
- coverage_results
coverage:
<<: *defaults
steps:
- attach_workspace:
at: .
- restore_cache:
key: v2-repo-{{ .Environment.CIRCLE_SHA1 }}
- restore_cache:
keys:
- gem-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
- gem-cache-{{ arch }}-{{ .Branch }}
- gem-cache
- run: bundle install --path vendor/bundle
- run:
name: Merge and check coverage
command: |
RUN_COVERAGE=true bundle exec rake simplecov:report_coverage
- store_artifacts:
path: ~/split_app/coverage
destination: coverage
workflows:
version: 2
build_and_test:
jobs:
- test
- coverage:
requires:
- test
# frozen_string_literal: true
# spec/rails_helper.rb
require 'active_support/inflector'
require 'simplecov'
SimpleCov.start 'rails' do
add_filter '/spec/'
add_filter '/config/'
add_filter '/vendor/'
Dir['app/*'].each do |dir|
add_group File.basename(dir).humanize, dir
end
minimum_coverage 100 unless ENV['CIRCLE_JOB']
end
# ...
# frozen_string_literal: true
# spec/simplecov_helper.rb
require 'active_support/inflector'
require "simplecov"
class SimpleCovHelper
def self.report_coverage(base_dir: "./coverage_results")
SimpleCov.start 'rails' do
skip_check_coverage = ENV.fetch("SKIP_COVERAGE_CHECK", "false")
add_filter '/spec/'
add_filter '/config/'
add_filter '/vendor/'
Dir['app/*'].each do |dir|
add_group File.basename(dir).humanize, dir
end
minimum_coverage(100) unless skip_check_coverage
merge_timeout(3600)
end
new(base_dir: base_dir).merge_results
end
attr_reader :base_dir
def initialize(base_dir:)
@base_dir = base_dir
end
def all_results
Dir["#{base_dir}/.resultset*.json"]
end
def merge_results
results = all_results.map { |file| SimpleCov::Result.from_hash(JSON.parse(File.read(file))) }
SimpleCov::ResultMerger.merge_results(*results).tap do |result|
SimpleCov::ResultMerger.store_result(result)
end
end
end
# frozen_string_literal: true
# lib/tasks/simplecov_parallel.rake
if Rails.env.test?
require_relative "../../spec/simplecov_helper"
namespace :simplecov do
desc "merge_results"
task report_coverage: :environment do
SimpleCovHelper.report_coverage
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment