Skip to content

Commit

Permalink
Fix SimpleCov config for guard MiniTest
Browse files Browse the repository at this point in the history
The code coverage analysis tool did not correctly report test
coverage results if executed via guard-minitest.

Externalizing the config into `.simplecov` is considered best practice:
simplecov-ruby/simplecov#235 (comment)
The `.simplecov` config is optimized for Cloud Stove based on the Rails
defaults from: https://github.com/colszowka/simplecov/blob/master/lib/simplecov/defaults.rb

Positive side effect: Adding Spring support for MiniTest accelerates
repeated test executions through reloading: https://github.com/guard/guard-minitest#spring

The new Rake task `rake test:coverage` also triggers coverage analysis.

SimpleCov is very fast (i.e., a couple of seconds for ~10 min Rails test suite)
and thus they enable coverage analysis by default. For conditional execution, see:
https://github.com/colszowka/simplecov#running-coverage-only-on-demand
  • Loading branch information
joe4dev committed Mar 18, 2016
1 parent 48f112a commit f34c95c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
36 changes: 36 additions & 0 deletions .simplecov
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# https://github.com/colszowka/simplecov#using-simplecov-for-centralized-config
# Maybe put some conditional here not to execute the code below unless ENV['COVERAGE'] == 'true'
SimpleCov.start do
# see https://github.com/colszowka/simplecov/blob/master/lib/simplecov/defaults.rb
load_profile 'test_frameworks'
coverage_dir 'coverage'
command_name 'MiniTest'
merge_timeout 3600 # 1 hour
track_files "{app,lib}/**/*.rb"

# Groups
add_group 'Controllers', 'app/controllers'
add_group 'Models', 'app/models'
# add_group 'Mailers', 'app/mailers'
add_group 'Helpers', 'app/helpers'
add_group 'Jobs', %w(app/jobs app/workers)
# add_group 'Libraries', 'lib'

add_group 'Provider updater', 'app/provider_updater'
add_group 'Long files' do |src_file|
src_file.lines.count > 100
end
class MaxLinesFilter < SimpleCov::Filter
def matches?(source_file)
source_file.lines.count < filter_argument
end
end
add_group 'Short files', MaxLinesFilter.new(5)

# Exclude these paths from analysis
add_filter '/config/'
add_filter '/db/'
add_filter 'lib/plugins'
add_filter 'vendor'
add_filter 'bundle'
end
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ group :development, :test do
gem 'webmock'

gem 'minitest-reporters'
gem 'simplecov', :require => false
gem 'simplecov', require: false

# Acceptance test framework for web applications: https://github.com/jnicklas/capybara
gem 'capybara'
Expand Down
2 changes: 1 addition & 1 deletion Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ guard :bundler do
files.each { |file| watch(helper.real_path(file)) }
end

guard :minitest do
guard :minitest, spring: true do
# with Minitest::Unit
watch(%r{^test/(.*)\/?test_(.*)\.rb$})
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
Expand Down
7 changes: 7 additions & 0 deletions lib/tasks/simplecov.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Usage: `rake test:coverage`
namespace :test do
task :coverage do
require 'simplecov'
Rake::Task['test'].execute
end
end
1 change: 0 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'simplecov'
SimpleCov.start 'rails'

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
Expand Down

0 comments on commit f34c95c

Please sign in to comment.