From 6ce11dedb422e083cab52f87f76b0129a28fbc64 Mon Sep 17 00:00:00 2001 From: ilyam8 Date: Sat, 4 Apr 2020 23:44:55 +0300 Subject: [PATCH 1/2] truthy: add `check-keys` option --- tests/rules/test_truthy.py | 26 ++++++++++++++++++++++++++ yamllint/rules/truthy.py | 27 +++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/tests/rules/test_truthy.py b/tests/rules/test_truthy.py index 82e4f6c..1c87184 100644 --- a/tests/rules/test_truthy.py +++ b/tests/rules/test_truthy.py @@ -114,3 +114,29 @@ class TruthyTestCase(RuleTestCase): '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\n' + 'Yes: 0\n' + 'yes: 0\n' + 'No: 0\n' + 'No: 0\n' + 'no: 0\n' + 'TRUE: 0\n' + 'True: 0\n' + 'true: 0\n' + 'FALSE: 0\n' + 'False: 0\n' + 'false: 0\n' + 'ON: 0\n' + 'On: 0\n' + 'on: 0\n' + 'OFF: 0\n' + 'Off: 0\n' + 'off: 0\n', + conf) diff --git a/yamllint/rules/truthy.py b/yamllint/rules/truthy.py index 7dd778f..64ccaaa 100644 --- a/yamllint/rules/truthy.py +++ b/yamllint/rules/truthy.py @@ -30,6 +30,9 @@ This can be useful to prevent surprises from YAML parsers transforming ``'False'``, ``'false'``, ``'YES'``, ``'Yes'``, ``'yes'``, ``'NO'``, ``'No'``, ``'no'``, ``'ON'``, ``'On'``, ``'on'``, ``'OFF'``, ``'Off'``, ``'off'``. +* ``check-keys`` disables verification for keys in mappings. By default, + ``truthy`` rule applies to both keys and values. Set this option to ``false`` + to prevent this. .. rubric:: Examples @@ -92,6 +95,22 @@ This can be useful to prevent surprises from YAML parsers transforming - 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 @@ TRUTHY = ['YES', 'Yes', 'yes', 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'] and 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): From 46ed0c02be46324cb74df31b35bcc2b05b5d5bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Wed, 8 Apr 2020 12:31:12 +0200 Subject: [PATCH 2/2] truthy: Add missing test removed from PR See https://github.com/adrienverge/yamllint/pull/247#discussion_r405421376. --- tests/rules/test_truthy.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/rules/test_truthy.py b/tests/rules/test_truthy.py index 1c87184..9229253 100644 --- a/tests/rules/test_truthy.py +++ b/tests/rules/test_truthy.py @@ -138,5 +138,9 @@ class TruthyTestCase(RuleTestCase): 'on: 0\n' 'OFF: 0\n' 'Off: 0\n' - 'off: 0\n', + 'off: 0\n' + 'YES:\n' + ' Yes:\n' + ' yes:\n' + ' on: 0\n', conf)