Last active
February 12, 2016 08:13
-
-
Save DanBradbury/338096684ac16afcfcd6 to your computer and use it in GitHub Desktop.
Replacing simplecov
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", " ")}</span>" | |
elsif !coverage[index].nil? | |
out << "<span style='background-color:green'>#{line.gsub!("\s", " ")}</span>" | |
else | |
out << "<span >#{line.gsub!("\s", " ")}</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