Make syntax errors prevail over all yamllint problems

pull/4/head
Adrien Vergé 9 years ago
parent effb4db3b4
commit 222f7a27c1

@ -37,8 +37,15 @@ class RuleTestCase(unittest.TestCase):
expected_problems = [] expected_problems = []
for key in kwargs: for key in kwargs:
assert key.startswith('problem') assert key.startswith('problem')
if len(kwargs[key]) > 2:
if kwargs[key][2] == 'syntax':
rule_id = None
else:
rule_id = kwargs[key][2]
else:
rule_id = self.rule_id
expected_problems.append( expected_problems.append(
LintProblem(kwargs[key][0], kwargs[key][1], rule=self.rule_id)) LintProblem(kwargs[key][0], kwargs[key][1], rule=rule_id))
expected_problems.sort() expected_problems.sort()
real_problems = list(lint(source, self.build_fake_config(conf))) real_problems = list(lint(source, self.build_fake_config(conf)))

@ -82,7 +82,7 @@ class DocumentStartTestCase(RuleTestCase):
'...\n' '...\n'
'second: document\n' 'second: document\n'
'---\n' '---\n'
'third: document\n', conf, problem=(4, 1)) 'third: document\n', conf, problem=(4, 1, 'syntax'))
def test_directives(self): def test_directives(self):
conf = 'document-start: {present: yes}' conf = 'document-start: {present: yes}'

@ -163,7 +163,7 @@ class IndentationTestCase(RuleTestCase):
self.check('---\n' self.check('---\n'
'- o:\n' '- o:\n'
' k1: v1\n' ' k1: v1\n'
'...\n', conf, problem=(3, 2)) '...\n', conf, problem=(3, 2, 'syntax'))
self.check('---\n' self.check('---\n'
'- o:\n' '- o:\n'
' k1: v1\n' ' k1: v1\n'
@ -192,18 +192,18 @@ class IndentationTestCase(RuleTestCase):
' f:\n' ' f:\n'
'g:\n' 'g:\n'
'...\n', None) '...\n', None)
# self.check('---\n' self.check('---\n'
# 'a:\n' 'a:\n'
# ' b:\n' ' b:\n'
# ' c:\n' ' c:\n'
# ' d:\n' ' d:\n'
# '...\n', None, problem=(5, 5)) '...\n', None, problem=(5, 4, 'syntax'))
# self.check('---\n' self.check('---\n'
# 'a:\n' 'a:\n'
# ' b:\n' ' b:\n'
# ' c:\n' ' c:\n'
# ' d:\n' ' d:\n'
# '...\n', None, problem=(5, 2)) '...\n', None, problem=(5, 2, 'syntax'))
def test_first_line(self): def test_first_line(self):
conf = ('indentation: {spaces: 2}\n' conf = ('indentation: {spaces: 2}\n'

@ -33,11 +33,11 @@ class TrailingSpacesTestCase(RuleTestCase):
self.check('', conf) self.check('', conf)
self.check('\n', conf) self.check('\n', conf)
self.check(' \n', conf, problem=(1, 1)) self.check(' \n', conf, problem=(1, 1))
self.check('\t\t\t\n', conf, problem=(1, 1)) self.check('\t\t\t\n', conf, problem=(1, 1, 'syntax'))
self.check('---\n' self.check('---\n'
'some: text \n', conf, problem=(2, 11)) 'some: text \n', conf, problem=(2, 11))
self.check('---\n' self.check('---\n'
'some: text\t\n', conf, problem=(2, 11)) 'some: text\t\n', conf, problem=(2, 11, 'syntax'))
def test_with_dos_new_lines(self): def test_with_dos_new_lines(self):
conf = ('trailing-spaces: {}\n' conf = ('trailing-spaces: {}\n'

@ -81,15 +81,16 @@ def _lint(buffer, conf):
# Insert the syntax error (if any) at the right place... # Insert the syntax error (if any) at the right place...
if (syntax_error and syntax_error.line <= problem.line and if (syntax_error and syntax_error.line <= problem.line and
syntax_error.column <= problem.column): syntax_error.column <= problem.column):
# ... unless there is already a yamllint error at the same place,
# in which case the syntax error is probably redundant.
# In such a case, the yamllint problem is preferred, except if its
# level is not 'error' (because the script needs to exit with a
# failure status, the syntax error is preferred here).
if (syntax_error.line != problem.line or
syntax_error.column != problem.column or
problem.level != 'error'):
yield syntax_error yield syntax_error
# If there is already a yamllint error at the same place, discard
# it as it is probably redundant (and maybe it's just a 'warning',
# in which case the script won't even exit with a failure status).
if (syntax_error.line == problem.line and
syntax_error.column == problem.column):
syntax_error = None
continue
syntax_error = None syntax_error = None
yield problem yield problem

Loading…
Cancel
Save