Skip to content

Commit

Permalink
refactor: adjust some of the --data-file option handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jan 25, 2022
1 parent ba884e4 commit 1a75ebb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 43 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Expand Up @@ -25,6 +25,11 @@ Unreleased
- Feature: Added the `lcov` command to generate reports in LCOV format.
Thanks, Bradley Burns. Closes `issue 587`_ and `issue 626`_.

- Feature: the coverage data file can now be specified on the command line with
the ``--data-file`` option in any command that reads or writes data. This is
in addition to the existing ``COVERAGE_FILE`` environment variable. Closes
`issue 624`_. Thanks, Nikita Bloshchanevich.

- Feature: coverage measurement data will now be written when a SIGTERM signal
is received by the process. This includes
:meth:`Process.terminate <python:multiprocessing.Process.terminate>`,
Expand All @@ -46,6 +51,7 @@ Unreleased
- Releases now have MacOS arm64 wheels for Apple Silicon (fixes `issue 1288`_).

.. _issue 587: https://github.com/nedbat/coveragepy/issues/587
.. _issue 624: https://github.com/nedbat/coveragepy/issues/624
.. _issue 626: https://github.com/nedbat/coveragepy/issues/626
.. _issue 883: https://github.com/nedbat/coveragepy/issues/883
.. _issue 1288: https://github.com/nedbat/coveragepy/issues/1288
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -109,6 +109,7 @@ Mickie Betz
Mike Fiedler
Naveen Yadav
Nathan Land
Nikita Bloshchanevich
Nils Kattenbeck
Noel O'Boyle
Olivier Grisel
Expand Down
47 changes: 28 additions & 19 deletions coverage/cmdline.py
Expand Up @@ -60,6 +60,27 @@ class Opts:
"Accepts Python regexes, which must be quoted."
),
)
combine_datafile = optparse.make_option(
'', '--data-file', action='store', metavar="DATAFILE",
help=(
"Base name of the data files to combine and write. " +
"Defaults to '.coverage'. [env: COVERAGE_FILE]"
),
)
input_datafile = optparse.make_option(
'', '--data-file', action='store', metavar="INFILE",
help=(
"Read coverage data for report generation from this file. " +
"Defaults to '.coverage'. [env: COVERAGE_FILE]"
),
)
output_datafile = optparse.make_option(
'', '--data-file', action='store', metavar="OUTFILE",
help=(
"Write the recorded coverage data to this file. " +
"Defaults to '.coverage'. [env: COVERAGE_FILE]"
),
)
debug = optparse.make_option(
'', '--debug', action='store', metavar="OPTS",
help="Debug options, separated by commas. [env: COVERAGE_DEBUG]",
Expand Down Expand Up @@ -129,17 +150,6 @@ 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 All @@ -148,7 +158,7 @@ class Opts:
'-p', '--parallel-mode', action='store_true',
help=(
"Append the machine name, process id and random number to the " +
".coverage data file name to simplify collecting data from " +
"data file name to simplify collecting data from " +
"many processes."
),
)
Expand Down Expand Up @@ -232,6 +242,7 @@ def __init__(self, *args, **kwargs):
concurrency=None,
context=None,
contexts=None,
data_file=None,
debug=None,
directory=None,
fail_under=None,
Expand Down Expand Up @@ -337,7 +348,7 @@ def get_prog_name(self):
Opts.rcfile,
]

REPORT_ARGS = [Opts.input_coverage]
REPORT_ARGS = [Opts.input_datafile]

