diff --git a/tests/rules/test_indentation.py b/tests/rules/test_indentation.py index b819278..fbfe500 100644 --- a/tests/rules/test_indentation.py +++ b/tests/rules/test_indentation.py @@ -1370,6 +1370,45 @@ class IndentationTestCase(RuleTestCase): ' key: value\n' '...\n', conf, problem=(2, 2)) + def test_nested_collections_with_spaces_consistent(self): + """Tests behavior of {spaces: consistent} in nested collections to + ensure wrong-indentation is properly caught--especially when the + expected indent value is initially unkown. For details, see + https://github.com/adrienverge/yamllint/issues/485. + """ + conf = ('indentation: {spaces: consistent,\n' + ' indent-sequences: true}') + self.check('---\n' + '- item:\n' + ' - elem\n' + '- item:\n' + ' - elem\n' + '...\n', conf, problem=(3, 3)) + conf = ('indentation: {spaces: consistent,\n' + ' indent-sequences: false}') + self.check('---\n' + '- item:\n' + ' - elem\n' + '- item:\n' + ' - elem\n' + '...\n', conf, problem=(5,5)) + conf = ('indentation: {spaces: consistent,\n' + ' indent-sequences: consistent}') + self.check('---\n' + '- item:\n' + ' - elem\n' + '- item:\n' + ' - elem\n' + '...\n', conf, problem=(5,5)) + conf = ('indentation: {spaces: consistent,\n' + ' indent-sequences: whatever}') + self.check('---\n' + '- item:\n' + ' - elem\n' + '- item:\n' + ' - elem\n' + '...\n', conf) + def test_return(self): conf = 'indentation: {spaces: consistent}' self.check('---\n' diff --git a/yamllint/rules/indentation.py b/yamllint/rules/indentation.py index 3853f6f..8a73ae5 100644 --- a/yamllint/rules/indentation.py +++ b/yamllint/rules/indentation.py @@ -341,9 +341,14 @@ def _check(conf, token, prev, next, nextnext, context): expected = detect_indent(expected, token) if found_indentation != expected: - yield LintProblem(token.start_mark.line + 1, found_indentation + 1, - 'wrong indentation: expected %d but found %d' % - (expected, found_indentation)) + if expected < 0: + message = 'wrong indentation: expected at least %d' % \ + (found_indentation + 1) + else: + message = 'wrong indentation: expected %d but found %d' % \ + (expected, found_indentation) + yield LintProblem(token.start_mark.line + 1, + found_indentation + 1, message) if (isinstance(token, yaml.ScalarToken) and conf['check-multi-line-strings']): @@ -493,8 +498,8 @@ def _check(conf, token, prev, next, nextnext, context): # 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 + # So we choose an unknown value (-1). + indent = -1 else: indent = detect_indent(context['stack'][-1].indent, next)