Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
506e066410 | ||
|
|
54f21c0514 | ||
|
|
36b4776778 | ||
|
|
3bdc1b6e1b | ||
|
|
c16934117b | ||
|
|
8ab680635b | ||
|
|
503bde9e70 | ||
|
|
1b379628d7 | ||
|
|
6a842229fd | ||
|
|
8b9eab33bf | ||
|
|
22e792a433 | ||
|
|
f713dc8be2 |
@@ -1,7 +1,7 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
# For use with pre-commit.
|
# For use with pre-commit.
|
||||||
# See usage instructions at http://pre-commit.com
|
# See usage instructions at https://pre-commit.com
|
||||||
|
|
||||||
- id: yamllint
|
- id: yamllint
|
||||||
name: yamllint
|
name: yamllint
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ python:
|
|||||||
- 3.6
|
- 3.6
|
||||||
- nightly
|
- nightly
|
||||||
install:
|
install:
|
||||||
- pip install pyyaml flake8 flake8-import-order coveralls sphinx
|
- pip install pyyaml flake8 flake8-import-order coveralls
|
||||||
- if [[ $TRAVIS_PYTHON_VERSION == 2.6 ]]; then pip install unittest2; fi
|
- if [[ $TRAVIS_PYTHON_VERSION == 2.6 ]]; then pip install unittest2; fi
|
||||||
|
- if [[ $TRAVIS_PYTHON_VERSION != 2* ]]; then pip install sphinx; fi
|
||||||
- pip install .
|
- pip install .
|
||||||
script:
|
script:
|
||||||
- if [[ $TRAVIS_PYTHON_VERSION != 2.6 ]]; then flake8 .; fi
|
- if [[ $TRAVIS_PYTHON_VERSION != 2.6 ]]; then flake8 .; fi
|
||||||
|
|||||||
@@ -1,6 +1,20 @@
|
|||||||
Changelog
|
Changelog
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
1.11.1 (2018-04-06)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Handle merge keys (`<<`) in the `key-duplicates` rule
|
||||||
|
- Update documentation about pre-commit
|
||||||
|
- Make examples for `ignore` rule clearer
|
||||||
|
- Clarify documentation on the 'truthy' rule
|
||||||
|
- Fix crash in parser due to a change in PyYAML > 3.12
|
||||||
|
|
||||||
|
1.11.0 (2018-02-21)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Add a new `octal-values` rule
|
||||||
|
|
||||||
1.10.0 (2017-11-05)
|
1.10.0 (2017-11-05)
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ You can either totally ignore files (they won't be looked at):
|
|||||||
|
|
||||||
ignore: |
|
ignore: |
|
||||||
/this/specific/file.yaml
|
/this/specific/file.yaml
|
||||||
/all/this/directory/
|
all/this/directory/
|
||||||
*.template.yaml
|
*.template.yaml
|
||||||
|
|
||||||
or ignore paths only for specific rules:
|
or ignore paths only for specific rules:
|
||||||
@@ -167,4 +167,4 @@ Here is a more complex example:
|
|||||||
trailing-spaces:
|
trailing-spaces:
|
||||||
ignore: |
|
ignore: |
|
||||||
*.ignore-trailing-spaces.yaml
|
*.ignore-trailing-spaces.yaml
|
||||||
/ascii-art/*
|
ascii-art/*
|
||||||
|
|||||||
@@ -99,6 +99,11 @@ new-lines
|
|||||||
|
|
||||||
.. automodule:: yamllint.rules.new_lines
|
.. automodule:: yamllint.rules.new_lines
|
||||||
|
|
||||||
|
octal-values
|
||||||
|
------------
|
||||||
|
|
||||||
|
.. automodule:: yamllint.rules.octal_values
|
||||||
|
|
||||||
trailing-spaces
|
trailing-spaces
|
||||||
---------------
|
---------------
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ universal = 1
|
|||||||
|
|
||||||
[flake8]
|
[flake8]
|
||||||
import-order-style = pep8
|
import-order-style = pep8
|
||||||
|
application-import-names = yamllint
|
||||||
|
|
||||||
[build_sphinx]
|
[build_sphinx]
|
||||||
all-files = 1
|
all-files = 1
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
# 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 tests.common import RuleTestCase
|
from tests.common import RuleTestCase
|
||||||
|
|
||||||
from yamllint.parser import token_or_comment_generator, Comment
|
from yamllint.parser import token_or_comment_generator, Comment
|
||||||
from yamllint.rules.indentation import check
|
from yamllint.rules.indentation import check
|
||||||
|
|
||||||
|
|||||||
@@ -78,6 +78,15 @@ class KeyDuplicatesTestCase(RuleTestCase):
|
|||||||
' duplicates with\n'
|
' duplicates with\n'
|
||||||
' many styles\n'
|
' many styles\n'
|
||||||
': 1\n', conf)
|
': 1\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'Merge Keys are OK:\n'
|
||||||
|
'anchor_one: &anchor_one\n'
|
||||||
|
' one: one\n'
|
||||||
|
'anchor_two: &anchor_two\n'
|
||||||
|
' two: two\n'
|
||||||
|
'anchor_reference:\n'
|
||||||
|
' <<: *anchor_one\n'
|
||||||
|
' <<: *anchor_two\n', conf)
|
||||||
|
|
||||||
def test_enabled(self):
|
def test_enabled(self):
|
||||||
conf = 'key-duplicates: enable'
|
conf = 'key-duplicates: enable'
|
||||||
@@ -147,6 +156,15 @@ class KeyDuplicatesTestCase(RuleTestCase):
|
|||||||
': 1\n', conf,
|
': 1\n', conf,
|
||||||
problem1=(3, 1), problem2=(4, 1), problem3=(5, 3),
|
problem1=(3, 1), problem2=(4, 1), problem3=(5, 3),
|
||||||
problem4=(7, 3))
|
problem4=(7, 3))
|
||||||
|
self.check('---\n'
|
||||||
|
'Merge Keys are OK:\n'
|
||||||
|
'anchor_one: &anchor_one\n'
|
||||||
|
' one: one\n'
|
||||||
|
'anchor_two: &anchor_two\n'
|
||||||
|
' two: two\n'
|
||||||
|
'anchor_reference:\n'
|
||||||
|
' <<: *anchor_one\n'
|
||||||
|
' <<: *anchor_two\n', conf)
|
||||||
|
|
||||||
def test_key_tokens_in_flow_sequences(self):
|
def test_key_tokens_in_flow_sequences(self):
|
||||||
conf = 'key-duplicates: enable'
|
conf = 'key-duplicates: enable'
|
||||||
|
|||||||
72
tests/rules/test_octal_values.py
Normal file
72
tests/rules/test_octal_values.py
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
# -*- 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.common import RuleTestCase
|
||||||
|
|
||||||
|
|
||||||
|
class OctalValuesTestCase(RuleTestCase):
|
||||||
|
rule_id = 'octal-values'
|
||||||
|
|
||||||
|
def test_disabled(self):
|
||||||
|
conf = ('octal-values: disable\n'
|
||||||
|
'new-line-at-end-of-file: disable\n'
|
||||||
|
'document-start: disable\n')
|
||||||
|
self.check('user-city: 010', conf)
|
||||||
|
self.check('user-city: 0o10', conf)
|
||||||
|
|
||||||
|
def test_implicit_octal_values(self):
|
||||||
|
conf = ('octal-values: {forbid-implicit-octal: true}\n'
|
||||||
|
'new-line-at-end-of-file: disable\n'
|
||||||
|
'document-start: disable\n')
|
||||||
|
self.check('user-city: 010', conf, problem=(1, 15))
|
||||||
|
self.check('user-city: abc', conf)
|
||||||
|
self.check('user-city: 010,0571', conf)
|
||||||
|
self.check("user-city: '010'", conf)
|
||||||
|
self.check('user-city: "010"', conf)
|
||||||
|
self.check('user-city:\n'
|
||||||
|
' - 010', conf, problem=(2, 8))
|
||||||
|
self.check('user-city: [010]', conf, problem=(1, 16))
|
||||||
|
self.check('user-city: {beijing: 010}', conf, problem=(1, 25))
|
||||||
|
self.check('explicit-octal: 0o10', conf)
|
||||||
|
self.check('not-number: 0abc', conf)
|
||||||
|
self.check('zero: 0', conf)
|
||||||
|
self.check('hex-value: 0x10', conf)
|
||||||
|
self.check('number-values:\n'
|
||||||
|
' - 0.10\n'
|
||||||
|
' - .01\n'
|
||||||
|
' - 0e3\n', conf)
|
||||||
|
|
||||||
|
def test_explicit_octal_values(self):
|
||||||
|
conf = ('octal-values: {forbid-explicit-octal: true}\n'
|
||||||
|
'new-line-at-end-of-file: disable\n'
|
||||||
|
'document-start: disable\n')
|
||||||
|
self.check('user-city: 0o10', conf, problem=(1, 16))
|
||||||
|
self.check('user-city: abc', conf)
|
||||||
|
self.check('user-city: 0o10,0571', conf)
|
||||||
|
self.check("user-city: '0o10'", conf)
|
||||||
|
self.check('user-city:\n'
|
||||||
|
' - 0o10', conf, problem=(2, 9))
|
||||||
|
self.check('user-city: [0o10]', conf, problem=(1, 17))
|
||||||
|
self.check('user-city: {beijing: 0o10}', conf, problem=(1, 26))
|
||||||
|
self.check('implicit-octal: 010', conf)
|
||||||
|
self.check('not-number: 0oabc', conf)
|
||||||
|
self.check('zero: 0', conf)
|
||||||
|
self.check('hex-value: 0x10', conf)
|
||||||
|
self.check('number-values:\n'
|
||||||
|
' - 0.10\n'
|
||||||
|
' - .01\n'
|
||||||
|
' - 0e3\n', conf)
|
||||||
|
self.check('user-city: "010"', conf)
|
||||||
@@ -30,10 +30,10 @@ try:
|
|||||||
except AssertionError:
|
except AssertionError:
|
||||||
import unittest2 as unittest
|
import unittest2 as unittest
|
||||||
|
|
||||||
from yamllint import cli
|
|
||||||
|
|
||||||
from tests.common import build_temp_workspace
|
from tests.common import build_temp_workspace
|
||||||
|
|
||||||
|
from yamllint import cli
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipIf(sys.version_info < (2, 7), 'Python 2.6 not supported')
|
@unittest.skipIf(sys.version_info < (2, 7), 'Python 2.6 not supported')
|
||||||
class CommandLineTestCase(unittest.TestCase):
|
class CommandLineTestCase(unittest.TestCase):
|
||||||
|
|||||||
@@ -27,11 +27,11 @@ try:
|
|||||||
except AssertionError:
|
except AssertionError:
|
||||||
import unittest2 as unittest
|
import unittest2 as unittest
|
||||||
|
|
||||||
|
from tests.common import build_temp_workspace
|
||||||
|
|
||||||
from yamllint import cli
|
from yamllint import cli
|
||||||
from yamllint import config
|
from yamllint import config
|
||||||
|
|
||||||
from tests.common import build_temp_workspace
|
|
||||||
|
|
||||||
|
|
||||||
class SimpleConfigTestCase(unittest.TestCase):
|
class SimpleConfigTestCase(unittest.TestCase):
|
||||||
def test_parse_config(self):
|
def test_parse_config(self):
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ indentation, etc."""
|
|||||||
|
|
||||||
|
|
||||||
APP_NAME = 'yamllint'
|
APP_NAME = 'yamllint'
|
||||||
APP_VERSION = '1.10.0'
|
APP_VERSION = '1.11.1'
|
||||||
APP_DESCRIPTION = __doc__
|
APP_DESCRIPTION = __doc__
|
||||||
|
|
||||||
__author__ = u'Adrien Vergé'
|
__author__ = u'Adrien Vergé'
|
||||||
|
|||||||
@@ -50,6 +50,9 @@ rules:
|
|||||||
new-line-at-end-of-file: enable
|
new-line-at-end-of-file: enable
|
||||||
new-lines:
|
new-lines:
|
||||||
type: unix
|
type: unix
|
||||||
|
octal-values:
|
||||||
|
forbid-implicit-octal: false
|
||||||
|
forbid-explicit-octal: false
|
||||||
trailing-spaces: enable
|
trailing-spaces: enable
|
||||||
truthy:
|
truthy:
|
||||||
level: warning
|
level: warning
|
||||||
|
|||||||
@@ -125,7 +125,8 @@ def token_or_comment_generator(buffer):
|
|||||||
curr = yaml_loader.get_token()
|
curr = yaml_loader.get_token()
|
||||||
while curr is not None:
|
while curr is not None:
|
||||||
next = yaml_loader.get_token()
|
next = yaml_loader.get_token()
|
||||||
nextnext = yaml_loader.peek_token()
|
nextnext = (yaml_loader.peek_token()
|
||||||
|
if yaml_loader.check_token() else None)
|
||||||
|
|
||||||
yield Token(curr.start_mark.line + 1, curr, prev, next, nextnext)
|
yield Token(curr.start_mark.line + 1, curr, prev, next, nextnext)
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ from yamllint.rules import (
|
|||||||
line_length,
|
line_length,
|
||||||
new_line_at_end_of_file,
|
new_line_at_end_of_file,
|
||||||
new_lines,
|
new_lines,
|
||||||
|
octal_values,
|
||||||
trailing_spaces,
|
trailing_spaces,
|
||||||
truthy,
|
truthy,
|
||||||
)
|
)
|
||||||
@@ -54,6 +55,7 @@ _RULES = {
|
|||||||
line_length.ID: line_length,
|
line_length.ID: line_length,
|
||||||
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,
|
||||||
|
octal_values.ID: octal_values,
|
||||||
trailing_spaces.ID: trailing_spaces,
|
trailing_spaces.ID: trailing_spaces,
|
||||||
truthy.ID: truthy,
|
truthy.ID: truthy,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,7 +91,9 @@ def check(conf, token, prev, next, nextnext, context):
|
|||||||
# This check is done because KeyTokens can be found inside flow
|
# This check is done because KeyTokens can be found inside flow
|
||||||
# sequences... strange, but allowed.
|
# sequences... strange, but allowed.
|
||||||
if len(context['stack']) > 0 and context['stack'][-1].type == MAP:
|
if len(context['stack']) > 0 and context['stack'][-1].type == MAP:
|
||||||
if next.value in context['stack'][-1].keys:
|
if (next.value in context['stack'][-1].keys and
|
||||||
|
# `<<` is "merge key", see http://yaml.org/type/merge.html
|
||||||
|
next.value != '<<'):
|
||||||
yield LintProblem(
|
yield LintProblem(
|
||||||
next.start_mark.line + 1, next.start_mark.column + 1,
|
next.start_mark.line + 1, next.start_mark.column + 1,
|
||||||
'duplication of key "%s" in mapping' % next.value)
|
'duplication of key "%s" in mapping' % next.value)
|
||||||
|
|||||||
93
yamllint/rules/octal_values.py
Normal file
93
yamllint/rules/octal_values.py
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Copyright (C) 2017 ScienJus
|
||||||
|
#
|
||||||
|
# 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 prevent values with octal numbers. In YAML, numbers that
|
||||||
|
start with ``0`` are interpreted as octal, but this is not always wanted.
|
||||||
|
For instance ``010`` is the city code of Beijing, and should not be
|
||||||
|
converted to ``8``.
|
||||||
|
|
||||||
|
.. rubric:: Examples
|
||||||
|
|
||||||
|
#. With ``octal-values: {forbid-implicit-octal: true}``
|
||||||
|
|
||||||
|
the following code snippets would **PASS**:
|
||||||
|
::
|
||||||
|
|
||||||
|
user:
|
||||||
|
city-code: '010'
|
||||||
|
|
||||||
|
the following code snippets would **PASS**:
|
||||||
|
::
|
||||||
|
|
||||||
|
user:
|
||||||
|
city-code: 010,021
|
||||||
|
|
||||||
|
the following code snippets would **FAIL**:
|
||||||
|
::
|
||||||
|
|
||||||
|
user:
|
||||||
|
city-code: 010
|
||||||
|
|
||||||
|
#. With ``octal-values: {forbid-explicit-octal: true}``
|
||||||
|
|
||||||
|
the following code snippets would **PASS**:
|
||||||
|
::
|
||||||
|
|
||||||
|
user:
|
||||||
|
city-code: '0o10'
|
||||||
|
|
||||||
|
the following code snippets would **FAIL**:
|
||||||
|
::
|
||||||
|
|
||||||
|
user:
|
||||||
|
city-code: 0o10
|
||||||
|
"""
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from yamllint.linter import LintProblem
|
||||||
|
|
||||||
|
|
||||||
|
ID = 'octal-values'
|
||||||
|
TYPE = 'token'
|
||||||
|
CONF = {'forbid-implicit-octal': bool,
|
||||||
|
'forbid-explicit-octal': bool}
|
||||||
|
|
||||||
|
|
||||||
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
if prev and isinstance(prev, yaml.tokens.TagToken):
|
||||||
|
return
|
||||||
|
|
||||||
|
if conf['forbid-implicit-octal']:
|
||||||
|
if isinstance(token, yaml.tokens.ScalarToken):
|
||||||
|
if not token.style:
|
||||||
|
val = token.value
|
||||||
|
if val.isdigit() and len(val) > 1 and val[0] == '0':
|
||||||
|
yield LintProblem(
|
||||||
|
token.start_mark.line + 1, token.end_mark.column + 1,
|
||||||
|
'forbidden implicit octal value "%s"' %
|
||||||
|
token.value)
|
||||||
|
|
||||||
|
if conf['forbid-explicit-octal']:
|
||||||
|
if isinstance(token, yaml.tokens.ScalarToken):
|
||||||
|
if not token.style:
|
||||||
|
val = token.value
|
||||||
|
if len(val) > 2 and val[:2] == '0o' and val[2:].isdigit():
|
||||||
|
yield LintProblem(
|
||||||
|
token.start_mark.line + 1, token.end_mark.column + 1,
|
||||||
|
'forbidden explicit octal value "%s"' %
|
||||||
|
token.value)
|
||||||
@@ -15,11 +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/>.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Use this rule to forbid truthy values that are not quoted nor explicitly typed.
|
Use this rule to forbid non-explictly typed truthy values other than ``true``
|
||||||
|
and ``false``, for example ``YES``, ``False`` and ``off``.
|
||||||
|
|
||||||
This would prevent YAML parsers from transforming ``[yes, FALSE, Off]`` into
|
This can be useful to prevent surprises from YAML parsers transforming
|
||||||
``[true, false, false]`` or ``{y: 1, yes: 2, on: 3, true: 4, True: 5}`` into
|
``[yes, FALSE, Off]`` into ``[true, false, false]`` or
|
||||||
``{y: 1, true: 5}``.
|
``{y: 1, yes: 2, on: 3, true: 4, True: 5}`` into ``{y: 1, true: 5}``.
|
||||||
|
|
||||||
.. rubric:: Examples
|
.. rubric:: Examples
|
||||||
|
|
||||||
@@ -34,8 +35,7 @@ This would prevent YAML parsers from transforming ``[yes, FALSE, Off]`` into
|
|||||||
|
|
||||||
"yes": 1
|
"yes": 1
|
||||||
"on": 2
|
"on": 2
|
||||||
"true": 3
|
"True": 3
|
||||||
"True": 4
|
|
||||||
|
|
||||||
explicit:
|
explicit:
|
||||||
string1: !!str True
|
string1: !!str True
|
||||||
@@ -62,8 +62,7 @@ This would prevent YAML parsers from transforming ``[yes, FALSE, Off]`` into
|
|||||||
|
|
||||||
yes: 1
|
yes: 1
|
||||||
on: 2
|
on: 2
|
||||||
true: 3
|
True: 3
|
||||||
True: 4
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
@@ -90,4 +89,4 @@ def check(conf, token, prev, next, nextnext, context):
|
|||||||
if token.value in TRUTHY and token.style is None:
|
if token.value in TRUTHY and token.style is None:
|
||||||
yield LintProblem(token.start_mark.line + 1,
|
yield LintProblem(token.start_mark.line + 1,
|
||||||
token.start_mark.column + 1,
|
token.start_mark.column + 1,
|
||||||
"truthy value is not quoted")
|
"truthy value should be true or false")
|
||||||
|
|||||||
Reference in New Issue
Block a user