From fc040a5a6af7d97ef1683e07e1e8491efa651df6 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Tue, 1 Sep 2020 22:55:00 +0100 Subject: [PATCH] Create Way to force package even if filepath exists Fixes: #268 --- coverage/config.py | 2 ++ coverage/control.py | 3 ++- coverage/inorout.py | 1 + tests/modules/ambigious/__init__.py | 0 tests/modules/ambigious/pkg1/__init__.py | 0 tests/modules/ambigious/pkg1/ambigious.py | 0 tests/test_api.py | 21 +++++++++++++++++++++ tests/test_config.py | 2 ++ 8 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/modules/ambigious/__init__.py create mode 100644 tests/modules/ambigious/pkg1/__init__.py create mode 100644 tests/modules/ambigious/pkg1/ambigious.py diff --git a/coverage/config.py b/coverage/config.py index 84d9758bf..2af4a1cc8 100644 --- a/coverage/config.py +++ b/coverage/config.py @@ -195,6 +195,7 @@ def __init__(self): self.run_include = None self.run_omit = None self.source = None + self.source_pkgs = [] self.timid = False self._crash = None @@ -361,6 +362,7 @@ def copy(self): ('run_include', 'run:include', 'list'), ('run_omit', 'run:omit', 'list'), ('source', 'run:source', 'list'), + ('source_pkgs', 'run:source_pkgs', 'list'), ('timid', 'run:timid', 'boolean'), ('_crash', 'run:_crash'), diff --git a/coverage/control.py b/coverage/control.py index d60db2126..879ceb567 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -100,7 +100,7 @@ def __init__( self, data_file=_DEFAULT_DATAFILE, data_suffix=None, cover_pylib=None, auto_data=False, timid=None, branch=None, config_file=True, source=None, omit=None, include=None, debug=None, - concurrency=None, check_preimported=False, context=None, + concurrency=None, check_preimported=False, context=None, **kwargs ): """ Many of these arguments duplicate and override values that can be @@ -191,6 +191,7 @@ def __init__( source=source, run_omit=omit, run_include=include, debug=debug, report_omit=omit, report_include=include, concurrency=concurrency, context=context, + **kwargs ) # This is injectable by tests. diff --git a/coverage/inorout.py b/coverage/inorout.py index ec5f2c1ac..fbd1a95ed 100644 --- a/coverage/inorout.py +++ b/coverage/inorout.py @@ -132,6 +132,7 @@ def __init__(self, warn, debug): def configure(self, config): """Apply the configuration to get ready for decision-time.""" + self.source_pkgs.extend(config.source_pkgs) for src in config.source or []: if os.path.isdir(src): self.source.append(canonical_filename(src)) diff --git a/tests/modules/ambigious/__init__.py b/tests/modules/ambigious/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/modules/ambigious/pkg1/__init__.py b/tests/modules/ambigious/pkg1/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/modules/ambigious/pkg1/ambigious.py b/tests/modules/ambigious/pkg1/ambigious.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_api.py b/tests/test_api.py index 5ede9158b..54e5f2227 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -895,6 +895,27 @@ def test_source_package_as_dir(self): # Because source= was specified, we do search for unexecuted files. self.assertEqual(lines['p1c'], 0) + def test_ambigious_source_package_as_dir(self): + # pkg1 is a directory and a pkg, since we cd into tests/modules/ambigious + self.chdir(self.nice_file(TESTS_DIR, 'modules', "ambigious")) + lines = self.coverage_usepkgs(source=["pkg1"]) + self.assertEqual( + self.coverage_usepkgs(source=["pkg1"]), + { + u'__init__.py': 0, u'__init__': 0, + u"ambigious.py": 0, u"ambigious": 0, + }, + ) + + def test_ambigious_source_package_as_package(self): + # pkg1 is a directory and a pkg, since we cd into tests/modules/ambigious + self.chdir(self.nice_file(TESTS_DIR, 'modules', "ambigious")) + lines = self.coverage_usepkgs(source_pkgs=["pkg1"]) + self.filenames_in(lines, "p1a p1b") + self.filenames_not_in(lines, "p2a p2b othera otherb osa osb ambigious") + # Because source= was specified, we do search for unexecuted files. + self.assertEqual(lines['p1c'], 0) + def test_source_package_as_package(self): lines = self.coverage_usepkgs(source=["pkg1.sub"]) self.filenames_not_in(lines, "p2a p2b othera otherb osa osb") diff --git a/tests/test_config.py b/tests/test_config.py index 89ecb17c6..dd86303f2 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -462,6 +462,7 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): ; this omit is overriden by the omit from [report] omit = twenty source = myapp + source_pkgs = ned plugins = plugins.a_plugin plugins.another @@ -553,6 +554,7 @@ def assert_config_settings_are_correct(self, cov): self.assertTrue(cov.config.parallel) self.assertEqual(cov.config.concurrency, ["thread"]) self.assertEqual(cov.config.source, ["myapp"]) + self.assertEqual(cov.config.source_pkgs, ["ned"]) self.assertEqual(cov.config.disable_warnings, ["abcd", "efgh"]) self.assertEqual(cov.get_exclude_list(), ["if 0:", r"pragma:?\s+no cover", "another_tab"])