summaryrefslogtreecommitdiff
path: root/coverage
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2018-03-19 06:39:34 -0400
committerNed Batchelder <ned@nedbatchelder.com>2018-03-19 06:39:34 -0400
commit782feaa4c88722ea653cd50c79fc7773bef4ea8a (patch)
treef5498833c8f3b60660d93b0603effb3d5bc08e32 /coverage
parent36cae68dd8787c2b0aa33945d508ed7ff88c9744 (diff)
downloadpython-coveragepy-782feaa4c88722ea653cd50c79fc7773bef4ea8a.tar.gz
Slight refactor of how we look for configuration files
Diffstat (limited to 'coverage')
-rw-r--r--coverage/config.py67
-rw-r--r--coverage/control.py9
2 files changed, 47 insertions, 29 deletions
diff --git a/coverage/config.py b/coverage/config.py
index 7b8f2bd..260dafd 100644
--- a/coverage/config.py
+++ b/coverage/config.py
@@ -175,8 +175,12 @@ class CoverageConfig(object):
def __init__(self):
"""Initialize the configuration attributes to their defaults."""
# Metadata about the config.
+ # We tried to read these config files.
self.attempted_config_files = []
- self.config_files = []
+ # We did read these config files, but maybe didn't find any content for us.
+ self.config_files_read = []
+ # The file that gave us our configuration.
+ self.config_file = None
# Defaults for [run] and [report]
self._include = None
@@ -262,7 +266,7 @@ class CoverageConfig(object):
if not files_read:
return False
- self.config_files.extend(files_read)
+ self.config_files_read.extend(files_read)
any_set = False
try:
@@ -305,9 +309,14 @@ class CoverageConfig(object):
# then it was used. If we're piggybacking on someone else's file,
# then it was only used if we found some settings in it.
if our_file:
- return True
+ used = True
else:
- return any_set
+ used = any_set
+
+ if used:
+ self.config_file = filename
+
+ return used
CONFIG_FILE_OPTIONS = [
# These are *args for _set_attr_from_config_option:
@@ -425,6 +434,28 @@ class CoverageConfig(object):
raise CoverageException("No such option: %r" % option_name)
+def config_files_to_try(config_file):
+ """What config files should we try to read?
+
+ Returns a list of tuples:
+ (filename, is_our_file, was_file_specified)
+ """
+
+ # Some API users were specifying ".coveragerc" to mean the same as
+ # True, so make it so.
+ if config_file == ".coveragerc":
+ config_file = True
+ specified_file = (config_file is not True)
+ if not specified_file:
+ config_file = ".coveragerc"
+ files_to_try = [
+ (config_file, True, specified_file),
+ ("setup.cfg", False, False),
+ ("tox.ini", False, False),
+ ]
+ return files_to_try
+
+
def read_coverage_config(config_file, **kwargs):
"""Read the coverage.py configuration.
@@ -435,10 +466,7 @@ def read_coverage_config(config_file, **kwargs):
setting values in the configuration.
Returns:
- config_file, config:
- config_file is the value to use for config_file in other
- invocations of coverage.
-
+ config:
config is a CoverageConfig object read from the appropriate
configuration file.
@@ -449,25 +477,14 @@ def read_coverage_config(config_file, **kwargs):
# 2) from a file:
if config_file:
- # Some API users were specifying ".coveragerc" to mean the same as
- # True, so make it so.
- if config_file == ".coveragerc":
- config_file = True
- specified_file = (config_file is not True)
- if not specified_file:
- config_file = ".coveragerc"
-
- for fname, our_file in [(config_file, True),
- ("setup.cfg", False),
- ("tox.ini", False)]:
- config_read = config.from_file(fname, our_file=our_file)
- is_config_file = fname == config_file
-
- if not config_read and is_config_file and specified_file:
- raise CoverageException("Couldn't read '%s' as a config file" % fname)
+ files_to_try = config_files_to_try(config_file)
+ for fname, our_file, specified_file in files_to_try:
+ config_read = config.from_file(fname, our_file=our_file)
if config_read:
break
+ if our_file and specified_file:
+ raise CoverageException("Couldn't read '%s' as a config file" % fname)
# 3) from environment variables:
env_data_file = os.environ.get('COVERAGE_FILE')
@@ -486,4 +503,4 @@ def read_coverage_config(config_file, **kwargs):
config.html_dir = os.path.expanduser(config.html_dir)
config.xml_output = os.path.expanduser(config.xml_output)
- return config_file, config
+ return config
diff --git a/coverage/control.py b/coverage/control.py
index b1d95eb..80012f5 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -128,7 +128,7 @@ class Coverage(object):
"""
# Build our configuration from a number of sources.
- self._config_file, self.config = read_coverage_config(
+ self.config = read_coverage_config(
config_file=config_file,
data_file=data_file, cover_pylib=cover_pylib, timid=timid,
branch=branch, parallel=bool_or_none(data_suffix),
@@ -220,7 +220,7 @@ class Coverage(object):
raise CoverageException( # pragma: only jython
"multiprocessing is not supported on this Python"
)
- patch_multiprocessing(rcfile=self._config_file)
+ patch_multiprocessing(rcfile=self.config.config_file)
# Multi-processing uses parallel for the subprocesses, so also use
# it for the main process.
self.config.parallel = True
@@ -828,8 +828,9 @@ class Coverage(object):
('tracer', self._collector.tracer_name()),
('plugins.file_tracers', plugin_info(self._plugins.file_tracers)),
('plugins.configurers', plugin_info(self._plugins.configurers)),
- ('config_files', self.config.attempted_config_files),
- ('configs_read', self.config.config_files),
+ ('configs_attempted', self.config.attempted_config_files),
+ ('configs_read', self.config.config_files_read),
+ ('config_file', self.config.config_file),
('data_path', self._data_files.filename),
('python', sys.version.replace('\n', '')),
('platform', platform.platform()),