-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
with_coverage.py
executable file
·134 lines (113 loc) · 4.51 KB
/
with_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
# -*- coding: utf-8 -*-
# pylint: disable=W0201
import os
from optparse import make_option
import django
from .. import settings
try:
from coverage import coverage
except ImportError:
from coverage.control import coverage
def default_config_path():
rcfile = settings.COVERAGE_RCFILE
if os.path.exists(rcfile):
return rcfile
return None
class CoverageTask(object):
if django.VERSION < (1, 8):
option_list = (
make_option(
"--coverage-rcfile",
dest="coverage_rcfile",
default="",
help="Specify configuration file."
),
make_option(
"--coverage-html-report",
dest="coverage_html_report_dir",
default=settings.COVERAGE_REPORT_HTML_DIR,
help="Directory to which HTML coverage report should be"
" written. If not specified, no report is generated."
),
make_option(
"--coverage-no-branch-measure",
action="store_false",
default=settings.COVERAGE_MEASURE_BRANCH,
dest="coverage_measure_branch",
help="Don't measure branch coverage."
),
make_option(
"--coverage-with-migrations",
action="store_true",
default=settings.COVERAGE_WITH_MIGRATIONS,
dest="coverage_with_migrations",
help="Don't measure migrations coverage."
),
make_option(
"--coverage-exclude",
action="append",
default=settings.COVERAGE_EXCLUDE_PATHS,
dest="coverage_excludes",
help="Paths to be excluded from coverage"
)
)
def __init__(self, **options):
self.output_dir = options['output_dir']
self.with_migrations = options['coverage_with_migrations']
self.html_dir = options['coverage_html_report_dir']
self.branch = options['coverage_measure_branch']
self.exclude_locations = options['coverage_excludes'] or None
self.coverage = coverage(
branch=self.branch,
source=settings.PROJECT_APPS,
omit=self.exclude_locations,
config_file=options.get('coverage_rcfile') or default_config_path()
)
@classmethod
def add_arguments(cls, parser):
parser.add_argument("--coverage-rcfile",
dest="coverage_rcfile", default="",
help="Specify configuration file.")
parser.add_argument("--coverage-html-report",
dest="coverage_html_report_dir", default=settings.COVERAGE_REPORT_HTML_DIR,
help="Directory to which HTML coverage report should be"
" written. If not specified, no report is generated.")
parser.add_argument("--coverage-no-branch-measure",
action="store_false", default=settings.COVERAGE_MEASURE_BRANCH,
dest="coverage_measure_branch",
help="Don't measure branch coverage.")
parser.add_argument("--coverage-with-migrations",
action="store_true", default=settings.COVERAGE_WITH_MIGRATIONS,
dest="coverage_with_migrations",
help="Don't measure migrations coverage.")
parser.add_argument("--coverage-exclude",
action="append", default=settings.COVERAGE_EXCLUDE_PATHS,
dest="coverage_excludes",
help="Paths to be excluded from coverage")
def setup_test_environment(self, **kwargs):
self.coverage.start()
def teardown_test_environment(self, **kwargs):
self.coverage.stop()
try:
self.coverage._harvest_data()
except AttributeError:
# coverage._harvest_data was renamed to coverage.get_data in
# coverage.py 4.0.
self.coverage.get_data()
morfs = [filename for filename in self.coverage.data.measured_files()
if self.want_file(filename)]
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
self.coverage.xml_report(
morfs=morfs,
outfile=os.path.join(self.output_dir, 'coverage.xml')
)
if self.html_dir:
self.coverage.html_report(
morfs=morfs,
directory=self.html_dir
)
def want_file(self, filename):
if not self.with_migrations and '/migrations/' in filename:
return False
return True