Skip to content

Commit

Permalink
big updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Barron committed Mar 31, 2006
1 parent 05f73d1 commit 1ccab1b
Show file tree
Hide file tree
Showing 25 changed files with 2,717 additions and 170 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -8,7 +8,7 @@ rescue Exception
nil
end

PKG_VERSION = "0.3"
PKG_VERSION = "0.3.3"

desc "Default Task"
task :default => :test
Expand Down
29 changes: 25 additions & 4 deletions bin/railscov
Expand Up @@ -22,26 +22,45 @@ EOF
'Currently only works with Rails apps.'
) { |value| options[:apply_to] = value }

opts.separator ''
opts.on('-r', '--report [DATABASE]',
'Generate a report using the optional file.') do |value|
options[:report] = true
options[:report_db] = value
end
opts.on('-o', '--output DIRECTORY',
'Directory for report output.') { |value| options[:report_out] = value }

opts.separator ''
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
exit!(0)
end
opts.on_tail('--version', 'Show version') do
puts "Insurance #{Insurance::VERSION}, #{Insurance::RELEASE_DATE}"
exit!(0)
end
end
opts.parse! ARGV

if options[:apply_to]
require 'insurance/generators/rails/loader'
InsuranceLoader::Generators::RailsLoader.load! options
exit
exit!(0)
end

if options[:report]
reportdb = options[:report_db] || 'insurance.db'
outdir = options[:report_out] || 'insurance'

puts "Running the report from #{reportdb}, output to #{outdir}"
Insurance::Formatter.run(reportdb, outdir)
exit!(0)
end

unless File.exist?('config/environment.rb')
puts 'Please run railscov from your RAILS_ROOT'
exit
exit!(0)
end


Expand Down Expand Up @@ -82,4 +101,6 @@ module Test
end
end

Insurance::RailsAnalyzer.run(Dir['test/unit/**/*.rb'] + Dir['test/functional/**/*.rb'])
Insurance::RailsAnalyzer.run(ARGV[0])

#Insurance::RailsAnalyzer.run(Dir['test/unit/**/*.rb'] + Dir['test/functional/**/*.rb'])
4 changes: 2 additions & 2 deletions lib/insurance.rb
Expand Up @@ -4,6 +4,6 @@
require 'insurance/formatter'

module Insurance
VERSION = '0.1'
RELEASE_DATE = '28 Oct 2005'
VERSION = '0.3.3'
RELEASE_DATE = '30 March 2006'
end
23 changes: 20 additions & 3 deletions lib/insurance/analyzer.rb
Expand Up @@ -5,9 +5,20 @@ module Insurance
END {
Insurance.set_trace_func nil

Dir.mkdir('insurance') unless File.exist?('insurance')
section = ARGV[0].split('/').last.to_sym
x = {}
FILELIST.each do |k,v|
x[k] = v.lines
end

if File.exist?('insurance.db')
data = Marshal.load(open('insurance.db'))
else
data = {}
end
data[section] = x

Insurance::Formatter.run(Insurance::FILELIST)
open('insurance.db', 'w').write(Marshal.dump(data))
}

class Analyzer
Expand Down Expand Up @@ -51,7 +62,11 @@ def self.filter(file)
end
end

def self.run(files)
def self.run(dir)
$thedirname = dir
files = Dir["#{dir}/**/*.rb"]
puts files.inspect

# The rails analyzer does not need to set the trace func, because that is
# done in the perversion of Test::Unit::TestCase.

Expand All @@ -60,6 +75,8 @@ def self.run(files)
# all of the application's models and controllers so that we can trace things
# at the class level. This is just easier to do than to try and figure out
# what should really be marked as hit in the output stage.

# files.each { |f| load f }

pipe = IO.popen('-', 'w+')
if pipe
Expand Down
84 changes: 67 additions & 17 deletions lib/insurance/formatter.rb
Expand Up @@ -6,35 +6,85 @@ module Insurance
class Formatter
include ERB::Util

def self.run(filelist)
files = filelist.keys.sort
def self.svn_blame_for(file)
if File.exist?(File.dirname(File.expand_path(file)) + '/.svn')
`svn blame #{file}`.split("\n").map {|l| l.split[1]}.map { |l| l.split('@')[0] }
else
[]
end
end

def self.run(dbfile, outputdir)
raw = Marshal.load(open(dbfile))

unless File.exist?(outputdir)
Dir.mkdir(outputdir)
end

asset_dir = File.dirname(__FILE__) + "/templates/assets"

files = raw.keys.inject([]) { |arr, k| arr += raw[k].keys; arr }.uniq.sort

project_name = File.basename Dir.pwd

File.open("insurance/index.html", 'w') do |f|
File.open("#{outputdir}/index.html", 'w') do |f|
f.write ERB.new(File.read("#{File.dirname(__FILE__)}/templates/index.rhtml")).result(binding)
puts "Wrote insurance/index.html"
puts "Wrote #{outputdir}/index.html"
end

