Skip to content

Commit 1a75ebb

Browse files
committed
refactor: adjust some of the --data-file option handling
1 parent ba884e4 commit 1a75ebb

File tree

4 files changed

+78
-43
lines changed

4 files changed

+78
-43
lines changed

CHANGES.rst

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ Unreleased
2525
- Feature: Added the `lcov` command to generate reports in LCOV format.
2626
Thanks, Bradley Burns. Closes `issue 587`_ and `issue 626`_.
2727

28+
- Feature: the coverage data file can now be specified on the command line with
29+
the ``--data-file`` option in any command that reads or writes data. This is
30+
in addition to the existing ``COVERAGE_FILE`` environment variable. Closes
31+
`issue 624`_. Thanks, Nikita Bloshchanevich.
32+
2833
- Feature: coverage measurement data will now be written when a SIGTERM signal
2934
is received by the process. This includes
3035
:meth:`Process.terminate <python:multiprocessing.Process.terminate>`,
@@ -46,6 +51,7 @@ Unreleased
4651
- Releases now have MacOS arm64 wheels for Apple Silicon (fixes `issue 1288`_).
4752

4853
.. _issue 587: https://github.com/nedbat/coveragepy/issues/587
54+
.. _issue 624: https://github.com/nedbat/coveragepy/issues/624
4955
.. _issue 626: https://github.com/nedbat/coveragepy/issues/626
5056
.. _issue 883: https://github.com/nedbat/coveragepy/issues/883
5157
.. _issue 1288: https://github.com/nedbat/coveragepy/issues/1288

CONTRIBUTORS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Mickie Betz
109109
Mike Fiedler
110110
Naveen Yadav
111111
Nathan Land
112+
Nikita Bloshchanevich
112113
Nils Kattenbeck
113114
Noel O'Boyle
114115
Olivier Grisel

coverage/cmdline.py

+28-19
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@ class Opts:
6060
"Accepts Python regexes, which must be quoted."
6161
),
6262
)
63+
combine_datafile = optparse.make_option(
64+
'', '--data-file', action='store', metavar="DATAFILE",
65+
help=(
66+
"Base name of the data files to combine and write. " +
67+
"Defaults to '.coverage'. [env: COVERAGE_FILE]"
68+
),
69+
)
70+
input_datafile = optparse.make_option(
71+
'', '--data-file', action='store', metavar="INFILE",
72+
help=(
73+
"Read coverage data for report generation from this file. " +
74+
"Defaults to '.coverage'. [env: COVERAGE_FILE]"
75+
),
76+
)
77+
output_datafile = optparse.make_option(
78+
'', '--data-file', action='store', metavar="OUTFILE",
79+
help=(
80+
"Write the recorded coverage data to this file. " +
81+
"Defaults to '.coverage'. [env: COVERAGE_FILE]"
82+
),
83+
)
6384
debug = optparse.make_option(
6485
'', '--debug', action='store', metavar="OPTS",
6586
help="Debug options, separated by commas. [env: COVERAGE_DEBUG]",
@@ -129,17 +150,6 @@ class Opts:
129150
metavar="OUTFILE",
130151
help="Write the LCOV report to this file. Defaults to 'coverage.lcov'",
131152
)
132-
output_coverage = optparse.make_option(
133-
'', '--data-file', action='store', dest="output_coverage",
134-
metavar="OUTFILE",
135-
help="Write the recorded coverage information to this file. Defaults to '.coverage'"
136-
)
137-
input_coverage = optparse.make_option(
138-
'', '--data-file', action='store', dest="input_coverage",
139-
metavar="INPUT",
140-
help="Read coverage data for report generation from this file (needed if you have "
141-
"specified -o previously). Defaults to '.coverage'"
142-
)
143153
json_pretty_print = optparse.make_option(
144154
'', '--pretty-print', action='store_true',
145155
help="Format the JSON for human readers.",
@@ -148,7 +158,7 @@ class Opts:
148158
'-p', '--parallel-mode', action='store_true',
149159
help=(
150160
"Append the machine name, process id and random number to the " +
151-
".coverage data file name to simplify collecting data from " +
161+
"data file name to simplify collecting data from " +
152162
"many processes."
153163
),
154164
)
@@ -232,6 +242,7 @@ def __init__(self, *args, **kwargs):
232242
concurrency=None,
233243
context=None,
234244
contexts=None,
245+
data_file=None,
235246
debug=None,
236247
directory=None,
237248
fail_under=None,
@@ -337,7 +348,7 @@ def get_prog_name(self):
337348
Opts.rcfile,
338349
]
339350

340-
REPORT_ARGS = [Opts.input_coverage]
351+
REPORT_ARGS = [Opts.input_datafile]
341352

