Rewrite syntax errors handling and test them

If a syntax errors occurs at the same place than a regular yamllint rule
error, only the yamllint one is issued.
pull/4/head
Adrien Vergé 9 years ago
parent bf96bdde01
commit 07c5b4177c

@ -64,12 +64,6 @@ class DocumentEndTestCase(RuleTestCase):
'---\n' '---\n'
'third: document\n' 'third: document\n'
'...\n', conf) '...\n', conf)
self.check('first: document\n'
'...\n'
'second: document\n'
'...\n'
'third: document\n'
'...\n', conf)
self.check('---\n' self.check('---\n'
'first: document\n' 'first: document\n'
'...\n' '...\n'

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2016 Adrien Vergé
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from tests.rules.common import RuleTestCase
class YamlLintTestCase(RuleTestCase):
rule_id = None # syntax error
def test_lint(self):
self.check('---\n'
'this is not: valid: YAML\n', None, problem=(2, 19))
self.check('---\n'
'this is: valid YAML\n'
'\n'
'this is an error: [\n'
'\n'
'...\n', None, problem=(6, 1))

@ -14,7 +14,10 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import yaml
from yamllint import config from yamllint import config
from yamllint.errors import LintProblem
from yamllint import parser from yamllint import parser
@ -28,23 +31,14 @@ __license__ = 'GPLv3'
__version__ = APP_VERSION __version__ = APP_VERSION
def _lint(buffer, conf): def get_costemic_problems(buffer, conf):
rules = config.get_enabled_rules(conf) rules = config.get_enabled_rules(conf)
# Split token rules from line rules # Split token rules from line rules
token_rules = [r for r in rules if r.TYPE == 'token'] token_rules = [r for r in rules if r.TYPE == 'token']
line_rules = [r for r in rules if r.TYPE == 'line'] line_rules = [r for r in rules if r.TYPE == 'line']
# If the document contains a syntax error, save it and yield it at the
# right line
syntax_error = parser.get_syntax_error(buffer)
for elem in parser.token_or_line_generator(buffer): for elem in parser.token_or_line_generator(buffer):
if syntax_error and syntax_error.line <= elem.line_no:
syntax_error.level = 'error'
yield syntax_error
syntax_error = None
if isinstance(elem, parser.Token): if isinstance(elem, parser.Token):
for rule in token_rules: for rule in token_rules:
rule_conf = conf[rule.ID] rule_conf = conf[rule.ID]
@ -62,6 +56,39 @@ def _lint(buffer, conf):
yield problem yield problem
def get_syntax_error(buffer):
try:
list(yaml.safe_load_all(buffer))
except yaml.error.MarkedYAMLError as e:
problem = LintProblem(e.problem_mark.line + 1,
e.problem_mark.column + 1,
'syntax error: ' + e.problem)
problem.level = 'error'
return problem
def _lint(buffer, conf):
# If the document contains a syntax error, save it and yield it at the
# right line
syntax_error = get_syntax_error(buffer)
for problem in get_costemic_problems(buffer, conf):
# Insert the syntax error (if any) at the right place...
if (syntax_error and syntax_error.line <= problem.line and
syntax_error.column <= problem.column):
# ... unless there is already a yamllint error, in which case the
# syntax error is probably redundant.
if (syntax_error.line != problem.line or
syntax_error.column != problem.column):
yield syntax_error
syntax_error = None
yield problem
if syntax_error:
yield syntax_error
def lint(input, conf): def lint(input, conf):
"""Lints a YAML source. """Lints a YAML source.

@ -16,8 +16,6 @@
import yaml import yaml
from yamllint.errors import LintProblem
class Line(object): class Line(object):
def __init__(self, line_no, buffer, start, end): def __init__(self, line_no, buffer, start, end):
@ -85,11 +83,3 @@ def token_or_line_generator(buffer):
else: else:
yield token yield token
token = next(token_gen, None) token = next(token_gen, None)
def get_syntax_error(buffer):
try:
yaml.safe_load_all(buffer)
except yaml.error.MarkedYAMLError as e:
return LintProblem(e.problem_mark.line + 1, e.problem_mark.column + 1,
'syntax error: ' + e.problem)

Loading…
Cancel
Save