filelist.each do |file, sf|
sf.post_analyze!
files.each do |file, lines|
contents = File.open(file, 'r').readlines
lines = []
contents.each_with_index do |line, num|
sline = line.strip
lines << num + 1 if sline.empty?
lines << num + 1 if sline =~ /^#/
lines << num + 1 if sline =~ /^\s*(?:end|\})\s*(?:#.*)?$/
lines << num + 1 if sline =~ /^(public|private|protected)/
lines << num + 1 if sline =~ /^(?:begin\s*(?:#.*)?|ensure\s*(?:#.*)?|else\s*(?:#.*)?)$/
lines << num + 1 if sline =~ /^(?:rescue)/
lines << num + 1 if sline =~ /^case\s*(?:#.*)?$/
lines << num + 1 if sline =~ /^(\)|\]|\})(?:#.*)?$/
end
[:unit, :functional, :integration].each do |suite|
if raw[suite][file]
raw[suite][file] += lines
end
end
end

# Create html output
files.each do |file, lines|
blame = svn_blame_for(file)

file_under_test = sf.name
percentage = sf.coverage_percent
file_under_test = file
percentage = 0 # XXX
File.open("#{outputdir}/#{file.gsub('/', '-')}.html", 'w') do |f|

contents = File.open(file, 'r').readlines

body = "<table class=\"ruby\">\n"

File.open("insurance/#{sf.name.gsub('/', '-')}.html", 'w') do |f|
contents = File.open(sf.name, 'r').readlines
body = "<pre class=\"ruby\">\n"
contents.each_with_index do |line, num|
unless sf.lines.include?(num + 1)
body << "#{'%3s' % (num + 1).to_s} <span class=\"unhit\">#{Syntax::Convertors::HTML.for_syntax('ruby').convert(line.chomp, false)}</span>\n"
else
body << "#{'%3s' % (num + 1).to_s} #{Syntax::Convertors::HTML.for_syntax('ruby').convert(line, false)}"
classes = []
[:unit, :functional, :integration].each do |suite|
if raw[suite][file] && raw[suite][file].include?(num+1)
classes << suite.to_s
end
end
lineno = (num + 1).to_s
body << "<tr><td class=\"lineno\">"
body << "<a href=\"txmt://open?url=file://#{File.expand_path(file)}&line=#{lineno}\">#{lineno}</a>"
body << "</td>"
unless blame.empty?
body << "<td class=\"blame\">#{blame[num]}</td>"
end
body << "<td><pre> <span class=\"codeline #{classes * ' '}\">#{Syntax::Convertors::HTML.for_syntax('ruby').convert(line, false)}</span>"
body << "</td></tr>"
end
body << "</pre>"
body << "</table>"
f.write ERB.new(File.read("#{File.dirname(__FILE__)}/templates/code-page.rhtml")).result(binding)
puts "Wrote insurance/#{sf.name.gsub('/', '-')}.html"
puts "Wrote #{outputdir}/#{file.gsub('/', '-')}.html"
end
end
end
Expand Down
46 changes: 40 additions & 6 deletions lib/insurance/generators/rails/insurance/templates/insurance.rake
@@ -1,7 +1,41 @@
desc 'Run Insurnace coverage analysis'
task :insurance => [ :prepare_test_database ] do
puts
puts 'You may want to get a beverage while this runs. It could take a while!'
puts
system 'railscov'
namespace 'insurance' do

task :banner do
puts ''
puts 'You might want to get a beverage. This could take a while!'
puts ''
end

desc 'Run Insurnace coverage analysis on unit tests'
task :units => :banner do
puts 'Analyzing unit suite'
sh %{railscov test/unit}
end

desc 'Run Insurnace coverage analysis on functional tests'
task :functionals => :banner do
puts 'Analyzing functional suite'
sh %{railscov test/functional}
end

desc 'Run Insurnace coverage analysis on integration tests'
task :integration => :banner do
puts 'Analyzing integration suite'
sh %{railscov test/integration}
end

desc 'Clean up Insurance temporary database'
task :clean do
rm_f 'insurance.db'
end

desc 'Generate the Insurance report'
task :report do
sh %{railscov -r}
end
end

desc 'Run Insurnace coverage analysis'
task :insurance => ['insurance:banner', 'insurance:clean',
'insurance:units', 'insurance:functionals',
'insurance:integration', 'insurance:report']
Binary file added lib/insurance/templates/assets/bg_body.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/insurance/templates/assets/bg_body2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/insurance/templates/assets/bg_header.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/insurance/templates/assets/bg_header2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/insurance/templates/assets/header_header.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/insurance/templates/assets/left.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/insurance/templates/assets/li_bullet.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/insurance/templates/assets/logo_greenery.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/insurance/templates/assets/menu_tab.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1ccab1b

Please sign in to comment.