Skip to content

Commit

Permalink
feat: use --data-file to configure the coverage database
Browse files Browse the repository at this point in the history
  • Loading branch information
nbfalcon authored and nedbat committed Jan 25, 2022
1 parent cfe14c2 commit ba884e4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
31 changes: 25 additions & 6 deletions coverage/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from coverage import env
from coverage.collector import CTracer
from coverage.config import CoverageConfig
from coverage.control import DEFAULT_DATAFILE
from coverage.data import combinable_files, debug_data_file
from coverage.debug import info_formatter, info_header, short_stack
from coverage.exceptions import _BaseCoverageException, _ExceptionDuringRun, NoSource
Expand Down Expand Up @@ -128,6 +129,17 @@ class Opts:
metavar="OUTFILE",
help="Write the LCOV report to this file. Defaults to 'coverage.lcov'",
)
output_coverage = optparse.make_option(
'', '--data-file', action='store', dest="output_coverage",
metavar="OUTFILE",
help="Write the recorded coverage information to this file. Defaults to '.coverage'"
)
input_coverage = optparse.make_option(
'', '--data-file', action='store', dest="input_coverage",
metavar="INPUT",
help="Read coverage data for report generation from this file (needed if you have "
"specified -o previously). Defaults to '.coverage'"
)
json_pretty_print = optparse.make_option(
'', '--pretty-print', action='store_true',
help="Format the JSON for human readers.",
Expand Down Expand Up @@ -325,6 +337,8 @@ def get_prog_name(self):
Opts.rcfile,
]

REPORT_ARGS = [Opts.input_coverage]

CMDS = {
'annotate': CmdOptionParser(
"annotate",
Expand All @@ -333,7 +347,7 @@ def get_prog_name(self):
Opts.ignore_errors,
Opts.include,
Opts.omit,
] + GLOBAL_ARGS,
] + REPORT_ARGS + GLOBAL_ARGS,
usage="[options] [modules]",
description=(
"Make annotated copies of the given files, marking statements that are executed " +
Expand All @@ -347,6 +361,7 @@ def get_prog_name(self):
Opts.append,
Opts.keep,
Opts.quiet,
Opts.output_coverage
] + GLOBAL_ARGS,
usage="[options] <path1> <path2> ... <pathN>",
description=(
Expand Down Expand Up @@ -374,7 +389,7 @@ def get_prog_name(self):
),

'erase': CmdOptionParser(
"erase", GLOBAL_ARGS,
"erase", [Opts.input_coverage] + GLOBAL_ARGS,
description="Erase previously collected coverage data.",
),

Expand All @@ -400,7 +415,7 @@ def get_prog_name(self):
Opts.no_skip_covered,
Opts.skip_empty,
Opts.title,
] + GLOBAL_ARGS,
] + REPORT_ARGS + GLOBAL_ARGS,
usage="[options] [modules]",
description=(
"Create an HTML report of the coverage of the files. " +
Expand All @@ -421,7 +436,7 @@ def get_prog_name(self):
Opts.json_pretty_print,
Opts.quiet,
Opts.show_contexts,
] + GLOBAL_ARGS,
] + REPORT_ARGS + GLOBAL_ARGS,
usage="[options] [modules]",
description="Generate a JSON report of coverage results.",
),
Expand Down Expand Up @@ -454,7 +469,7 @@ def get_prog_name(self):
Opts.skip_covered,
Opts.no_skip_covered,
Opts.skip_empty,
] + GLOBAL_ARGS,
] + REPORT_ARGS + GLOBAL_ARGS,
usage="[options] [modules]",
description="Report coverage statistics on modules.",
),
Expand All @@ -469,6 +484,7 @@ def get_prog_name(self):
Opts.include,
Opts.module,
Opts.omit,
Opts.output_coverage,
Opts.pylib,
Opts.parallel_mode,
Opts.source,
Expand All @@ -488,7 +504,7 @@ def get_prog_name(self):
Opts.output_xml,
Opts.quiet,
Opts.skip_empty,
] + GLOBAL_ARGS,
] + REPORT_ARGS + GLOBAL_ARGS,
usage="[options] [modules]",
description="Generate an XML report of coverage results.",
),
Expand Down Expand Up @@ -591,8 +607,11 @@ def command_line(self, argv):
else:
concurrency = None

