mirror of
https://github.com/adrienverge/yamllint.git
synced 2025-02-25 18:55:20 -06:00
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:
parent
bc7ac81707
commit
0f073f7a09
@ -21,6 +21,7 @@ except ImportError:
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from tests.common import build_temp_workspace
|
from tests.common import build_temp_workspace
|
||||||
@ -54,13 +55,16 @@ class SimpleConfigTestCase(unittest.TestCase):
|
|||||||
' this-one-does-not-exist: enable\n')
|
' this-one-does-not-exist: enable\n')
|
||||||
|
|
||||||
def test_missing_option(self):
|
def test_missing_option(self):
|
||||||
with self.assertRaisesRegexp(
|
c = config.YamlLintConfig('rules:\n'
|
||||||
config.YamlLintConfigError,
|
' colons: enable\n')
|
||||||
'invalid config: missing option "max-spaces-before" '
|
self.assertEqual(c.rules['colons']['max-spaces-before'], 0)
|
||||||
'for rule "colons"'):
|
self.assertEqual(c.rules['colons']['max-spaces-after'], 1)
|
||||||
config.YamlLintConfig('rules:\n'
|
|
||||||
|
c = config.YamlLintConfig('rules:\n'
|
||||||
' colons:\n'
|
' colons:\n'
|
||||||
' max-spaces-after: 1\n')
|
' max-spaces-before: 9\n')
|
||||||
|
self.assertEqual(c.rules['colons']['max-spaces-before'], 9)
|
||||||
|
self.assertEqual(c.rules['colons']['max-spaces-after'], 1)
|
||||||
|
|
||||||
def test_unknown_option(self):
|
def test_unknown_option(self):
|
||||||
with self.assertRaisesRegexp(
|
with self.assertRaisesRegexp(
|
||||||
@ -111,17 +115,22 @@ class SimpleConfigTestCase(unittest.TestCase):
|
|||||||
' indent-sequences: YES!\n'
|
' indent-sequences: YES!\n'
|
||||||
' check-multi-line-strings: false\n')
|
' check-multi-line-strings: false\n')
|
||||||
|
|
||||||
|
def test_enable_disable_keywords(self):
|
||||||
|
c = config.YamlLintConfig('rules:\n'
|
||||||
|
' colons: enable\n'
|
||||||
|
' hyphens: disable\n')
|
||||||
|
self.assertEqual(c.rules['colons'], {'level': 'error',
|
||||||
|
'max-spaces-after': 1,
|
||||||
|
'max-spaces-before': 0})
|
||||||
|
self.assertEqual(c.rules['hyphens'], False)
|
||||||
|
|
||||||
def test_validate_rule_conf(self):
|
def test_validate_rule_conf(self):
|
||||||
class Rule(object):
|
class Rule(object):
|
||||||
ID = 'fake'
|
ID = 'fake'
|
||||||
|
|
||||||
self.assertFalse(config.validate_rule_conf(Rule, False))
|
self.assertFalse(config.validate_rule_conf(Rule, False))
|
||||||
self.assertFalse(config.validate_rule_conf(Rule, 'disable'))
|
|
||||||
|
|
||||||
self.assertEqual(config.validate_rule_conf(Rule, {}),
|
self.assertEqual(config.validate_rule_conf(Rule, {}),
|
||||||
{'level': 'error'})
|
{'level': 'error'})
|
||||||
self.assertEqual(config.validate_rule_conf(Rule, 'enable'),
|
|
||||||
{'level': 'error'})
|
|
||||||
|
|
||||||
config.validate_rule_conf(Rule, {'level': 'error'})
|
config.validate_rule_conf(Rule, {'level': 'error'})
|
||||||
config.validate_rule_conf(Rule, {'level': 'warning'})
|
config.validate_rule_conf(Rule, {'level': 'warning'})
|
||||||
@ -129,22 +138,22 @@ class SimpleConfigTestCase(unittest.TestCase):
|
|||||||
config.validate_rule_conf, Rule, {'level': 'warn'})
|
config.validate_rule_conf, Rule, {'level': 'warn'})
|
||||||
|
|
||||||
Rule.CONF = {'length': int}
|
Rule.CONF = {'length': int}
|
||||||
|
Rule.DEFAULT = {'length': 80}
|
||||||
config.validate_rule_conf(Rule, {'length': 8})
|
config.validate_rule_conf(Rule, {'length': 8})
|
||||||
self.assertRaises(config.YamlLintConfigError,
|
config.validate_rule_conf(Rule, {})
|
||||||
config.validate_rule_conf, Rule, {})
|
|
||||||
self.assertRaises(config.YamlLintConfigError,
|
self.assertRaises(config.YamlLintConfigError,
|
||||||
config.validate_rule_conf, Rule, {'height': 8})
|
config.validate_rule_conf, Rule, {'height': 8})
|
||||||
|
|
||||||
Rule.CONF = {'a': bool, 'b': int}
|
Rule.CONF = {'a': bool, 'b': int}
|
||||||
|
Rule.DEFAULT = {'a': True, 'b': -42}
|
||||||
config.validate_rule_conf(Rule, {'a': True, 'b': 0})
|
config.validate_rule_conf(Rule, {'a': True, 'b': 0})
|
||||||
self.assertRaises(config.YamlLintConfigError,
|
config.validate_rule_conf(Rule, {'a': True})
|
||||||
config.validate_rule_conf, Rule, {'a': True})
|
config.validate_rule_conf(Rule, {'b': 0})
|
||||||
self.assertRaises(config.YamlLintConfigError,
|
|
||||||
config.validate_rule_conf, Rule, {'b': 0})
|
|
||||||
self.assertRaises(config.YamlLintConfigError,
|
self.assertRaises(config.YamlLintConfigError,
|
||||||
config.validate_rule_conf, Rule, {'a': 1, 'b': 0})
|
config.validate_rule_conf, Rule, {'a': 1, 'b': 0})
|
||||||
|
|
||||||
Rule.CONF = {'choice': (True, 88, 'str')}
|
Rule.CONF = {'choice': (True, 88, 'str')}
|
||||||
|
Rule.DEFAULT = {'choice': 88}
|
||||||
config.validate_rule_conf(Rule, {'choice': True})
|
config.validate_rule_conf(Rule, {'choice': True})
|
||||||
config.validate_rule_conf(Rule, {'choice': 88})
|
config.validate_rule_conf(Rule, {'choice': 88})
|
||||||
config.validate_rule_conf(Rule, {'choice': 'str'})
|
config.validate_rule_conf(Rule, {'choice': 'str'})
|
||||||
@ -156,8 +165,10 @@ class SimpleConfigTestCase(unittest.TestCase):
|
|||||||
config.validate_rule_conf, Rule, {'choice': 'abc'})
|
config.validate_rule_conf, Rule, {'choice': 'abc'})
|
||||||
|
|
||||||
Rule.CONF = {'choice': (int, 'hardcoded')}
|
Rule.CONF = {'choice': (int, 'hardcoded')}
|
||||||
|
Rule.DEFAULT = {'choice': 1337}
|
||||||
config.validate_rule_conf(Rule, {'choice': 42})
|
config.validate_rule_conf(Rule, {'choice': 42})
|
||||||
config.validate_rule_conf(Rule, {'choice': 'hardcoded'})
|
config.validate_rule_conf(Rule, {'choice': 'hardcoded'})
|
||||||
|
config.validate_rule_conf(Rule, {})
|
||||||
self.assertRaises(config.YamlLintConfigError,
|
self.assertRaises(config.YamlLintConfigError,
|
||||||
config.validate_rule_conf, Rule, {'choice': False})
|
config.validate_rule_conf, Rule, {'choice': False})
|
||||||
self.assertRaises(config.YamlLintConfigError,
|
self.assertRaises(config.YamlLintConfigError,
|
||||||
@ -165,7 +176,7 @@ class SimpleConfigTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
|
|
||||||
class ExtendedConfigTestCase(unittest.TestCase):
|
class ExtendedConfigTestCase(unittest.TestCase):
|
||||||
def test_extend_add_rule(self):
|
def test_extend_on_object(self):
|
||||||
old = config.YamlLintConfig('rules:\n'
|
old = config.YamlLintConfig('rules:\n'
|
||||||
' colons:\n'
|
' colons:\n'
|
||||||
' max-spaces-before: 0\n'
|
' max-spaces-before: 0\n'
|
||||||
@ -182,60 +193,130 @@ class ExtendedConfigTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(len(new.enabled_rules(None)), 2)
|
self.assertEqual(len(new.enabled_rules(None)), 2)
|
||||||
|
|
||||||
|
def test_extend_on_file(self):
|
||||||
|
with tempfile.NamedTemporaryFile('w') as f:
|
||||||
|
f.write('rules:\n'
|
||||||
|
' colons:\n'
|
||||||
|
' max-spaces-before: 0\n'
|
||||||
|
' max-spaces-after: 1\n')
|
||||||
|
f.flush()
|
||||||
|
c = config.YamlLintConfig('extends: ' + f.name + '\n'
|
||||||
|
'rules:\n'
|
||||||
|
' hyphens:\n'
|
||||||
|
' max-spaces-after: 2\n')
|
||||||
|
|
||||||
|
self.assertEqual(sorted(c.rules.keys()), ['colons', 'hyphens'])
|
||||||
|
self.assertEqual(c.rules['colons']['max-spaces-before'], 0)
|
||||||
|
self.assertEqual(c.rules['colons']['max-spaces-after'], 1)
|
||||||
|
self.assertEqual(c.rules['hyphens']['max-spaces-after'], 2)
|
||||||
|
|
||||||
|
self.assertEqual(len(c.enabled_rules(None)), 2)
|
||||||
|
|
||||||
def test_extend_remove_rule(self):
|
def test_extend_remove_rule(self):
|
||||||
old = config.YamlLintConfig('rules:\n'
|
with tempfile.NamedTemporaryFile('w') as f:
|
||||||
' colons:\n'
|
f.write('rules:\n'
|
||||||
' max-spaces-before: 0\n'
|
' colons:\n'
|
||||||
' max-spaces-after: 1\n'
|
' max-spaces-before: 0\n'
|
||||||
' hyphens:\n'
|
' max-spaces-after: 1\n'
|
||||||
' max-spaces-after: 2\n')
|
' hyphens:\n'
|
||||||
new = config.YamlLintConfig('rules:\n'
|
' max-spaces-after: 2\n')
|
||||||
' colons: disable\n')
|
f.flush()
|
||||||
new.extend(old)
|
c = config.YamlLintConfig('extends: ' + f.name + '\n'
|
||||||
|
'rules:\n'
|
||||||
|
' colons: disable\n')
|
||||||
|
|
||||||
self.assertEqual(sorted(new.rules.keys()), ['colons', 'hyphens'])
|
self.assertEqual(sorted(c.rules.keys()), ['colons', 'hyphens'])
|
||||||
self.assertFalse(new.rules['colons'])
|
self.assertFalse(c.rules['colons'])
|
||||||
self.assertEqual(new.rules['hyphens']['max-spaces-after'], 2)
|
self.assertEqual(c.rules['hyphens']['max-spaces-after'], 2)
|
||||||
|
|
||||||
self.assertEqual(len(new.enabled_rules(None)), 1)
|
self.assertEqual(len(c.enabled_rules(None)), 1)
|
||||||
|
|
||||||
def test_extend_edit_rule(self):
|
def test_extend_edit_rule(self):
|
||||||
old = config.YamlLintConfig('rules:\n'
|
with tempfile.NamedTemporaryFile('w') as f:
|
||||||
' colons:\n'
|
f.write('rules:\n'
|
||||||
' max-spaces-before: 0\n'
|
' colons:\n'
|
||||||
' max-spaces-after: 1\n'
|
' max-spaces-before: 0\n'
|
||||||
' hyphens:\n'
|
' max-spaces-after: 1\n'
|
||||||
' max-spaces-after: 2\n')
|
' hyphens:\n'
|
||||||
new = config.YamlLintConfig('rules:\n'
|
' max-spaces-after: 2\n')
|
||||||
' colons:\n'
|
f.flush()
|
||||||
' max-spaces-before: 3\n'
|
c = config.YamlLintConfig('extends: ' + f.name + '\n'
|
||||||
' max-spaces-after: 4\n')
|
'rules:\n'
|
||||||
new.extend(old)
|
' colons:\n'
|
||||||
|
' max-spaces-before: 3\n'
|
||||||
|
' max-spaces-after: 4\n')
|
||||||
|
|
||||||
self.assertEqual(sorted(new.rules.keys()), ['colons', 'hyphens'])
|
self.assertEqual(sorted(c.rules.keys()), ['colons', 'hyphens'])
|
||||||
self.assertEqual(new.rules['colons']['max-spaces-before'], 3)
|
self.assertEqual(c.rules['colons']['max-spaces-before'], 3)
|
||||||
self.assertEqual(new.rules['colons']['max-spaces-after'], 4)
|
self.assertEqual(c.rules['colons']['max-spaces-after'], 4)
|
||||||
self.assertEqual(new.rules['hyphens']['max-spaces-after'], 2)
|
self.assertEqual(c.rules['hyphens']['max-spaces-after'], 2)
|
||||||
|
|
||||||
self.assertEqual(len(new.enabled_rules(None)), 2)
|
self.assertEqual(len(c.enabled_rules(None)), 2)
|
||||||
|
|
||||||
def test_extend_reenable_rule(self):
|
def test_extend_reenable_rule(self):
|
||||||
old = config.YamlLintConfig('rules:\n'
|
with tempfile.NamedTemporaryFile('w') as f:
|
||||||
' colons:\n'
|
f.write('rules:\n'
|
||||||
' max-spaces-before: 0\n'
|
' colons:\n'
|
||||||
' max-spaces-after: 1\n'
|
' max-spaces-before: 0\n'
|
||||||
' hyphens: disable\n')
|
' max-spaces-after: 1\n'
|
||||||
new = config.YamlLintConfig('rules:\n'
|
' hyphens: disable\n')
|
||||||
' hyphens:\n'
|
f.flush()
|
||||||
' max-spaces-after: 2\n')
|
c = config.YamlLintConfig('extends: ' + f.name + '\n'
|
||||||
new.extend(old)
|
'rules:\n'
|
||||||
|
' hyphens:\n'
|
||||||
|
' max-spaces-after: 2\n')
|
||||||
|
|
||||||
self.assertEqual(sorted(new.rules.keys()), ['colons', 'hyphens'])
|
self.assertEqual(sorted(c.rules.keys()), ['colons', 'hyphens'])
|
||||||
self.assertEqual(new.rules['colons']['max-spaces-before'], 0)
|
self.assertEqual(c.rules['colons']['max-spaces-before'], 0)
|
||||||
self.assertEqual(new.rules['colons']['max-spaces-after'], 1)
|
self.assertEqual(c.rules['colons']['max-spaces-after'], 1)
|
||||||
self.assertEqual(new.rules['hyphens']['max-spaces-after'], 2)
|
self.assertEqual(c.rules['hyphens']['max-spaces-after'], 2)
|
||||||
|
|
||||||
self.assertEqual(len(new.enabled_rules(None)), 2)
|
self.assertEqual(len(c.enabled_rules(None)), 2)
|
||||||
|
|
||||||
|
def test_extend_recursive_default_values(self):
|
||||||
|
with tempfile.NamedTemporaryFile('w') as f:
|
||||||
|
f.write('rules:\n'
|
||||||
|
' braces:\n'
|
||||||
|
' max-spaces-inside: 1248\n')
|
||||||
|
f.flush()
|
||||||
|
c = config.YamlLintConfig('extends: ' + f.name + '\n'
|
||||||
|
'rules:\n'
|
||||||
|
' braces:\n'
|
||||||
|
' min-spaces-inside-empty: 2357\n')
|
||||||
|
|
||||||
|
self.assertEqual(c.rules['braces']['min-spaces-inside'], 0)
|
||||||
|
self.assertEqual(c.rules['braces']['max-spaces-inside'], 1248)
|
||||||
|
self.assertEqual(c.rules['braces']['min-spaces-inside-empty'], 2357)
|
||||||
|
self.assertEqual(c.rules['braces']['max-spaces-inside-empty'], -1)
|
||||||
|
|
||||||
|
with tempfile.NamedTemporaryFile('w') as f:
|
||||||
|
f.write('rules:\n'
|
||||||
|
' colons:\n'
|
||||||
|
' max-spaces-before: 1337\n')
|
||||||
|
f.flush()
|
||||||
|
c = config.YamlLintConfig('extends: ' + f.name + '\n'
|
||||||
|
'rules:\n'
|
||||||
|
' colons: enable\n')
|
||||||
|
|
||||||
|
self.assertEqual(c.rules['colons']['max-spaces-before'], 1337)
|
||||||
|
self.assertEqual(c.rules['colons']['max-spaces-after'], 1)
|
||||||
|
|
||||||
|
with tempfile.NamedTemporaryFile('w') as f1, \
|
||||||
|
tempfile.NamedTemporaryFile('w') as f2:
|
||||||
|
f1.write('rules:\n'
|
||||||
|
' colons:\n'
|
||||||
|
' max-spaces-before: 1337\n')
|
||||||
|
f1.flush()
|
||||||
|
f2.write('extends: ' + f1.name + '\n'
|
||||||
|
'rules:\n'
|
||||||
|
' colons: disable\n')
|
||||||
|
f2.flush()
|
||||||
|
c = config.YamlLintConfig('extends: ' + f2.name + '\n'
|
||||||
|
'rules:\n'
|
||||||
|
' colons: enable\n')
|
||||||
|
|
||||||
|
self.assertEqual(c.rules['colons']['max-spaces-before'], 0)
|
||||||
|
self.assertEqual(c.rules['colons']['max-spaces-after'], 1)
|
||||||
|
|
||||||
|
|
||||||
class ExtendedLibraryConfigTestCase(unittest.TestCase):
|
class ExtendedLibraryConfigTestCase(unittest.TestCase):
|
||||||
@ -267,6 +348,9 @@ class ExtendedLibraryConfigTestCase(unittest.TestCase):
|
|||||||
self.assertEqual(sorted(new.rules.keys()), sorted(old.rules.keys()))
|
self.assertEqual(sorted(new.rules.keys()), sorted(old.rules.keys()))
|
||||||
for rule in new.rules:
|
for rule in new.rules:
|
||||||
self.assertEqual(new.rules[rule], old.rules[rule])
|
self.assertEqual(new.rules[rule], old.rules[rule])
|
||||||
|
self.assertEqual(new.rules['empty-lines']['max'], 42)
|
||||||
|
self.assertEqual(new.rules['empty-lines']['max-start'], 43)
|
||||||
|
self.assertEqual(new.rules['empty-lines']['max-end'], 44)
|
||||||
|
|
||||||
def test_extend_config_override_rule_partly(self):
|
def test_extend_config_override_rule_partly(self):
|
||||||
old = config.YamlLintConfig('extends: default')
|
old = config.YamlLintConfig('extends: default')
|
||||||
@ -280,6 +364,9 @@ class ExtendedLibraryConfigTestCase(unittest.TestCase):
|
|||||||
self.assertEqual(sorted(new.rules.keys()), sorted(old.rules.keys()))
|
self.assertEqual(sorted(new.rules.keys()), sorted(old.rules.keys()))
|
||||||
for rule in new.rules:
|
for rule in new.rules:
|
||||||
self.assertEqual(new.rules[rule], old.rules[rule])
|
self.assertEqual(new.rules[rule], old.rules[rule])
|
||||||
|
self.assertEqual(new.rules['empty-lines']['max'], 2)
|
||||||
|
self.assertEqual(new.rules['empty-lines']['max-start'], 42)
|
||||||
|
self.assertEqual(new.rules['empty-lines']['max-end'], 0)
|
||||||
|
|
||||||
|
|
||||||
class IgnorePathConfigTestCase(unittest.TestCase):
|
class IgnorePathConfigTestCase(unittest.TestCase):
|
||||||
|
@ -1,59 +1,28 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
rules:
|
rules:
|
||||||
braces:
|
braces: enable
|
||||||
min-spaces-inside: 0
|
brackets: enable
|
||||||
max-spaces-inside: 0
|
colons: enable
|
||||||
min-spaces-inside-empty: -1
|
commas: enable
|
||||||
max-spaces-inside-empty: -1
|
|
||||||
brackets:
|
|
||||||
min-spaces-inside: 0
|
|
||||||
max-spaces-inside: 0
|
|
||||||
min-spaces-inside-empty: -1
|
|
||||||
max-spaces-inside-empty: -1
|
|
||||||
colons:
|
|
||||||
max-spaces-before: 0
|
|
||||||
max-spaces-after: 1
|
|
||||||
commas:
|
|
||||||
max-spaces-before: 0
|
|
||||||
min-spaces-after: 1
|
|
||||||
max-spaces-after: 1
|
|
||||||
comments:
|
comments:
|
||||||
level: warning
|
level: warning
|
||||||
require-starting-space: true
|
|
||||||
min-spaces-from-content: 2
|
|
||||||
comments-indentation:
|
comments-indentation:
|
||||||
level: warning
|
level: warning
|
||||||
document-end: disable
|
document-end: disable
|
||||||
document-start:
|
document-start:
|
||||||
level: warning
|
level: warning
|
||||||
present: true
|
empty-lines: enable
|
||||||
empty-lines:
|
empty-values: enable
|
||||||
max: 2
|
hyphens: enable
|
||||||
max-start: 0
|
indentation: enable
|
||||||
max-end: 0
|
|
||||||
quoted-strings: disable
|
|
||||||
empty-values:
|
|
||||||
forbid-in-block-mappings: false
|
|
||||||
forbid-in-flow-mappings: false
|
|
||||||
hyphens:
|
|
||||||
max-spaces-after: 1
|
|
||||||
indentation:
|
|
||||||
spaces: consistent
|
|
||||||
indent-sequences: true
|
|
||||||
check-multi-line-strings: false
|
|
||||||
key-duplicates: enable
|
key-duplicates: enable
|
||||||
key-ordering: disable
|
key-ordering: disable
|
||||||
line-length:
|
line-length: enable
|
||||||
max: 80
|
|
||||||
allow-non-breakable-words: true
|
|
||||||
allow-non-breakable-inline-mappings: false
|
|
||||||
new-line-at-end-of-file: enable
|
new-line-at-end-of-file: enable
|
||||||
new-lines:
|
new-lines: enable
|
||||||
type: unix
|
octal-values: enable
|
||||||
octal-values:
|
quoted-strings: disable
|
||||||
forbid-implicit-octal: false
|
|
||||||
forbid-explicit-octal: false
|
|
||||||
trailing-spaces: enable
|
trailing-spaces: enable
|
||||||
truthy:
|
truthy:
|
||||||
level: warning
|
level: warning
|
||||||
|
@ -74,6 +74,11 @@ class YamlLintConfig(object):
|
|||||||
raise YamlLintConfigError('invalid config: not a dict')
|
raise YamlLintConfigError('invalid config: not a dict')
|
||||||
|
|
||||||
self.rules = conf.get('rules', {})
|
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?
|
# Does this conf override another conf that we need to load?
|
||||||
if 'extends' in conf:
|
if 'extends' in conf:
|
||||||
@ -102,10 +107,8 @@ class YamlLintConfig(object):
|
|||||||
|
|
||||||
|
|
||||||
def validate_rule_conf(rule, conf):
|
def validate_rule_conf(rule, conf):
|
||||||
if conf is False or conf == 'disable':
|
if conf is False: # disable
|
||||||
return False
|
return False
|
||||||
elif conf == 'enable':
|
|
||||||
conf = {}
|
|
||||||
|
|
||||||
if isinstance(conf, dict):
|
if isinstance(conf, dict):
|
||||||
if ('ignore' in conf and
|
if ('ignore' in conf and
|
||||||
@ -123,6 +126,7 @@ def validate_rule_conf(rule, conf):
|
|||||||
'invalid config: level should be "error" or "warning"')
|
'invalid config: level should be "error" or "warning"')
|
||||||
|
|
||||||
options = getattr(rule, 'CONF', {})
|
options = getattr(rule, 'CONF', {})
|
||||||
|
options_default = getattr(rule, 'DEFAULT', {})
|
||||||
for optkey in conf:
|
for optkey in conf:
|
||||||
if optkey in ('ignore', 'level'):
|
if optkey in ('ignore', 'level'):
|
||||||
continue
|
continue
|
||||||
@ -143,9 +147,7 @@ def validate_rule_conf(rule, conf):
|
|||||||
% (optkey, rule.ID, options[optkey].__name__))
|
% (optkey, rule.ID, options[optkey].__name__))
|
||||||
for optkey in options:
|
for optkey in options:
|
||||||
if optkey not in conf:
|
if optkey not in conf:
|
||||||
raise YamlLintConfigError(
|
conf[optkey] = options_default[optkey]
|
||||||
'invalid config: missing option "%s" for rule "%s"' %
|
|
||||||
(optkey, rule.ID))
|
|
||||||
else:
|
else:
|
||||||
raise YamlLintConfigError(('invalid config: rule "%s": should be '
|
raise YamlLintConfigError(('invalid config: rule "%s": should be '
|
||||||
'either "enable", "disable" or a dict')
|
'either "enable", "disable" or a dict')
|
||||||
|
@ -101,6 +101,10 @@ CONF = {'min-spaces-inside': int,
|
|||||||
'max-spaces-inside': int,
|
'max-spaces-inside': int,
|
||||||
'min-spaces-inside-empty': int,
|
'min-spaces-inside-empty': int,
|
||||||
'max-spaces-inside-empty': int}
|
'max-spaces-inside-empty': int}
|
||||||
|
DEFAULT = {'min-spaces-inside': 0,
|
||||||
|
'max-spaces-inside': 0,
|
||||||
|
'min-spaces-inside-empty': -1,
|
||||||
|
'max-spaces-inside-empty': -1}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
@ -102,6 +102,10 @@ CONF = {'min-spaces-inside': int,
|
|||||||
'max-spaces-inside': int,
|
'max-spaces-inside': int,
|
||||||
'min-spaces-inside-empty': int,
|
'min-spaces-inside-empty': int,
|
||||||
'max-spaces-inside-empty': int}
|
'max-spaces-inside-empty': int}
|
||||||
|
DEFAULT = {'min-spaces-inside': 0,
|
||||||
|
'max-spaces-inside': 0,
|
||||||
|
'min-spaces-inside-empty': -1,
|
||||||
|
'max-spaces-inside-empty': -1}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
@ -79,6 +79,8 @@ ID = 'colons'
|
|||||||
TYPE = 'token'
|
TYPE = 'token'
|
||||||
CONF = {'max-spaces-before': int,
|
CONF = {'max-spaces-before': int,
|
||||||
'max-spaces-after': int}
|
'max-spaces-after': int}
|
||||||
|
DEFAULT = {'max-spaces-before': 0,
|
||||||
|
'max-spaces-after': 1}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
@ -103,6 +103,9 @@ TYPE = 'token'
|
|||||||
CONF = {'max-spaces-before': int,
|
CONF = {'max-spaces-before': int,
|
||||||
'min-spaces-after': int,
|
'min-spaces-after': int,
|
||||||
'max-spaces-after': int}
|
'max-spaces-after': int}
|
||||||
|
DEFAULT = {'max-spaces-before': 0,
|
||||||
|
'min-spaces-after': 1,
|
||||||
|
'max-spaces-after': 1}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
@ -68,6 +68,8 @@ ID = 'comments'
|
|||||||
TYPE = 'comment'
|
TYPE = 'comment'
|
||||||
CONF = {'require-starting-space': bool,
|
CONF = {'require-starting-space': bool,
|
||||||
'min-spaces-from-content': int}
|
'min-spaces-from-content': int}
|
||||||
|
DEFAULT = {'require-starting-space': True,
|
||||||
|
'min-spaces-from-content': 2}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, comment):
|
def check(conf, comment):
|
||||||
|
@ -82,6 +82,7 @@ from yamllint.linter import LintProblem
|
|||||||
ID = 'document-end'
|
ID = 'document-end'
|
||||||
TYPE = 'token'
|
TYPE = 'token'
|
||||||
CONF = {'present': bool}
|
CONF = {'present': bool}
|
||||||
|
DEFAULT = {'present': True}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
@ -72,6 +72,7 @@ from yamllint.linter import LintProblem
|
|||||||
ID = 'document-start'
|
ID = 'document-start'
|
||||||
TYPE = 'token'
|
TYPE = 'token'
|
||||||
CONF = {'present': bool}
|
CONF = {'present': bool}
|
||||||
|
DEFAULT = {'present': True}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
@ -58,6 +58,9 @@ TYPE = 'line'
|
|||||||
CONF = {'max': int,
|
CONF = {'max': int,
|
||||||
'max-start': int,
|
'max-start': int,
|
||||||
'max-end': int}
|
'max-end': int}
|
||||||
|
DEFAULT = {'max': 2,
|
||||||
|
'max-start': 0,
|
||||||
|
'max-end': 0}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, line):
|
def check(conf, line):
|
||||||
|
@ -75,6 +75,8 @@ ID = 'empty-values'
|
|||||||
TYPE = 'token'
|
TYPE = 'token'
|
||||||
CONF = {'forbid-in-block-mappings': bool,
|
CONF = {'forbid-in-block-mappings': bool,
|
||||||
'forbid-in-flow-mappings': bool}
|
'forbid-in-flow-mappings': bool}
|
||||||
|
DEFAULT = {'forbid-in-block-mappings': False,
|
||||||
|
'forbid-in-flow-mappings': False}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
@ -76,6 +76,7 @@ from yamllint.rules.common import spaces_after
|
|||||||
ID = 'hyphens'
|
ID = 'hyphens'
|
||||||
TYPE = 'token'
|
TYPE = 'token'
|
||||||
CONF = {'max-spaces-after': int}
|
CONF = {'max-spaces-after': int}
|
||||||
|
DEFAULT = {'max-spaces-after': 1}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
@ -201,6 +201,9 @@ TYPE = 'token'
|
|||||||
CONF = {'spaces': (int, 'consistent'),
|
CONF = {'spaces': (int, 'consistent'),
|
||||||
'indent-sequences': (bool, 'whatever', 'consistent'),
|
'indent-sequences': (bool, 'whatever', 'consistent'),
|
||||||
'check-multi-line-strings': bool}
|
'check-multi-line-strings': bool}
|
||||||
|
DEFAULT = {'spaces': 'consistent',
|
||||||
|
'indent-sequences': True,
|
||||||
|
'check-multi-line-strings': False}
|
||||||
|
|
||||||
ROOT, B_MAP, F_MAP, B_SEQ, F_SEQ, B_ENT, KEY, VAL = range(8)
|
ROOT, B_MAP, F_MAP, B_SEQ, F_SEQ, B_ENT, KEY, VAL = range(8)
|
||||||
labels = ('ROOT', 'B_MAP', 'F_MAP', 'B_SEQ', 'F_SEQ', 'B_ENT', 'KEY', 'VAL')
|
labels = ('ROOT', 'B_MAP', 'F_MAP', 'B_SEQ', 'F_SEQ', 'B_ENT', 'KEY', 'VAL')
|
||||||
|
@ -61,7 +61,6 @@ from yamllint.linter import LintProblem
|
|||||||
|
|
||||||
ID = 'key-duplicates'
|
ID = 'key-duplicates'
|
||||||
TYPE = 'token'
|
TYPE = 'token'
|
||||||
CONF = {}
|
|
||||||
|
|
||||||
MAP, SEQ = range(2)
|
MAP, SEQ = range(2)
|
||||||
|
|
||||||
|
@ -72,7 +72,6 @@ from yamllint.linter import LintProblem
|
|||||||
|
|
||||||
ID = 'key-ordering'
|
ID = 'key-ordering'
|
||||||
TYPE = 'token'
|
TYPE = 'token'
|
||||||
CONF = {}
|
|
||||||
|
|
||||||
MAP, SEQ = range(2)
|
MAP, SEQ = range(2)
|
||||||
|
|
||||||
|
@ -102,6 +102,9 @@ TYPE = 'line'
|
|||||||
CONF = {'max': int,
|
CONF = {'max': int,
|
||||||
'allow-non-breakable-words': bool,
|
'allow-non-breakable-words': bool,
|
||||||
'allow-non-breakable-inline-mappings': bool}
|
'allow-non-breakable-inline-mappings': bool}
|
||||||
|
DEFAULT = {'max': 80,
|
||||||
|
'allow-non-breakable-words': True,
|
||||||
|
'allow-non-breakable-inline-mappings': False}
|
||||||
|
|
||||||
|
|
||||||
def check_inline_mapping(line):
|
def check_inline_mapping(line):
|
||||||
|
@ -30,6 +30,7 @@ from yamllint.linter import LintProblem
|
|||||||
ID = 'new-lines'
|
ID = 'new-lines'
|
||||||
TYPE = 'line'
|
TYPE = 'line'
|
||||||
CONF = {'type': ('unix', 'dos')}
|
CONF = {'type': ('unix', 'dos')}
|
||||||
|
DEFAULT = {'type': 'unix'}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, line):
|
def check(conf, line):
|
||||||
|
@ -66,6 +66,8 @@ ID = 'octal-values'
|
|||||||
TYPE = 'token'
|
TYPE = 'token'
|
||||||
CONF = {'forbid-implicit-octal': bool,
|
CONF = {'forbid-implicit-octal': bool,
|
||||||
'forbid-explicit-octal': bool}
|
'forbid-explicit-octal': bool}
|
||||||
|
DEFAULT = {'forbid-implicit-octal': False,
|
||||||
|
'forbid-explicit-octal': False}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
@ -46,6 +46,7 @@ from yamllint.linter import LintProblem
|
|||||||
ID = 'quoted-strings'
|
ID = 'quoted-strings'
|
||||||
TYPE = 'token'
|
TYPE = 'token'
|
||||||
CONF = {'quote-type': ('any', 'single', 'double')}
|
CONF = {'quote-type': ('any', 'single', 'double')}
|
||||||
|
DEFAULT = {'quote-type': 'any'}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
@ -71,7 +71,6 @@ from yamllint.linter import LintProblem
|
|||||||
|
|
||||||
ID = 'truthy'
|
ID = 'truthy'
|
||||||
TYPE = 'token'
|
TYPE = 'token'
|
||||||
CONF = {}
|
|
||||||
|
|
||||||
TRUTHY = ['YES', 'Yes', 'yes',
|
TRUTHY = ['YES', 'Yes', 'yes',
|
||||||
'NO', 'No', 'no',
|
'NO', 'No', 'no',
|
||||||
|
Loading…
Reference in New Issue
Block a user