Skip to content

Instantly share code, notes, and snippets.

@DanBradbury
Last active February 12, 2016 08:13
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 DanBradbury/338096684ac16afcfcd6 to your computer and use it in GitHub Desktop.
Save DanBradbury/338096684ac16afcfcd6 to your computer and use it in GitHub Desktop.
Replacing simplecov
# add to the top of your spec_helper.rb
require 'yardcov'
YardCov.start
# require files
# ...
Rspec.configure do |config|
# ...
config.after(:suite) do
YardCov.report_results
end
end
module YardCov
class SourceFile
attr_accessor :content, :filename, :coverage
def initialize(path, coverage)
@filename = path
@coverage = coverage
File.open(path, "rb") { |f| @content = f.readlines }
end
def friendly_path
@filename.split("/").last
end
end
class << self
attr_accessor :pid, :source_files
def start
@pid = Process.pid
@source_files = []
Coverage.start
end
def report_results
puts "Coverage Results\n"+"="*100+"\n"
cov_results = Coverage.result
root = File.dirname(__FILE__)[0..-6]
filelist = Dir['./app/apis/*.rb'].map! { |f| root+f[1..-1] }
filelist.each do |file|
results = cov_results[file]
send_results(file, results)
end
print_results
end
private
# add to the filelist and print out the coverage stats for the file
def send_results(path, coverage)
@source_file = YardCov::SourceFile.new(path, coverage)
@source_files << @source_file
puts "Results for: #{path}"
results = coverage.compact.sort
total_lines = (results.length*1.00).to_f
first = results.find_index(1)
covered_lines = total_lines-first
percentage = (covered_lines/total_lines).round(2)*100
puts "#{percentage}% Covered (#{covered_lines} of #{total_lines} Lines Covered)"
end
# spits out a 'pretty' view of your coverage (minimal at best)
def print_results
out = File.open("results.html", "wb")
@source_files.each do |file|
out << "<h1>#{file.friendly_path}</h1>"
out << "<code>\n"
file.content.each_with_index do |line, index|
coverage = file.coverage
if coverage[index] == 0
out << "<span style='background-color:red'>#{line.gsub!("\s", "&nbsp;")}</span>"
elsif !coverage[index].nil?
out << "<span style='background-color:green'>#{line.gsub!("\s", "&nbsp;")}</span>"
else
out << "<span >#{line.gsub!("\s", "&nbsp;")}</span>"
end
out << "<br>"
end
out << "</code>"
out << "<hr>"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment