From e6403890f2f87073a0546073734c21d827a5366a Mon Sep 17 00:00:00 2001 From: ilyam8 Date: Sat, 4 Apr 2020 23:44:55 +0300 Subject: [PATCH] rule truthy: add `check-keys` option --- tests/rules/test_truthy.py | 15 +++++++++++++++ yamllint/rules/truthy.py | 27 +++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/tests/rules/test_truthy.py b/tests/rules/test_truthy.py index 82e4f6c92..74de5e555 100644 --- a/tests/rules/test_truthy.py +++ b/tests/rules/test_truthy.py @@ -114,3 +114,18 @@ def test_explicit_types(self): 'boolean5: !!bool off\n' 'boolean6: !!bool NO\n', conf) + + def test_check_keys_disabled(self): + conf = ('truthy:\n' + ' allowed-values: []\n' + ' check-keys: false\n' + 'key-duplicates: disable\n') + self.check('---\n' + 'YES: 0\nYes: 0\nyes: 0\n' + 'No: 0\nNo: 0\nno: 0\n' + 'TRUE: 0\nTrue: 0\ntrue: 0\n' + 'FALSE: 0\nFalse: 0\nfalse: 0\n' + 'ON: 0\nOn: 0\non: 0\n' + 'OFF: 0\nOff: 0\noff: 0\n' + 'YES:\n Yes:\n yes:\n on: 0\n', + conf) diff --git a/yamllint/rules/truthy.py b/yamllint/rules/truthy.py index 7dd778f25..92c5fffed 100644 --- a/yamllint/rules/truthy.py +++ b/yamllint/rules/truthy.py @@ -31,6 +31,9 @@ ``'No'``, ``'no'``, ``'ON'``, ``'On'``, ``'on'``, ``'OFF'``, ``'Off'``, ``'off'``. +* ``check-keys`` by default, truthy rule applies to both keys and values. +To disable key verification set this option to ``'false'``. + .. rubric:: Examples #. With ``truthy: {}`` @@ -92,6 +95,22 @@ - false - on - off + +#. With ``truthy: {check-keys: false}`` + + the following code snippet would **PASS**: + :: + + yes: 1 + on: 2 + true: 3 + + the following code snippet would **FAIL**: + :: + + yes: Yes + on: On + true: True """ import yaml @@ -109,14 +128,18 @@ ID = 'truthy' TYPE = 'token' -CONF = {'allowed-values': list(TRUTHY)} -DEFAULT = {'allowed-values': ['true', 'false']} +CONF = {'allowed-values': list(TRUTHY), 'check-keys': bool} +DEFAULT = {'allowed-values': ['true', 'false'], 'check-keys': True} def check(conf, token, prev, next, nextnext, context): if prev and isinstance(prev, yaml.tokens.TagToken): return + if not conf['check-keys']: + if isinstance(prev, yaml.tokens.KeyToken) and isinstance(token, yaml.tokens.ScalarToken): + return + if isinstance(token, yaml.tokens.ScalarToken): if (token.value in (set(TRUTHY) - set(conf['allowed-values'])) and token.style is None):