CMDS = {
'annotate': CmdOptionParser(
Expand All @@ -361,7 +372,7 @@ def get_prog_name(self):
Opts.append,
Opts.keep,
Opts.quiet,
Opts.output_coverage
Opts.combine_datafile
] + GLOBAL_ARGS,
usage="[options] <path1> <path2> ... <pathN>",
description=(
Expand Down Expand Up @@ -389,7 +400,7 @@ def get_prog_name(self):
),

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

Expand Down Expand Up @@ -484,7 +495,7 @@ def get_prog_name(self):
Opts.include,
Opts.module,
Opts.omit,
Opts.output_coverage,
Opts.output_datafile,
Opts.pylib,
Opts.parallel_mode,
Opts.source,
Expand Down Expand Up @@ -607,11 +618,9 @@ 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_file=options.data_file or DEFAULT_DATAFILE,
data_suffix=options.parallel_mode,
cover_pylib=options.pylib,
timid=options.timid,
Expand Down
67 changes: 43 additions & 24 deletions doc/cmd.rst
Expand Up @@ -142,11 +142,13 @@ There are many options:
path, to be run as 'python -m' would run it.
--omit=PAT1,PAT2,... Omit files whose paths match one of these patterns.
Accepts shell-style wildcards, which must be quoted.
--data-file=OUTFILE Write the recorded coverage data to this file.
Defaults to '.coverage'. [env: COVERAGE_FILE]
-L, --pylib Measure coverage even inside the Python installed
library, which isn't done by default.
-p, --parallel-mode Append the machine name, process id and random number
to the .coverage data file name to simplify collecting
data from many processes.
to the data file name to simplify collecting data from
many processes.
--source=SRC1,SRC2,...
A list of directories or importable names of code to
measure.
Expand All @@ -158,7 +160,7 @@ There are many options:
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
tried. [env: COVERAGE_RCFILE]
.. [[[end]]] (checksum: bf76ace21288ca9d3c558ccd5fb82b08)
.. [[[end]]] (checksum: 3ec48d96422f8b3aed3cf5a8b223891f)
If you want :ref:`branch coverage <branch>` measurement, use the ``--branch``
flag. Otherwise only statement coverage is measured.
Expand Down Expand Up @@ -387,16 +389,20 @@ want to keep those files, use the ``--keep`` command-line option.
directory are combined.
Options:
-a, --append Append coverage data to .coverage, otherwise it starts
clean each time.
--keep Keep original coverage files, otherwise they are deleted.
-q, --quiet Don't print messages about what is happening.
--debug=OPTS Debug options, separated by commas. [env: COVERAGE_DEBUG]
-h, --help Get help on this command.
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
'setup.cfg', 'tox.ini', and 'pyproject.toml' are tried.
[env: COVERAGE_RCFILE]
.. [[[end]]] (checksum: ddd34bbd27ab1fda8dabce80e4d67795)
-a, --append Append coverage data to .coverage, otherwise it starts
clean each time.
--keep Keep original coverage files, otherwise they are
deleted.
-q, --quiet Don't print messages about what is happening.
--data-file=DATAFILE Base name of the data files to combine and write.
Defaults to '.coverage'. [env: COVERAGE_FILE]
--debug=OPTS Debug options, separated by commas. [env:
COVERAGE_DEBUG]
-h, --help Get help on this command.
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
tried. [env: COVERAGE_RCFILE]
.. [[[end]]] (checksum: 6cba18a0531f9d2f7af67e472b96eb6b)
.. _cmd_erase:
Expand All @@ -415,12 +421,15 @@ To erase the collected data, use the **erase** command:
Erase previously collected coverage data.
Options:
--debug=OPTS Debug options, separated by commas. [env: COVERAGE_DEBUG]
-h, --help Get help on this command.
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
'setup.cfg', 'tox.ini', and 'pyproject.toml' are tried.
[env: COVERAGE_RCFILE]
.. [[[end]]] (checksum: 27f64e800a037c7e8f90289affdd5f13)
--data-file=INFILE Read coverage data for report generation from this file.
Defaults to '.coverage'. [env: COVERAGE_FILE]
--debug=OPTS Debug options, separated by commas. [env:
COVERAGE_DEBUG]
-h, --help Get help on this command.
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
'setup.cfg', 'tox.ini', and 'pyproject.toml' are tried.
[env: COVERAGE_RCFILE]
.. [[[end]]] (checksum: e3dec8ef7687d3525682904340e8cf54)
If your configuration file indicates parallel data collection, **erase** will
remove all of the data files.
Expand Down Expand Up @@ -505,13 +514,15 @@ as a percentage.
--skip-covered Skip files with 100% coverage.
--no-skip-covered Disable --skip-covered.
--skip-empty Skip files with no code.
--data-file=INFILE Read coverage data for report generation from this
file. Defaults to '.coverage'. [env: COVERAGE_FILE]
--debug=OPTS Debug options, separated by commas. [env:
COVERAGE_DEBUG]
-h, --help Get help on this command.
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
tried. [env: COVERAGE_RCFILE]
.. [[[end]]] (checksum: e5e77534929d2579f9d022227ef97313)
.. [[[end]]] (checksum: 97565fdb6f1eefbeeb12d56151fa5e63)
The ``-m`` flag also shows the line numbers of missing statements::

Expand Down Expand Up @@ -619,13 +630,15 @@ Click the keyboard icon in the upper right to see the complete list.
--no-skip-covered Disable --skip-covered.
--skip-empty Skip files with no code.
--title=TITLE A text string to use as the title on the HTML.
--data-file=INFILE Read coverage data for report generation from this
file. Defaults to '.coverage'. [env: COVERAGE_FILE]
--debug=OPTS Debug options, separated by commas. [env:
COVERAGE_DEBUG]
-h, --help Get help on this command.
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
tried. [env: COVERAGE_RCFILE]
.. [[[end]]] (checksum: 75eda57d99b6c7b736f8ab2d60cc765d)
.. [[[end]]] (checksum: e3208f3b38a44ca81e0235e867f4fd1c)
The title of the report can be set with the ``title`` setting in the
``[html]`` section of the configuration file, or the ``--title`` switch on
Expand Down Expand Up @@ -691,13 +704,15 @@ compatible with `Cobertura`_.
'coverage.xml'
-q, --quiet Don't print messages about what is happening.
--skip-empty Skip files with no code.
--data-file=INFILE Read coverage data for report generation from this
file. Defaults to '.coverage'. [env: COVERAGE_FILE]
--debug=OPTS Debug options, separated by commas. [env:
COVERAGE_DEBUG]
-h, --help Get help on this command.
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
tried. [env: COVERAGE_RCFILE]
.. [[[end]]] (checksum: 7f5bcdcacbd60e32514201f24c56c17f)
.. [[[end]]] (checksum: 3dc4450c0a723109f987c4b6f968be43)
You can specify the name of the output file with the ``-o`` switch.

Expand Down Expand Up @@ -776,13 +791,15 @@ The **json** command writes coverage data to a "coverage.json" file.
--pretty-print Format the JSON for human readers.
-q, --quiet Don't print messages about what is happening.
--show-contexts Show contexts for covered lines.
--data-file=INFILE Read coverage data for report generation from this
file. Defaults to '.coverage'. [env: COVERAGE_FILE]
--debug=OPTS Debug options, separated by commas. [env:
COVERAGE_DEBUG]
-h, --help Get help on this command.
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
tried. [env: COVERAGE_RCFILE]
.. [[[end]]] (checksum: 6fbe1ca09a8f0379a5e1794d8ac14e79)
.. [[[end]]] (checksum: fdc9af899380fbb78599d08a70e564fc)
You can specify the name of the output file with the ``-o`` switch. The JSON
can be nicely formatted by specifying the ``--pretty-print`` switch.
Expand Down Expand Up @@ -880,13 +897,15 @@ For example::
quoted.
--omit=PAT1,PAT2,... Omit files whose paths match one of these patterns.
Accepts shell-style wildcards, which must be quoted.
--data-file=INFILE Read coverage data for report generation from this
file. Defaults to '.coverage'. [env: COVERAGE_FILE]
--debug=OPTS Debug options, separated by commas. [env:
COVERAGE_DEBUG]
-h, --help Get help on this command.
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
tried. [env: COVERAGE_RCFILE]
.. [[[end]]] (checksum: 8c3175a256f38215016d03b66de23d5b)
.. [[[end]]] (checksum: aa41bad1cd4c08efc3276b5dca01dea3)
Other common reporting options are described above in :ref:`cmd_reporting`.

Expand Down

0 comments on commit 1a75ebb

Please sign in to comment.