Skip to content

Instantly share code, notes, and snippets.

@cbankston
Last active July 21, 2017 19:50
Show Gist options
  • Save cbankston/6b2344ec233b9a9a22b7726bcfe517f4 to your computer and use it in GitHub Desktop.
Save cbankston/6b2344ec233b9a9a22b7726bcfe517f4 to your computer and use it in GitHub Desktop.
Ruby code coverage config
# contents of spec/support/coverage.rb
require 'simplecov'
require 'simplecov-rcov'
SimpleCov.formatters = [
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::RcovFormatter
]
SimpleCov.start 'rails'
# require all files to get more accurate code_coverage data
# https://github.com/colszowka/simplecov/issues/16#issuecomment-31076575
base_result = {}
Dir['**/*.rb'].each do |file|
absolute = File::expand_path(file)
lines = File.readlines(absolute, :encoding => 'UTF-8')
base_result[absolute] = lines.map do |l|
l.strip!
l.empty? || l =~ /^else$/ || l =~ /^end$/ || l[0] == '#' ? nil : 0
end
end
SimpleCov.at_exit do
original_result = SimpleCov::Result.new(Coverage.result).original_result
original_result.keys.each do |covered_file|
base_result.delete(covered_file)
end
merged = original_result.merge_resultset(base_result)
result = SimpleCov::Result.new(merged)
result.format!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment