Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1235eba94e | ||
|
|
11a14d4df8 | ||
|
|
233a70adb3 | ||
|
|
e81b73c111 | ||
|
|
3989a09d32 | ||
|
|
5cc900f2a8 | ||
|
|
851b9ac42c | ||
|
|
5c4c208b98 | ||
|
|
d08eb22081 | ||
|
|
a5b384ab21 | ||
|
|
cfea0661ed | ||
|
|
07c5b4177c | ||
|
|
bf96bdde01 | ||
|
|
e2d68dac14 |
18
.travis.yml
Normal file
18
.travis.yml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
language: python
|
||||||
|
python:
|
||||||
|
- 2.6
|
||||||
|
- 2.7
|
||||||
|
- 3.3
|
||||||
|
- 3.4
|
||||||
|
- 3.5
|
||||||
|
- nightly
|
||||||
|
install:
|
||||||
|
- pip install pyyaml flake8 coveralls
|
||||||
|
- pip install .
|
||||||
|
script:
|
||||||
|
- flake8 .
|
||||||
|
- yamllint $(git ls-files '*.yml')
|
||||||
|
- coverage run --source=yamllint setup.py test
|
||||||
|
after_success:
|
||||||
|
coveralls
|
||||||
@@ -1 +1,6 @@
|
|||||||
# yamllint
|
# yamllint
|
||||||
|
|
||||||
|
A linter for YAML files.
|
||||||
|
|
||||||
|
[](https://travis-ci.org/adrienverge/yamllint)
|
||||||
|
[](https://coveralls.io/github/adrienverge/yamllint?branch=master)
|
||||||
|
|||||||
6
setup.py
6
setup.py
@@ -44,7 +44,7 @@ setup(
|
|||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
scripts=['bin/yamllint'],
|
scripts=['bin/yamllint'],
|
||||||
package_data={'yamllint': ['conf/*.yml']},
|
package_data={'yamllint': ['conf/*.yml']},
|
||||||
install_requires=[
|
install_requires=['pyyaml'],
|
||||||
'pyyaml>=3'
|
tests_require=['nose'],
|
||||||
],
|
test_suite='nose.collector',
|
||||||
)
|
)
|
||||||
|
|||||||
108
tests/rules/test_braces.py
Normal file
108
tests/rules/test_braces.py
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
# -*- 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 ColonTestCase(RuleTestCase):
|
||||||
|
rule_id = 'braces'
|
||||||
|
|
||||||
|
def test_disabled(self):
|
||||||
|
conf = 'braces: disable'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict1: {}\n'
|
||||||
|
'dict2: { }\n'
|
||||||
|
'dict3: { a: 1, b}\n'
|
||||||
|
'dict4: {a: 1, b, c: 3 }\n'
|
||||||
|
'dict5: {a: 1, b, c: 3 }\n'
|
||||||
|
'dict6: { a: 1, b, c: 3 }\n'
|
||||||
|
'dict7: { a: 1, b, c: 3 }\n', conf)
|
||||||
|
|
||||||
|
def test_min_spaces(self):
|
||||||
|
conf = 'braces: {max-spaces-inside: -1, min-spaces-inside: 0}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {}\n', conf)
|
||||||
|
|
||||||
|
conf = 'braces: {max-spaces-inside: -1, min-spaces-inside: 1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {}\n', conf, problem=(2, 8))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { }\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: 1, b}\n', conf,
|
||||||
|
problem1=(2, 8), problem2=(2, 15))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {\n'
|
||||||
|
' a: 1,\n'
|
||||||
|
' b\n'
|
||||||
|
'}\n', conf)
|
||||||
|
|
||||||
|
conf = 'braces: {max-spaces-inside: -1, min-spaces-inside: 3}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf,
|
||||||
|
problem1=(2, 9), problem2=(2, 17))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf)
|
||||||
|
|
||||||
|
def test_max_spaces(self):
|
||||||
|
conf = 'braces: {max-spaces-inside: 0, min-spaces-inside: -1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {}\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { }\n', conf, problem=(2, 8))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: 1, b}\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf,
|
||||||
|
problem1=(2, 8), problem2=(2, 16))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf,
|
||||||
|
problem1=(2, 10), problem2=(2, 20))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {\n'
|
||||||
|
' a: 1,\n'
|
||||||
|
' b\n'
|
||||||
|
'}\n', conf)
|
||||||
|
|
||||||
|
conf = 'braces: {max-spaces-inside: 3, min-spaces-inside: -1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf,
|
||||||
|
problem1=(2, 11), problem2=(2, 23))
|
||||||
|
|
||||||
|
def test_min_and_max_spaces(self):
|
||||||
|
conf = 'braces: {max-spaces-inside: 0, min-spaces-inside: 0}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {}\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { }\n', conf, problem=(2, 8))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b}\n', conf, problem=(2, 10))
|
||||||
|
|
||||||
|
conf = 'braces: {max-spaces-inside: 1, min-spaces-inside: 1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: 1, b, c: 3 }\n', conf, problem=(2, 8))
|
||||||
|
|
||||||
|
conf = 'braces: {max-spaces-inside: 2, min-spaces-inside: 0}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: 1, b, c: 3 }\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b, c: 3 }\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b, c: 3 }\n', conf, problem=(2, 10))
|
||||||
107
tests/rules/test_brackets.py
Normal file
107
tests/rules/test_brackets.py
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
# -*- 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 ColonTestCase(RuleTestCase):
|
||||||
|
rule_id = 'brackets'
|
||||||
|
|
||||||
|
def test_disabled(self):
|
||||||
|
conf = 'brackets: disable'
|
||||||
|
self.check('---\n'
|
||||||
|
'array1: []\n'
|
||||||
|
'array2: [ ]\n'
|
||||||
|
'array3: [ a, b]\n'
|
||||||
|
'array4: [a, b, c ]\n'
|
||||||
|
'array5: [a, b, c ]\n'
|
||||||
|
'array6: [ a, b, c ]\n'
|
||||||
|
'array7: [ a, b, c ]\n', conf)
|
||||||
|
|
||||||
|
def test_min_spaces(self):
|
||||||
|
conf = 'brackets: {max-spaces-inside: -1, min-spaces-inside: 0}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: []\n', conf)
|
||||||
|
|
||||||
|
conf = 'brackets: {max-spaces-inside: -1, min-spaces-inside: 1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: []\n', conf, problem=(2, 9))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ ]\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [a, b]\n', conf, problem1=(2, 9), problem2=(2, 13))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [\n'
|
||||||
|
' a,\n'
|
||||||
|
' b\n'
|
||||||
|
']\n', conf)
|
||||||
|
|
||||||
|
conf = 'brackets: {max-spaces-inside: -1, min-spaces-inside: 3}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf,
|
||||||
|
problem1=(2, 10), problem2=(2, 15))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf)
|
||||||
|
|
||||||
|
def test_max_spaces(self):
|
||||||
|
conf = 'brackets: {max-spaces-inside: 0, min-spaces-inside: -1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: []\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ ]\n', conf, problem=(2, 9))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [a, b]\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf,
|
||||||
|
problem1=(2, 9), problem2=(2, 14))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf,
|
||||||
|
problem1=(2, 11), problem2=(2, 18))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [\n'
|
||||||
|
' a,\n'
|
||||||
|
' b\n'
|
||||||
|
']\n', conf)
|
||||||
|
|
||||||
|
conf = 'brackets: {max-spaces-inside: 3, min-spaces-inside: -1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b ]\n', conf,
|
||||||
|
problem1=(2, 12), problem2=(2, 21))
|
||||||
|
|
||||||
|
def test_min_and_max_spaces(self):
|
||||||
|
conf = 'brackets: {max-spaces-inside: 0, min-spaces-inside: 0}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: []\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ ]\n', conf, problem=(2, 9))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b]\n', conf, problem=(2, 11))
|
||||||
|
|
||||||
|
conf = 'brackets: {max-spaces-inside: 1, min-spaces-inside: 1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [a, b, c ]\n', conf, problem=(2, 9))
|
||||||
|
|
||||||
|
conf = 'brackets: {max-spaces-inside: 2, min-spaces-inside: 0}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [a, b, c ]\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b, c ]\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [ a, b, c ]\n', conf, problem=(2, 11))
|
||||||
@@ -32,17 +32,17 @@ class ColonTestCase(RuleTestCase):
|
|||||||
' val\n'
|
' val\n'
|
||||||
' property : value\n'
|
' property : value\n'
|
||||||
' prop2 : val2\n'
|
' prop2 : val2\n'
|
||||||
' propriété : [ valeur ]\n'
|
' propriété : [valeur]\n'
|
||||||
' o:\n'
|
' o:\n'
|
||||||
' k1: [v1, v2]\n'
|
' k1: [v1, v2]\n'
|
||||||
' p:\n'
|
' p:\n'
|
||||||
' - k3: >\n'
|
' - k3: >\n'
|
||||||
' val\n'
|
' val\n'
|
||||||
' - o: { k1: v1 }\n'
|
' - o: {k1: v1}\n'
|
||||||
' - p: kdjf\n'
|
' - p: kdjf\n'
|
||||||
' - q: val0\n'
|
' - q: val0\n'
|
||||||
' q2:\n'
|
' - q2:\n'
|
||||||
' - val1\n'
|
' - val1\n'
|
||||||
'...\n', conf)
|
'...\n', conf)
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
'object:\n'
|
'object:\n'
|
||||||
@@ -54,7 +54,7 @@ class ColonTestCase(RuleTestCase):
|
|||||||
' val\n'
|
' val\n'
|
||||||
' property: value\n'
|
' property: value\n'
|
||||||
' prop2: val2\n'
|
' prop2: val2\n'
|
||||||
' propriété: [ valeur ]\n'
|
' propriété: [valeur]\n'
|
||||||
' o:\n'
|
' o:\n'
|
||||||
' k1: [v1, v2]\n', conf)
|
' k1: [v1, v2]\n', conf)
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
@@ -64,10 +64,10 @@ class ColonTestCase(RuleTestCase):
|
|||||||
' val\n'
|
' val\n'
|
||||||
' - k3: >\n'
|
' - k3: >\n'
|
||||||
' val\n'
|
' val\n'
|
||||||
' - o: { k1: v1 }\n'
|
' - o: {k1: v1}\n'
|
||||||
' - o: { k1: v1 }\n'
|
' - o: {k1: v1}\n'
|
||||||
' q2:\n'
|
' - q2:\n'
|
||||||
' - val1\n'
|
' - val1\n'
|
||||||
'...\n', conf)
|
'...\n', conf)
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
'a: {b: {c: d, e : f}}\n', conf)
|
'a: {b: {c: d, e : f}}\n', conf)
|
||||||
|
|||||||
154
tests/rules/test_commas.py
Normal file
154
tests/rules/test_commas.py
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
# -*- 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 CommaTestCase(RuleTestCase):
|
||||||
|
rule_id = 'commas'
|
||||||
|
|
||||||
|
def test_disabled(self):
|
||||||
|
conf = 'commas: disable'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: b , c: "1 2 3", d: e , f: [g, h]}\n'
|
||||||
|
'array: [\n'
|
||||||
|
' elem ,\n'
|
||||||
|
' key: val ,\n'
|
||||||
|
']\n'
|
||||||
|
'map: {\n'
|
||||||
|
' key1: val1 ,\n'
|
||||||
|
' key2: val2,\n'
|
||||||
|
'}\n'
|
||||||
|
'...\n', conf)
|
||||||
|
|
||||||
|
def test_before_enabled(self):
|
||||||
|
conf = 'commas: {max-spaces-before: 0, max-spaces-after: -1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [1, 2, 3, 4]\n'
|
||||||
|
'...\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [1, 2 , 3, 4]\n'
|
||||||
|
'...\n', conf, problem=(2, 13))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [1 , 2, 3 , 4]\n'
|
||||||
|
'...\n', conf, problem1=(2, 10), problem2=(2, 23))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: b, c: "1 2 3", d: e, f: [g, h]}\n'
|
||||||
|
'...\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: b, c: "1 2 3" , d: e, f: [g, h]}\n'
|
||||||
|
'...\n', conf, problem=(2, 24))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: b , c: "1 2 3", d: e, f: [g , h]}\n'
|
||||||
|
'...\n', conf, problem1=(2, 12), problem2=(2, 42))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [\n'
|
||||||
|
' elem,\n'
|
||||||
|
' key: val,\n'
|
||||||
|
']\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [\n'
|
||||||
|
' elem ,\n'
|
||||||
|
' key: val,\n'
|
||||||
|
']\n', conf, problem=(3, 7))
|
||||||
|
self.check('---\n'
|
||||||
|
'map: {\n'
|
||||||
|
' key1: val1,\n'
|
||||||
|
' key2: val2,\n'
|
||||||
|
'}\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'map: {\n'
|
||||||
|
' key1: val1,\n'
|
||||||
|
' key2: val2 ,\n'
|
||||||
|
'}\n', conf, problem=(4, 13))
|
||||||
|
|
||||||
|
def test_before_max(self):
|
||||||
|
conf = 'commas: {max-spaces-before: 3, max-spaces-after: -1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [1 , 2, 3 , 4]\n'
|
||||||
|
'...\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [1 , 2, 3 , 4]\n'
|
||||||
|
'...\n', conf, problem=(2, 20))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [\n'
|
||||||
|
' elem1 ,\n'
|
||||||
|
' elem2 ,\n'
|
||||||
|
' key: val,\n'
|
||||||
|
']\n', conf, problem=(4, 11))
|
||||||
|
|
||||||
|
def test_after_enabled(self):
|
||||||
|
conf = 'commas: {max-spaces-before: -1, max-spaces-after: 1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [1, 2, 3, 4]\n'
|
||||||
|
'...\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [1, 2, 3, 4]\n'
|
||||||
|
'...\n', conf, problem=(2, 15))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [1, 2, 3, 4]\n'
|
||||||
|
'...\n', conf, problem1=(2, 12), problem2=(2, 22))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: b , c: "1 2 3", d: e, f: [g, h]}\n'
|
||||||
|
'...\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: b , c: "1 2 3", d: e, f: [g, h]}\n'
|
||||||
|
'...\n', conf, problem=(2, 27))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: b , c: "1 2 3", d: e, f: [g, h]}\n'
|
||||||
|
'...\n', conf, problem1=(2, 15), problem2=(2, 44))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [\n'
|
||||||
|
' elem,\n'
|
||||||
|
' key: val,\n'
|
||||||
|
']\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [\n'
|
||||||
|
' elem, key: val,\n'
|
||||||
|
']\n', conf, problem=(3, 9))
|
||||||
|
self.check('---\n'
|
||||||
|
'map: {\n'
|
||||||
|
' key1: val1, key2: [val2, val3]\n'
|
||||||
|
'}\n', conf, problem1=(3, 16), problem2=(3, 30))
|
||||||
|
|
||||||
|
def test_after_max(self):
|
||||||
|
conf = 'commas: {max-spaces-before: -1, max-spaces-after: 3}'
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [1, 2, 3, 4]\n'
|
||||||
|
'...\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [1, 2, 3, 4]\n'
|
||||||
|
'...\n', conf, problem=(2, 21))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: b , c: "1 2 3", d: e, f: [g, h]}\n'
|
||||||
|
'...\n', conf, problem1=(2, 31), problem2=(2, 49))
|
||||||
|
|
||||||
|
def test_both_before_and_after(self):
|
||||||
|
conf = 'commas: {max-spaces-before: 0, max-spaces-after: 1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: b , c: "1 2 3", d: e , f: [g, h]}\n'
|
||||||
|
'array: [\n'
|
||||||
|
' elem ,\n'
|
||||||
|
' key: val ,\n'
|
||||||
|
']\n'
|
||||||
|
'map: {\n'
|
||||||
|
' key1: val1 ,\n'
|
||||||
|
' key2: val2,\n'
|
||||||
|
'}\n'
|
||||||
|
'...\n', conf,
|
||||||
|
problem1=(2, 12), problem2=(2, 16), problem3=(2, 31),
|
||||||
|
problem4=(2, 36), problem5=(2, 50), problem6=(4, 8),
|
||||||
|
problem7=(5, 11), problem8=(8, 13))
|
||||||
133
tests/rules/test_comments.py
Normal file
133
tests/rules/test_comments.py
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
# -*- 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 CommentsTestCase(RuleTestCase):
|
||||||
|
rule_id = 'comments'
|
||||||
|
|
||||||
|
def test_disabled(self):
|
||||||
|
conf = ('comments: disable\n'
|
||||||
|
'comments-indentation: disable\n')
|
||||||
|
self.check('---\n'
|
||||||
|
'#comment\n'
|
||||||
|
'\n'
|
||||||
|
'test: # description\n'
|
||||||
|
' - foo # bar\n'
|
||||||
|
' - hello #world\n'
|
||||||
|
'\n'
|
||||||
|
'# comment 2\n'
|
||||||
|
'#comment 3\n'
|
||||||
|
' #comment 3 bis\n'
|
||||||
|
' # comment 3 ter\n'
|
||||||
|
'\n'
|
||||||
|
'string: "Une longue phrase." # this is French\n', conf)
|
||||||
|
|
||||||
|
def test_starting_space(self):
|
||||||
|
conf = ('comments:\n'
|
||||||
|
' require-starting-space: yes\n'
|
||||||
|
' min-spaces-from-content: -1\n'
|
||||||
|
'comments-indentation: disable\n')
|
||||||
|
self.check('---\n'
|
||||||
|
'# comment\n'
|
||||||
|
'\n'
|
||||||
|
'test: # description\n'
|
||||||
|
' - foo # bar\n'
|
||||||
|
' - hello # world\n'
|
||||||
|
'\n'
|
||||||
|
'# comment 2\n'
|
||||||
|
'# comment 3\n'
|
||||||
|
' # comment 3 bis\n'
|
||||||
|
' # comment 3 ter\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'#comment\n'
|
||||||
|
'\n'
|
||||||
|
'test: # description\n'
|
||||||
|
' - foo # bar\n'
|
||||||
|
' - hello #world\n'
|
||||||
|
'\n'
|
||||||
|
'# comment 2\n'
|
||||||
|
'#comment 3\n'
|
||||||
|
' #comment 3 bis\n'
|
||||||
|
' # comment 3 ter\n', conf,
|
||||||
|
problem1=(2, 2), problem2=(6, 13),
|
||||||
|
problem4=(9, 2), problem5=(10, 4))
|
||||||
|
|
||||||
|
def test_spaces_from_content(self):
|
||||||
|
conf = ('comments:\n'
|
||||||
|
' require-starting-space: no\n'
|
||||||
|
' min-spaces-from-content: 2\n')
|
||||||
|
self.check('---\n'
|
||||||
|
'# comment\n'
|
||||||
|
'\n'
|
||||||
|
'test: # description\n'
|
||||||
|
' - foo # bar\n'
|
||||||
|
' - hello #world\n'
|
||||||
|
'\n'
|
||||||
|
'string: "Une longue phrase." # this is French\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'# comment\n'
|
||||||
|
'\n'
|
||||||
|
'test: # description\n'
|
||||||
|
' - foo # bar\n'
|
||||||
|
' - hello #world\n'
|
||||||
|
'\n'
|
||||||
|
'string: "Une longue phrase." # this is French\n', conf,
|
||||||
|
problem1=(4, 7), problem2=(6, 11), problem3=(8, 30))
|
||||||
|
|
||||||
|
def test_both(self):
|
||||||
|
conf = ('comments:\n'
|
||||||
|
' require-starting-space: yes\n'
|
||||||
|
' min-spaces-from-content: 2\n'
|
||||||
|
'comments-indentation: disable\n')
|
||||||
|
self.check('---\n'
|
||||||
|
'#comment\n'
|
||||||
|
'\n'
|
||||||
|
'test: # description\n'
|
||||||
|
' - foo # bar\n'
|
||||||
|
' - hello #world\n'
|
||||||
|
'\n'
|
||||||
|
'# comment 2\n'
|
||||||
|
'#comment 3\n'
|
||||||
|
' #comment 3 bis\n'
|
||||||
|
' # comment 3 ter\n'
|
||||||
|
'\n'
|
||||||
|
'string: "Une longue phrase." # this is French\n', conf,
|
||||||
|
problem1=(2, 2),
|
||||||
|
problem2=(4, 7),
|
||||||
|
problem3=(6, 11), problem4=(6, 12),
|
||||||
|
problem5=(9, 2),
|
||||||
|
problem6=(10, 4),
|
||||||
|
problem7=(13, 30))
|
||||||
|
|
||||||
|
def test_empty_comment(self):
|
||||||
|
conf = ('comments:\n'
|
||||||
|
' require-starting-space: yes\n'
|
||||||
|
' min-spaces-from-content: 2\n')
|
||||||
|
self.check('---\n'
|
||||||
|
'# This is paragraph 1.\n'
|
||||||
|
'#\n'
|
||||||
|
'# This is paragraph 2.\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'inline: comment #\n'
|
||||||
|
'foo: bar\n', conf)
|
||||||
|
|
||||||
|
def test_first_line(self):
|
||||||
|
conf = ('comments:\n'
|
||||||
|
' require-starting-space: yes\n'
|
||||||
|
' min-spaces-from-content: 2\n')
|
||||||
|
self.check('# comment\n', conf)
|
||||||
121
tests/rules/test_comments_indentation.py
Normal file
121
tests/rules/test_comments_indentation.py
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
# -*- 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 CommentsIndentationTestCase(RuleTestCase):
|
||||||
|
rule_id = 'comments-indentation'
|
||||||
|
|
||||||
|
def test_disable(self):
|
||||||
|
conf = 'comments-indentation: disable'
|
||||||
|
self.check('---\n'
|
||||||
|
' # line 1\n'
|
||||||
|
'# line 2\n'
|
||||||
|
' # line 3\n'
|
||||||
|
' # line 4\n'
|
||||||
|
'\n'
|
||||||
|
'obj:\n'
|
||||||
|
' # these\n'
|
||||||
|
' # are\n'
|
||||||
|
' # [good]\n'
|
||||||
|
'# bad\n'
|
||||||
|
' # comments\n'
|
||||||
|
' a: b\n'
|
||||||
|
'\n'
|
||||||
|
'obj1:\n'
|
||||||
|
' a: 1\n'
|
||||||
|
' # comments\n'
|
||||||
|
'\n'
|
||||||
|
'obj2:\n'
|
||||||
|
' b: 2\n'
|
||||||
|
'\n'
|
||||||
|
'# empty\n'
|
||||||
|
'#\n'
|
||||||
|
'# comment\n'
|
||||||
|
'...\n', conf)
|
||||||
|
|
||||||
|
def test_enabled(self):
|
||||||
|
conf = 'comments-indentation: {}'
|
||||||
|
self.check('---\n'
|
||||||
|
'# line 1\n'
|
||||||
|
'# line 2\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
' # line 1\n'
|
||||||
|
'# line 2\n', conf, problem=(2, 2))
|
||||||
|
self.check('---\n'
|
||||||
|
' # line 1\n'
|
||||||
|
' # line 2\n', conf, problem1=(2, 3), problem2=(3, 3))
|
||||||
|
self.check('---\n'
|
||||||
|
'obj:\n'
|
||||||
|
' # normal\n'
|
||||||
|
' a: b\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'obj:\n'
|
||||||
|
' # bad\n'
|
||||||
|
' a: b\n', conf, problem=(3, 2))
|
||||||
|
self.check('---\n'
|
||||||
|
'obj:\n'
|
||||||
|
'# bad\n'
|
||||||
|
' a: b\n', conf, problem=(3, 1))
|
||||||
|
self.check('---\n'
|
||||||
|
'obj:\n'
|
||||||
|
' # bad\n'
|
||||||
|
' a: b\n', conf, problem=(3, 4))
|
||||||
|
self.check('---\n'
|
||||||
|
'obj:\n'
|
||||||
|
' # these\n'
|
||||||
|
' # are\n'
|
||||||
|
' # [good]\n'
|
||||||
|
'# bad\n'
|
||||||
|
' # comments\n'
|
||||||
|
' a: b\n', conf,
|
||||||
|
problem1=(3, 2), problem2=(4, 4),
|
||||||
|
problem3=(6, 1), problem4=(7, 7))
|
||||||
|
self.check('---\n'
|
||||||
|
'obj1:\n'
|
||||||
|
' a: 1\n'
|
||||||
|
' # comments\n'
|
||||||
|
'\n'
|
||||||
|
'obj2:\n'
|
||||||
|
' b: 2\n', conf, problem=(4, 3))
|
||||||
|
self.check('---\n'
|
||||||
|
'obj1:\n'
|
||||||
|
' a: 1\n'
|
||||||
|
' # comments\n'
|
||||||
|
' b: 2\n', conf)
|
||||||
|
|
||||||
|
def test_first_line(self):
|
||||||
|
conf = 'comments-indentation: {}'
|
||||||
|
self.check('# comment\n', conf)
|
||||||
|
self.check(' # comment\n', conf, problem=(1, 3))
|
||||||
|
|
||||||
|
def test_no_newline_at_end(self):
|
||||||
|
conf = ('comments-indentation: {}\n'
|
||||||
|
'new-line-at-end-of-file: disable\n')
|
||||||
|
self.check('# comment', conf)
|
||||||
|
self.check(' # comment', conf, problem=(1, 3))
|
||||||
|
|
||||||
|
def test_empty_comment(self):
|
||||||
|
conf = 'comments-indentation: {}'
|
||||||
|
self.check('---\n'
|
||||||
|
'# hey\n'
|
||||||
|
'# normal\n'
|
||||||
|
'#\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'# hey\n'
|
||||||
|
'# normal\n'
|
||||||
|
' #\n', conf, problem=(4, 2))
|
||||||
74
tests/rules/test_common.py
Normal file
74
tests/rules/test_common.py
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
# -*- 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/>.
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from yamllint.rules.common import Comment, get_comments_between_tokens
|
||||||
|
|
||||||
|
|
||||||
|
class CommonTestCase(unittest.TestCase):
|
||||||
|
def check_comments(self, buffer, *expected):
|
||||||
|
yaml_loader = yaml.BaseLoader(buffer)
|
||||||
|
|
||||||
|
comments = []
|
||||||
|
|
||||||
|
next = yaml_loader.peek_token()
|
||||||
|
while next is not None:
|
||||||
|
curr = yaml_loader.get_token()
|
||||||
|
next = yaml_loader.peek_token()
|
||||||
|
for comment in get_comments_between_tokens(curr, next):
|
||||||
|
comments.append(comment)
|
||||||
|
|
||||||
|
self.assertEqual(comments, list(expected))
|
||||||
|
|
||||||
|
def test_get_comments_between_tokens(self):
|
||||||
|
self.check_comments('# comment\n',
|
||||||
|
Comment(1, 1, '# comment', 0))
|
||||||
|
self.check_comments('---\n'
|
||||||
|
'# comment\n'
|
||||||
|
'...\n',
|
||||||
|
Comment(2, 1, '# comment', 0))
|
||||||
|
self.check_comments('---\n'
|
||||||
|
'# no newline char',
|
||||||
|
Comment(2, 1, '# no newline char', 0))
|
||||||
|
self.check_comments('# just comment',
|
||||||
|
Comment(1, 1, '# just comment', 0))
|
||||||
|
self.check_comments('\n'
|
||||||
|
' # indented comment\n',
|
||||||
|
Comment(2, 4, '# indented comment', 0))
|
||||||
|
self.check_comments('\n'
|
||||||
|
'# trailing spaces \n',
|
||||||
|
Comment(2, 1, '# trailing spaces ', 0))
|
||||||
|
self.check_comments('# comment one\n'
|
||||||
|
'\n'
|
||||||
|
'key: val # key=val\n'
|
||||||
|
'\n'
|
||||||
|
'# this is\n'
|
||||||
|
'# a block \n'
|
||||||
|
'# comment\n'
|
||||||
|
'\n'
|
||||||
|
'other:\n'
|
||||||
|
' - foo # equals\n'
|
||||||
|
' # bar\n',
|
||||||
|
Comment(1, 1, '# comment one', 0),
|
||||||
|
Comment(3, 11, '# key=val', 0),
|
||||||
|
Comment(5, 1, '# this is', 0),
|
||||||
|
Comment(6, 1, '# a block ', 0),
|
||||||
|
Comment(7, 1, '# comment', 0),
|
||||||
|
Comment(10, 10, '# equals', 0),
|
||||||
|
Comment(11, 10, '# bar', 0))
|
||||||
@@ -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'
|
||||||
|
|||||||
@@ -85,6 +85,20 @@ class DocumentStartTestCase(RuleTestCase):
|
|||||||
'third: document\n', conf, problem=(4, 1))
|
'third: document\n', conf, problem=(4, 1))
|
||||||
|
|
||||||
def test_directives(self):
|
def test_directives(self):
|
||||||
# TODO
|
conf = 'document-start: {present: yes}'
|
||||||
# %YAML 1.2
|
self.check('%YAML 1.2\n'
|
||||||
pass
|
'---\n'
|
||||||
|
'doc: ument\n'
|
||||||
|
'...\n', conf)
|
||||||
|
self.check('%YAML 1.2\n'
|
||||||
|
'%TAG ! tag:clarkevans.com,2002:\n'
|
||||||
|
'---\n'
|
||||||
|
'doc: ument\n'
|
||||||
|
'...\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'doc: 1\n'
|
||||||
|
'...\n'
|
||||||
|
'%YAML 1.2\n'
|
||||||
|
'---\n'
|
||||||
|
'doc: 2\n'
|
||||||
|
'...\n', conf)
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ class IndentationTestCase(RuleTestCase):
|
|||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
' object:\n'
|
' object:\n'
|
||||||
' val: 1\n'
|
' val: 1\n'
|
||||||
'...\n', conf, problem=(2, 2))
|
'...\n', conf, problem1=(2, 2), problem2=(3, 6))
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
'- el1\n'
|
'- el1\n'
|
||||||
'- el2:\n'
|
'- el2:\n'
|
||||||
@@ -132,8 +132,9 @@ class IndentationTestCase(RuleTestCase):
|
|||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
' - el1\n'
|
' - el1\n'
|
||||||
' - el2:\n'
|
' - el2:\n'
|
||||||
' - subel\n'
|
' - subel\n'
|
||||||
'...\n', conf, problem1=(2, 3), problem2=(4, 7))
|
'...\n', conf,
|
||||||
|
problem1=(2, 3), problem2=(3, 3), problem3=(4, 5))
|
||||||
|
|
||||||
def test_multi_lines(self):
|
def test_multi_lines(self):
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
@@ -154,18 +155,32 @@ class IndentationTestCase(RuleTestCase):
|
|||||||
'...\n', None)
|
'...\n', None)
|
||||||
|
|
||||||
def test_nested_collections(self):
|
def test_nested_collections(self):
|
||||||
|
conf = 'indentation: {spaces: 2}'
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
'- o:\n'
|
'- o:\n'
|
||||||
' k1: v1\n'
|
' k1: v1\n'
|
||||||
'...\n', None)
|
'...\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'- o:\n'
|
||||||
|
' k1: v1\n'
|
||||||
|
'...\n', conf, problem=(3, 2))
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
'- o:\n'
|
'- o:\n'
|
||||||
' k1: v1\n'
|
' k1: v1\n'
|
||||||
'...\n', None, problem=(3, 4))
|
'...\n', conf, problem=(3, 4))
|
||||||
|
conf = 'indentation: {spaces: 4}'
|
||||||
|
self.check('---\n'
|
||||||
|
'- o:\n'
|
||||||
|
' k1: v1\n'
|
||||||
|
'...\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'- o:\n'
|
||||||
|
' k1: v1\n'
|
||||||
|
'...\n', conf, problem=(3, 4))
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
'- o:\n'
|
'- o:\n'
|
||||||
' k1: v1\n'
|
' k1: v1\n'
|
||||||
'...\n', None, problem=(3, 6))
|
'...\n', conf, problem=(3, 6))
|
||||||
|
|
||||||
def test_return(self):
|
def test_return(self):
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
@@ -189,3 +204,23 @@ class IndentationTestCase(RuleTestCase):
|
|||||||
# ' c:\n'
|
# ' c:\n'
|
||||||
# ' d:\n'
|
# ' d:\n'
|
||||||
# '...\n', None, problem=(5, 2))
|
# '...\n', None, problem=(5, 2))
|
||||||
|
|
||||||
|
def test_first_line(self):
|
||||||
|
conf = ('indentation: {spaces: 2}\n'
|
||||||
|
'document-start: disable\n')
|
||||||
|
self.check(' a: 1\n', conf, problem=(1, 3))
|
||||||
|
|
||||||
|
def test_broken_inline_flows(self):
|
||||||
|
conf = 'indentation: {spaces: 2}'
|
||||||
|
self.check('---\n'
|
||||||
|
'obj: {\n'
|
||||||
|
' a: 1,\n'
|
||||||
|
' b: 2,\n'
|
||||||
|
' c: 3\n'
|
||||||
|
'}\n', conf, problem1=(4, 4), problem2=(5, 2))
|
||||||
|
self.check('---\n'
|
||||||
|
'list: [\n'
|
||||||
|
' 1,\n'
|
||||||
|
' 2,\n'
|
||||||
|
' 3\n'
|
||||||
|
']\n', conf, problem1=(4, 4), problem2=(5, 2))
|
||||||
|
|||||||
37
tests/rules/test_syntax_error.py
Normal file
37
tests/rules/test_syntax_error.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
# -*- 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))
|
||||||
|
|
||||||
|
def test_directives(self):
|
||||||
|
self.check('%YAML 1.2\n'
|
||||||
|
'%TAG ! tag:clarkevans.com,2002:\n'
|
||||||
|
'doc: ument\n'
|
||||||
|
'...\n', None, problem=(3, 1))
|
||||||
@@ -65,8 +65,8 @@ class ParserTestCase(unittest.TestCase):
|
|||||||
e = list(token_generator(''))
|
e = list(token_generator(''))
|
||||||
self.assertEqual(len(e), 2)
|
self.assertEqual(len(e), 2)
|
||||||
self.assertEqual(e[0].prev, None)
|
self.assertEqual(e[0].prev, None)
|
||||||
self.assertIsInstance(e[0].curr, yaml.Token)
|
self.assertTrue(isinstance(e[0].curr, yaml.Token))
|
||||||
self.assertIsInstance(e[0].next, yaml.Token)
|
self.assertTrue(isinstance(e[0].next, yaml.Token))
|
||||||
self.assertEqual(e[1].prev, e[0].curr)
|
self.assertEqual(e[1].prev, e[0].curr)
|
||||||
self.assertEqual(e[1].curr, e[0].next)
|
self.assertEqual(e[1].curr, e[0].next)
|
||||||
self.assertEqual(e[1].next, None)
|
self.assertEqual(e[1].next, None)
|
||||||
@@ -74,20 +74,20 @@ class ParserTestCase(unittest.TestCase):
|
|||||||
e = list(token_generator('---\n'
|
e = list(token_generator('---\n'
|
||||||
'k: v\n'))
|
'k: v\n'))
|
||||||
self.assertEqual(len(e), 9)
|
self.assertEqual(len(e), 9)
|
||||||
self.assertIsInstance(e[3].curr, yaml.KeyToken)
|
self.assertTrue(isinstance(e[3].curr, yaml.KeyToken))
|
||||||
self.assertIsInstance(e[5].curr, yaml.ValueToken)
|
self.assertTrue(isinstance(e[5].curr, yaml.ValueToken))
|
||||||
|
|
||||||
def test_token_or_line_generator(self):
|
def test_token_or_line_generator(self):
|
||||||
e = list(token_or_line_generator('---\n'
|
e = list(token_or_line_generator('---\n'
|
||||||
'k: v\n'))
|
'k: v\n'))
|
||||||
self.assertEqual(len(e), 12)
|
self.assertEqual(len(e), 12)
|
||||||
self.assertIsInstance(e[0], Token)
|
self.assertTrue(isinstance(e[0], Token))
|
||||||
self.assertIsInstance(e[0].curr, yaml.StreamStartToken)
|
self.assertTrue(isinstance(e[0].curr, yaml.StreamStartToken))
|
||||||
self.assertIsInstance(e[1], Token)
|
self.assertTrue(isinstance(e[1], Token))
|
||||||
self.assertIsInstance(e[1].curr, yaml.DocumentStartToken)
|
self.assertTrue(isinstance(e[1].curr, yaml.DocumentStartToken))
|
||||||
self.assertIsInstance(e[2], Line)
|
self.assertTrue(isinstance(e[2], Line))
|
||||||
self.assertIsInstance(e[3].curr, yaml.BlockMappingStartToken)
|
self.assertTrue(isinstance(e[3].curr, yaml.BlockMappingStartToken))
|
||||||
self.assertIsInstance(e[4].curr, yaml.KeyToken)
|
self.assertTrue(isinstance(e[4].curr, yaml.KeyToken))
|
||||||
self.assertIsInstance(e[6].curr, yaml.ValueToken)
|
self.assertTrue(isinstance(e[6].curr, yaml.ValueToken))
|
||||||
self.assertIsInstance(e[8], Line)
|
self.assertTrue(isinstance(e[8], Line))
|
||||||
self.assertIsInstance(e[11], Line)
|
self.assertTrue(isinstance(e[11], Line))
|
||||||
|
|||||||
@@ -14,13 +14,16 @@
|
|||||||
# 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
|
||||||
|
|
||||||
|
|
||||||
APP_NAME = 'yamllint'
|
APP_NAME = 'yamllint'
|
||||||
APP_VERSION = '0.1.0'
|
APP_VERSION = '0.2.0'
|
||||||
APP_DESCRIPTION = 'Lint YAML files.'
|
APP_DESCRIPTION = 'A linter for YAML files.'
|
||||||
|
|
||||||
__author__ = 'Adrien Vergé'
|
__author__ = 'Adrien Vergé'
|
||||||
__copyright__ = 'Copyright 2016, Adrien Vergé'
|
__copyright__ = 'Copyright 2016, Adrien Vergé'
|
||||||
@@ -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.
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,24 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
rules:
|
rules:
|
||||||
#block-sequence-indentation:
|
braces:
|
||||||
# present: yes
|
min-spaces-inside: 0
|
||||||
|
max-spaces-inside: 0
|
||||||
|
brackets:
|
||||||
|
min-spaces-inside: 0
|
||||||
|
max-spaces-inside: 0
|
||||||
colons:
|
colons:
|
||||||
max-spaces-before: 0
|
max-spaces-before: 0
|
||||||
max-spaces-after: 1
|
max-spaces-after: 1
|
||||||
#comma:
|
commas:
|
||||||
# max-spaces-before: 0
|
max-spaces-before: 0
|
||||||
# max-spaces-after: 1
|
max-spaces-after: 1
|
||||||
#comment:
|
comments:
|
||||||
# min-spaces-after: 1
|
level: warning
|
||||||
# min-spaces-before: 2
|
require-starting-space: yes
|
||||||
|
min-spaces-from-content: 2
|
||||||
|
comments-indentation:
|
||||||
|
level: warning
|
||||||
document-end: disable
|
document-end: disable
|
||||||
document-start:
|
document-start:
|
||||||
level: warning
|
level: warning
|
||||||
@@ -24,16 +31,12 @@ rules:
|
|||||||
max-spaces-after: 1
|
max-spaces-after: 1
|
||||||
indentation:
|
indentation:
|
||||||
spaces: 2
|
spaces: 2
|
||||||
#comments: yes
|
|
||||||
line-length:
|
line-length:
|
||||||
max: 80
|
max: 80
|
||||||
new-line-at-end-of-file: {level: error}
|
new-line-at-end-of-file: {level: error}
|
||||||
new-lines:
|
new-lines:
|
||||||
type: unix
|
type: unix
|
||||||
#spaces-in-brackets: [ 1, 2 ]
|
#sequences-indentation:
|
||||||
# min: 1
|
# level: warning
|
||||||
# max: 1
|
# present: yes
|
||||||
#spaces-in-braces: { df: d }
|
|
||||||
# min: 1
|
|
||||||
# max: 1
|
|
||||||
trailing-spaces: {}
|
trailing-spaces: {}
|
||||||
|
|||||||
@@ -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)
|
|
||||||
|
|||||||
@@ -15,7 +15,12 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from yamllint.rules import (
|
from yamllint.rules import (
|
||||||
|
braces,
|
||||||
|
brackets,
|
||||||
colons,
|
colons,
|
||||||
|
commas,
|
||||||
|
comments,
|
||||||
|
comments_indentation,
|
||||||
document_end,
|
document_end,
|
||||||
document_start,
|
document_start,
|
||||||
empty_lines,
|
empty_lines,
|
||||||
@@ -28,7 +33,12 @@ from yamllint.rules import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
_RULES = {
|
_RULES = {
|
||||||
|
braces.ID: braces,
|
||||||
|
brackets.ID: brackets,
|
||||||
colons.ID: colons,
|
colons.ID: colons,
|
||||||
|
commas.ID: commas,
|
||||||
|
comments.ID: comments,
|
||||||
|
comments_indentation.ID: comments_indentation,
|
||||||
document_end.ID: document_end,
|
document_end.ID: document_end,
|
||||||
document_start.ID: document_start,
|
document_start.ID: document_start,
|
||||||
empty_lines.ID: empty_lines,
|
empty_lines.ID: empty_lines,
|
||||||
|
|||||||
47
yamllint/rules/braces.py
Normal file
47
yamllint/rules/braces.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
# -*- 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/>.
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from yamllint.rules.common import spaces_after, spaces_before
|
||||||
|
|
||||||
|
|
||||||
|
ID = 'braces'
|
||||||
|
TYPE = 'token'
|
||||||
|
CONF = {'min-spaces-inside': int,
|
||||||
|
'max-spaces-inside': int}
|
||||||
|
|
||||||
|
|
||||||
|
def check(conf, token, prev, next):
|
||||||
|
if isinstance(token, yaml.FlowMappingStartToken):
|
||||||
|
problem = spaces_after(token, prev, next,
|
||||||
|
min=conf['min-spaces-inside'],
|
||||||
|
max=conf['max-spaces-inside'],
|
||||||
|
min_desc='too few spaces inside braces',
|
||||||
|
max_desc='too many spaces inside braces')
|
||||||
|
if problem is not None:
|
||||||
|
yield problem
|
||||||
|
|
||||||
|
elif (isinstance(token, yaml.FlowMappingEndToken) and
|
||||||
|
(prev is None or
|
||||||
|
not isinstance(prev, yaml.FlowMappingStartToken))):
|
||||||
|
problem = spaces_before(token, prev, next,
|
||||||
|
min=conf['min-spaces-inside'],
|
||||||
|
max=conf['max-spaces-inside'],
|
||||||
|
min_desc='too few spaces inside braces',
|
||||||
|
max_desc='too many spaces inside braces')
|
||||||
|
if problem is not None:
|
||||||
|
yield problem
|
||||||
47
yamllint/rules/brackets.py
Normal file
47
yamllint/rules/brackets.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
# -*- 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/>.
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from yamllint.rules.common import spaces_after, spaces_before
|
||||||
|
|
||||||
|
|
||||||
|
ID = 'brackets'
|
||||||
|
TYPE = 'token'
|
||||||
|
CONF = {'min-spaces-inside': int,
|
||||||
|
'max-spaces-inside': int}
|
||||||
|
|
||||||
|
|
||||||
|
def check(conf, token, prev, next):
|
||||||
|
if isinstance(token, yaml.FlowSequenceStartToken):
|
||||||
|
problem = spaces_after(token, prev, next,
|
||||||
|
min=conf['min-spaces-inside'],
|
||||||
|
max=conf['max-spaces-inside'],
|
||||||
|
min_desc='too few spaces inside brackets',
|
||||||
|
max_desc='too many spaces inside brackets')
|
||||||
|
if problem is not None:
|
||||||
|
yield problem
|
||||||
|
|
||||||
|
elif (isinstance(token, yaml.FlowSequenceEndToken) and
|
||||||
|
(prev is None or
|
||||||
|
not isinstance(prev, yaml.FlowSequenceStartToken))):
|
||||||
|
problem = spaces_before(token, prev, next,
|
||||||
|
min=conf['min-spaces-inside'],
|
||||||
|
max=conf['max-spaces-inside'],
|
||||||
|
min_desc='too few spaces inside brackets',
|
||||||
|
max_desc='too many spaces inside brackets')
|
||||||
|
if problem is not None:
|
||||||
|
yield problem
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from yamllint.errors import LintProblem
|
from yamllint.rules.common import spaces_after, spaces_before
|
||||||
|
|
||||||
|
|
||||||
ID = 'colons'
|
ID = 'colons'
|
||||||
@@ -27,20 +27,14 @@ CONF = {'max-spaces-before': int,
|
|||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next):
|
||||||
if isinstance(token, yaml.ValueToken):
|
if isinstance(token, yaml.ValueToken):
|
||||||
if (prev is not None and
|
problem = spaces_before(token, prev, next,
|
||||||
prev.end_mark.line == token.start_mark.line and
|
max=conf['max-spaces-before'],
|
||||||
conf['max-spaces-before'] != - 1 and
|
max_desc='too many spaces before colon')
|
||||||
(prev.end_mark.pointer + conf['max-spaces-before'] <
|
if problem is not None:
|
||||||
token.start_mark.pointer)):
|
yield problem
|
||||||
yield LintProblem(token.start_mark.line + 1,
|
|
||||||
token.start_mark.column,
|
|
||||||
'too many spaces before colon')
|
|
||||||
|
|
||||||
if (next is not None and
|
problem = spaces_after(token, prev, next,
|
||||||
token.end_mark.line == next.start_mark.line and
|
max=conf['max-spaces-after'],
|
||||||
conf['max-spaces-after'] != - 1 and
|
max_desc='too many spaces after colon')
|
||||||
(next.start_mark.pointer - token.end_mark.pointer >
|
if problem is not None:
|
||||||
conf['max-spaces-after'])):
|
yield problem
|
||||||
yield LintProblem(token.start_mark.line + 1,
|
|
||||||
next.start_mark.column,
|
|
||||||
'too many spaces after colon')
|
|
||||||
|
|||||||
40
yamllint/rules/commas.py
Normal file
40
yamllint/rules/commas.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
# -*- 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/>.
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from yamllint.rules.common import spaces_after, spaces_before
|
||||||
|
|
||||||
|
|
||||||
|
ID = 'commas'
|
||||||
|
TYPE = 'token'
|
||||||
|
CONF = {'max-spaces-before': int,
|
||||||
|
'max-spaces-after': int}
|
||||||
|
|
||||||
|
|
||||||
|
def check(conf, token, prev, next):
|
||||||
|
if isinstance(token, yaml.FlowEntryToken):
|
||||||
|
problem = spaces_before(token, prev, next,
|
||||||
|
max=conf['max-spaces-before'],
|
||||||
|
max_desc='too many spaces before comma')
|
||||||
|
if problem is not None:
|
||||||
|
yield problem
|
||||||
|
|
||||||
|
problem = spaces_after(token, prev, next,
|
||||||
|
max=conf['max-spaces-after'],
|
||||||
|
max_desc='too many spaces after comma')
|
||||||
|
if problem is not None:
|
||||||
|
yield problem
|
||||||
44
yamllint/rules/comments.py
Normal file
44
yamllint/rules/comments.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
# -*- 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/>.
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from yamllint.errors import LintProblem
|
||||||
|
from yamllint.rules.common import get_comments_between_tokens
|
||||||
|
|
||||||
|
|
||||||
|
ID = 'comments'
|
||||||
|
TYPE = 'token'
|
||||||
|
CONF = {'require-starting-space': bool,
|
||||||
|
'min-spaces-from-content': int}
|
||||||
|
|
||||||
|
|
||||||
|
def check(conf, token, prev, next):
|
||||||
|
for comment in get_comments_between_tokens(token, next):
|
||||||
|
if (conf['min-spaces-from-content'] != -1 and
|
||||||
|
not isinstance(token, yaml.StreamStartToken) and
|
||||||
|
comment.line == token.end_mark.line + 1 and
|
||||||
|
comment.pointer - token.end_mark.pointer <
|
||||||
|
conf['min-spaces-from-content']):
|
||||||
|
yield LintProblem(comment.line, comment.column,
|
||||||
|
'too few spaces before comment')
|
||||||
|
|
||||||
|
if (conf['require-starting-space'] and
|
||||||
|
comment.pointer + 1 < len(comment.buffer) and
|
||||||
|
comment.buffer[comment.pointer + 1] != ' ' and
|
||||||
|
comment.buffer[comment.pointer + 1] != '\n'):
|
||||||
|
yield LintProblem(comment.line, comment.column + 1,
|
||||||
|
'missing starting space in comment')
|
||||||
43
yamllint/rules/comments_indentation.py
Normal file
43
yamllint/rules/comments_indentation.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
# -*- 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/>.
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from yamllint.errors import LintProblem
|
||||||
|
from yamllint.rules.common import get_comments_between_tokens
|
||||||
|
|
||||||
|
|
||||||
|
ID = 'comments-indentation'
|
||||||
|
TYPE = 'token'
|
||||||
|
|
||||||
|
|
||||||
|
def check(conf, token, prev, next):
|
||||||
|
if prev is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
token_indent = token.start_mark.column
|
||||||
|
if isinstance(token, yaml.StreamEndToken):
|
||||||
|
token_indent = 0
|
||||||
|
|
||||||
|
skip_first = True
|
||||||
|
if isinstance(prev, yaml.StreamStartToken):
|
||||||
|
skip_first = False
|
||||||
|
|
||||||
|
for comment in get_comments_between_tokens(prev, token,
|
||||||
|
skip_first_line=skip_first):
|
||||||
|
if comment.column != token_indent + 1:
|
||||||
|
yield LintProblem(comment.line, comment.column,
|
||||||
|
'comment not intended like content')
|
||||||
93
yamllint/rules/common.py
Normal file
93
yamllint/rules/common.py
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
# -*- 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/>.
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from yamllint.errors import LintProblem
|
||||||
|
|
||||||
|
|
||||||
|
def spaces_after(token, prev, next, min=-1, max=-1,
|
||||||
|
min_desc=None, max_desc=None):
|
||||||
|
if next is not None and token.end_mark.line == next.start_mark.line:
|
||||||
|
spaces = next.start_mark.pointer - token.end_mark.pointer
|
||||||
|
if max != - 1 and spaces > max:
|
||||||
|
return LintProblem(token.start_mark.line + 1,
|
||||||
|
next.start_mark.column, max_desc)
|
||||||
|
elif min != - 1 and spaces < min:
|
||||||
|
return LintProblem(token.start_mark.line + 1,
|
||||||
|
next.start_mark.column + 1, min_desc)
|
||||||
|
|
||||||
|
|
||||||
|
def spaces_before(token, prev, next, min=-1, max=-1,
|
||||||
|
min_desc=None, max_desc=None):
|
||||||
|
if prev is not None and prev.end_mark.line == token.start_mark.line:
|
||||||
|
spaces = token.start_mark.pointer - prev.end_mark.pointer
|
||||||
|
if max != - 1 and spaces > max:
|
||||||
|
return LintProblem(token.start_mark.line + 1,
|
||||||
|
token.start_mark.column, max_desc)
|
||||||
|
elif min != - 1 and spaces < min:
|
||||||
|
return LintProblem(token.start_mark.line + 1,
|
||||||
|
token.start_mark.column + 1, min_desc)
|
||||||
|
|
||||||
|
|
||||||
|
class Comment(object):
|
||||||
|
def __init__(self, line, column, buffer, pointer):
|
||||||
|
self.line = line
|
||||||
|
self.column = column
|
||||||
|
self.buffer = buffer
|
||||||
|
self.pointer = pointer
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
end = self.buffer.find('\n', self.pointer)
|
||||||
|
if end == -1:
|
||||||
|
end = self.buffer.find('\0', self.pointer)
|
||||||
|
if end != -1:
|
||||||
|
return self.buffer[self.pointer:end]
|
||||||
|
return self.buffer[self.pointer:]
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return (self.line == other.line and
|
||||||
|
self.column == other.column and
|
||||||
|
str(self) == str(other))
|
||||||
|
|
||||||
|
|
||||||
|
def get_comments_between_tokens(token1, token2, skip_first_line=False):
|
||||||
|
if token2 is None:
|
||||||
|
buf = token1.end_mark.buffer[token1.end_mark.pointer:]
|
||||||
|
elif (token1.end_mark.line == token2.start_mark.line and
|
||||||
|
not isinstance(token1, yaml.StreamStartToken) and
|
||||||
|
not isinstance(token2, yaml.StreamEndToken)):
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
buf = token1.end_mark.buffer[token1.end_mark.pointer:
|
||||||
|
token2.start_mark.pointer]
|
||||||
|
|
||||||
|
line_no = token1.end_mark.line + 1
|
||||||
|
column_no = token1.end_mark.column + 1
|
||||||
|
pointer = token1.end_mark.pointer
|
||||||
|
|
||||||
|
for line in buf.split('\n'):
|
||||||
|
if skip_first_line:
|
||||||
|
skip_first_line = False
|
||||||
|
else:
|
||||||
|
pos = line.find('#')
|
||||||
|
if pos != -1:
|
||||||
|
yield Comment(line_no, column_no + pos,
|
||||||
|
token1.end_mark.buffer, pointer + pos)
|
||||||
|
|
||||||
|
pointer += len(line) + 1
|
||||||
|
line_no += 1
|
||||||
|
column_no = 1
|
||||||
@@ -24,15 +24,14 @@ TYPE = 'token'
|
|||||||
CONF = {'present': bool}
|
CONF = {'present': bool}
|
||||||
|
|
||||||
|
|
||||||
# TODO: Don't fail if document contains directives such as
|
|
||||||
# %YAML 1.2
|
|
||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next):
|
||||||
if conf['present']:
|
if conf['present']:
|
||||||
if ((isinstance(prev, yaml.StreamStartToken) or
|
if (isinstance(prev, (yaml.StreamStartToken,
|
||||||
isinstance(prev, yaml.DocumentEndToken)) and
|
yaml.DocumentEndToken,
|
||||||
not (isinstance(token, yaml.DocumentStartToken) or
|
yaml.DirectiveToken)) and
|
||||||
isinstance(token, yaml.StreamEndToken))):
|
not isinstance(token, (yaml.DocumentStartToken,
|
||||||
|
yaml.DirectiveToken,
|
||||||
|
yaml.StreamEndToken))):
|
||||||
yield LintProblem(token.start_mark.line + 1, 1,
|
yield LintProblem(token.start_mark.line + 1, 1,
|
||||||
'missing document start "---"')
|
'missing document start "---"')
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from yamllint.errors import LintProblem
|
from yamllint.rules.common import spaces_after
|
||||||
|
|
||||||
|
|
||||||
ID = 'hyphens'
|
ID = 'hyphens'
|
||||||
@@ -26,9 +26,8 @@ CONF = {'max-spaces-after': int}
|
|||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next):
|
||||||
if isinstance(token, yaml.BlockEntryToken):
|
if isinstance(token, yaml.BlockEntryToken):
|
||||||
if token.end_mark.line == next.start_mark.line:
|
problem = spaces_after(token, prev, next,
|
||||||
if (next.start_mark.pointer - token.end_mark.pointer >
|
max=conf['max-spaces-after'],
|
||||||
conf['max-spaces-after']):
|
max_desc='too many spaces after hyphen')
|
||||||
yield LintProblem(token.start_mark.line + 1,
|
if problem is not None:
|
||||||
next.start_mark.column,
|
yield problem
|
||||||
'too many spaces after hyphen')
|
|
||||||
|
|||||||
@@ -25,39 +25,46 @@ CONF = {'spaces': int}
|
|||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next):
|
||||||
if isinstance(token, yaml.StreamEndToken):
|
if isinstance(token, (yaml.StreamStartToken, yaml.StreamEndToken)):
|
||||||
return
|
return
|
||||||
|
|
||||||
if (prev is None or isinstance(prev, yaml.StreamStartToken) or
|
# Check if first token in line
|
||||||
isinstance(prev, yaml.DirectiveToken) or
|
if (not isinstance(prev, (yaml.StreamStartToken, yaml.DirectiveToken)) and
|
||||||
isinstance(prev, yaml.DocumentStartToken)):
|
token.start_mark.line == prev.end_mark.line):
|
||||||
if token.start_mark.column != 0:
|
|
||||||
yield LintProblem(
|
|
||||||
token.end_mark.line + 1, token.start_mark.column + 1,
|
|
||||||
'found indentation of %d instead of %d' %
|
|
||||||
(token.start_mark.column, 0))
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if token.start_mark.line > prev.end_mark.line:
|
if token.start_mark.column % conf['spaces'] != 0:
|
||||||
|
yield LintProblem(
|
||||||
|
token.end_mark.line + 1, token.start_mark.column + 1,
|
||||||
|
'indentation is not a multiple of %d' % conf['spaces'])
|
||||||
|
return
|
||||||
|
|
||||||
|
if isinstance(prev, (yaml.StreamStartToken,
|
||||||
|
yaml.DirectiveToken,
|
||||||
|
yaml.DocumentStartToken,
|
||||||
|
yaml.DocumentEndToken)):
|
||||||
|
indent = 0
|
||||||
|
else:
|
||||||
buffer = prev.end_mark.buffer
|
buffer = prev.end_mark.buffer
|
||||||
|
|
||||||
start = buffer.rfind('\n', 0, prev.end_mark.pointer) + 1
|
start = buffer.rfind('\n', 0, prev.end_mark.pointer) + 1
|
||||||
prev_indent = 0
|
|
||||||
|
|
||||||
# YAML recognizes two white space characters: space and tab.
|
indent = 0
|
||||||
# http://yaml.org/spec/1.2/spec.html#id2775170
|
while buffer[start + indent] == ' ':
|
||||||
while buffer[start + prev_indent] in ' \t':
|
indent += 1
|
||||||
prev_indent += 1
|
|
||||||
|
|
||||||
# Discard any leading '- '
|
if token.start_mark.column > indent:
|
||||||
if (buffer[start + prev_indent:start + prev_indent + 2] == '- '):
|
if not isinstance(prev, (yaml.BlockSequenceStartToken,
|
||||||
prev_indent += 2
|
yaml.BlockMappingStartToken,
|
||||||
while buffer[start + prev_indent] in ' \t':
|
yaml.FlowSequenceStartToken,
|
||||||
prev_indent += 1
|
yaml.FlowMappingStartToken,
|
||||||
|
yaml.KeyToken,
|
||||||
|
yaml.ValueToken)):
|
||||||
|
yield LintProblem(
|
||||||
|
token.end_mark.line + 1, token.start_mark.column + 1,
|
||||||
|
'unexpected indentation')
|
||||||
|
|
||||||
if (token.start_mark.column > prev_indent and
|
elif token.start_mark.column != indent + conf['spaces']:
|
||||||
token.start_mark.column != prev_indent + conf['spaces']):
|
|
||||||
yield LintProblem(
|
yield LintProblem(
|
||||||
token.end_mark.line + 1, token.start_mark.column + 1,
|
token.end_mark.line + 1, token.start_mark.column + 1,
|
||||||
'found indentation of %d instead of %d' %
|
'found indentation of %d instead of %d' %
|
||||||
(token.start_mark.column, prev_indent + conf['spaces']))
|
(token.start_mark.column, indent + conf['spaces']))
|
||||||
|
|||||||
Reference in New Issue
Block a user