mirror of
https://github.com/adrienverge/yamllint.git
synced 2025-02-16 13:04:45 -06:00
Config: Allow types in multiple choices
For instance, allow rules with: CONF = {'choice': (int, 'hardcoded-string'), 'string-or-bool': (str, bool)}
This commit is contained in:
parent
adefe38a0d
commit
fa420499c7
@ -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):
|
||||
|
@ -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]))
|
||||
|
Loading…
Reference in New Issue
Block a user