-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNXSublimeCoverage.py
More file actions
352 lines (263 loc) · 10.3 KB
/
NXSublimeCoverage.py
File metadata and controls
352 lines (263 loc) · 10.3 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import os
import sys
import json
import fnmatch
import sublime
import sublime_plugin
from .utils.lcovParse import walkFile
ST3 = int(sublime.version()) >= 3000
LCOV = 'LCOV_INFO'
COVERAGE = 'COVERAGE_JSON'
debug = lambda *args: sys.stdout.write("\n%s" % " ".join(map(str, args)))
REGION_KEY_BRANCH_COVERED = 'NXSublimeCoverageBranchCovered'
REGION_KEY_BRANCH_UNCOVERED = 'NXSublimeCoverageBranchUncovered'
REGION_KEY_COVERED = 'NXSublimeCoverageCovered'
REGION_KEY_UNCOVERED = 'NXSublimeCoverageUnCovered'
REGION_TEXT_FLAGS_COVERED = sublime.DRAW_NO_FILL
REGION_TEXT_FLAGS_UNCOVERED = sublime.DRAW_NO_FILL
REGION_LINE_FLAGS_COVERED = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE
REGION_LINE_FLAGS_UNCOVERED = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE
REGION_FLAGS_BRANCH_COVERED = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_SOLID_UNDERLINE
REGION_FLAGS_BRANCH_UNCOVERED = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_SOLID_UNDERLINE
REGION_TEXT_SCOPE_COVERED = 'markup.inserted'
REGION_TEXT_SCOPE_UNCOVERED = 'markup.deleted.diff'
REGION_LINE_SCOPE_COVERED = 'markup.inserted'
REGION_LINE_SCOPE_UNCOVERED = 'markup.deleted.diff'
REGION_SCOPE_BRANCH_COVERED = 'markup.inserted'
REGION_SCOPE_BRANCH_UNCOVERED = 'markup.changed.diff'
def getCoverageDir():
settings = sublime.load_settings("NXSublimeCoverage.sublime-settings")
return settings.get("coverageDir") or 'coverage'
def find_project_root(file_path):
"""
Project Root is defined as the parent directory
that contains a directory called 'coverage'
"""
if os.access(os.path.join(file_path, getCoverageDir()), os.R_OK):
return file_path
parent, current = os.path.split(file_path)
if current:
return find_project_root(parent)
def find_file_in_dir(directory, file):
"""
Returns latest file in specifed direcotry or None if cannot find it
"""
files = []
for root, dirnames, filenames in os.walk(directory):
for filename in fnmatch.filter(filenames, file):
files.append(os.path.join(root, filename))
getmtime = lambda key: os.path.getmtime(os.path.join(directory, key))
found_file = None
if files:
files.sort(key=getmtime, reverse=True)
found_file = files.pop(0)
return found_file
def read_file(file_path):
debug("reading text file: " + file_path)
with open(file_path, 'r', encoding='utf-8') as file:
try:
return file.read()
except IOError:
return None
def read_json(file_path):
debug("reading json file:" + file_path)
with open(file_path, 'r', encoding='utf-8') as file:
try:
return json.load(file)
except IOError:
return None
def parse_lcov(lcov_file):
return walkFile(lcov_file)
def test_filename(filename, relative_filename):
return filename.replace("./", "").endswith(relative_filename)
def get_file_info(lcov_data):
files = att
# for fileInfo in lcov_data:
# name = fileInfo.file
# files[name] or=
# hit: 0
# total: 0
# covered: 0
# coverage: 0
# name: name
# lines: {}
# fdata = files[name]
# for detail in fileInfo.lines.details when detail.line
# unless fdata.lines[detail.line]
# fdata.total++
# total++
# fdata.lines[detail.line] or=
# hit: 0
# no: detail.line
# klass: 'lcov-info-no-coverage'
# range: [[detail.line - 1, 0], [detail.line - 1, 0]]
# line = fdata.lines[detail.line]
# if detail.hit > 0
# line.klass = 'lcov-info-has-coverage'
# unless line.hit
# fdata.covered++
# covered++
# line.hit += detail.hit
# fdata.hit += detail.hit
# hit += detail.hit
# fdata.coverage = (if fdata.total then fdata.covered/fdata.total else 0)*100
def clear_coverage(view):
view.erase_regions(REGION_KEY_COVERED)
view.erase_regions(REGION_KEY_UNCOVERED)
view.erase_regions(REGION_KEY_BRANCH_COVERED)
view.erase_regions(REGION_KEY_BRANCH_UNCOVERED)
class ShowNxCoverageCommand(sublime_plugin.TextCommand):
def message(self, message):
if self.view.window():
sublime.status_message(message)
def readLcovReport(self, coverage_dir, file):
report = parse_lcov(read_file(os.path.join(coverage_dir, file)))
if report:
return [LCOV, report]
debug("Can't read coverage report from file: " + str(file))
return None
def readCoverageReport(self, coverage_dir, file):
report = read_json(os.path.join(coverage_dir, file))
if report:
return [COVERAGE, report]
debug("Can't read coverage report from file: " + str(file))
return None
def findReports(self, project_root):
# get name of currently opened file
coverage_dir = os.path.join(project_root, getCoverageDir())
coverage_file = find_file_in_dir(coverage_dir, 'coverage-final.json')
lcov_info = find_file_in_dir(coverage_dir, 'lcov.info')
debug("project_root", project_root)
debug("coverage_dir", coverage_dir)
debug("coverage_file", coverage_file)
debug("lcov_info", lcov_info)
if coverage_file:
return self.readCoverageReport(coverage_dir, coverage_file)
if lcov_info:
return self.readLcovReport(coverage_dir, lcov_info)
debug("Can't find any coverage files in project root: " + str(project_root))
return None
"""
Highlight uncovered lines in the current file
based on a previous coverage run.
"""
def run(self, edit):
clear_coverage(self.view)
# get name of currently opened file
filename = self.view.file_name()
if not filename:
self.message("Could not show coverage, no filename associated with this view.")
return None
project_root = find_project_root(filename)
if not project_root:
self.message("Could not find coverage directory.")
return None
relative_filename = filename.replace(project_root + "/", "")
reports = self.findReports(project_root)
if reports is None:
self.message("No Reports available")
return
[reportType, reportData] = reports
debug("Found: ", reportType)
if reportType is COVERAGE:
return self.parseCoverageReport(relative_filename, reportData)
if reportType is LCOV:
return self.parseLcovReport(relative_filename, reportData)
def parseLcovReport(self, relative_filename, reports):
outlines = {}
view = self.view
debug("Found reports for the following number of files: " + str(len(reports)))
if not reports:
view.set_status(REGION_KEY_COVERED, "UNCOVERED!")
self.message("Can't find the coverage json file in project root: " + project_root)
return
for file_report in reports:
filename = file_report.file.replace("./", "")
if not filename.endswith(relative_filename):
continue
debug("Found test reports for file " + str(relative_filename))
lines = file_report.lines
if not lines:
self.message("No lines found in coverage")
return
for line in lines.details:
outlines[line.get("line")] = line.get("hit") > 0
badOutlines = []
goodOutlines = []
for lineNumber in outlines:
region = view.full_line(view.text_point(int(lineNumber) - 1, 0))
if outlines[lineNumber]:
goodOutlines.append(region)
else:
badOutlines.append(region)
flags = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE if ST3 else sublime.HIDDEN
if goodOutlines:
view.add_regions(REGION_KEY_COVERED, goodOutlines, REGION_LINE_SCOPE_COVERED, "dot", REGION_LINE_FLAGS_COVERED)
if badOutlines:
view.add_regions(REGION_KEY_UNCOVERED, badOutlines, REGION_LINE_SCOPE_UNCOVERED, "dot", REGION_LINE_FLAGS_UNCOVERED)
def createRegion(self, row1, col1, row2, col2):
point1 = self.view.text_point(int(row1) - 1, col1)
point2 = self.view.text_point(int(row2) - 1, col2)
return sublime.Region(point1, point2)
def startEndRegion(self, item):
start = item['start']
end = item['end']
if (start['column'] is None) or (start['line'] is None) or (end['column'] is None) or (end['line'] is None):
return debug("invalid region", item)
return self.createRegion(start['line'], start['column'], end['line'], end['column'])
def parseCoverageReport(self, relative_filename, reports):
outlines = {}
view = self.view
debug("Found reports for the following number of files: " + str(len(reports)))
report = None
for report_filename in reports:
filename = report_filename.replace("./", "")
if not filename.endswith(relative_filename):
continue
report = reports[report_filename]
break
if report is None:
self.message("No report found for " + relative_filename)
return None
# the following might need to be done for branches as well
statementMap = report['statementMap']
branchMap = report['branchMap']
# statements
good_statements = []
bad_statements = []
statements = report['s']
for statement_index in statements:
statement = statementMap[statement_index]
region = self.startEndRegion(statement)
if region:
if statements[statement_index]:
good_statements.append(region)
else:
bad_statements.append(region)
good_branches = []
bad_branches = []
branches = report['b']
for branch_index in branches:
branch = branchMap[branch_index]
locations = branch['locations']
for index, count in enumerate(branches[branch_index]):
region = self.startEndRegion(locations[index])
if region:
if count:
good_branches.append(region)
else:
bad_branches.append(region)
if good_statements:
view.add_regions(REGION_KEY_COVERED, good_statements, REGION_LINE_SCOPE_COVERED, 'dot', REGION_LINE_FLAGS_COVERED)
if bad_statements:
view.add_regions(REGION_KEY_UNCOVERED, bad_statements, REGION_TEXT_SCOPE_UNCOVERED, '', REGION_TEXT_FLAGS_UNCOVERED)
if good_branches:
view.add_regions(REGION_KEY_BRANCH_COVERED, good_branches, REGION_SCOPE_BRANCH_COVERED, '', REGION_FLAGS_BRANCH_COVERED)
if bad_branches:
view.add_regions(REGION_KEY_BRANCH_UNCOVERED, bad_branches, REGION_SCOPE_BRANCH_UNCOVERED, '', REGION_FLAGS_BRANCH_UNCOVERED)
class ClearNxCoverageCommand(sublime_plugin.TextCommand):
"""
Remove highlights created by plugin.
"""
def run(self, edit):
clear_coverage(self.view)