data_file = getattr(options, "output_coverage", None) \
or getattr(options, "input_coverage", None)
# Do something.
self.coverage = Coverage(
data_file=data_file or DEFAULT_DATAFILE,
data_suffix=options.parallel_mode,
cover_pylib=options.pylib,
timid=options.timid,
Expand Down
7 changes: 4 additions & 3 deletions coverage/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def override_config(cov, **kwargs):
cov.config = original_config


_DEFAULT_DATAFILE = DefaultValue("MISSING")
DEFAULT_DATAFILE = DefaultValue("MISSING")
_DEFAULT_DATAFILE = DEFAULT_DATAFILE # Just in case, for backwards compatibility

class Coverage:
"""Programmatic access to coverage.py.
Expand Down Expand Up @@ -103,7 +104,7 @@ def current(cls):
return None

def __init__(
self, data_file=_DEFAULT_DATAFILE, data_suffix=None, cover_pylib=None,
self, data_file=DEFAULT_DATAFILE, data_suffix=None, cover_pylib=None,
auto_data=False, timid=None, branch=None, config_file=True,
source=None, source_pkgs=None, omit=None, include=None, debug=None,
concurrency=None, check_preimported=False, context=None,
Expand Down Expand Up @@ -200,7 +201,7 @@ def __init__(
# data_file=None means no disk file at all. data_file missing means
# use the value from the config file.
self._no_disk = data_file is None
if data_file is _DEFAULT_DATAFILE:
if data_file is DEFAULT_DATAFILE:
data_file = None

self.config = None
Expand Down
25 changes: 25 additions & 0 deletions tests/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import coverage
import coverage.cmdline
from coverage import env
from coverage.control import DEFAULT_DATAFILE
from coverage.config import CoverageConfig
from coverage.exceptions import _ExceptionDuringRun
from coverage.version import __url__
Expand Down Expand Up @@ -57,6 +58,7 @@ class BaseCmdLineTest(CoverageTest):
contexts=None,
)
_defaults.Coverage(
data_file=DEFAULT_DATAFILE,
cover_pylib=None, data_suffix=None, timid=None, branch=None,
config_file=True, source=None, include=None, omit=None, debug=None,
concurrency=None, check_preimported=True, context=None, messages=True,
Expand Down Expand Up @@ -250,6 +252,11 @@ def test_combine(self):
cov.combine(None, strict=True, keep=False)
cov.save()
""")
self.cmd_executes("combine --data-file=foo.cov", """\
cov = Coverage(data_file="foo.cov")
cov.combine(None, strict=True, keep=False)
cov.save()
""")

def test_combine_doesnt_confuse_options_with_args(self):
# https://github.com/nedbat/coveragepy/issues/385
Expand Down Expand Up @@ -305,6 +312,10 @@ def test_erase(self):
cov = Coverage()
cov.erase()
""")
self.cmd_executes("erase --data-file=foo.cov", """\
cov = Coverage(data_file="foo.cov")
cov.erase()
""")

def test_version(self):
# coverage --version
Expand Down Expand Up @@ -558,6 +569,11 @@ def test_report(self):
cov.load()
cov.report(sort='-foo')
""")
self.cmd_executes("report --data-file=foo.cov.2", """\
cov = Coverage(data_file="foo.cov.2")
cov.load()
cov.report(show_missing=None)
""")

def test_run(self):
# coverage run [-p] [-L] [--timid] MODULE.py [ARG1 ARG2 ...]
Expand Down Expand Up @@ -684,6 +700,15 @@ def test_run(self):
cov.stop()
cov.save()
""")
self.cmd_executes("run --data-file=output.coverage foo.py", """\
cov = Coverage(data_file="output.coverage")
runner = PyRunner(['foo.py'], as_module=False)
runner.prepare()
cov.start()
runner.run()
cov.stop()
cov.save()
""")

def test_multiprocessing_needs_config_file(self):
# You can't use command-line args to add options to multiprocessing
Expand Down

0 comments on commit ba884e4

Please sign in to comment.