342353
CMDS = {
343354
'annotate': CmdOptionParser(
@@ -361,7 +372,7 @@ def get_prog_name(self):
361372
Opts.append,
362373
Opts.keep,
363374
Opts.quiet,
364-
Opts.output_coverage
375+
Opts.combine_datafile
365376
] + GLOBAL_ARGS,
366377
usage="[options] <path1> <path2> ... <pathN>",
367378
description=(
@@ -389,7 +400,7 @@ def get_prog_name(self):
389400
),
390401

391402
'erase': CmdOptionParser(
392-
"erase", [Opts.input_coverage] + GLOBAL_ARGS,
403+
"erase", [Opts.input_datafile] + GLOBAL_ARGS,
393404
description="Erase previously collected coverage data.",
394405
),
395406

@@ -484,7 +495,7 @@ def get_prog_name(self):
484495
Opts.include,
485496
Opts.module,
486497
Opts.omit,
487-
Opts.output_coverage,
498+
Opts.output_datafile,
488499
Opts.pylib,
489500
Opts.parallel_mode,
490501
Opts.source,
@@ -607,11 +618,9 @@ def command_line(self, argv):
607618
else:
608619
concurrency = None
609620

610-
data_file = getattr(options, "output_coverage", None) \
611-
or getattr(options, "input_coverage", None)
612621
# Do something.
613622
self.coverage = Coverage(
614-
data_file=data_file or DEFAULT_DATAFILE,
623+
data_file=options.data_file or DEFAULT_DATAFILE,
615624
data_suffix=options.parallel_mode,
616625
cover_pylib=options.pylib,
617626
timid=options.timid,

doc/cmd.rst

+43-24
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,13 @@ There are many options:
142142
path, to be run as 'python -m' would run it.
143143
--omit=PAT1,PAT2,... Omit files whose paths match one of these patterns.
144144
Accepts shell-style wildcards, which must be quoted.
145+
--data-file=OUTFILE Write the recorded coverage data to this file.
146+
Defaults to '.coverage'. [env: COVERAGE_FILE]
145147
-L, --pylib Measure coverage even inside the Python installed
146148
library, which isn't done by default.
147149
-p, --parallel-mode Append the machine name, process id and random number
148-
to the .coverage data file name to simplify collecting
149-
data from many processes.
150+
to the data file name to simplify collecting data from
151+
many processes.
150152
--source=SRC1,SRC2,...
151153
A list of directories or importable names of code to
152154
measure.
@@ -158,7 +160,7 @@ There are many options:
158160
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
159161
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
160162
tried. [env: COVERAGE_RCFILE]
161-
.. [[[end]]] (checksum: bf76ace21288ca9d3c558ccd5fb82b08)
163+
.. [[[end]]] (checksum: 3ec48d96422f8b3aed3cf5a8b223891f)
162164
163165
If you want :ref:`branch coverage <branch>` measurement, use the ``--branch``
164166
flag. Otherwise only statement coverage is measured.
@@ -387,16 +389,20 @@ want to keep those files, use the ``--keep`` command-line option.
387389
directory are combined.
388390
389391
Options:
390-
-a, --append Append coverage data to .coverage, otherwise it starts
391-
clean each time.
392-
--keep Keep original coverage files, otherwise they are deleted.
393-
-q, --quiet Don't print messages about what is happening.
394-
--debug=OPTS Debug options, separated by commas. [env: COVERAGE_DEBUG]
395-
-h, --help Get help on this command.
396-
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
397-
'setup.cfg', 'tox.ini', and 'pyproject.toml' are tried.
398-
[env: COVERAGE_RCFILE]
399-
.. [[[end]]] (checksum: ddd34bbd27ab1fda8dabce80e4d67795)
392+
-a, --append Append coverage data to .coverage, otherwise it starts
393+
clean each time.
394+
--keep Keep original coverage files, otherwise they are
395+
deleted.
396+
-q, --quiet Don't print messages about what is happening.
397+
--data-file=DATAFILE Base name of the data files to combine and write.
398+
Defaults to '.coverage'. [env: COVERAGE_FILE]
399+
--debug=OPTS Debug options, separated by commas. [env:
400+
COVERAGE_DEBUG]
401+
-h, --help Get help on this command.
402+
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
403+
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
404+
tried. [env: COVERAGE_RCFILE]
405+
.. [[[end]]] (checksum: 6cba18a0531f9d2f7af67e472b96eb6b)
400406
401407
402408
.. _cmd_erase:
@@ -415,12 +421,15 @@ To erase the collected data, use the **erase** command:
415421
Erase previously collected coverage data.
416422
417423
Options:
418-
--debug=OPTS Debug options, separated by commas. [env: COVERAGE_DEBUG]
419-
-h, --help Get help on this command.
420-
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
421-
'setup.cfg', 'tox.ini', and 'pyproject.toml' are tried.
422-
[env: COVERAGE_RCFILE]
423-
.. [[[end]]] (checksum: 27f64e800a037c7e8f90289affdd5f13)
424+
--data-file=INFILE Read coverage data for report generation from this file.
425+
Defaults to '.coverage'. [env: COVERAGE_FILE]
426+
--debug=OPTS Debug options, separated by commas. [env:
427+
COVERAGE_DEBUG]
428+
-h, --help Get help on this command.
429+
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
430+
'setup.cfg', 'tox.ini', and 'pyproject.toml' are tried.
431+
[env: COVERAGE_RCFILE]
432+
.. [[[end]]] (checksum: e3dec8ef7687d3525682904340e8cf54)
424433
425434
If your configuration file indicates parallel data collection, **erase** will
426435
remove all of the data files.
@@ -505,13 +514,15 @@ as a percentage.
505514
--skip-covered Skip files with 100% coverage.
506515
--no-skip-covered Disable --skip-covered.
507516
--skip-empty Skip files with no code.
517+
--data-file=INFILE Read coverage data for report generation from this
518+
file. Defaults to '.coverage'. [env: COVERAGE_FILE]
508519
--debug=OPTS Debug options, separated by commas. [env:
509520
COVERAGE_DEBUG]
510521
-h, --help Get help on this command.
511522
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
512523
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
513524
tried. [env: COVERAGE_RCFILE]
514-
.. [[[end]]] (checksum: e5e77534929d2579f9d022227ef97313)
525+
.. [[[end]]] (checksum: 97565fdb6f1eefbeeb12d56151fa5e63)
515526
516527
The ``-m`` flag also shows the line numbers of missing statements::
517528

@@ -619,13 +630,15 @@ Click the keyboard icon in the upper right to see the complete list.
619630
--no-skip-covered Disable --skip-covered.
620631
--skip-empty Skip files with no code.
621632
--title=TITLE A text string to use as the title on the HTML.
633+
--data-file=INFILE Read coverage data for report generation from this
634+
file. Defaults to '.coverage'. [env: COVERAGE_FILE]
622635
--debug=OPTS Debug options, separated by commas. [env:
623636
COVERAGE_DEBUG]
624637
-h, --help Get help on this command.
625638
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
626639
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
627640
tried. [env: COVERAGE_RCFILE]
628-
.. [[[end]]] (checksum: 75eda57d99b6c7b736f8ab2d60cc765d)
641+
.. [[[end]]] (checksum: e3208f3b38a44ca81e0235e867f4fd1c)
629642
630643
The title of the report can be set with the ``title`` setting in the
631644
``[html]`` section of the configuration file, or the ``--title`` switch on
@@ -691,13 +704,15 @@ compatible with `Cobertura`_.
691704
'coverage.xml'
692705
-q, --quiet Don't print messages about what is happening.
693706
--skip-empty Skip files with no code.
707+
--data-file=INFILE Read coverage data for report generation from this
708+
file. Defaults to '.coverage'. [env: COVERAGE_FILE]
694709
--debug=OPTS Debug options, separated by commas. [env:
695710
COVERAGE_DEBUG]
696711
-h, --help Get help on this command.
697712
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
698713
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
699714
tried. [env: COVERAGE_RCFILE]
700-
.. [[[end]]] (checksum: 7f5bcdcacbd60e32514201f24c56c17f)
715+
.. [[[end]]] (checksum: 3dc4450c0a723109f987c4b6f968be43)
701716
702717
You can specify the name of the output file with the ``-o`` switch.
703718

@@ -776,13 +791,15 @@ The **json** command writes coverage data to a "coverage.json" file.
776791
--pretty-print Format the JSON for human readers.
777792
-q, --quiet Don't print messages about what is happening.
778793
--show-contexts Show contexts for covered lines.
794+
--data-file=INFILE Read coverage data for report generation from this
795+
file. Defaults to '.coverage'. [env: COVERAGE_FILE]
779796
--debug=OPTS Debug options, separated by commas. [env:
780797
COVERAGE_DEBUG]
781798
-h, --help Get help on this command.
782799
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
783800
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
784801
tried. [env: COVERAGE_RCFILE]
785-
.. [[[end]]] (checksum: 6fbe1ca09a8f0379a5e1794d8ac14e79)
802+
.. [[[end]]] (checksum: fdc9af899380fbb78599d08a70e564fc)
786803
787804
You can specify the name of the output file with the ``-o`` switch. The JSON
788805
can be nicely formatted by specifying the ``--pretty-print`` switch.
@@ -880,13 +897,15 @@ For example::
880897
quoted.
881898
--omit=PAT1,PAT2,... Omit files whose paths match one of these patterns.
882899
Accepts shell-style wildcards, which must be quoted.
900+
--data-file=INFILE Read coverage data for report generation from this
901+
file. Defaults to '.coverage'. [env: COVERAGE_FILE]
883902
--debug=OPTS Debug options, separated by commas. [env:
884903
COVERAGE_DEBUG]
885904
-h, --help Get help on this command.
886905
--rcfile=RCFILE Specify configuration file. By default '.coveragerc',
887906
'setup.cfg', 'tox.ini', and 'pyproject.toml' are
888907
tried. [env: COVERAGE_RCFILE]
889-
.. [[[end]]] (checksum: 8c3175a256f38215016d03b66de23d5b)
908+
.. [[[end]]] (checksum: aa41bad1cd4c08efc3276b5dca01dea3)
890909
891910
Other common reporting options are described above in :ref:`cmd_reporting`.
892911

0 commit comments

Comments
 (0)