Skip to content

Commit 65b9007

Browse files
committed
The [paths] setting is ordered. #649
1 parent 12e019d commit 65b9007

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

CHANGES.rst

+5
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,17 @@ Unreleased
3333
fixes `issue 745`_ (about not being able to run unittest tests that spawn
3434
subprocesses), and `issue 838`_, which described the problem directly.
3535

36+
- The ``[paths]`` configuration section is now ordered. If you specify more
37+
than one list of patterns, the first one that matches will be used. Fixes
38+
`issue 649`_.
39+
3640
- The :func:`.coverage.numbits.register_sqlite_functions` function now also
3741
registers `numbits_to_nums` for use in SQLite queries. Thanks, Simon
3842
Willison.
3943

4044
- Python 3.9a1 is supported.
4145

46+
.. _issue 649: https://github.com/nedbat/coveragepy/issues/649
4247
.. _issue 745: https://github.com/nedbat/coveragepy/issues/745
4348
.. _issue 838: https://github.com/nedbat/coveragepy/issues/838
4449

coverage/config.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def __init__(self):
226226
self.json_show_contexts = False
227227

228228
# Defaults for [paths]
229-
self.paths = {}
229+
self.paths = collections.OrderedDict()
230230

231231
# Options for plugins
232232
self.plugin_options = {}
@@ -536,6 +536,9 @@ def read_coverage_config(config_file, **kwargs):
536536
config.data_file = os.path.expanduser(config.data_file)
537537
config.html_dir = os.path.expanduser(config.html_dir)
538538
config.xml_output = os.path.expanduser(config.xml_output)
539-
config.paths = {k: [os.path.expanduser(f) for f in v] for k, v in config.paths.items()}
539+
config.paths = collections.OrderedDict(
540+
(k, [os.path.expanduser(f) for f in v])
541+
for k, v in config.paths.items()
542+
)
540543

541544
return config

doc/config.rst

+3
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ In this example, data collected for "/jenkins/build/1234/src/module.py" will be
238238
combined with data for "c:\myproj\src\module.py", and will be reported against
239239
the source file found at "src/module.py".
240240

241+
If you specify more than one list of paths, they will be considered in order.
242+
The first list that has a match will be used.
243+
241244
See :ref:`cmd_combining` for more information.
242245

243246

tests/test_api.py

+43
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,49 @@ def test_combining_with_a_used_coverage(self):
425425
cov.combine()
426426
self.check_code1_code2(cov)
427427

428+
def test_ordered_combine(self):
429+
# https://github.com/nedbat/coveragepy/issues/649
430+
# The order of the [paths] setting matters
431+
def make_data_file():
432+
data = coverage.CoverageData(".coverage.1")
433+
data.add_lines({os.path.abspath('ci/girder/g1.py'): dict.fromkeys(range(10))})
434+
data.add_lines({os.path.abspath('ci/girder/plugins/p1.py'): dict.fromkeys(range(10))})
435+
data.write()
436+
437+
def get_combined_filenames():
438+
cov = coverage.Coverage()
439+
cov.combine()
440+
cov.save()
441+
data = cov.get_data()
442+
filenames = {os.path.relpath(f) for f in data.measured_files()}
443+
return filenames
444+
445+
# Case 1: get the order right.
446+
make_data_file()
447+
self.make_file(".coveragerc", """\
448+
[paths]
449+
plugins =
450+
plugins/
451+
ci/girder/plugins/
452+
girder =
453+
girder/
454+
ci/girder/
455+
""")
456+
assert get_combined_filenames() == {'girder/g1.py', 'plugins/p1.py'}
457+
458+
# Case 2: get the order wrong.
459+
make_data_file()
460+
self.make_file(".coveragerc", """\
461+
[paths]
462+
girder =
463+
girder/
464+
ci/girder/
465+
plugins =
466+
plugins/
467+
ci/girder/plugins/
468+
""")
469+
assert get_combined_filenames() == {'girder/g1.py', 'girder/plugins/p1.py'}
470+
428471
def test_warnings(self):
429472
self.make_file("hello.py", """\
430473
import sys, os

0 commit comments

Comments
 (0)