Skip to content

Commit 477fd70

Browse files
committed
Add an input coverage option to report commands
-o for coverage run is not very useful if the commands that consume coverage and generate reports/coverage (json/xml/html/combine ...) can't take that file as input, so add a -c/--input-coverage option for these commands.
1 parent cbcdedb commit 477fd70

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

coverage/cmdline.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ class Opts:
129129
metavar="OUTFILE",
130130
help="Write the recorded coverage information to this file. Defaults to '.coverage'"
131131
)
132+
input_coverage = optparse.make_option(
133+
'-c', '--input-coverage', action='store', dest="input_coverage",
134+
metavar="INPUT",
135+
help="Read coverage data for report generation from this file (needed if you have "
136+
"specified -o previously). Defaults to '.coverage'"
137+
)
132138
json_pretty_print = optparse.make_option(
133139
'', '--pretty-print', action='store_true',
134140
help="Format the JSON for human readers.",
@@ -326,6 +332,8 @@ def get_prog_name(self):
326332
Opts.rcfile,
327333
]
328334

335+
REPORT_ARGS = [Opts.input_coverage]
336+
329337
CMDS = {
330338
'annotate': CmdOptionParser(
331339
"annotate",
@@ -334,7 +342,7 @@ def get_prog_name(self):
334342
Opts.ignore_errors,
335343
Opts.include,
336344
Opts.omit,
337-
] + GLOBAL_ARGS,
345+
] + REPORT_ARGS + GLOBAL_ARGS,
338346
usage="[options] [modules]",
339347
description=(
340348
"Make annotated copies of the given files, marking statements that are executed " +
@@ -348,6 +356,7 @@ def get_prog_name(self):
348356
Opts.append,
349357
Opts.keep,
350358
Opts.quiet,
359+
Opts.output_coverage
351360
] + GLOBAL_ARGS,
352361
usage="[options] <path1> <path2> ... <pathN>",
353362
description=(
@@ -401,7 +410,7 @@ def get_prog_name(self):
401410
Opts.no_skip_covered,
402411
Opts.skip_empty,
403412
Opts.title,
404-
] + GLOBAL_ARGS,
413+
] + REPORT_ARGS + GLOBAL_ARGS,
405414
usage="[options] [modules]",
406415
description=(
407416
"Create an HTML report of the coverage of the files. " +
@@ -422,7 +431,7 @@ def get_prog_name(self):
422431
Opts.json_pretty_print,
423432
Opts.quiet,
424433
Opts.show_contexts,
425-
] + GLOBAL_ARGS,
434+
] + REPORT_ARGS + GLOBAL_ARGS,
426435
usage="[options] [modules]",
427436
description="Generate a JSON report of coverage results."
428437
),
@@ -441,7 +450,7 @@ def get_prog_name(self):
441450
Opts.skip_covered,
442451
Opts.no_skip_covered,
443452
Opts.skip_empty,
444-
] + GLOBAL_ARGS,
453+
] + REPORT_ARGS + GLOBAL_ARGS,
445454
usage="[options] [modules]",
446455
description="Report coverage statistics on modules."
447456
),
@@ -476,7 +485,7 @@ def get_prog_name(self):
476485
Opts.output_xml,
477486
Opts.quiet,
478487
Opts.skip_empty,
479-
] + GLOBAL_ARGS,
488+
] + REPORT_ARGS + GLOBAL_ARGS,
480489
usage="[options] [modules]",
481490
description="Generate an XML report of coverage results."
482491
),
@@ -579,8 +588,7 @@ def command_line(self, argv):
579588
else:
580589
concurrency = None
581590

582-
data_file = options.outfile if options.action in ["run", "combine"] \
583-
else getattr(options, "input_coverage", None)
591+
data_file = options.outfile if options.action == "run" else None
584592
# Do something.
585593
self.coverage = Coverage(
586594
data_file=data_file or DEFAULT_DATAFILE,

tests/test_cmdline.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ def test_combine(self):
247247
cov.combine(None, strict=True, keep=False)
248248
cov.save()
249249
""")
250+
self.cmd_executes("combine -o foo.cov", """\
251+
cov = Coverage(data_file="foo.cov")
252+
cov.combine(None, strict=True, keep=False)
253+
cov.save()
254+
""")
250255

251256
def test_combine_doesnt_confuse_options_with_args(self):
252257
# https://github.com/nedbat/coveragepy/issues/385
@@ -512,6 +517,11 @@ def test_report(self):
512517
cov.load()
513518
cov.report(sort='-foo')
514519
""")
520+
self.cmd_executes("report -c foo.cov.2", """\
521+
cov = Coverage(data_file="foo.cov.2")
522+
cov.load()
523+
cov.report(show_missing=None)
524+
""")
515525

516526
def test_run(self):
517527
# coverage run [-p] [-L] [--timid] MODULE.py [ARG1 ARG2 ...]

0 commit comments

Comments
 (0)