|
| 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 |
0 commit comments