-
Notifications
You must be signed in to change notification settings - Fork 1
/
precommit
executable file
·63 lines (51 loc) · 1.81 KB
/
precommit
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
#!/usr/bin/python
"""This is a hideous script designed to live in the root of the working
tree as "precommit". This allows a precommit hook like the one
described in
http://schettino72.wordpress.com/2008/01/20/how-to-execute-tests-on-a-bazaar-pre-commit-hook/
to cancel a commit if the tests do not pass or test coverage is not
100%."""
import os
import sys
import coverage
coverage.erase()
coverage.start()
sys.argv = ['./manage.py', 'test']
from example import manage
manage.execute_manager(manage.settings)
coverage.stop()
cov = coverage.the_coverage
cov.save()
total_statements = 0
total_tested = 0
test_results=0
morfs = cov.filter_by_prefix(cov.cexecuted.keys(),
omit_prefixes=['/usr', '<doctest', 'coverage'])
morfs.sort(cov.morf_name_compare)
max_name = max([5,] + map(len, map(cov.morf_name, morfs)))
fmt_name = "%%- %ds" % max_name
fmt = fmt_name + "% 6d % 6d % 5d%%"
header = str(fmt_name + "% 6s % 6s % 6s %s") % ('Filename', 'Stmts', 'Exec', 'Cover', 'Missing')
for morf in morfs:
_, statements, missing, readable = cov.analysis(morf)
num_statements = len(statements)
if not num_statements:
continue
total_statements += num_statements
num_tested = num_statements - len(missing)
total_tested += num_tested
if num_tested < num_statements:
if not test_results:
print header
print '-' * len(header)
test_results += 1
print str(fmt + " %s") % (cov.morf_name(morf), num_statements,
num_tested, 100.0 * num_tested / num_statements,
str(readable))
if total_tested < total_statements:
s = fmt % ('TOTAL', total_statements, total_tested, 100.0
* total_tested / total_statements)
print '=' * len(s)
print s
print '\t*** ERROR: Test Coverage Incomplete ***'
sys.exit(test_results)