summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2014-10-11 22:12:28 -0400
committerNed Batchelder <ned@nedbatchelder.com>2014-10-11 22:12:28 -0400
commit24b8bf63dc7d0fbf6abc3918892c3c2022342b45 (patch)
treefe8deeda84de81aa113b8c66ea2b01e7c58c0e55
parent325906b1e80d65d400eaf4c6f928f662b6d114f8 (diff)
downloadpython-coveragepy-24b8bf63dc7d0fbf6abc3918892c3c2022342b45.tar.gz
Config tweaking applies to plugin options also
-rw-r--r--coverage/config.py29
-rw-r--r--tests/test_config.py26
2 files changed, 51 insertions, 4 deletions
diff --git a/coverage/config.py b/coverage/config.py
index f262a04..4d599ee 100644
--- a/coverage/config.py
+++ b/coverage/config.py
@@ -2,6 +2,8 @@
import os, re, sys
from coverage.backward import string_class, iitems
+from coverage.misc import CoverageException
+
# In py3, # ConfigParser was renamed to the more-standard configparser
try:
@@ -268,16 +270,35 @@ class CoverageConfig(object):
return self.plugin_options.get(plugin, {})
# TODO: docs for this.
- def __setitem__(self, names, value):
+ def __setitem__(self, option_name, value):
+ # Check all the hard-coded options.
for option_spec in self.CONFIG_FILE_OPTIONS:
attr, where = option_spec[:2]
- if where == names:
+ if where == option_name:
setattr(self, attr, value)
return
+ # See if it's a plugin option.
+ plugin_name, _, key = option_name.partition(":")
+ if key and plugin_name in self.plugins:
+ self.plugin_options.setdefault(plugin_name, {})[key] = value
+ return
+
+ # If we get here, we didn't find the option.
+ raise CoverageException("No such option: %r" % option_name)
+
# TODO: docs for this.
- def __getitem__(self, names):
+ def __getitem__(self, option_name):
+ # Check all the hard-coded options.
for option_spec in self.CONFIG_FILE_OPTIONS:
attr, where = option_spec[:2]
- if where == names:
+ if where == option_name:
return getattr(self, attr)
+
+ # See if it's a plugin option.
+ plugin_name, _, key = option_name.partition(":")
+ if key and plugin_name in self.plugins:
+ return self.plugin_options.get(plugin_name, {}).get(key)
+
+ # If we get here, we didn't find the option.
+ raise CoverageException("No such option: %r" % option_name)
diff --git a/tests/test_config.py b/tests/test_config.py
index 109a2fb..232d228 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -136,6 +136,32 @@ class ConfigTest(CoverageTest):
self.assertFalse(cov.config["run:branch"])
self.assertEqual(cov.config["run:data_file"], "fooey.dat")
+ def test_tweak_error_checking(self):
+ # Trying to set an unknown config value raises an error.
+ cov = coverage.coverage()
+ with self.assertRaises(CoverageException):
+ cov.config["run:xyzzy"] = 12
+ with self.assertRaises(CoverageException):
+ cov.config["xyzzy:foo"] = 12
+ with self.assertRaises(CoverageException):
+ _ = cov.config["run:xyzzy"]
+ with self.assertRaises(CoverageException):
+ _ = cov.config["xyzzy:foo"]
+
+ def test_tweak_plugin_options(self):
+ # Plugin options have a more flexible syntax.
+ cov = coverage.coverage()
+ cov.config["run:plugins"] = ["fooey.plugin", "xyzzy.coverage.plugin"]
+ cov.config["fooey.plugin:xyzzy"] = 17
+ cov.config["xyzzy.coverage.plugin:plugh"] = ["a", "b"]
+ with self.assertRaises(CoverageException):
+ cov.config["no_such.plugin:foo"] = 23
+
+ self.assertEqual(cov.config["fooey.plugin:xyzzy"], 17)
+ self.assertEqual(cov.config["xyzzy.coverage.plugin:plugh"], ["a", "b"])
+ with self.assertRaises(CoverageException):
+ _ = cov.config["no_such.plugin:foo"]
+
class ConfigFileTest(CoverageTest):
"""Tests of the config file settings in particular."""