more updates, more work on instrumenting, it reports back, whoa

This commit is contained in:
John Bintz 2012-03-26 17:40:20 -04:00
parent e28703b047
commit 91027aee61
4 changed files with 22 additions and 6 deletions

View File

@ -33,7 +33,7 @@ module Flowerbox
end end
def instrument_js? def instrument_js?
!@instrument_files.empty? !instrument_files.empty?
end end
def test_with(what) def test_with(what)

View File

@ -37,6 +37,15 @@ module Flowerbox
end end
def instrument(data) def instrument(data)
results = []
data.flatten.first.each do |filename, lines|
results += lines.reject { |line| line == nil }
end
visited = results.find_all { |result| result == 1 }.length
Flowerbox.notify "Coverage: %d/%d lines (%0.2f%% of instrumented code)" % [ visited, results.length, (visited.to_f / results.length.to_f) * 100 ]
end end
def ensure_alive def ensure_alive
@ -97,7 +106,7 @@ module Flowerbox
@results @results
rescue => e rescue => e
case e case e
when ExecJS::RuntimeError, ExecJS::ProgramError when ExecJS::RuntimeError, ExecJS::ProgramError, Sprockets::FileNotFound
handle_coffeescript_compilation_error(e) handle_coffeescript_compilation_error(e)
else else
raise e raise e

View File

@ -38,7 +38,6 @@ module Flowerbox
return @environment if @environment return @environment if @environment
@environment = Sprockets::Environment.new @environment = Sprockets::Environment.new
@environment.cache = cache
if Flowerbox.instrument_js? if Flowerbox.instrument_js?
require 'flowerbox/tilt/instrument_js' require 'flowerbox/tilt/instrument_js'

View File

@ -14,11 +14,14 @@ module Flowerbox
output = [ output = [
'if (typeof __$instrument == "undefined") { __$instrument = {} }', 'if (typeof __$instrument == "undefined") { __$instrument = {} }',
"__$instrument['#{file}'] = [];", "__$instrument['#{file}'] = [];",
"__$instrument['#{file}'][#{lines.length - 1}] = 0;" "__$instrument['#{file}'][#{lines.length - 1}] = null;"
] ]
prior_comma_end = comma_end = false prior_comma_end = comma_end = false
lines_instrumented = []
code_output = []
lines.each_with_index do |line, index| lines.each_with_index do |line, index|
line.rstrip! line.rstrip!
@ -26,12 +29,17 @@ module Flowerbox
if line[%r{; *$}] if line[%r{; *$}]
line = line + instrument line = line + instrument
lines_instrumented << index
end end
output << line code_output << line
end end
output.join("\n") lines_instrumented.each do |line|
output << "__$instrument['#{file}'][#{line}] = 0;"
end
(output + code_output).join("\n")
else else
data data
end end