Skip to content

Commit 6799453

Browse files
committed
Merge pull request #1069 from bf4/coverage
Add test coverage; account for no artifacts on CI
2 parents 2375420 + c401722 commit 6799453

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

.simplecov

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# https://github.com/colszowka/simplecov#using-simplecov-for-centralized-config
2+
# see https://github.com/colszowka/simplecov/blob/master/lib/simplecov/defaults.rb
3+
# vim: set ft=ruby
4+
5+
## DEFINE VARIABLES
6+
@minimum_coverage = ENV.fetch('COVERAGE_MINIMUM') {
7+
case (defined?(RUBY_ENGINE) && RUBY_ENGINE) || "ruby"
8+
when 'jruby', 'rbx'
9+
96.0
10+
else
11+
98.3
12+
end
13+
}.to_f.round(2)
14+
# rubocop:disable Style/DoubleNegation
15+
ENV['FULL_BUILD'] ||= ENV['CI']
16+
@running_ci = !!(ENV['FULL_BUILD'] =~ /\Atrue\z/i)
17+
@generate_report = @running_ci || !!(ENV['COVERAGE'] =~ /\Atrue\z/i)
18+
@output = STDOUT
19+
# rubocop:enable Style/DoubleNegation
20+
21+
## CONFIGURE SIMPLECOV
22+
SimpleCov.pid = $$ # In case there's any forking
23+
24+
SimpleCov.profiles.define 'app' do
25+
coverage_dir 'coverage'
26+
load_profile 'test_frameworks'
27+
28+
add_group 'Libraries', 'lib'
29+
30+
add_group 'Long files' do |src_file|
31+
src_file.lines.count > 100
32+
end
33+
class MaxLinesFilter < SimpleCov::Filter
34+
def matches?(source_file)
35+
source_file.lines.count < filter_argument
36+
end
37+
end
38+
add_group 'Short files', MaxLinesFilter.new(5)
39+
40+
# Exclude these paths from analysis
41+
add_filter '/config/'
42+
add_filter '/db/'
43+
add_filter 'tasks'
44+
end
45+
46+
## START TRACKING COVERAGE (before activating SimpleCov)
47+
require 'coverage'
48+
Coverage.start
49+
50+
## ADD SOME CUSTOM REPORTING AT EXIT
51+
SimpleCov.at_exit do
52+
header = "#{'*' * 20} SimpleCov Results #{'*' * 20}"
53+
@output.puts
54+
@output.puts header
55+
@output.puts SimpleCov.result.format!
56+
percent = Float(SimpleCov.result.covered_percent)
57+
if percent < @minimum_coverage
58+
@output.puts "Spec coverage was not high enough: "\
59+
"#{percent.round(2)} is < #{@minimum_coverage}%\n"
60+
exit 1 if @generate_report
61+
else
62+
@output.puts "Nice job! Spec coverage (#{percent.round(2)}) "\
63+
"is still at or above #{@minimum_coverage}%\n"
64+
end
65+
@output.puts
66+
@output.puts '*' * header.size
67+
end
68+
69+
## CAPTURE CONFIG IN CLOSURE 'AppCoverage.start'
70+
## to defer running until test/test_helper.rb is loaded.
71+
# rubocop:disable Style/MultilineBlockChain
72+
AppCoverage = Class.new do
73+
def initialize(&block)
74+
@block = block
75+
end
76+
77+
def start
78+
@block.call
79+
end
80+
end.new do
81+
SimpleCov.start 'app'
82+
if @generate_report
83+
if @running_ci
84+
require 'codeclimate-test-reporter'
85+
@output.puts '[COVERAGE] Running with SimpleCov Simple Formatter and CodeClimate Test Reporter'
86+
formatters = [
87+
SimpleCov::Formatter::SimpleFormatter,
88+
CodeClimate::TestReporter::Formatter
89+
]
90+
else
91+
@output.puts '[COVERAGE] Running with SimpleCov HTML Formatter'
92+
formatters = [SimpleCov::Formatter::HTMLFormatter]
93+
end
94+
else
95+
formatters = []
96+
end
97+
SimpleCov.formatters = formatters
98+
end
99+
# rubocop:enable Style/MultilineBlockChain

Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ group :test do
2929
gem 'activerecord'
3030
gem 'sqlite3', platform: :ruby
3131
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
32+
gem 'codeclimate-test-reporter', require: false
33+
end
34+
35+
group :test, :development do
36+
gem 'simplecov', '~> 0.10', require: false
3237
end
3338

3439
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem

Rakefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
begin
2+
require 'simplecov'
3+
rescue LoadError
4+
end
5+
16
require 'bundler/gem_tasks'
27

38
begin

test/support/simplecov.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# https://github.com/colszowka/simplecov/pull/400
2+
# https://github.com/ruby/ruby/blob/trunk/lib/English.rb
3+
unless defined?(English)
4+
# The exception object passed to +raise+.
5+
alias $ERROR_INFO $! # rubocop:disable Style/SpecialGlobalVars
6+
end

test/test_helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
require 'bundler/setup'
22

3+
begin
4+
require 'simplecov'
5+
# HACK: till https://github.com/colszowka/simplecov/pull/400 is merged and released.
6+
# Otherwise you may get:
7+
# simplecov-0.10.0/lib/simplecov/defaults.rb:50: warning: global variable `$ERROR_INFO' not initialized
8+
require 'support/simplecov'
9+
AppCoverage.start
10+
rescue LoadError
11+
STDERR.puts 'Running without SimpleCov'
12+
end
13+
314
require 'timecop'
415
require 'rails'
516
require 'action_controller'

0 commit comments

Comments
 (0)