forked from luciad/libgpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoveralls.rb
144 lines (120 loc) · 3.38 KB
/
coveralls.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env ruby
require 'json'
require 'net/http'
require 'pathname'
require 'pp'
require 'optparse'
require 'uri'
class Coveralls
def initialize(*args)
@verbose = false
OptionParser.new do |opts|
opts.banner = "Useage: coveralls.rb [options]"
opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
@verbose = v
end
end.parse!(args)
@dirs = args.dup
end
def report_coverage
post_report(generate_report)
end
private
def generate_report
# Generate gcov reports
puts "Generating gcov reports" if verbose?
@dirs.each do |dir|
Dir["#{File.expand_path(dir)}/**/*.gcno"].each do |gcno|
puts " #{gcno}" if verbose?
run_gcov("#{File.dirname(gcno)}/#{File.basename(gcno, '.gcno')}.o")
end
end
# Convert gcov reports to coveralls report
puts "Generating coveralls.io report" if verbose?
repo_root = Pathname.new(gitroot)
source_files = []
@dirs.each do |dir|
Dir["#{File.expand_path(dir)}/**/*.gcov"].each do |gcov|
puts " #{gcov}" if verbose?
source_files << report(gcov, repo_root)
end
end
report = {
'service_name' => 'travis-ci',
'service_job_id' => ENV['TRAVIS_JOB_ID'],
'git' => {
'head' => {
'id' => gitlog('%H'),
'author_name' => gitlog('%aN'),
'author_email' => gitlog('%ae'),
'committer_name' => gitlog('%cN'),
'committer_email' => gitlog('%ce'),
'message' => gitlog('%s'),
},
'branch' => ENV['TRAVIS_BRANCH'] || gitbranch,
'remotes' => gitremotes
},
'run_at' => Time.now.utc.to_s
}
puts JSON.pretty_generate(report) if verbose?
report['source_files'] = source_files
report_path = File.expand_path('report.json')
File.open(report_path, 'w') { |f| JSON.dump( report, f ) }
report_path
end
def report(gcov_file, root_dir)
source_file = ''
src = ''
cov = []
gcov_line = /([^\:]+)\:([^\:]+)\:(.*)/
File.foreach(gcov_file) do |line|
match = gcov_line.match(line)
next unless match
exec_count = match[1].strip
line_no = match[2].strip.to_i
source_line = match[3]
if line_no == 0
tag,info = source_line.split(':', 2)
source_file = info.strip if tag == 'Source'
else
src << "\n" unless src.empty?
src << source_line
if exec_count.index('-')
cov << nil
else
cov << exec_count.to_i
end
end
end
{
'name' => Pathname.new(source_file).relative_path_from(root_dir).to_s,
'source' => src,
'coverage' => cov
}
end
def post_report(report)
system('curl', "-Fjson_file=@#{report}", 'https://coveralls.io/api/v1/jobs')
end
def verbose?
@verbose
end
def debug?
true
end
def gitlog(fmt)
`git --no-pager log -1 --pretty=format:#{fmt}`
end
def gitroot()
`git rev-parse --show-toplevel`.strip
end
def gitbranch()
`git rev-parse --abbrev-ref HEAD`.strip
end
def gitremotes()
`git remote -v`.lines.find_all { |l| l.index '(fetch)' }.map { |l| { 'name' => l.split[0], 'url' => l.split[1] } }
end
def run_gcov(file_name)
system('gcov', File.basename(file_name), :chdir => File.dirname(file_name))
end
end
Coveralls.new(*ARGV).report_coverage