diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2016-11-20 08:21:02 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2016-11-20 08:21:02 -0500 |
commit | afe3955c48db6c4ef6501e657ba7fc03a1b69d5d (patch) | |
tree | a2f61f99b054116ce64d016fcf58ae27753e5992 /coverage/config.py | |
parent | d524b6d7ea0cf363d5fd67b6a79994483f972425 (diff) | |
download | python-coveragepy-git-afe3955c48db6c4ef6501e657ba7fc03a1b69d5d.tar.gz |
Move the config logic out of the Coverage constructor
Diffstat (limited to 'coverage/config.py')
-rw-r--r-- | coverage/config.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/coverage/config.py b/coverage/config.py index 663bc64a..7adcba39 100644 --- a/coverage/config.py +++ b/coverage/config.py @@ -382,3 +382,60 @@ class CoverageConfig(object): # If we get here, we didn't find the option. raise CoverageException("No such option: %r" % option_name) + + +def read_coverage_config(config_file, **kwargs): + """Read configuration, returning a `CoverageConfig` object. + + Arguments: + config_file: a boolean or string, see the `Coverage` class for the + tricky details. + all others: key + + Returns: + config_file, config: + config_file is the value to use for config_file in other + invocations of coverage. + + config is a CoverageConfig object read from the appropriate + configuration file. + + """ + # Build the configuration from a number of sources: + # 1) defaults: + config = CoverageConfig() + + # 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" # pylint: disable=redefined-variable-type + + for fname, prefix in [(config_file, ""), + ("setup.cfg", "coverage:"), + ("tox.ini", "coverage:")]: + config_read = config.from_file(fname, section_prefix=prefix) + 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) + + if config_read: + break + + # 3) from environment variables: + env_data_file = os.environ.get('COVERAGE_FILE') + if env_data_file: + config.data_file = env_data_file + debugs = os.environ.get('COVERAGE_DEBUG') + if debugs: + config.debug.extend(d.strip() for d in debugs.split(",")) + + # 4) from constructor arguments: + config.from_args(**kwargs) + + return config_file, config |