-
Notifications
You must be signed in to change notification settings - Fork 38
/
cli.py
334 lines (302 loc) · 9.38 KB
/
cli.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
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
import click
from pycobertura.cobertura import Cobertura
from pycobertura.reporters import (
GitHubAnnotationReporter,
HtmlReporter,
TextReporter,
CsvReporter,
MarkdownReporter,
JsonReporter,
YamlReporter,
HtmlReporterDelta,
TextReporterDelta,
CsvReporterDelta,
MarkdownReporterDelta,
JsonReporterDelta,
YamlReporterDelta,
GitHubAnnotationReporterDelta,
)
from pycobertura.filesystem import filesystem_factory
from pycobertura.utils import get_dir_from_file_path
pycobertura = click.Group()
reporters = {
"html": HtmlReporter,
"text": TextReporter,
"csv": CsvReporter,
"markdown": MarkdownReporter,
"json": JsonReporter,
"yaml": YamlReporter,
"github-annotation": GitHubAnnotationReporter,
}
class ExitCodes:
OK = 0
EXCEPTION = 1
COVERAGE_WORSENED = 2
NOT_ALL_CHANGES_COVERED = 3
def get_exit_code(differ, source):
# Compute the non-zero exit code. This is a 2-step process which involves
# checking whether code coverage is any better first and then check if all
# changes are covered (stricter) which can only be done if the source code
# is available (and enabled via the --source option).
if not differ.has_better_coverage():
return ExitCodes.COVERAGE_WORSENED
if source:
if differ.has_all_changes_covered():
return ExitCodes.OK
else:
return ExitCodes.NOT_ALL_CHANGES_COVERED
else:
return ExitCodes.OK
@pycobertura.command()
@click.argument("cobertura_file")
@click.option(
"--ignore-regex",
default=None,
type=str,
help="Regex for which files to ignore in output\n\t",
)
@click.option("-f", "--format", default="text", type=click.Choice(list(reporters)))
@click.option(
"-delim",
"--delimiter",
default=";",
type=str,
help="Delimiter for csv format, e.g. ,;\n\t",
)
@click.option(
"--annotation-title",
default="pycobertura",
type=str,
help="annotation title for github annotation format",
)
@click.option(
"--annotation-level",
default="notice",
type=str,
help="annotation level for github annotation format",
)
@click.option(
"--annotation-message",
default="not covered",
type=str,
help="annotation message for github annotation format",
)
@click.option(
"-o",
"--output",
metavar="<file>",
type=click.File("wb"),
help="Write output to <file> instead of stdout.",
)
@click.option(
"-s",
"--source",
metavar="<source-dir-or-zip>",
help="Provide path to source code directory for HTML output. The path can "
"also be a zip archive instead of a directory.",
)
@click.option(
"-p",
"--source-prefix",
metavar="<dir-prefix>",
help="For every file found in the coverage report, it will use this "
"prefix to lookup files on disk. This is especially useful when "
"the --source is a zip archive and the files were zipped under "
"a directory prefix that is not part of the source.",
)
def show(
cobertura_file,
ignore_regex,
format,
delimiter,
output,
source,
source_prefix,
annotation_level,
annotation_title,
annotation_message,
):
"""show coverage summary of a Cobertura report"""
if not source:
source = get_dir_from_file_path(cobertura_file)
cobertura = Cobertura(
cobertura_file,
filesystem=filesystem_factory(source, source_prefix=source_prefix),
)
Reporter = reporters[format]
reporter = Reporter(cobertura, ignore_regex)
if format == "csv":
report = reporter.generate(delimiter)
elif format == "github-annotation":
report = reporter.generate(
annotation_level=annotation_level,
annotation_title=annotation_title,
annotation_message=annotation_message,
)
else:
report = reporter.generate()
if not isinstance(report, bytes):
report = report.encode("utf-8")
isatty = True if output is None else output.isatty()
click.echo(report, file=output, nl=isatty)
delta_reporters = {
"text": TextReporterDelta,
"csv": CsvReporterDelta,
"markdown": MarkdownReporterDelta,
"html": HtmlReporterDelta,
"json": JsonReporterDelta,
"yaml": YamlReporterDelta,
"github-annotation": GitHubAnnotationReporterDelta,
}
@pycobertura.command(
help="""\
The diff command compares and shows the changes between two Cobertura reports.
NOTE: Reporting missing lines or showing the source code with the diff command
can only be accurately computed if the versions of the source code used to
generate each of the coverage reports is accessible. By default, the source
will read from the Cobertura report and resolved relatively from the report's
location. If the source is not accessible from the report's location, the
options `--source1` and `--source2` are necessary to point to the source code
directories (or zip archives). If the source is not available at all, pass
`--no-source` but missing lines and source code will not be reported.
"""
)
@click.argument("cobertura_file1")
@click.argument("cobertura_file2")
@click.option(
"--ignore-regex",
default=None,
type=str,
help="Regex for which files to ignore in output\n\t",
)
@click.option(
"-delim",
"--delimiter",
default=";",
type=str,
help="Delimiter for csv format, e.g. ,;\n\t",
)
@click.option(
"--color/--no-color",
default=None,
help="Colorize the output. By default, pycobertura emits color codes only "
"when standard output is connected to a terminal. This has no effect "
"with the HTML output format.",
)
@click.option(
"-f", "--format", default="text", type=click.Choice(list(delta_reporters))
)
@click.option(
"-o",
"--output",
metavar="<file>",
type=click.File("wb"),
help="Write output to <file> instead of stdout.",
)
@click.option(
"-s1",
"--source1",
metavar="<source-dir1-or-zip-archive>",
help="Provide path to source code directory or zip archive of first "
"Cobertura report. This is necessary if the filename path defined "
"in the report is not accessible from the location of the report.",
)
@click.option(
"-s2",
"--source2",
metavar="<source-dir2-or-zip-archive>",
help="Like --source1 but for the second coverage report of the diff.",
)
@click.option(
"-p1",
"--source-prefix1",
metavar="<dir-prefix1>",
help="For every file found in the coverage report, it will use this "
"prefix to lookup files on disk. This is especially useful when "
"the --source1 is a zip archive and the files were zipped under "
"a directory prefix that is not part of the source",
)
@click.option(
"-p2",
"--source-prefix2",
metavar="<dir-prefix2>",
help="Like --source-prefix1, but for applies for --source2.",
)
@click.option(
"--source/--no-source",
default=True,
help="Show missing lines and source code. When enabled (default), this "
"option requires access to the source code that was used to generate "
"both Cobertura reports (see --source1 and --source2). When "
"`--no-source` is passed, missing lines and the source code will "
"not be displayed.",
)
@click.option(
"--annotation-title",
default="pycobertura",
type=str,
help="annotation title for github annotation format",
)
@click.option(
"--annotation-level",
default="notice",
type=str,
help="annotation level for github annotation format",
)
@click.option(
"--annotation-message",
default="not covered",
type=str,
help="annotation message for github annotation format",
)
def diff(
cobertura_file1,
cobertura_file2,
ignore_regex,
delimiter,
color,
format,
output,
source1,
source2,
source_prefix1,
source_prefix2,
source,
annotation_level,
annotation_title,
annotation_message,
):
"""compare coverage of two Cobertura reports"""
# Assume that the source is located in the same directory as the provided
# coverage files if no source directories are provided.
if not source1:
source1 = get_dir_from_file_path(cobertura_file1)
if not source2:
source2 = get_dir_from_file_path(cobertura_file2)
filesystem1 = filesystem_factory(source1, source_prefix=source_prefix1)
cobertura1 = Cobertura(cobertura_file1, filesystem=filesystem1)
filesystem2 = filesystem_factory(source2, source_prefix=source_prefix2)
cobertura2 = Cobertura(cobertura_file2, filesystem=filesystem2)
Reporter = delta_reporters[format]
reporter_args = [cobertura1, cobertura2, ignore_regex]
reporter_kwargs = {"show_source": source}
isatty = True if output is None else output.isatty()
if format in {"text", "csv", "json", "markdown", "yaml"}:
color = isatty if color is None else color is True
reporter_kwargs["color"] = color
reporter = Reporter(*reporter_args, **reporter_kwargs)
if format == "csv":
report = reporter.generate(delimiter)
elif format == "github-annotation":
report = reporter.generate(
annotation_level=annotation_level,
annotation_message=annotation_message,
annotation_title=annotation_title,
)
else:
report = reporter.generate()
if not isinstance(report, bytes):
report = report.encode("utf-8")
click.echo(report, file=output, nl=isatty, color=color)
exit_code = get_exit_code(reporter.differ, source)
raise SystemExit(exit_code)