Last active
May 31, 2016 03:34
-
-
Save quantizor/9ff110729ea73add55c887623708fd90 to your computer and use it in GitHub Desktop.
Cobertura XML -> Coveralls via Ruby + Travis CI
This file contains 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
require 'coveralls' | |
require 'nokogiri' | |
require 'digest' | |
# converts cobertura xml output into the necessary coveralls JSON payload | |
# and sends it on its way | |
# use like `ruby pack_n_ship_to_coveralls.rb xml1.xml xml2.xml xml3.xml` | |
# replace `ENV['TRAVIS_JOB_ID']` as needed if you don't use Travis CI | |
files = ARGV | |
source_files = [] | |
cwd_prefix = Dir.pwd + '/' | |
unless files.empty? | |
files.each do |filename| | |
xml = Nokogiri::XML(File.open(filename).read, 'r') | |
source_path = xml.css('sources source')[0].content | |
xml.css('classes class').each do |klass| | |
klass_path = klass.attr('filename') | |
klass_absolute_path = source_path + '/' + klass_path | |
klass_repo_path = klass_absolute_path[ | |
cwd_prefix.length..klass_absolute_path.length | |
] | |
klass_file = File.open(klass_absolute_path, 'r') | |
klass_file_num_lines = klass_file.each_line.count | |
klass_file_md5 = Digest::MD5.hexdigest(klass_file.read) | |
klass_file.close | |
klass_line_coverage = Array.new(klass_file_num_lines) | |
klass.css('lines line').each do |line| | |
klass_line_coverage[line.attr('number').to_i - 1] = line.attr('hits').to_i | |
end | |
source_files.push({ | |
:name => klass_repo_path, | |
:source_digest => klass_file_md5, | |
:coverage => klass_line_coverage, | |
}) | |
end | |
end | |
end | |
# Craft the payload... | |
payload = { | |
:service_job_id => ENV['TRAVIS_JOB_ID'], | |
:service_name => 'travis-pro', | |
:source_files => source_files, | |
} | |
Coveralls::API.post_json('jobs', payload) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment