Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
559ad5574b | ||
|
|
adcb2d2953 | ||
|
|
e948509fe5 | ||
|
|
6dae8f5b6e | ||
|
|
073462a87d | ||
|
|
4b9ba9e201 | ||
|
|
5294ff5552 | ||
|
|
1f472bc144 | ||
|
|
c163135ee5 | ||
|
|
f656cf42d2 | ||
|
|
9b72a2d29a | ||
|
|
d7c17c7e7c |
@@ -16,7 +16,7 @@ indentation, etc.
|
|||||||
:target: https://coveralls.io/github/adrienverge/yamllint?branch=master
|
:target: https://coveralls.io/github/adrienverge/yamllint?branch=master
|
||||||
:alt: Code coverage status
|
:alt: Code coverage status
|
||||||
.. image:: https://readthedocs.org/projects/yamllint/badge/?version=latest
|
.. image:: https://readthedocs.org/projects/yamllint/badge/?version=latest
|
||||||
:target: http://yamllint.readthedocs.org/en/latest/?badge=latest
|
:target: https://yamllint.readthedocs.io/en/latest/?badge=latest
|
||||||
:alt: Documentation status
|
:alt: Documentation status
|
||||||
|
|
||||||
Written in Python (compatible with Python 2 & 3).
|
Written in Python (compatible with Python 2 & 3).
|
||||||
@@ -24,7 +24,7 @@ Written in Python (compatible with Python 2 & 3).
|
|||||||
Documentation
|
Documentation
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
http://yamllint.readthedocs.io/
|
https://yamllint.readthedocs.io/
|
||||||
|
|
||||||
Overview
|
Overview
|
||||||
--------
|
--------
|
||||||
@@ -82,7 +82,7 @@ Usage
|
|||||||
# Output a parsable format (for syntax checking in editors like Vim, emacs...)
|
# Output a parsable format (for syntax checking in editors like Vim, emacs...)
|
||||||
yamllint -f parsable file.yaml
|
yamllint -f parsable file.yaml
|
||||||
|
|
||||||
`Read more in the complete documentation! <http://yamllint.readthedocs.io/>`_
|
`Read more in the complete documentation! <https://yamllint.readthedocs.io/>`_
|
||||||
|
|
||||||
Features
|
Features
|
||||||
^^^^^^^^
|
^^^^^^^^
|
||||||
@@ -119,7 +119,7 @@ or for a whole block:
|
|||||||
consectetur : adipiscing elit
|
consectetur : adipiscing elit
|
||||||
# yamllint enable
|
# yamllint enable
|
||||||
|
|
||||||
`Read more in the complete documentation! <http://yamllint.readthedocs.io/>`_
|
`Read more in the complete documentation! <https://yamllint.readthedocs.io/>`_
|
||||||
|
|
||||||
License
|
License
|
||||||
-------
|
-------
|
||||||
|
|||||||
@@ -93,3 +93,8 @@ trailing-spaces
|
|||||||
---------------
|
---------------
|
||||||
|
|
||||||
.. automodule:: yamllint.rules.trailing_spaces
|
.. automodule:: yamllint.rules.trailing_spaces
|
||||||
|
|
||||||
|
truthy
|
||||||
|
---------------
|
||||||
|
|
||||||
|
.. automodule:: yamllint.rules.truthy
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -42,7 +42,7 @@ setup(
|
|||||||
'Topic :: Software Development :: Testing',
|
'Topic :: Software Development :: Testing',
|
||||||
],
|
],
|
||||||
|
|
||||||
packages=find_packages(),
|
packages=find_packages(exclude=['tests', 'tests.*']),
|
||||||
entry_points={'console_scripts': ['yamllint=yamllint.cli:run']},
|
entry_points={'console_scripts': ['yamllint=yamllint.cli:run']},
|
||||||
package_data={'yamllint': ['conf/*.yaml'],
|
package_data={'yamllint': ['conf/*.yaml'],
|
||||||
'tests': ['yaml-1.2-spec-examples/*']},
|
'tests': ['yaml-1.2-spec-examples/*']},
|
||||||
|
|||||||
@@ -102,13 +102,13 @@ class CommentsIndentationTestCase(RuleTestCase):
|
|||||||
' a: 1\n'
|
' a: 1\n'
|
||||||
' # b: 2\n'
|
' # b: 2\n'
|
||||||
'# this object is useless\n'
|
'# this object is useless\n'
|
||||||
'obj2: no\n', conf)
|
'obj2: "no"\n', conf)
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
'obj1:\n'
|
'obj1:\n'
|
||||||
' a: 1\n'
|
' a: 1\n'
|
||||||
'# this object is useless\n'
|
'# this object is useless\n'
|
||||||
' # b: 2\n'
|
' # b: 2\n'
|
||||||
'obj2: no\n', conf, problem=(5, 3))
|
'obj2: "no"\n', conf, problem=(5, 3))
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
'obj1:\n'
|
'obj1:\n'
|
||||||
' a: 1\n'
|
' a: 1\n'
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ class LineLengthTestCase(RuleTestCase):
|
|||||||
self.check('---\n' + 81 * 'a' + '\n', conf)
|
self.check('---\n' + 81 * 'a' + '\n', conf)
|
||||||
self.check(1000 * 'b', conf)
|
self.check(1000 * 'b', conf)
|
||||||
self.check('---\n' + 1000 * 'b' + '\n', conf)
|
self.check('---\n' + 1000 * 'b' + '\n', conf)
|
||||||
|
self.check('content: |\n'
|
||||||
|
' {% this line is' + 99 * ' really' + ' long %}\n',
|
||||||
|
conf)
|
||||||
|
|
||||||
def test_default(self):
|
def test_default(self):
|
||||||
conf = ('line-length: {max: 80}\n'
|
conf = ('line-length: {max: 80}\n'
|
||||||
@@ -145,3 +148,10 @@ class LineLengthTestCase(RuleTestCase):
|
|||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
'- long line: and+some+space+at+the+end \n',
|
'- long line: and+some+space+at+the+end \n',
|
||||||
conf, problem=(2, 21))
|
conf, problem=(2, 21))
|
||||||
|
|
||||||
|
# See https://github.com/adrienverge/yamllint/issues/21
|
||||||
|
conf = 'line-length: {allow-non-breakable-inline-mappings: yes}'
|
||||||
|
self.check('---\n'
|
||||||
|
'content: |\n'
|
||||||
|
' {% this line is' + 99 * ' really' + ' long %}\n',
|
||||||
|
conf, problem=(3, 81))
|
||||||
|
|||||||
68
tests/rules/test_truthy.py
Normal file
68
tests/rules/test_truthy.py
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Copyright (C) 2016 Peter Ericson
|
||||||
|
#
|
||||||
|
# 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.common import RuleTestCase
|
||||||
|
|
||||||
|
|
||||||
|
class TruthyTestCase(RuleTestCase):
|
||||||
|
rule_id = 'truthy'
|
||||||
|
|
||||||
|
def test_disabled(self):
|
||||||
|
conf = 'truthy: disable'
|
||||||
|
self.check('---\n'
|
||||||
|
'1: True\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'True: 1\n', conf)
|
||||||
|
|
||||||
|
def test_enabled(self):
|
||||||
|
conf = 'truthy: enable\n'
|
||||||
|
self.check('---\n'
|
||||||
|
'1: True\n'
|
||||||
|
'True: 1\n',
|
||||||
|
conf, problem1=(2, 4), problem2=(3, 1))
|
||||||
|
self.check('---\n'
|
||||||
|
'1: "True"\n'
|
||||||
|
'"True": 1\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'[\n'
|
||||||
|
' true, false,\n'
|
||||||
|
' "false", "FALSE",\n'
|
||||||
|
' "true", "True",\n'
|
||||||
|
' True, FALSE,\n'
|
||||||
|
' on, OFF,\n'
|
||||||
|
' NO, Yes\n'
|
||||||
|
']\n', conf,
|
||||||
|
problem1=(6, 3), problem2=(6, 9),
|
||||||
|
problem3=(7, 3), problem4=(7, 7),
|
||||||
|
problem5=(8, 3), problem6=(8, 7))
|
||||||
|
|
||||||
|
def test_explicit_types(self):
|
||||||
|
conf = 'truthy: enable\n'
|
||||||
|
self.check('---\n'
|
||||||
|
'string1: !!str True\n'
|
||||||
|
'string2: !!str yes\n'
|
||||||
|
'string3: !!str off\n'
|
||||||
|
'encoded: !!binary |\n'
|
||||||
|
' True\n'
|
||||||
|
' OFF\n'
|
||||||
|
' pad==\n' # this decodes as 'N\xbb\x9e8Qii'
|
||||||
|
'boolean1: !!bool true\n'
|
||||||
|
'boolean2: !!bool "false"\n'
|
||||||
|
'boolean3: !!bool FALSE\n'
|
||||||
|
'boolean4: !!bool True\n'
|
||||||
|
'boolean5: !!bool off\n'
|
||||||
|
'boolean6: !!bool NO\n',
|
||||||
|
conf)
|
||||||
@@ -119,6 +119,8 @@ conf_overrides = {
|
|||||||
'example-8.22': ('indentation: disable\n'),
|
'example-8.22': ('indentation: disable\n'),
|
||||||
'example-10.1': ('colons: {max-spaces-before: 2}\n'),
|
'example-10.1': ('colons: {max-spaces-before: 2}\n'),
|
||||||
'example-10.2': ('indentation: {indent-sequences: no}\n'),
|
'example-10.2': ('indentation: {indent-sequences: no}\n'),
|
||||||
|
'example-10.8': ('truthy: disable\n'),
|
||||||
|
'example-10.9': ('truthy: disable\n'),
|
||||||
}
|
}
|
||||||
|
|
||||||
files = os.listdir(os.path.join(os.path.dirname(os.path.realpath(__file__)),
|
files = os.listdir(os.path.join(os.path.dirname(os.path.realpath(__file__)),
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ indentation, etc."""
|
|||||||
|
|
||||||
|
|
||||||
APP_NAME = 'yamllint'
|
APP_NAME = 'yamllint'
|
||||||
APP_VERSION = '1.4.0'
|
APP_VERSION = '1.5.0'
|
||||||
APP_DESCRIPTION = __doc__
|
APP_DESCRIPTION = __doc__
|
||||||
|
|
||||||
__author__ = u'Adrien Vergé'
|
__author__ = u'Adrien Vergé'
|
||||||
|
|||||||
@@ -43,3 +43,5 @@ rules:
|
|||||||
new-lines:
|
new-lines:
|
||||||
type: unix
|
type: unix
|
||||||
trailing-spaces: enable
|
trailing-spaces: enable
|
||||||
|
truthy:
|
||||||
|
level: warning
|
||||||
|
|||||||
@@ -26,3 +26,4 @@ rules:
|
|||||||
line-length:
|
line-length:
|
||||||
level: warning
|
level: warning
|
||||||
allow-non-breakable-inline-mappings: yes
|
allow-non-breakable-inline-mappings: yes
|
||||||
|
truthy: disable
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ from yamllint.rules import (
|
|||||||
new_line_at_end_of_file,
|
new_line_at_end_of_file,
|
||||||
new_lines,
|
new_lines,
|
||||||
trailing_spaces,
|
trailing_spaces,
|
||||||
|
truthy,
|
||||||
)
|
)
|
||||||
|
|
||||||
_RULES = {
|
_RULES = {
|
||||||
@@ -50,6 +51,7 @@ _RULES = {
|
|||||||
new_line_at_end_of_file.ID: new_line_at_end_of_file,
|
new_line_at_end_of_file.ID: new_line_at_end_of_file,
|
||||||
new_lines.ID: new_lines,
|
new_lines.ID: new_lines,
|
||||||
trailing_spaces.ID: trailing_spaces,
|
trailing_spaces.ID: trailing_spaces,
|
||||||
|
truthy.ID: truthy,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -102,13 +102,18 @@ CONF = {'max': int,
|
|||||||
|
|
||||||
def check_inline_mapping(line):
|
def check_inline_mapping(line):
|
||||||
loader = yaml.SafeLoader(line.content)
|
loader = yaml.SafeLoader(line.content)
|
||||||
while loader.peek_token():
|
try:
|
||||||
if isinstance(loader.get_token(), yaml.BlockMappingStartToken):
|
while loader.peek_token():
|
||||||
while loader.peek_token():
|
if isinstance(loader.get_token(), yaml.BlockMappingStartToken):
|
||||||
if isinstance(loader.get_token(), yaml.ValueToken):
|
while loader.peek_token():
|
||||||
t = loader.get_token()
|
if isinstance(loader.get_token(), yaml.ValueToken):
|
||||||
if isinstance(t, yaml.ScalarToken):
|
t = loader.get_token()
|
||||||
return ' ' not in line.content[t.start_mark.column:]
|
if isinstance(t, yaml.ScalarToken):
|
||||||
|
return (
|
||||||
|
' ' not in line.content[t.start_mark.column:])
|
||||||
|
except yaml.scanner.ScannerError:
|
||||||
|
pass
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
93
yamllint/rules/truthy.py
Normal file
93
yamllint/rules/truthy.py
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Copyright (C) 2016 Peter Ericson
|
||||||
|
#
|
||||||
|
# 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/>.
|
||||||
|
|
||||||
|
"""
|
||||||
|
Use this rule to forbid truthy values that are not quoted nor explicitly typed.
|
||||||
|
|
||||||
|
This would prevent YAML parsers to tranform ``[yes, FALSE, Off]`` into ``[true,
|
||||||
|
false, false]`` or ``{y: 1, yes: 2, on: 3, true: 4, True: 5}`` into ``{y: 1,
|
||||||
|
true: 5}``.
|
||||||
|
|
||||||
|
.. rubric:: Examples
|
||||||
|
|
||||||
|
#. With ``truthy: {}``
|
||||||
|
|
||||||
|
the following code snippet would **PASS**:
|
||||||
|
::
|
||||||
|
|
||||||
|
boolean: true
|
||||||
|
|
||||||
|
object: {"True": 1, 1: "True"}
|
||||||
|
|
||||||
|
"yes": 1
|
||||||
|
"on": 2
|
||||||
|
"true": 3
|
||||||
|
"True": 4
|
||||||
|
|
||||||
|
explicit:
|
||||||
|
string1: !!str True
|
||||||
|
string2: !!str yes
|
||||||
|
string3: !!str off
|
||||||
|
encoded: !!binary |
|
||||||
|
True
|
||||||
|
OFF
|
||||||
|
pad== # this decodes as 'N\xbb\x9e8Qii'
|
||||||
|
boolean1: !!bool true
|
||||||
|
boolean2: !!bool "false"
|
||||||
|
boolean3: !!bool FALSE
|
||||||
|
boolean4: !!bool True
|
||||||
|
boolean5: !!bool off
|
||||||
|
boolean6: !!bool NO
|
||||||
|
|
||||||
|
the following code snippet would **FAIL**:
|
||||||
|
::
|
||||||
|
|
||||||
|
object: {True: 1, 1: True}
|
||||||
|
|
||||||
|
the following code snippet would **FAIL**:
|
||||||
|
::
|
||||||
|
|
||||||
|
yes: 1
|
||||||
|
on: 2
|
||||||
|
true: 3
|
||||||
|
True: 4
|
||||||
|
"""
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from yamllint.linter import LintProblem
|
||||||
|
|
||||||
|
ID = 'truthy'
|
||||||
|
TYPE = 'token'
|
||||||
|
CONF = {}
|
||||||
|
|
||||||
|
TRUTHY = ['YES', 'Yes', 'yes',
|
||||||
|
'NO', 'No', 'no',
|
||||||
|
'TRUE', 'True', # 'true' is a boolean
|
||||||
|
'FALSE', 'False', # 'false' is a boolean
|
||||||
|
'ON', 'On', 'on',
|
||||||
|
'OFF', 'Off', 'off']
|
||||||
|
|
||||||
|
|
||||||
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
if prev and isinstance(prev, yaml.tokens.TagToken):
|
||||||
|
return
|
||||||
|
|
||||||
|
if isinstance(token, yaml.tokens.ScalarToken):
|
||||||
|
if token.value in TRUTHY and token.style is None:
|
||||||
|
yield LintProblem(token.start_mark.line + 1,
|
||||||
|
token.start_mark.column + 1,
|
||||||
|
"truthy value is not quoted")
|
||||||
Reference in New Issue
Block a user