From 8fca8a7a333487655ca22b6e34070e12e40c6526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Sun, 6 Mar 2016 07:44:32 +0100 Subject: [PATCH] Config: Allow 'enable' keyword for rules In the same manner as 'disable', 'enable' allows setting a rule on without worrying about its options. --- tests/rules/test_comments_indentation.py | 8 ++++---- tests/rules/test_key_duplicates.py | 4 ++-- tests/rules/test_new_line_at_end_of_file.py | 2 +- tests/rules/test_trailing_spaces.py | 4 ++-- tests/test_config.py | 7 ++++++- yamllint/conf/default.yml | 6 +++--- yamllint/config.py | 5 ++++- 7 files changed, 22 insertions(+), 14 deletions(-) diff --git a/tests/rules/test_comments_indentation.py b/tests/rules/test_comments_indentation.py index aa50505..4e1b9f4 100644 --- a/tests/rules/test_comments_indentation.py +++ b/tests/rules/test_comments_indentation.py @@ -49,7 +49,7 @@ class CommentsIndentationTestCase(RuleTestCase): '...\n', conf) def test_enabled(self): - conf = 'comments-indentation: {}' + conf = 'comments-indentation: enable' self.check('---\n' '# line 1\n' '# line 2\n', conf) @@ -123,18 +123,18 @@ class CommentsIndentationTestCase(RuleTestCase): '...\n', conf) def test_first_line(self): - conf = 'comments-indentation: {}' + conf = 'comments-indentation: enable' self.check('# comment\n', conf) self.check(' # comment\n', conf, problem=(1, 3)) def test_no_newline_at_end(self): - conf = ('comments-indentation: {}\n' + conf = ('comments-indentation: enable\n' 'new-line-at-end-of-file: disable\n') self.check('# comment', conf) self.check(' # comment', conf, problem=(1, 3)) def test_empty_comment(self): - conf = 'comments-indentation: {}' + conf = 'comments-indentation: enable' self.check('---\n' '# hey\n' '# normal\n' diff --git a/tests/rules/test_key_duplicates.py b/tests/rules/test_key_duplicates.py index b49c0e7..362cbba 100644 --- a/tests/rules/test_key_duplicates.py +++ b/tests/rules/test_key_duplicates.py @@ -80,7 +80,7 @@ class KeyDuplicatesTestCase(RuleTestCase): ': 1\n', conf) def test_enabled(self): - conf = 'key-duplicates: {}' + conf = 'key-duplicates: enable' self.check('---\n' 'block mapping:\n' ' key: a\n' @@ -149,7 +149,7 @@ class KeyDuplicatesTestCase(RuleTestCase): problem4=(7, 3)) def test_key_tokens_in_flow_sequences(self): - conf = 'key-duplicates: {}' + conf = 'key-duplicates: enable' self.check('---\n' '[\n' ' flow: sequence, with, key: value, mappings\n' diff --git a/tests/rules/test_new_line_at_end_of_file.py b/tests/rules/test_new_line_at_end_of_file.py index ff90976..effb2e1 100644 --- a/tests/rules/test_new_line_at_end_of_file.py +++ b/tests/rules/test_new_line_at_end_of_file.py @@ -30,7 +30,7 @@ class NewLineAtEndOfFileTestCase(RuleTestCase): self.check('Sentence.\n', conf) def test_enabled(self): - conf = ('new-line-at-end-of-file: {}\n' + conf = ('new-line-at-end-of-file: enable\n' 'empty-lines: disable\n' 'document-start: disable\n') self.check('', conf) diff --git a/tests/rules/test_trailing_spaces.py b/tests/rules/test_trailing_spaces.py index 58ad026..0ea4718 100644 --- a/tests/rules/test_trailing_spaces.py +++ b/tests/rules/test_trailing_spaces.py @@ -29,7 +29,7 @@ class TrailingSpacesTestCase(RuleTestCase): 'some: text \n', conf) def test_enabled(self): - conf = 'trailing-spaces: {}' + conf = 'trailing-spaces: enable' self.check('', conf) self.check('\n', conf) self.check(' \n', conf, problem=(1, 1)) @@ -40,7 +40,7 @@ class TrailingSpacesTestCase(RuleTestCase): 'some: text\t\n', conf, problem=(2, 11, 'syntax')) def test_with_dos_new_lines(self): - conf = ('trailing-spaces: {}\n' + conf = ('trailing-spaces: enable\n' 'new-lines: {type: dos}\n') self.check('---\r\n' 'some: text\r\n', conf) diff --git a/tests/test_config.py b/tests/test_config.py index 92336d6..2c537e2 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -37,7 +37,7 @@ class SimpleConfigTestCase(unittest.TestCase): config.YamlLintConfigError, 'invalid config: no such rule: "this-one-does-not-exist"'): config.YamlLintConfig('rules:\n' - ' this-one-does-not-exist: {}\n') + ' this-one-does-not-exist: enable\n') def test_missing_option(self): with self.assertRaisesRegexp( @@ -65,6 +65,11 @@ class SimpleConfigTestCase(unittest.TestCase): self.assertEqual(config.validate_rule_conf(Rule, False), False) self.assertEqual(config.validate_rule_conf(Rule, 'disable'), False) + self.assertEqual(config.validate_rule_conf(Rule, {}), + {'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': 'warning'}) self.assertRaises(config.YamlLintConfigError, diff --git a/yamllint/conf/default.yml b/yamllint/conf/default.yml index 71bd825..6aba213 100644 --- a/yamllint/conf/default.yml +++ b/yamllint/conf/default.yml @@ -34,11 +34,11 @@ rules: spaces: 2 indent-sequences: yes check-multi-line-strings: no - key-duplicates: {} + key-duplicates: enable line-length: max: 80 allow-non-breakable-words: yes - new-line-at-end-of-file: {level: error} + new-line-at-end-of-file: enable new-lines: type: unix - trailing-spaces: {} + trailing-spaces: enable diff --git a/yamllint/config.py b/yamllint/config.py index a558654..279198f 100644 --- a/yamllint/config.py +++ b/yamllint/config.py @@ -83,6 +83,8 @@ class YamlLintConfig(object): def validate_rule_conf(rule, conf): if conf is False or conf == 'disable': return False + elif conf == 'enable': + conf = {} if type(conf) == dict: if 'level' not in conf: @@ -117,7 +119,8 @@ def validate_rule_conf(rule, conf): (optkey, rule.ID)) else: raise YamlLintConfigError(('invalid config: rule "%s": should be ' - 'either "disable" or a dict') % rule.ID) + 'either "enable", "disable" or a dict') + % rule.ID) return conf