Skip to content

Instantly share code, notes, and snippets.

@samhendley
Created May 18, 2010 16:59
Show Gist options
  • Save samhendley/0ea3404276df86296099 to your computer and use it in GitHub Desktop.
Save samhendley/0ea3404276df86296099 to your computer and use it in GitHub Desktop.
namespace :test do
task :instrument_js do
`jscoverage public/javascripts/ public/instrumented/`
`sed 's/javascripts/instrumented/g' app/views/layouts/layout.html.erb > TMPFILE && mv TMPFILE app/views/layouts/layout.html.erb`
end
end
module JsCoverageToCobertura
def collect_coverage
@@coverage ||= {}
eval_function = <<EOF
if (! window.jscoverage_report) {
window.jscoverage_report = function jscoverage_report(dir) {
if(window._$jscoverage == undefined) return "";
var pad = function (s) {
return '0000'.substr(s.length) + s;
};
var quote = function (s) {
return '"' + s.replace(/[\\u0000-\\u001f"\\\\\\u007f-\\uffff]/g, function (c) {
switch (c) {
case '\\b':
return '\\\\b';
case '\\f':
return '\\\\f';
case '\\n':
return '\\\\n';
case '\\r':
return '\\\\r';
case '\\t':
return '\\\\t';
case '"':
return '\\\\"';
case '\\\\':
return '\\\\\\\\';
default:
return '\\\\u' + pad(c.charCodeAt(0).toString(16));
}
}) + '"';
};
var json = [];
for (var file in window._$jscoverage) {
var coverage = window._$jscoverage[file];
var array = [];
var length = coverage.length;
for (var line = 0; line < length; line++) {
var value = coverage[line];
if (value === undefined || value === null) {
value = 'null';
}else{
coverage[line] = 0; //stops double counting
}
array.push(value);
}
json.push(quote(file) + ':{"coverage":[' + array.join(',') + ']}');
}
json = '{' + json.join(',') + '}';
return json;
};
};
window.jscoverage_report()
EOF
new_results = @selenium.js_eval(eval_function)
unless new_results == ''
new_results = new_results.parse_json
new_results.each do |file, data|
unless @@coverage[file]
@@coverage[file] = data['coverage']
else
previous = @@coverage[file]
data['coverage'].each_with_index do |a,index|
previous[index] += a.to_i if a
end
end
end
end
end
def generate_cobertura_xml
return if @@coverage.none?
total_lines = 0
total_hits = 0
class_text = ""
@@coverage.each do |file, coverage_array|
lines = []
source_lines = 0
hit_lines = 0
coverage_array.each_with_index do |hits, line|
if hits
lines << "<line branch=\"false\" hits=\"#{hits}\" number=\"#{line}\"/>"
source_lines += 1
hit_lines += 1 if hits.to_i > 0
end
end
total_lines += source_lines
total_hits += hit_lines
class_text += "<class branch-rate=\"0\" complexity=\"0.0\" filename=\"#{file}\" line-rate=\"#{hit_lines.to_f/source_lines rescue 0}\" name=\"#{file}\"><lines>\n "
class_text += lines.join("\n ")
class_text += "\n </lines></class>"
end
javascript_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../public/javascripts'))
text = <<EOF
<?xml version="1.0" ?>
<!DOCTYPE coverage
SYSTEM 'http://cobertura.sourceforge.net/xml/coverage-03.dtd'>
<coverage>
<sources>
<source>
#{javascript_dir}
</source>
</sources>
<packages>
<package branch-rate="0" complexity="0.0" line-rate="#{total_hits.to_f/total_lines rescue 0}" name="application">
<classes>
#{class_text}
</classes>
</package>
</packages>
</coverage>
EOF
File.open(File.join(File.dirname(__FILE__),"coverage.xml"), 'w') do |f|
f.write text
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment