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 | 52f7bb335ce25063cd33a91c4f9e757adb18e99c (patch) | |
tree | f7bbaa4cd4f31823be6a2c262ee5fdcff5d1e6d7 /coverage/config.py | |
parent | 8c19329691ba161715c8f7b464ff1f080f3e3671 (diff) | |
download | python-coveragepy-52f7bb335ce25063cd33a91c4f9e757adb18e99c.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 663bc64..7adcba3 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 |