From fa420499c72aaac4126439b75120511b5638a4af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Fri, 4 Mar 2016 17:40:06 +0100 Subject: [PATCH] Config: Allow types in multiple choices For instance, allow rules with: CONF = {'choice': (int, 'hardcoded-string'), 'string-or-bool': (str, bool)} --- tests/test_config.py | 47 ++++++++++++++++++++++++++++++++++++++++++++ yamllint/config.py | 3 ++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/tests/test_config.py b/tests/test_config.py index 21d533d..92336d6 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -58,6 +58,53 @@ class SimpleConfigTestCase(unittest.TestCase): ' max-spaces-after: 1\n' ' abcdef: yes\n') + def test_validate_rule_conf(self): + class Rule(object): + ID = 'fake' + + self.assertEqual(config.validate_rule_conf(Rule, False), False) + self.assertEqual(config.validate_rule_conf(Rule, 'disable'), False) + + config.validate_rule_conf(Rule, {'level': 'error'}) + config.validate_rule_conf(Rule, {'level': 'warning'}) + self.assertRaises(config.YamlLintConfigError, + config.validate_rule_conf, Rule, {'level': 'warn'}) + + Rule.CONF = {'length': int} + config.validate_rule_conf(Rule, {'length': 8}) + self.assertRaises(config.YamlLintConfigError, + config.validate_rule_conf, Rule, {}) + self.assertRaises(config.YamlLintConfigError, + config.validate_rule_conf, Rule, {'height': 8}) + + Rule.CONF = {'a': bool, 'b': int} + config.validate_rule_conf(Rule, {'a': True, 'b': 0}) + self.assertRaises(config.YamlLintConfigError, + config.validate_rule_conf, Rule, {'a': True}) + self.assertRaises(config.YamlLintConfigError, + config.validate_rule_conf, Rule, {'b': 0}) + self.assertRaises(config.YamlLintConfigError, + config.validate_rule_conf, Rule, {'a': 1, 'b': 0}) + + Rule.CONF = {'choice': (True, 88, 'str')} + config.validate_rule_conf(Rule, {'choice': True}) + config.validate_rule_conf(Rule, {'choice': 88}) + config.validate_rule_conf(Rule, {'choice': 'str'}) + self.assertRaises(config.YamlLintConfigError, + config.validate_rule_conf, Rule, {'choice': False}) + self.assertRaises(config.YamlLintConfigError, + config.validate_rule_conf, Rule, {'choice': 99}) + self.assertRaises(config.YamlLintConfigError, + config.validate_rule_conf, Rule, {'choice': 'abc'}) + + Rule.CONF = {'choice': (int, 'hardcoded')} + config.validate_rule_conf(Rule, {'choice': 42}) + config.validate_rule_conf(Rule, {'choice': 'hardcoded'}) + self.assertRaises(config.YamlLintConfigError, + config.validate_rule_conf, Rule, {'choice': False}) + self.assertRaises(config.YamlLintConfigError, + config.validate_rule_conf, Rule, {'choice': 'abc'}) + class ExtendedConfigTestCase(unittest.TestCase): def test_extend_add_rule(self): diff --git a/yamllint/config.py b/yamllint/config.py index 0814dfd..a558654 100644 --- a/yamllint/config.py +++ b/yamllint/config.py @@ -100,7 +100,8 @@ def validate_rule_conf(rule, conf): 'invalid config: unknown option "%s" for rule "%s"' % (optkey, rule.ID)) if type(options[optkey]) == tuple: - if conf[optkey] not in options[optkey]: + if (conf[optkey] not in options[optkey] and + type(conf[optkey]) not in options[optkey]): raise YamlLintConfigError( 'invalid config: option "%s" of "%s" should be in %s' % (optkey, rule.ID, options[optkey]))