From 228c47ab770eb9981b64211edff846d053a6ad74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Sat, 25 Feb 2017 22:35:23 +0100 Subject: [PATCH] fix(indentation): Fix seq indent detection with consistent spaces In the case when the conf is as follows: indentation: spaces: consistent indent-sequences: true and there is no indented block before the first block sequence, and this block sequence is not indented, then the spaces number is computed as zero (while it obviously shouldn't be). This causes such a document to fail on 4th line, instead of 2nd: a: - b c: - d This commit fixes that, and adds corresponding tests. Fixes: #39 --- tests/rules/test_indentation.py | 138 +++++++++++++++++++++++++++++++- yamllint/rules/indentation.py | 14 +++- 2 files changed, 150 insertions(+), 2 deletions(-) diff --git a/tests/rules/test_indentation.py b/tests/rules/test_indentation.py index e435304..2733ea7 100644 --- a/tests/rules/test_indentation.py +++ b/tests/rules/test_indentation.py @@ -632,7 +632,7 @@ class IndentationTestCase(RuleTestCase): ' date: 1991\n' '...\n', conf) - def test_consistent(self): + def test_consistent_spaces(self): conf = ('indentation: {spaces: consistent,\n' ' indent-sequences: whatever}\n' 'document-start: disable\n') @@ -713,6 +713,142 @@ class IndentationTestCase(RuleTestCase): '- b\n' '- c\n', conf) + def test_consistent_spaces_and_indent_sequences(self): + conf = 'indentation: {spaces: consistent, indent-sequences: true}' + self.check('---\n' + 'list one:\n' + '- 1\n' + '- 2\n' + '- 3\n' + 'list two:\n' + ' - a\n' + ' - b\n' + ' - c\n', conf, problem1=(3, 1)) + self.check('---\n' + 'list one:\n' + ' - 1\n' + ' - 2\n' + ' - 3\n' + 'list two:\n' + ' - a\n' + ' - b\n' + ' - c\n', conf, problem1=(7, 5)) + self.check('---\n' + 'list one:\n' + ' - 1\n' + ' - 2\n' + ' - 3\n' + 'list two:\n' + '- a\n' + '- b\n' + '- c\n', conf, problem1=(7, 1)) + + conf = 'indentation: {spaces: consistent, indent-sequences: false}' + self.check('---\n' + 'list one:\n' + '- 1\n' + '- 2\n' + '- 3\n' + 'list two:\n' + ' - a\n' + ' - b\n' + ' - c\n', conf, problem1=(7, 5)) + self.check('---\n' + 'list one:\n' + '- 1\n' + '- 2\n' + '- 3\n' + 'list two:\n' + ' - a\n' + ' - b\n' + ' - c\n', conf, problem1=(7, 3)) + self.check('---\n' + 'list one:\n' + ' - 1\n' + ' - 2\n' + ' - 3\n' + 'list two:\n' + '- a\n' + '- b\n' + '- c\n', conf, problem1=(3, 3)) + + conf = ('indentation: {spaces: consistent,\n' + ' indent-sequences: consistent}') + self.check('---\n' + 'list one:\n' + '- 1\n' + '- 2\n' + '- 3\n' + 'list two:\n' + ' - a\n' + ' - b\n' + ' - c\n', conf, problem1=(7, 5)) + self.check('---\n' + 'list one:\n' + ' - 1\n' + ' - 2\n' + ' - 3\n' + 'list two:\n' + '- a\n' + '- b\n' + '- c\n', conf, problem1=(7, 1)) + self.check('---\n' + 'list one:\n' + '- 1\n' + '- 2\n' + '- 3\n' + 'list two:\n' + '- a\n' + '- b\n' + '- c\n', conf) + self.check('---\n' + 'list one:\n' + ' - 1\n' + ' - 2\n' + ' - 3\n' + 'list two:\n' + ' - a\n' + ' - b\n' + ' - c\n', conf, problem1=(7, 5)) + + conf = 'indentation: {spaces: consistent, indent-sequences: whatever}' + self.check('---\n' + 'list one:\n' + '- 1\n' + '- 2\n' + '- 3\n' + 'list two:\n' + ' - a\n' + ' - b\n' + ' - c\n', conf) + self.check('---\n' + 'list one:\n' + ' - 1\n' + ' - 2\n' + ' - 3\n' + 'list two:\n' + '- a\n' + '- b\n' + '- c\n', conf) + self.check('---\n' + 'list one:\n' + '- 1\n' + '- 2\n' + '- 3\n' + 'list two:\n' + '- a\n' + '- b\n' + '- c\n', conf) + self.check('---\n' + 'list one:\n' + ' - 1\n' + ' - 2\n' + ' - 3\n' + 'list two:\n' + ' - a\n' + ' - b\n' + ' - c\n', conf, problem1=(7, 5)) + def test_indent_sequences_whatever(self): conf = 'indentation: {spaces: 4, indent-sequences: whatever}' self.check('---\n' diff --git a/yamllint/rules/indentation.py b/yamllint/rules/indentation.py index db62f38..432c23c 100644 --- a/yamllint/rules/indentation.py +++ b/yamllint/rules/indentation.py @@ -469,7 +469,19 @@ def _check(conf, token, prev, next, nextnext, context): if context['indent-sequences'] is False: indent = context['stack'][-1].indent elif context['indent-sequences'] is True: - indent = detect_indent(context['stack'][-1].indent, next) + if (context['spaces'] == 'consistent' and + next.start_mark.column - + context['stack'][-1].indent == 0): + # In this case, the block sequence item is not indented + # (while it should be), but we don't know yet the + # indentation it should have (because `spaces` is + # `consistent` and its value has not been computed yet + # -- this is probably the beginning of the document). + # So we choose an arbitrary value (2). + indent = 2 + else: + indent = detect_indent(context['stack'][-1].indent, + next) else: # 'whatever' or 'consistent' if next.start_mark.column == context['stack'][-1].indent: # key: