From 2d8639c3a18fa66fe348e4d3eb1e1435b970a1c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Fri, 10 Apr 2020 19:06:59 +0200 Subject: [PATCH] quoted-strings: Fix broken rule for list items The rule worked for values like: flow-map: {a: foo, b: "bar"} block-map: a: foo b: "bar" But not for: flow-seq: [foo, "bar"] block-seq: - foo - "bar" Also add tests to make sure there will be no regression. Fixes: #208. --- tests/rules/test_quoted_strings.py | 73 +++++++++++++++++++++++------- yamllint/rules/quoted_strings.py | 5 +- 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/tests/rules/test_quoted_strings.py b/tests/rules/test_quoted_strings.py index 3f0d8a8..c0c0ff3 100644 --- a/tests/rules/test_quoted_strings.py +++ b/tests/rules/test_quoted_strings.py @@ -51,8 +51,14 @@ class QuotedTestCase(RuleTestCase): 'binary: !!binary binstring\n' 'integer: !!int intstring\n' 'boolean2: !!bool boolstring\n' - 'boolean3: !!bool "quotedboolstring"\n', - conf, problem=(4, 10)) + 'boolean3: !!bool "quotedboolstring"\n' + 'block-seq:\n' + ' - foo\n' # fails + ' - "foo"\n' + 'flow-seq: [foo, "foo"]\n' # fails + 'flow-map: {a: foo, b: "foo"}\n', # fails + conf, problem1=(4, 10), problem2=(17, 5), + problem3=(19, 12), problem4=(20, 15)) self.check('---\n' 'multiline string 1: |\n' ' line 1\n' @@ -85,9 +91,16 @@ class QuotedTestCase(RuleTestCase): 'binary: !!binary binstring\n' 'integer: !!int intstring\n' 'boolean2: !!bool boolstring\n' - 'boolean3: !!bool "quotedboolstring"\n', - conf, problem1=(4, 10), problem2=(5, 10), - problem3=(6, 10), problem4=(7, 10)) + 'boolean3: !!bool "quotedboolstring"\n' + 'block-seq:\n' + ' - foo\n' # fails + ' - "foo"\n' # fails + 'flow-seq: [foo, "foo"]\n' # fails + 'flow-map: {a: foo, b: "foo"}\n', # fails + conf, problem1=(4, 10), problem2=(5, 10), problem3=(6, 10), + problem4=(7, 10), problem5=(17, 5), problem6=(18, 5), + problem7=(19, 12), problem8=(19, 17), problem9=(20, 15), + problem10=(20, 23)) self.check('---\n' 'multiline string 1: |\n' ' line 1\n' @@ -120,8 +133,14 @@ class QuotedTestCase(RuleTestCase): 'binary: !!binary binstring\n' 'integer: !!int intstring\n' 'boolean2: !!bool boolstring\n' - 'boolean3: !!bool "quotedboolstring"\n', - conf, problem1=(4, 10), problem2=(8, 10)) + 'boolean3: !!bool "quotedboolstring"\n' + 'block-seq:\n' + ' - foo\n' # fails + ' - "foo"\n' + 'flow-seq: [foo, "foo"]\n' # fails + 'flow-map: {a: foo, b: "foo"}\n', # fails + conf, problem1=(4, 10), problem2=(8, 10), problem3=(17, 5), + problem4=(19, 12), problem5=(20, 15)) self.check('---\n' 'multiline string 1: |\n' ' line 1\n' @@ -189,7 +208,12 @@ class QuotedTestCase(RuleTestCase): 'binary: !!binary binstring\n' 'integer: !!int intstring\n' 'boolean2: !!bool boolstring\n' - 'boolean3: !!bool "quotedboolstring"\n', + 'boolean3: !!bool "quotedboolstring"\n' + 'block-seq:\n' + ' - foo\n' # fails + ' - "foo"\n' + 'flow-seq: [foo, "foo"]\n' # fails + 'flow-map: {a: foo, b: "foo"}\n', # fails conf) self.check('---\n' 'multiline string 1: |\n' @@ -223,9 +247,14 @@ class QuotedTestCase(RuleTestCase): 'binary: !!binary binstring\n' 'integer: !!int intstring\n' 'boolean2: !!bool boolstring\n' - 'boolean3: !!bool "quotedboolstring"\n', - conf, problem2=(5, 10), - problem3=(6, 10), problem4=(7, 10)) + 'boolean3: !!bool "quotedboolstring"\n' + 'block-seq:\n' + ' - foo\n' # fails + ' - "foo"\n' + 'flow-seq: [foo, "foo"]\n' # fails + 'flow-map: {a: foo, b: "foo"}\n', # fails + conf, problem1=(5, 10), problem2=(6, 10), problem3=(7, 10), + problem4=(18, 5), problem5=(19, 17), problem6=(20, 23)) self.check('---\n' 'multiline string 1: |\n' ' line 1\n' @@ -292,8 +321,14 @@ class QuotedTestCase(RuleTestCase): 'binary: !!binary binstring\n' 'integer: !!int intstring\n' 'boolean2: !!bool boolstring\n' - 'boolean3: !!bool "quotedboolstring"\n', - conf, problem1=(5, 10), problem2=(8, 10)) + 'boolean3: !!bool "quotedboolstring"\n' + 'block-seq:\n' + ' - foo\n' + ' - "foo"\n' # fails + 'flow-seq: [foo, "foo"]\n' # fails + 'flow-map: {a: foo, b: "foo"}\n', # fails + conf, problem1=(5, 10), problem2=(8, 10), problem3=(18, 5), + problem4=(19, 17), problem5=(20, 23)) self.check('---\n' 'multiline string 1: |\n' ' line 1\n' @@ -327,9 +362,15 @@ class QuotedTestCase(RuleTestCase): 'binary: !!binary binstring\n' 'integer: !!int intstring\n' 'boolean2: !!bool boolstring\n' - 'boolean3: !!bool "quotedboolstring"\n', - conf, problem1=(5, 10), problem2=(6, 10), - problem3=(7, 10), problem4=(8, 10)) + 'boolean3: !!bool "quotedboolstring"\n' + 'block-seq:\n' + ' - foo\n' + ' - "foo"\n' # fails + 'flow-seq: [foo, "foo"]\n' # fails + 'flow-map: {a: foo, b: "foo"}\n', # fails + conf, problem1=(5, 10), problem2=(6, 10), problem3=(7, 10), + problem4=(8, 10), problem5=(18, 5), problem6=(19, 17), + problem7=(20, 23)) self.check('---\n' 'multiline string 1: |\n' ' line 1\n' diff --git a/yamllint/rules/quoted_strings.py b/yamllint/rules/quoted_strings.py index aaa635d..df675a6 100644 --- a/yamllint/rules/quoted_strings.py +++ b/yamllint/rules/quoted_strings.py @@ -89,7 +89,10 @@ def quote_match(quote_type, token_style): def check(conf, token, prev, next, nextnext, context): if not (isinstance(token, yaml.tokens.ScalarToken) and - isinstance(prev, (yaml.ValueToken, yaml.TagToken))): + isinstance(prev, (yaml.BlockEntryToken, yaml.FlowEntryToken, + yaml.FlowSequenceStartToken, yaml.TagToken, + yaml.ValueToken))): + return # Ignore explicit types, e.g. !!str testtest or !!int 42