config: Do not require all rule options to be set
Before, it was required to specify all the options when customizing a
rule. For instance, one could use `empty-lines: enable` or `empty-lines:
{max: 1, max-start: 2, max-end: 2}`, but not just `empty-lines: {max:
1}` (it would fail with *invalid config: missing option "max-start" for
rule "empty-lines"*).
This was a minor problem for users, but it prevented the addition of new
options to existing rules, see [1] for an example. If a new option was
added, updating yamllint for all users that customize the rule would
produce a crash (*invalid config: missing option ...*).
To avoid that, let's embed default values inside the rules themselves,
instead of keeping them in `conf/default.yaml`.
This refactor should not have any impact on existing projects. I've
manually checked that it did not change the output of tests, on
different projects:
- ansible/ansible: `test/runner/ansible-test sanity --python 3.7 --test yamllint`
- ansible/molecule: `yamllint -s test/ molecule/`
- Neo23x0/sigma: `make test-yaml`
- markstory/lint-review: `yamllint .`
[1]: https://github.com/adrienverge/yamllint/pull/151
This commit is contained in:
@@ -74,6 +74,11 @@ class YamlLintConfig(object):
|
||||
raise YamlLintConfigError('invalid config: not a dict')
|
||||
|
||||
self.rules = conf.get('rules', {})
|
||||
for rule in self.rules:
|
||||
if self.rules[rule] == 'enable':
|
||||
self.rules[rule] = {}
|
||||
elif self.rules[rule] == 'disable':
|
||||
self.rules[rule] = False
|
||||
|
||||
# Does this conf override another conf that we need to load?
|
||||
if 'extends' in conf:
|
||||
@@ -102,10 +107,8 @@ class YamlLintConfig(object):
|
||||
|
||||
|
||||
def validate_rule_conf(rule, conf):
|
||||
if conf is False or conf == 'disable':
|
||||
if conf is False: # disable
|
||||
return False
|
||||
elif conf == 'enable':
|
||||
conf = {}
|
||||
|
||||
if isinstance(conf, dict):
|
||||
if ('ignore' in conf and
|
||||
@@ -123,6 +126,7 @@ def validate_rule_conf(rule, conf):
|
||||
'invalid config: level should be "error" or "warning"')
|
||||
|
||||
options = getattr(rule, 'CONF', {})
|
||||
options_default = getattr(rule, 'DEFAULT', {})
|
||||
for optkey in conf:
|
||||
if optkey in ('ignore', 'level'):
|
||||
continue
|
||||
@@ -143,9 +147,7 @@ def validate_rule_conf(rule, conf):
|
||||
% (optkey, rule.ID, options[optkey].__name__))
|
||||
for optkey in options:
|
||||
if optkey not in conf:
|
||||
raise YamlLintConfigError(
|
||||
'invalid config: missing option "%s" for rule "%s"' %
|
||||
(optkey, rule.ID))
|
||||
conf[optkey] = options_default[optkey]
|
||||
else:
|
||||
raise YamlLintConfigError(('invalid config: rule "%s": should be '
|
||||
'either "enable", "disable" or a dict')
|
||||
|
||||
Reference in New Issue
Block a user