-
Notifications
You must be signed in to change notification settings - Fork 0
/
coverage.py
238 lines (204 loc) · 9.32 KB
/
coverage.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# -*- coding: utf-8 -*-
#
# Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de>
# Copyright (C) 2007-2010 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://bitten.edgewall.org/wiki/License.
from genshi.builder import tag
from trac.core import *
from trac.mimeview.api import IHTMLPreviewAnnotator
from trac.resource import Resource
from trac.util.datefmt import to_timestamp
from trac.web.api import IRequestFilter
from trac.web.chrome import add_stylesheet, add_ctxtnav, add_warning
from bitten.api import IReportChartGenerator, IReportSummarizer
from bitten.model import BuildConfig, Build, Report
from bitten.util.repository import get_repos, get_resource_path
__docformat__ = 'restructuredtext en'
class TestCoverageChartGenerator(Component):
implements(IReportChartGenerator)
# IReportChartGenerator methods
def get_supported_categories(self):
return ['coverage']
def generate_chart_data(self, req, config, category):
assert category == 'coverage'
db = self.env.get_db_cnx()
cursor = db.cursor()
cursor.execute("""
SELECT build.rev, SUM(%s) AS loc, SUM(%s * %s / 100) AS cov
FROM bitten_build AS build
LEFT OUTER JOIN bitten_report AS report ON (report.build=build.id)
LEFT OUTER JOIN bitten_report_item AS item_lines
ON (item_lines.report=report.id AND item_lines.name='lines')
LEFT OUTER JOIN bitten_report_item AS item_percentage
ON (item_percentage.report=report.id AND item_percentage.name='percentage' AND
item_percentage.item=item_lines.item)
WHERE build.config=%%s AND report.category='coverage'
AND build.rev_time >= %%s AND build.rev_time <= %%s
GROUP BY build.rev_time, build.rev, build.platform
ORDER BY build.rev_time""" % (db.cast('item_lines.value', 'int'),
db.cast('item_lines.value', 'int'),
db.cast('item_percentage.value', 'int')),
(config.name,
config.min_rev_time(self.env),
config.max_rev_time(self.env)))
prev_rev = None
coverage = []
for rev, loc, cov in cursor:
if rev != prev_rev:
coverage.append([rev, 0, 0])
if loc > coverage[-1][1]:
coverage[-1][1] = int(loc)
if cov > coverage[-1][2]:
coverage[-1][2] = int(cov)
prev_rev = rev
data = {'title': 'Test Coverage',
'data': [
{'label': 'Lines of code', 'data': [[item[0], item[1]] for item in coverage], 'lines': {'fill': True}},
{'label': 'Coverage', 'data': [[item[0], item[2]] for item in coverage]},
],
'options': {
'legend': {'position': 'sw', 'backgroundOpacity': 0.7},
'xaxis': {'tickDecimals': 0},
'yaxis': {'tickDecimals': 0},
'grid': { 'hoverable': 'true'},
'selection': {'mode': 'x'},
},
}
return 'json.txt', {"json": data}
class TestCoverageSummarizer(Component):
implements(IReportSummarizer)
# IReportSummarizer methods
def get_supported_categories(self):
return ['coverage']
def render_summary(self, req, config, build, step, category):
assert category == 'coverage'
db = self.env.get_db_cnx()
cursor = db.cursor()
cursor.execute("""
SELECT item_name.value AS unit, item_file.value AS file,
max(item_lines.value) AS loc, max(item_percentage.value) AS cov
FROM bitten_report AS report
LEFT OUTER JOIN bitten_report_item AS item_name
ON (item_name.report=report.id AND item_name.name='name')
LEFT OUTER JOIN bitten_report_item AS item_file
ON (item_file.report=report.id AND item_file.item=item_name.item AND
item_file.name='file')
LEFT OUTER JOIN bitten_report_item AS item_lines
ON (item_lines.report=report.id AND item_lines.item=item_name.item AND
item_lines.name='lines')
LEFT OUTER JOIN bitten_report_item AS item_percentage
ON (item_percentage.report=report.id AND
item_percentage.item=item_name.item AND
item_percentage.name='percentage')
WHERE category='coverage' AND build=%s AND step=%s
GROUP BY file, item_name.value
ORDER BY item_name.value""", (build.id, step.name))
units = []
total_loc, total_cov = 0, 0
for unit, file, loc, cov in cursor:
try:
loc, cov = int(loc), float(cov)
except TypeError:
continue # no rows
if loc:
d = {'name': unit, 'loc': loc, 'cov': int(cov)}
if file:
d['href'] = req.href.browser(config.path, file, rev=build.rev, annotate='coverage')
units.append(d)
total_loc += loc
total_cov += loc * cov
coverage = 0
if total_loc != 0:
coverage = total_cov // total_loc
return 'bitten_summary_coverage.html', {
'units': units,
'totals': {'loc': total_loc, 'cov': int(coverage)}
}
class TestCoverageAnnotator(Component):
"""
"""
implements(IRequestFilter, IHTMLPreviewAnnotator)
# IRequestFilter methods
def pre_process_request(self, req, handler):
return handler
def post_process_request(self, req, template, data, content_type):
""" Adds a 'Coverage' context navigation menu item. """
resource = data and data.get('context') \
and data.get('context').resource or None
if resource and isinstance(resource, Resource) \
and resource.realm=='source' and data.get('file') \
and not req.args.get('annotate', '') == 'coverage':
add_ctxtnav(req, tag.a('Coverage',
title='Annotate file with test coverage '
'data (if available)',
href=req.href.browser(get_resource_path(resource),
annotate='coverage', rev=req.args.get('rev'),
created=data.get('created_rev') or data.get('rev')),
rel='nofollow'))
return template, data, content_type
# IHTMLPreviewAnnotator methods
def get_annotation_type(self):
return 'coverage', 'Cov', 'Code coverage'
def get_annotation_data(self, context):
add_stylesheet(context.req, 'bitten/bitten_coverage.css')
resource = context.resource
# attempt to use the version passed in with the request,
# otherwise fall back to the latest version of this file.
version = context.req.args.get('rev') or resource.version
# get the last change revision for the file so that we can
# pick coverage data as latest(version >= file_revision)
created = context.req.args.get('created') or resource.version
full_path = get_resource_path(resource)
_name, repos, _path = get_repos(self.env, full_path, None)
version_time = to_timestamp(repos.get_changeset(version).date)
if version != created:
created_time = to_timestamp(repos.get_changeset(created).date)
else:
created_time = version_time
self.log.debug("Looking for coverage report for %s@%s [%s:%s]..." % (
full_path, str(resource.version), created, version))
db = self.env.get_db_cnx()
cursor = db.cursor()
cursor.execute("""
SELECT b.id, b.rev, i2.value
FROM bitten_config AS c
INNER JOIN bitten_build AS b ON c.name=b.config
INNER JOIN bitten_report AS r ON b.id=r.build
INNER JOIN bitten_report_item AS i1 ON r.id=i1.report
INNER JOIN bitten_report_item AS i2 ON (i1.item=i2.item
AND i1.report=i2.report)
WHERE i2.name='line_hits'
AND b.rev_time>=%s
AND b.rev_time<=%s
AND i1.name='file'
AND """ + db.concat('c.path', "'/'", 'i1.value') + """=%s
ORDER BY b.rev_time DESC LIMIT 1""" ,
(created_time, version_time, full_path))
row = cursor.fetchone()
if row:
build_id, build_rev, line_hits = row
coverage = line_hits.split()
self.log.debug("Coverage annotate for %s@%s using build %d: %s",
resource.id, build_rev, build_id, coverage)
return coverage
add_warning(context.req, "No coverage annotation found for "
"/%s for revision range [%s:%s]." % (
resource.id.lstrip('/'), version, created))
return []
def annotate_row(self, context, row, lineno, line, data):
from genshi.builder import tag
lineno -= 1 # 0-based index for data
if lineno >= len(data):
row.append(tag.th())
return
row_data = data[lineno]
if row_data == '-':
row.append(tag.th())
elif row_data == '0':
row.append(tag.th(row_data, class_='uncovered'))
else:
row.append(tag.th(row_data, class_='covered'))