diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2018-04-19 07:01:14 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2018-04-19 07:01:14 -0400 |
commit | c1f14311c417483b8d92c7d998f6ab0a71229f9d (patch) | |
tree | f63c24b09fd147e2b8a097acd1b74fce9087a1cd | |
parent | 752ff804305d84ca12dbfc07211d27c5026f1682 (diff) | |
download | python-coveragepy-c1f14311c417483b8d92c7d998f6ab0a71229f9d.tar.gz |
COVERAGE_RCFILE can specify the config file location. #650
-rw-r--r-- | CHANGES.rst | 5 | ||||
-rw-r--r-- | coverage/config.py | 8 | ||||
-rw-r--r-- | tests/test_config.py | 15 |
3 files changed, 27 insertions, 1 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index ced7026..85db3b2 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -17,10 +17,15 @@ Change history for Coverage.py Unreleased (might become 4.6) ----------------------------- +- The location of the configuration file can now be specified with a + `COVERAGE_RCFILE` environment variable, as requested in `issue 650`_. + - A new warning (already-imported) is issued if measurable files have already been imported before coverage.py started measurement. See :ref:`cmd_warnings` for more information. +.. _issue 650: https://bitbucket.org/ned/coveragepy/issues/650/allow-setting-configuration-file-location + .. _changes_451: diff --git a/coverage/config.py b/coverage/config.py index 260dafd..285cb21 100644 --- a/coverage/config.py +++ b/coverage/config.py @@ -447,6 +447,12 @@ def config_files_to_try(config_file): config_file = True specified_file = (config_file is not True) if not specified_file: + # No file was specified. Check COVERAGE_RCFILE. + config_file = os.environ.get('COVERAGE_RCFILE') + if config_file: + specified_file = True + if not specified_file: + # Still no file specified. Default to .coveragerc config_file = ".coveragerc" files_to_try = [ (config_file, True, specified_file), @@ -483,7 +489,7 @@ def read_coverage_config(config_file, **kwargs): config_read = config.from_file(fname, our_file=our_file) if config_read: break - if our_file and specified_file: + if specified_file: raise CoverageException("Couldn't read '%s' as a config file" % fname) # 3) from environment variables: diff --git a/tests/test_config.py b/tests/test_config.py index 0b4d40b..bbfa467 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -103,6 +103,21 @@ class ConfigTest(CoverageTest): cov = coverage.Coverage() self.assertEqual(cov.config.debug, ["dataio", "pids", "callers", "fooey"]) + def test_rcfile_from_environment(self): + self.make_file("here.ini", """\ + [run] + data_file = overthere.dat + """) + self.set_environ("COVERAGE_RCFILE", "here.ini") + cov = coverage.Coverage() + self.assertEqual(cov.config.data_file, "overthere.dat") + + def test_missing_rcfile_from_environment(self): + self.set_environ("COVERAGE_RCFILE", "nowhere.ini") + msg = "Couldn't read 'nowhere.ini' as a config file" + with self.assertRaisesRegex(CoverageException, msg): + coverage.Coverage() + def test_parse_errors(self): # Im-parsable values raise CoverageException, with details. bad_configs_and_msgs = [ |