-
Notifications
You must be signed in to change notification settings - Fork 1
/
terminal-coverage.py
executable file
·136 lines (119 loc) · 4.73 KB
/
terminal-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
#!/usr/bin/env python
# great kurt-idea: make "lessopen" shit work with this, so "if less a
# file, and a .coverae 'up there somewhere' then highlight it"
# prints out annotated coverage to the terminal, with a
# banner-per-file showing coverage, and a total at the end.
from __future__ import print_function
import sys
import math
import cPickle
from os.path import split
import colors
from pygments import highlight
from pygments.formatters import Terminal256Formatter, TerminalFormatter
from pygments.lexers import get_lexer_by_name
if True:
for i in range(16):
print('%03d' % (i*16), end='')
for j in range(16):
print(colors.color(' ', bg=(i*16+j)), end='')
print()
# import os
# os.exit(0)
from coverage import coverage
cov = coverage()
cov.load()
if False:
a = cov._analyze(cov.data.measured_files()[3])
print(a)
print(dir(a))
print(cov.data.measured_files())
print(a.statements)
print(a.missing)
def print_banner(fname, percent, fill):
print(colors.color('-' * fill, bg=226, fg=236))
maxsize = fill - len('coverage: ') - 3
truncfname = fname[-maxsize:]
if len(truncfname) != len(fname):
truncfname = '...' + truncfname
print(colors.color(('coverage: ' + truncfname).ljust(fill), bg=226, fg=236))
grsize = int(fill * percent)
if grsize >= 5:
prcnt_formatted = '%3d%%' % int(percent * 100.0)
gr = colors.color(prcnt_formatted + (' ' * (grsize - 4)), fg=255, bg=22)
else:
gr = colors.color(' ' * grsize, bg=22)
red = colors.color(' ' * int(math.ceil(fill * (1.0 - percent))), bg=52)
#print(colors.color('{}%'.format(int(percent * 100.0)).ljust(fill), bg=226, fg=236))
print(gr + red)
print(colors.color('-' * fill, bg=226, fg=236))
total_statements = 0
total_missing = 0
total_files = 0
for fname in cov.data.measured_files():
if len(sys.argv) > 1:
match = False
for arg in sys.argv[1:]:
if arg in fname:
match = True
break
if not match:
continue
try:
covdata = cov._analyze(fname)
except Exception:
print("failed:", fname)
continue
percent = 1.0 # if no statements, it's all covered, right?
if covdata.numbers.n_statements:
percent = float(covdata.numbers.n_statements - covdata.numbers.n_missing) / covdata.numbers.n_statements
total_statements += covdata.numbers.n_statements
total_missing += covdata.numbers.n_missing
total_files += 1
fill = 100
print_banner(fname, percent, fill)
lines = highlight(
open(fname).read(), get_lexer_by_name('python'),
#formatter=Terminal256Formatter(style='paraiso_dark')
formatter=Terminal256Formatter(style='monokai')
#formatter=Terminal256Formatter(style='igor')
#formatter=TerminalFormatter(bg='dark', colorscheme='solarized')
)
lines = lines.split('\n')
if False:
print(dir(covdata.numbers))
print(dir(covdata))
print(covdata.statements)
assert len(lines) == covdata.numbers.n_statements
print(dir(covdata))
print(covdata.branch_lines())
cfg = dict(
branch_bg = 52,
)
#formatter=Terminal256Formatter(style='solarized-dark')))
import string
for (i, line) in enumerate(lines):
spaces = fill - len(colors.strip_color(line))
spaces = ' ' * spaces
if (i + 1) not in covdata.missing:
if False:#(i + 1) in covdata.statements:
print((colors.color(unichr(0x258f), fg=46, bg=22) + colors.color(line + spaces, bg=22)).encode('utf8'))
else:
if (i + 1) in covdata.excluded:
line = colors.strip_color(line)
print((colors.color(unichr(0x258f), fg=46, bg=236) + colors.color(line + spaces, bg=236, fg=242)).encode('utf8'))
elif (i + 1) in covdata.branch_lines():
line = colors.strip_color(line)
print((colors.color(unichr(0x258a), bg=cfg['branch_bg'], fg=160) + colors.color(line + spaces, bg=cfg['branch_bg'])).encode('utf8'))
else:
print((colors.color(unichr(0x258f), fg=46) + line + spaces).encode('utf8'))
else:
#print((colors.color(unichr(0x258f), fg=160, bg=236) + colors.color(line + spaces, bg=236)).encode('utf8'))
print((colors.color(unichr(0x258f), fg=160, bg=52) + colors.color(line + spaces, bg=52)).encode('utf8'))
if total_statements == 0:
print("Didn't find any coverage information.")
else:
covr = total_statements - total_missing
percent = float(covr) / total_statements
msg = "%d of %d statements in %d files" % (covr, total_statements, total_files)
print_banner(msg, percent, 100)