Compare commits
19 Commits
feature/pl
...
v1.26.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33ce0fa960 | ||
|
|
43744902e9 | ||
|
|
85ccd625a3 | ||
|
|
e53ea093e2 | ||
|
|
5d8ef2ea23 | ||
|
|
4515269233 | ||
|
|
66bf76a362 | ||
|
|
8f682481c7 | ||
|
|
0fff4e29e4 | ||
|
|
1b378ed5b9 | ||
|
|
a3fc64d134 | ||
|
|
ee4d163ff8 | ||
|
|
22335b294d | ||
|
|
0f9dffde23 | ||
|
|
cef0b48993 | ||
|
|
11b1f1c14e | ||
|
|
9ee8c27ac9 | ||
|
|
8eebab68ab | ||
|
|
2103bd73de |
54
.github/workflows/ci.yaml
vendored
Normal file
54
.github/workflows/ci.yaml
vendored
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
---
|
||||||
|
|
||||||
|
name: CI
|
||||||
|
|
||||||
|
on: # yamllint disable-line rule:truthy
|
||||||
|
push:
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
name: Linters
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v2
|
||||||
|
with:
|
||||||
|
python-version: 3.9
|
||||||
|
- run: python -m pip install flake8 flake8-import-order doc8 sphinx
|
||||||
|
- run: python -m pip install .
|
||||||
|
- run: flake8 .
|
||||||
|
- run: doc8 $(git ls-files '*.rst')
|
||||||
|
- run: yamllint --strict $(git ls-files '*.yaml' '*.yml')
|
||||||
|
- run: python setup.py build_sphinx
|
||||||
|
|
||||||
|
test:
|
||||||
|
name: Tests
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
python-version:
|
||||||
|
- 3.5
|
||||||
|
- 3.6
|
||||||
|
- 3.7
|
||||||
|
- 3.8
|
||||||
|
- 3.9
|
||||||
|
- nightly
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: Set up Python ${{ matrix.pyver }}
|
||||||
|
uses: actions/setup-python@v2
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.pyver }}
|
||||||
|
- name: Fix GitHub Actions path
|
||||||
|
run: echo /home/runner/.local/bin >>$GITHUB_PATH
|
||||||
|
- run: pip install coveralls
|
||||||
|
- run: pip install .
|
||||||
|
- run: coverage run --source=yamllint -m unittest discover
|
||||||
|
- name: Coveralls
|
||||||
|
uses: AndreMiras/coveralls-python-action@develop
|
||||||
28
.travis.yml
28
.travis.yml
@@ -1,28 +0,0 @@
|
|||||||
---
|
|
||||||
dist: xenial # required for Python >= 3.7 (travis-ci/travis-ci#9069)
|
|
||||||
language: python
|
|
||||||
python:
|
|
||||||
- 2.7
|
|
||||||
- 3.5
|
|
||||||
- 3.6
|
|
||||||
- 3.7
|
|
||||||
- 3.8
|
|
||||||
- nightly
|
|
||||||
env:
|
|
||||||
- REMOVE_LOCALES=false
|
|
||||||
- REMOVE_LOCALES=true
|
|
||||||
install:
|
|
||||||
- pip install pyyaml coveralls flake8 flake8-import-order doc8
|
|
||||||
- if [[ $TRAVIS_PYTHON_VERSION != 2* ]]; then pip install sphinx; fi
|
|
||||||
- pip install .
|
|
||||||
- if [[ $REMOVE_LOCALES = "true" ]]; then sudo rm -rf /usr/lib/locale/*; fi
|
|
||||||
script:
|
|
||||||
- if [[ $TRAVIS_PYTHON_VERSION != nightly ]]; then flake8 .; fi
|
|
||||||
- if [[ $TRAVIS_PYTHON_VERSION != 2* ]]; then doc8 $(git ls-files '*.rst'); fi
|
|
||||||
- yamllint --strict $(git ls-files '*.yaml' '*.yml')
|
|
||||||
- coverage run --source=yamllint -m unittest discover
|
|
||||||
- if [[ $TRAVIS_PYTHON_VERSION != 2* ]]; then
|
|
||||||
python setup.py build_sphinx;
|
|
||||||
fi
|
|
||||||
after_success:
|
|
||||||
coveralls
|
|
||||||
@@ -1,11 +1,32 @@
|
|||||||
Changelog
|
Changelog
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
1.26.2 (2021-08-03)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Fix ``python_requires`` to comply with PEP 345 and PEP 440
|
||||||
|
|
||||||
|
1.26.1 (2021-04-06)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Remove runtime dependency ``setuptools`` for Python < 3.8
|
||||||
|
- Fix ``line_length`` to skip all hash signs starting comment
|
||||||
|
|
||||||
|
1.26.0 (2021-01-29)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- End support for Python 2 and Python 3.4, add support for Python 3.9
|
||||||
|
- Add ``forbid: non-empty`` option to ``braces`` and ``brackets`` rules
|
||||||
|
- Fix ``quoted-strings`` for explicit octal recognition
|
||||||
|
- Add documentation for integration with Arcanist
|
||||||
|
- Fix typos in changelog and README
|
||||||
|
- Stop using deprecated ``python setup.py test`` in tests
|
||||||
|
|
||||||
1.25.0 (2020-09-29)
|
1.25.0 (2020-09-29)
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
- Run tests on Travis both with and without UTF-8 locales
|
- Run tests on Travis both with and without UTF-8 locales
|
||||||
- Improve documentationon with default values to rules with options
|
- Improve documentation with default values to rules with options
|
||||||
- Improve documentation with a Python API usage example
|
- Improve documentation with a Python API usage example
|
||||||
- Fix documentation on ``commas`` examples
|
- Fix documentation on ``commas`` examples
|
||||||
- Packaging: move setuptools' configuration from ``setup.py`` to ``setup.cfg``
|
- Packaging: move setuptools' configuration from ``setup.py`` to ``setup.cfg``
|
||||||
|
|||||||
@@ -19,11 +19,7 @@ indentation, etc.
|
|||||||
:target: https://yamllint.readthedocs.io/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 3 only).
|
||||||
|
|
||||||
⚠ Python 2 upstream support stopped on January 1, 2020. yamllint will keep
|
|
||||||
best-effort support for Python 2.7 until January 1, 2021. Passed that date,
|
|
||||||
yamllint will drop all Python 2-related code.
|
|
||||||
|
|
||||||
Documentation
|
Documentation
|
||||||
-------------
|
-------------
|
||||||
|
|||||||
@@ -16,47 +16,3 @@ Basic example of running the linter from Python:
|
|||||||
|
|
||||||
.. automodule:: yamllint.linter
|
.. automodule:: yamllint.linter
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
Develop rule plugins
|
|
||||||
---------------------
|
|
||||||
|
|
||||||
yamllint provides a plugin mechanism using setuptools (pkg_resources) to allow
|
|
||||||
adding custom rules. So, you can extend yamllint and add rules with your own
|
|
||||||
custom yamllint rule plugins if you developed them.
|
|
||||||
|
|
||||||
Yamllint rule plugins must satisfy the followings.
|
|
||||||
|
|
||||||
#. It must be a Python package installable using pip and distributed under
|
|
||||||
GPLv3+ same as yamllint.
|
|
||||||
#. It must contains the entry point configuration in ``setup.cfg`` or something
|
|
||||||
similar packaging configuration files, to make it installed and working as a
|
|
||||||
yamllint plugin like below. (``<plugin_name>`` is that plugin name and
|
|
||||||
``<plugin_src_dir>`` is a dir where the rule modules exist.)
|
|
||||||
::
|
|
||||||
|
|
||||||
[options.entry_points]
|
|
||||||
yamllint.plugins.rules =
|
|
||||||
<plugin_name> = <plugin_src_dir>
|
|
||||||
|
|
||||||
#. It must contain custom yamllint rule modules:
|
|
||||||
|
|
||||||
- Each rule module must define a couple of global variables, ID and TYPE. ID
|
|
||||||
must not conflicts with other rules' ID.
|
|
||||||
- Each rule module must define a function named 'check' to test input data
|
|
||||||
complies with the rule.
|
|
||||||
- Each rule module may have other global variables.
|
|
||||||
|
|
||||||
- CONF to define its configuration parameters and those types.
|
|
||||||
- DEFAULT to provide default values for each configuration parameters.
|
|
||||||
|
|
||||||
#. It must define a global variable RULES_MAP to provide mappings of rule ID
|
|
||||||
and rule modules to yamllint like this.
|
|
||||||
::
|
|
||||||
|
|
||||||
RULES_MAP = {
|
|
||||||
# rule ID: rule module
|
|
||||||
a_custom_rule.ID: a_custom_rule
|
|
||||||
}
|
|
||||||
|
|
||||||
To develop yamllint rules, the default rules themselves in yamllint may become
|
|
||||||
good references.
|
|
||||||
|
|||||||
@@ -51,3 +51,22 @@ An example workflow using GitHub Actions:
|
|||||||
|
|
||||||
- name: Lint YAML files
|
- name: Lint YAML files
|
||||||
run: yamllint .
|
run: yamllint .
|
||||||
|
|
||||||
|
Integration with Arcanist
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
You can configure yamllint to run on ``arc lint``. Here is an example
|
||||||
|
``.arclint`` file that makes use of this configuration.
|
||||||
|
|
||||||
|
.. code:: json
|
||||||
|
|
||||||
|
{
|
||||||
|
"linters": {
|
||||||
|
"yamllint": {
|
||||||
|
"type": "script-and-regex",
|
||||||
|
"script-and-regex.script": "yamllint",
|
||||||
|
"script-and-regex.regex": "/^(?P<line>\\d+):(?P<offset>\\d+) +(?P<severity>warning|error) +(?P<message>.*) +\\((?P<name>.*)\\)$/m",
|
||||||
|
"include": "(\\.(yml|yaml)$)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
11
setup.cfg
11
setup.cfg
@@ -26,14 +26,12 @@ classifiers =
|
|||||||
Environment :: Console
|
Environment :: Console
|
||||||
Intended Audience :: Developers
|
Intended Audience :: Developers
|
||||||
License :: OSI Approved :: GNU General Public License v3 (GPLv3)
|
License :: OSI Approved :: GNU General Public License v3 (GPLv3)
|
||||||
Programming Language :: Python :: 2
|
|
||||||
Programming Language :: Python :: 2.7
|
|
||||||
Programming Language :: Python :: 3
|
Programming Language :: Python :: 3
|
||||||
Programming Language :: Python :: 3.4
|
|
||||||
Programming Language :: Python :: 3.5
|
Programming Language :: Python :: 3.5
|
||||||
Programming Language :: Python :: 3.6
|
Programming Language :: Python :: 3.6
|
||||||
Programming Language :: Python :: 3.7
|
Programming Language :: Python :: 3.7
|
||||||
Programming Language :: Python :: 3.8
|
Programming Language :: Python :: 3.8
|
||||||
|
Programming Language :: Python :: 3.9
|
||||||
Topic :: Software Development
|
Topic :: Software Development
|
||||||
Topic :: Software Development :: Debuggers
|
Topic :: Software Development :: Debuggers
|
||||||
Topic :: Software Development :: Quality Assurance
|
Topic :: Software Development :: Quality Assurance
|
||||||
@@ -48,13 +46,13 @@ project_urls =
|
|||||||
[options]
|
[options]
|
||||||
packages = find:
|
packages = find:
|
||||||
|
|
||||||
python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
|
python_requires = >=3.5
|
||||||
|
|
||||||
include_package_data = True
|
include_package_data = True
|
||||||
install_requires =
|
install_requires =
|
||||||
pathspec >= 0.5.3
|
pathspec >= 0.5.3
|
||||||
pyyaml
|
pyyaml
|
||||||
setuptools
|
setuptools; python_version < "3.8"
|
||||||
|
|
||||||
test_suite = tests
|
test_suite = tests
|
||||||
|
|
||||||
@@ -69,3 +67,6 @@ yamllint = conf/*.yaml
|
|||||||
[options.entry_points]
|
[options.entry_points]
|
||||||
console_scripts =
|
console_scripts =
|
||||||
yamllint = yamllint.cli:run
|
yamllint = yamllint.cli:run
|
||||||
|
|
||||||
|
[coverage:run]
|
||||||
|
relative_files = True
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
# Copyright (C) 2020 Satoru SATOH
|
|
||||||
#
|
|
||||||
# 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/>.
|
|
||||||
|
|
||||||
"""yamllint plugin entry point
|
|
||||||
"""
|
|
||||||
from __future__ import absolute_import
|
|
||||||
|
|
||||||
from . import override_comments
|
|
||||||
|
|
||||||
|
|
||||||
RULES_MAP = {
|
|
||||||
override_comments.ID: override_comments
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
#
|
|
||||||
# Copyright (C) 2020 Satoru SATOH
|
|
||||||
#
|
|
||||||
# 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 override some comments' rules.
|
|
||||||
|
|
||||||
.. rubric:: Options
|
|
||||||
|
|
||||||
* Use ``forbid`` to control comments. Set to ``true`` to forbid comments
|
|
||||||
completely.
|
|
||||||
|
|
||||||
.. rubric:: Examples
|
|
||||||
|
|
||||||
#. With ``override-comments: {forbid: true}``
|
|
||||||
|
|
||||||
the following code snippet would **PASS**:
|
|
||||||
::
|
|
||||||
|
|
||||||
foo: 1
|
|
||||||
|
|
||||||
the following code snippet would **FAIL**:
|
|
||||||
::
|
|
||||||
|
|
||||||
# baz
|
|
||||||
foo: 1
|
|
||||||
|
|
||||||
.. rubric:: Default values (when enabled)
|
|
||||||
|
|
||||||
.. code-block:: yaml
|
|
||||||
|
|
||||||
rules:
|
|
||||||
override-comments:
|
|
||||||
forbid: False
|
|
||||||
|
|
||||||
"""
|
|
||||||
from yamllint.linter import LintProblem
|
|
||||||
|
|
||||||
|
|
||||||
ID = 'override-comments'
|
|
||||||
TYPE = 'comment'
|
|
||||||
CONF = {'forbid': bool}
|
|
||||||
DEFAULT = {'forbid': False}
|
|
||||||
|
|
||||||
|
|
||||||
def check(conf, comment):
|
|
||||||
"""Check if comments are found.
|
|
||||||
"""
|
|
||||||
if conf['forbid']:
|
|
||||||
yield LintProblem(comment.line_no, comment.column_no,
|
|
||||||
'forbidden comment')
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
[metadata]
|
|
||||||
name = yamllint_plugin_example
|
|
||||||
version = 1.0.0
|
|
||||||
|
|
||||||
[options]
|
|
||||||
packages = find:
|
|
||||||
install_requires = yamllint
|
|
||||||
|
|
||||||
[options.entry_points]
|
|
||||||
yamllint.plugins.rules =
|
|
||||||
example = yamllint_plugin_example
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
import setuptools
|
|
||||||
setuptools.setup()
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
# Copyright (C) 2020 Satoru SATOH
|
|
||||||
#
|
|
||||||
# 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/>.
|
|
||||||
|
|
||||||
"""yamllint plugin entry point
|
|
||||||
"""
|
|
||||||
from __future__ import absolute_import
|
|
||||||
|
|
||||||
from . import override_comments, random_failure
|
|
||||||
|
|
||||||
|
|
||||||
RULES_MAP = {
|
|
||||||
override_comments.ID: override_comments,
|
|
||||||
random_failure.ID: random_failure,
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
#
|
|
||||||
# Copyright (C) 2020 Satoru SATOH
|
|
||||||
#
|
|
||||||
# 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 override some comments' rules.
|
|
||||||
|
|
||||||
.. rubric:: Options
|
|
||||||
|
|
||||||
* Use ``forbid`` to control comments. Set to ``true`` to forbid comments
|
|
||||||
completely.
|
|
||||||
|
|
||||||
.. rubric:: Examples
|
|
||||||
|
|
||||||
#. With ``override-comments: {forbid: true}``
|
|
||||||
|
|
||||||
the following code snippet would **PASS**:
|
|
||||||
::
|
|
||||||
|
|
||||||
foo: 1
|
|
||||||
|
|
||||||
the following code snippet would **FAIL**:
|
|
||||||
::
|
|
||||||
|
|
||||||
# baz
|
|
||||||
foo: 1
|
|
||||||
|
|
||||||
.. rubric:: Default values (when enabled)
|
|
||||||
|
|
||||||
.. code-block:: yaml
|
|
||||||
|
|
||||||
rules:
|
|
||||||
override-comments:
|
|
||||||
forbid: False
|
|
||||||
|
|
||||||
"""
|
|
||||||
from yamllint.linter import LintProblem
|
|
||||||
|
|
||||||
|
|
||||||
ID = 'override-comments'
|
|
||||||
TYPE = 'comment'
|
|
||||||
CONF = {'forbid': bool}
|
|
||||||
DEFAULT = {'forbid': False}
|
|
||||||
|
|
||||||
|
|
||||||
def check(conf, comment):
|
|
||||||
"""Check if comments are found.
|
|
||||||
"""
|
|
||||||
if conf['forbid']:
|
|
||||||
yield LintProblem(comment.line_no, comment.column_no,
|
|
||||||
'forbidden comment')
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
# Copyright (C) 2020 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 random
|
|
||||||
|
|
||||||
from yamllint.linter import LintProblem
|
|
||||||
|
|
||||||
|
|
||||||
ID = 'random-failure'
|
|
||||||
TYPE = 'token'
|
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
|
||||||
if random.random() > 0.9:
|
|
||||||
yield LintProblem(token.start_mark.line + 1,
|
|
||||||
token.start_mark.column + 1,
|
|
||||||
'random failure')
|
|
||||||
@@ -61,6 +61,30 @@ class ColonTestCase(RuleTestCase):
|
|||||||
' a: 1\n'
|
' a: 1\n'
|
||||||
'}\n', conf, problem=(2, 8))
|
'}\n', conf, problem=(2, 8))
|
||||||
|
|
||||||
|
conf = ('braces:\n'
|
||||||
|
' forbid: non-empty\n')
|
||||||
|
self.check('---\n'
|
||||||
|
'dict:\n'
|
||||||
|
' a: 1\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {}\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {\n'
|
||||||
|
'}\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {\n'
|
||||||
|
'# commented: value\n'
|
||||||
|
'# another: value2\n'
|
||||||
|
'}\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a}\n', conf, problem=(2, 8))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: 1}\n', conf, problem=(2, 8))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {\n'
|
||||||
|
' a: 1\n'
|
||||||
|
'}\n', conf, problem=(2, 8))
|
||||||
|
|
||||||
def test_min_spaces(self):
|
def test_min_spaces(self):
|
||||||
conf = ('braces:\n'
|
conf = ('braces:\n'
|
||||||
' max-spaces-inside: -1\n'
|
' max-spaces-inside: -1\n'
|
||||||
|
|||||||
@@ -60,6 +60,29 @@ class ColonTestCase(RuleTestCase):
|
|||||||
' b\n'
|
' b\n'
|
||||||
']\n', conf, problem=(2, 9))
|
']\n', conf, problem=(2, 9))
|
||||||
|
|
||||||
|
conf = ('brackets:\n'
|
||||||
|
' forbid: non-empty\n')
|
||||||
|
self.check('---\n'
|
||||||
|
'array:\n'
|
||||||
|
' - a\n'
|
||||||
|
' - b\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: []\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [\n\n'
|
||||||
|
']\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [\n'
|
||||||
|
'# a comment\n'
|
||||||
|
']\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [a, b]\n', conf, problem=(2, 9))
|
||||||
|
self.check('---\n'
|
||||||
|
'array: [\n'
|
||||||
|
' a,\n'
|
||||||
|
' b\n'
|
||||||
|
']\n', conf, problem=(2, 9))
|
||||||
|
|
||||||
def test_min_spaces(self):
|
def test_min_spaces(self):
|
||||||
conf = ('brackets:\n'
|
conf = ('brackets:\n'
|
||||||
' max-spaces-inside: -1\n'
|
' max-spaces-inside: -1\n'
|
||||||
|
|||||||
@@ -14,9 +14,6 @@
|
|||||||
# 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 sys
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
from tests.common import RuleTestCase
|
from tests.common import RuleTestCase
|
||||||
|
|
||||||
|
|
||||||
@@ -119,6 +116,27 @@ class LineLengthTestCase(RuleTestCase):
|
|||||||
'long_line: http://localhost/very/very/long/url\n'
|
'long_line: http://localhost/very/very/long/url\n'
|
||||||
'...\n', conf, problem=(2, 21))
|
'...\n', conf, problem=(2, 21))
|
||||||
|
|
||||||
|
conf = 'line-length: {max: 20, allow-non-breakable-words: true}'
|
||||||
|
self.check('---\n'
|
||||||
|
'# http://www.verylongurlurlurlurlurlurlurlurl.com\n'
|
||||||
|
'key:\n'
|
||||||
|
' subkey: value\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'## http://www.verylongurlurlurlurlurlurlurlurl.com\n'
|
||||||
|
'key:\n'
|
||||||
|
' subkey: value\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'# # http://www.verylongurlurlurlurlurlurlurlurl.com\n'
|
||||||
|
'key:\n'
|
||||||
|
' subkey: value\n', conf,
|
||||||
|
problem=(2, 21))
|
||||||
|
self.check('---\n'
|
||||||
|
'#A http://www.verylongurlurlurlurlurlurlurlurl.com\n'
|
||||||
|
'key:\n'
|
||||||
|
' subkey: value\n', conf,
|
||||||
|
problem1=(2, 2, 'comments'),
|
||||||
|
problem2=(2, 21, 'line-length'))
|
||||||
|
|
||||||
conf = ('line-length: {max: 20, allow-non-breakable-words: true}\n'
|
conf = ('line-length: {max: 20, allow-non-breakable-words: true}\n'
|
||||||
'trailing-spaces: disable')
|
'trailing-spaces: disable')
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
@@ -159,7 +177,6 @@ class LineLengthTestCase(RuleTestCase):
|
|||||||
' {% this line is' + 99 * ' really' + ' long %}\n',
|
' {% this line is' + 99 * ' really' + ' long %}\n',
|
||||||
conf, problem=(3, 81))
|
conf, problem=(3, 81))
|
||||||
|
|
||||||
@unittest.skipIf(sys.version_info < (3, 0), 'Python 2 not supported')
|
|
||||||
def test_unicode(self):
|
def test_unicode(self):
|
||||||
conf = 'line-length: {max: 53}'
|
conf = 'line-length: {max: 53}'
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
|
|||||||
@@ -436,3 +436,21 @@ class QuotedTestCase(RuleTestCase):
|
|||||||
'- foo bar\n'
|
'- foo bar\n'
|
||||||
'- "foo bar"\n',
|
'- "foo bar"\n',
|
||||||
conf, problem1=(3, 3), problem2=(7, 3), problem3=(11, 3))
|
conf, problem1=(3, 3), problem2=(7, 3), problem3=(11, 3))
|
||||||
|
|
||||||
|
def test_octal_values(self):
|
||||||
|
conf = 'quoted-strings: {required: true}\n'
|
||||||
|
|
||||||
|
self.check('---\n'
|
||||||
|
'- 100\n'
|
||||||
|
'- 0100\n'
|
||||||
|
'- 0o100\n'
|
||||||
|
'- 777\n'
|
||||||
|
'- 0777\n'
|
||||||
|
'- 0o777\n'
|
||||||
|
'- 800\n'
|
||||||
|
'- 0800\n'
|
||||||
|
'- 0o800\n'
|
||||||
|
'- "0800"\n'
|
||||||
|
'- "0o800"\n',
|
||||||
|
conf,
|
||||||
|
problem1=(9, 3), problem2=(10, 3))
|
||||||
|
|||||||
@@ -14,10 +14,7 @@
|
|||||||
# 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/>.
|
||||||
|
|
||||||
try:
|
from io import StringIO
|
||||||
from cStringIO import StringIO
|
|
||||||
except ImportError:
|
|
||||||
from io import StringIO
|
|
||||||
import fcntl
|
import fcntl
|
||||||
import locale
|
import locale
|
||||||
import os
|
import os
|
||||||
@@ -246,19 +243,19 @@ class CommandLineTestCase(unittest.TestCase):
|
|||||||
cli.run(())
|
cli.run(())
|
||||||
self.assertNotEqual(ctx.returncode, 0)
|
self.assertNotEqual(ctx.returncode, 0)
|
||||||
self.assertEqual(ctx.stdout, '')
|
self.assertEqual(ctx.stdout, '')
|
||||||
self.assertRegexpMatches(ctx.stderr, r'^usage')
|
self.assertRegex(ctx.stderr, r'^usage')
|
||||||
|
|
||||||
with RunContext(self) as ctx:
|
with RunContext(self) as ctx:
|
||||||
cli.run(('--unknown-arg', ))
|
cli.run(('--unknown-arg', ))
|
||||||
self.assertNotEqual(ctx.returncode, 0)
|
self.assertNotEqual(ctx.returncode, 0)
|
||||||
self.assertEqual(ctx.stdout, '')
|
self.assertEqual(ctx.stdout, '')
|
||||||
self.assertRegexpMatches(ctx.stderr, r'^usage')
|
self.assertRegex(ctx.stderr, r'^usage')
|
||||||
|
|
||||||
with RunContext(self) as ctx:
|
with RunContext(self) as ctx:
|
||||||
cli.run(('-c', './conf.yaml', '-d', 'relaxed', 'file'))
|
cli.run(('-c', './conf.yaml', '-d', 'relaxed', 'file'))
|
||||||
self.assertNotEqual(ctx.returncode, 0)
|
self.assertNotEqual(ctx.returncode, 0)
|
||||||
self.assertEqual(ctx.stdout, '')
|
self.assertEqual(ctx.stdout, '')
|
||||||
self.assertRegexpMatches(
|
self.assertRegex(
|
||||||
ctx.stderr.splitlines()[-1],
|
ctx.stderr.splitlines()[-1],
|
||||||
r'^yamllint: error: argument -d\/--config-data: '
|
r'^yamllint: error: argument -d\/--config-data: '
|
||||||
r'not allowed with argument -c\/--config-file$'
|
r'not allowed with argument -c\/--config-file$'
|
||||||
@@ -269,21 +266,21 @@ class CommandLineTestCase(unittest.TestCase):
|
|||||||
cli.run(('-', 'file'))
|
cli.run(('-', 'file'))
|
||||||
self.assertNotEqual(ctx.returncode, 0)
|
self.assertNotEqual(ctx.returncode, 0)
|
||||||
self.assertEqual(ctx.stdout, '')
|
self.assertEqual(ctx.stdout, '')
|
||||||
self.assertRegexpMatches(ctx.stderr, r'^usage')
|
self.assertRegex(ctx.stderr, r'^usage')
|
||||||
|
|
||||||
def test_run_with_bad_config(self):
|
def test_run_with_bad_config(self):
|
||||||
with RunContext(self) as ctx:
|
with RunContext(self) as ctx:
|
||||||
cli.run(('-d', 'rules: {a: b}', 'file'))
|
cli.run(('-d', 'rules: {a: b}', 'file'))
|
||||||
self.assertEqual(ctx.returncode, -1)
|
self.assertEqual(ctx.returncode, -1)
|
||||||
self.assertEqual(ctx.stdout, '')
|
self.assertEqual(ctx.stdout, '')
|
||||||
self.assertRegexpMatches(ctx.stderr, r'^invalid config: no such rule')
|
self.assertRegex(ctx.stderr, r'^invalid config: no such rule')
|
||||||
|
|
||||||
def test_run_with_empty_config(self):
|
def test_run_with_empty_config(self):
|
||||||
with RunContext(self) as ctx:
|
with RunContext(self) as ctx:
|
||||||
cli.run(('-d', '', 'file'))
|
cli.run(('-d', '', 'file'))
|
||||||
self.assertEqual(ctx.returncode, -1)
|
self.assertEqual(ctx.returncode, -1)
|
||||||
self.assertEqual(ctx.stdout, '')
|
self.assertEqual(ctx.stdout, '')
|
||||||
self.assertRegexpMatches(ctx.stderr, r'^invalid config: not a dict')
|
self.assertRegex(ctx.stderr, r'^invalid config: not a dict')
|
||||||
|
|
||||||
def test_run_with_config_file(self):
|
def test_run_with_config_file(self):
|
||||||
with open(os.path.join(self.wd, 'config'), 'w') as f:
|
with open(os.path.join(self.wd, 'config'), 'w') as f:
|
||||||
@@ -300,6 +297,7 @@ class CommandLineTestCase(unittest.TestCase):
|
|||||||
cli.run(('-c', f.name, os.path.join(self.wd, 'a.yaml')))
|
cli.run(('-c', f.name, os.path.join(self.wd, 'a.yaml')))
|
||||||
self.assertEqual(ctx.returncode, 1)
|
self.assertEqual(ctx.returncode, 1)
|
||||||
|
|
||||||
|
@unittest.skipIf(os.environ.get('GITHUB_RUN_ID'), '$HOME not overridable')
|
||||||
def test_run_with_user_global_config_file(self):
|
def test_run_with_user_global_config_file(self):
|
||||||
home = os.path.join(self.wd, 'fake-home')
|
home = os.path.join(self.wd, 'fake-home')
|
||||||
dir = os.path.join(home, '.config', 'yamllint')
|
dir = os.path.join(home, '.config', 'yamllint')
|
||||||
@@ -386,7 +384,7 @@ class CommandLineTestCase(unittest.TestCase):
|
|||||||
with RunContext(self) as ctx:
|
with RunContext(self) as ctx:
|
||||||
cli.run(('--version', ))
|
cli.run(('--version', ))
|
||||||
self.assertEqual(ctx.returncode, 0)
|
self.assertEqual(ctx.returncode, 0)
|
||||||
self.assertRegexpMatches(ctx.stdout + ctx.stderr, r'yamllint \d+\.\d+')
|
self.assertRegex(ctx.stdout + ctx.stderr, r'yamllint \d+\.\d+')
|
||||||
|
|
||||||
def test_run_non_existing_file(self):
|
def test_run_non_existing_file(self):
|
||||||
path = os.path.join(self.wd, 'i-do-not-exist.yaml')
|
path = os.path.join(self.wd, 'i-do-not-exist.yaml')
|
||||||
@@ -395,7 +393,7 @@ class CommandLineTestCase(unittest.TestCase):
|
|||||||
cli.run(('-f', 'parsable', path))
|
cli.run(('-f', 'parsable', path))
|
||||||
self.assertEqual(ctx.returncode, -1)
|
self.assertEqual(ctx.returncode, -1)
|
||||||
self.assertEqual(ctx.stdout, '')
|
self.assertEqual(ctx.stdout, '')
|
||||||
self.assertRegexpMatches(ctx.stderr, r'No such file or directory')
|
self.assertRegex(ctx.stderr, r'No such file or directory')
|
||||||
|
|
||||||
def test_run_one_problem_file(self):
|
def test_run_one_problem_file(self):
|
||||||
path = os.path.join(self.wd, 'a.yaml')
|
path = os.path.join(self.wd, 'a.yaml')
|
||||||
|
|||||||
@@ -14,10 +14,7 @@
|
|||||||
# 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/>.
|
||||||
|
|
||||||
try:
|
from io import StringIO
|
||||||
from cStringIO import StringIO
|
|
||||||
except ImportError:
|
|
||||||
from io import StringIO
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
@@ -48,7 +45,7 @@ class SimpleConfigTestCase(unittest.TestCase):
|
|||||||
config.YamlLintConfig('not: valid: yaml')
|
config.YamlLintConfig('not: valid: yaml')
|
||||||
|
|
||||||
def test_unknown_rule(self):
|
def test_unknown_rule(self):
|
||||||
with self.assertRaisesRegexp(
|
with self.assertRaisesRegex(
|
||||||
config.YamlLintConfigError,
|
config.YamlLintConfigError,
|
||||||
'invalid config: no such rule: "this-one-does-not-exist"'):
|
'invalid config: no such rule: "this-one-does-not-exist"'):
|
||||||
config.YamlLintConfig('rules:\n'
|
config.YamlLintConfig('rules:\n'
|
||||||
@@ -67,7 +64,7 @@ class SimpleConfigTestCase(unittest.TestCase):
|
|||||||
self.assertEqual(c.rules['colons']['max-spaces-after'], 1)
|
self.assertEqual(c.rules['colons']['max-spaces-after'], 1)
|
||||||
|
|
||||||
def test_unknown_option(self):
|
def test_unknown_option(self):
|
||||||
with self.assertRaisesRegexp(
|
with self.assertRaisesRegex(
|
||||||
config.YamlLintConfigError,
|
config.YamlLintConfigError,
|
||||||
'invalid config: unknown option "abcdef" for rule "colons"'):
|
'invalid config: unknown option "abcdef" for rule "colons"'):
|
||||||
config.YamlLintConfig('rules:\n'
|
config.YamlLintConfig('rules:\n'
|
||||||
@@ -105,7 +102,7 @@ class SimpleConfigTestCase(unittest.TestCase):
|
|||||||
self.assertEqual(c.rules['indentation']['check-multi-line-strings'],
|
self.assertEqual(c.rules['indentation']['check-multi-line-strings'],
|
||||||
False)
|
False)
|
||||||
|
|
||||||
with self.assertRaisesRegexp(
|
with self.assertRaisesRegex(
|
||||||
config.YamlLintConfigError,
|
config.YamlLintConfigError,
|
||||||
'invalid config: option "indent-sequences" of "indentation" '
|
'invalid config: option "indent-sequences" of "indentation" '
|
||||||
'should be in '):
|
'should be in '):
|
||||||
|
|||||||
@@ -47,16 +47,15 @@ class ModuleTestCase(unittest.TestCase):
|
|||||||
subprocess.check_output([PYTHON, '-m', 'yamllint'],
|
subprocess.check_output([PYTHON, '-m', 'yamllint'],
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
self.assertEqual(ctx.exception.returncode, 2)
|
self.assertEqual(ctx.exception.returncode, 2)
|
||||||
self.assertRegexpMatches(ctx.exception.output.decode(),
|
self.assertRegex(ctx.exception.output.decode(), r'^usage: yamllint')
|
||||||
r'^usage: yamllint')
|
|
||||||
|
|
||||||
def test_run_module_on_bad_dir(self):
|
def test_run_module_on_bad_dir(self):
|
||||||
with self.assertRaises(subprocess.CalledProcessError) as ctx:
|
with self.assertRaises(subprocess.CalledProcessError) as ctx:
|
||||||
subprocess.check_output([PYTHON, '-m', 'yamllint',
|
subprocess.check_output([PYTHON, '-m', 'yamllint',
|
||||||
'/does/not/exist'],
|
'/does/not/exist'],
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
self.assertRegexpMatches(ctx.exception.output.decode(),
|
self.assertRegex(ctx.exception.output.decode(),
|
||||||
r'No such file or directory')
|
r'No such file or directory')
|
||||||
|
|
||||||
def test_run_module_on_file(self):
|
def test_run_module_on_file(self):
|
||||||
out = subprocess.check_output(
|
out = subprocess.check_output(
|
||||||
|
|||||||
@@ -1,80 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
# Copyright (C) 2020 Satoru SATOH
|
|
||||||
#
|
|
||||||
# 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 warnings
|
|
||||||
|
|
||||||
try:
|
|
||||||
from unittest import mock
|
|
||||||
except ImportError: # for python 2.7
|
|
||||||
mock = False
|
|
||||||
|
|
||||||
from tests.plugins import example
|
|
||||||
|
|
||||||
import yamllint.plugins
|
|
||||||
|
|
||||||
|
|
||||||
class FakeEntryPoint(object):
|
|
||||||
"""Fake object to mimic pkg_resources.EntryPoint.
|
|
||||||
"""
|
|
||||||
RULES_MAP = example.RULES_MAP
|
|
||||||
|
|
||||||
def load(self):
|
|
||||||
"""Fake method to return self.
|
|
||||||
"""
|
|
||||||
return self
|
|
||||||
|
|
||||||
|
|
||||||
class BrokenEntryPoint(FakeEntryPoint):
|
|
||||||
"""Fake object to mimic load failure of pkg_resources.EntryPoint.
|
|
||||||
"""
|
|
||||||
def load(self):
|
|
||||||
raise ImportError("This entry point should fail always!")
|
|
||||||
|
|
||||||
|
|
||||||
class PluginFunctionsTestCase(unittest.TestCase):
|
|
||||||
|
|
||||||
def test_validate_rule_module(self):
|
|
||||||
fun = yamllint.plugins.validate_rule_module
|
|
||||||
rule_mod = example.override_comments
|
|
||||||
|
|
||||||
self.assertFalse(fun(object()))
|
|
||||||
self.assertTrue(fun(rule_mod))
|
|
||||||
|
|
||||||
@unittest.skipIf(not mock, "unittest.mock is not available")
|
|
||||||
def test_validate_rule_module_using_mock(self):
|
|
||||||
fun = yamllint.plugins.validate_rule_module
|
|
||||||
rule_mod = example.override_comments
|
|
||||||
|
|
||||||
with mock.patch.object(rule_mod, "ID", False):
|
|
||||||
self.assertFalse(fun(rule_mod))
|
|
||||||
|
|
||||||
with mock.patch.object(rule_mod, "TYPE", False):
|
|
||||||
self.assertFalse(fun(rule_mod))
|
|
||||||
|
|
||||||
with mock.patch.object(rule_mod, "check", True):
|
|
||||||
self.assertFalse(fun(rule_mod))
|
|
||||||
|
|
||||||
def test_load_plugin_rules_itr(self):
|
|
||||||
fun = yamllint.plugins.load_plugin_rules_itr
|
|
||||||
|
|
||||||
self.assertEqual(list(fun([])), [])
|
|
||||||
self.assertEqual(sorted(fun([FakeEntryPoint(),
|
|
||||||
FakeEntryPoint()])),
|
|
||||||
sorted(FakeEntryPoint.RULES_MAP.items()))
|
|
||||||
|
|
||||||
with warnings.catch_warnings():
|
|
||||||
warnings.simplefilter("ignore")
|
|
||||||
self.assertEqual(list(fun([BrokenEntryPoint()])), [])
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
# Copyright (C) 2020 Satoru SATOH
|
|
||||||
#
|
|
||||||
# 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
|
|
||||||
|
|
||||||
try:
|
|
||||||
from unittest import mock
|
|
||||||
except ImportError: # for python 2.7
|
|
||||||
mock = False
|
|
||||||
|
|
||||||
from tests.plugins import example
|
|
||||||
|
|
||||||
import yamllint.rules
|
|
||||||
|
|
||||||
|
|
||||||
RULE_NEVER_EXISTS = "rule_never_exists"
|
|
||||||
PLUGIN_RULES = example.RULES_MAP
|
|
||||||
|
|
||||||
|
|
||||||
class TestCase(unittest.TestCase):
|
|
||||||
"""Test cases for yamllint.rules.__init__.*.
|
|
||||||
"""
|
|
||||||
def test_get_default_rule(self):
|
|
||||||
self.assertEqual(yamllint.rules.get(yamllint.rules.braces.ID),
|
|
||||||
yamllint.rules.braces)
|
|
||||||
|
|
||||||
def test_get_rule_does_not_exist(self):
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
yamllint.rules.get(RULE_NEVER_EXISTS)
|
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipIf(not mock, "unittest.mock is not available")
|
|
||||||
class TestCaseUsingMock(unittest.TestCase):
|
|
||||||
"""Test cases for yamllint.rules.__init__.* using mock.
|
|
||||||
"""
|
|
||||||
def test_get_default_rule_with_plugins(self):
|
|
||||||
with mock.patch.dict(yamllint.rules._EXTERNAL_RULES, PLUGIN_RULES):
|
|
||||||
self.assertEqual(yamllint.rules.get(yamllint.rules.braces.ID),
|
|
||||||
yamllint.rules.braces)
|
|
||||||
|
|
||||||
def test_get_plugin_rules(self):
|
|
||||||
plugin_rule_id = example.override_comments.ID
|
|
||||||
plugin_rule_mod = example.override_comments
|
|
||||||
|
|
||||||
with mock.patch.dict(yamllint.rules._EXTERNAL_RULES, PLUGIN_RULES):
|
|
||||||
self.assertEqual(yamllint.rules.get(plugin_rule_id),
|
|
||||||
plugin_rule_mod)
|
|
||||||
|
|
||||||
def test_get_rule_does_not_exist_with_plugins(self):
|
|
||||||
with mock.patch.dict(yamllint.rules._EXTERNAL_RULES, PLUGIN_RULES):
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
yamllint.rules.get(RULE_NEVER_EXISTS)
|
|
||||||
@@ -22,7 +22,7 @@ indentation, etc."""
|
|||||||
|
|
||||||
|
|
||||||
APP_NAME = 'yamllint'
|
APP_NAME = 'yamllint'
|
||||||
APP_VERSION = '1.25.0'
|
APP_VERSION = '1.26.2'
|
||||||
APP_DESCRIPTION = __doc__
|
APP_DESCRIPTION = __doc__
|
||||||
|
|
||||||
__author__ = u'Adrien Vergé'
|
__author__ = u'Adrien Vergé'
|
||||||
|
|||||||
@@ -29,7 +29,5 @@ rules:
|
|||||||
octal-values: disable
|
octal-values: disable
|
||||||
quoted-strings: disable
|
quoted-strings: disable
|
||||||
trailing-spaces: enable
|
trailing-spaces: enable
|
||||||
random-failure: enable
|
|
||||||
override-comments: {forbid: true}
|
|
||||||
truthy:
|
truthy:
|
||||||
level: warning
|
level: warning
|
||||||
|
|||||||
@@ -233,7 +233,7 @@ def run(input, conf, filepath=None):
|
|||||||
if conf.is_file_ignored(filepath):
|
if conf.is_file_ignored(filepath):
|
||||||
return ()
|
return ()
|
||||||
|
|
||||||
if isinstance(input, (type(b''), type(u''))): # compat with Python 2 & 3
|
if isinstance(input, (bytes, str)):
|
||||||
return _run(input, conf, filepath)
|
return _run(input, conf, filepath)
|
||||||
elif hasattr(input, 'read'): # Python 2's file or Python 3's io.IOBase
|
elif hasattr(input, 'read'): # Python 2's file or Python 3's io.IOBase
|
||||||
# We need to have everything in memory to parse correctly
|
# We need to have everything in memory to parse correctly
|
||||||
|
|||||||
@@ -1,62 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
# Copyright (C) 2020 Satoru SATOH
|
|
||||||
#
|
|
||||||
# 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/>.
|
|
||||||
"""
|
|
||||||
Plugin module utilizing setuptools (pkg_resources) to allow users to add their
|
|
||||||
own custom lint rules.
|
|
||||||
"""
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
import pkg_resources
|
|
||||||
|
|
||||||
|
|
||||||
PACKAGE_GROUP = "yamllint.plugins.rules"
|
|
||||||
|
|
||||||
|
|
||||||
def validate_rule_module(rule_mod):
|
|
||||||
"""Test if given rule module is valid.
|
|
||||||
"""
|
|
||||||
return (getattr(rule_mod, "ID", False) and
|
|
||||||
getattr(rule_mod, "TYPE", False)
|
|
||||||
) and callable(getattr(rule_mod, "check", False))
|
|
||||||
|
|
||||||
|
|
||||||
def load_plugin_rules_itr(entry_points=None, group=PACKAGE_GROUP):
|
|
||||||
"""Load custom lint rule plugins."""
|
|
||||||
if not entry_points:
|
|
||||||
entry_points = pkg_resources.iter_entry_points(group)
|
|
||||||
|
|
||||||
rule_ids = set()
|
|
||||||
for entry in entry_points:
|
|
||||||
try:
|
|
||||||
rules = entry.load()
|
|
||||||
for rule_id, rule_mod in rules.RULES_MAP.items():
|
|
||||||
if rule_id in rule_ids or not validate_rule_module(rule_mod):
|
|
||||||
continue
|
|
||||||
|
|
||||||
print(rule_id, rule_mod)###
|
|
||||||
yield (rule_id, rule_mod)
|
|
||||||
rule_ids.add(rule_id)
|
|
||||||
|
|
||||||
# pkg_resources.EntryPoint.resolve may throw ImportError.
|
|
||||||
except (AttributeError, ImportError):
|
|
||||||
warnings.warn("Could not load the plugin: {}".format(entry),
|
|
||||||
RuntimeWarning)
|
|
||||||
|
|
||||||
|
|
||||||
def get_plugin_rules_map():
|
|
||||||
"""Get a mappings of plugin rule's IDs and rules."""
|
|
||||||
return dict((rule_id, rule_mod)
|
|
||||||
for rule_id, rule_mod in load_plugin_rules_itr())
|
|
||||||
@@ -14,7 +14,6 @@
|
|||||||
# 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 yamllint.plugins
|
|
||||||
from yamllint.rules import (
|
from yamllint.rules import (
|
||||||
braces,
|
braces,
|
||||||
brackets,
|
brackets,
|
||||||
@@ -63,14 +62,9 @@ _RULES = {
|
|||||||
truthy.ID: truthy,
|
truthy.ID: truthy,
|
||||||
}
|
}
|
||||||
|
|
||||||
_EXTERNAL_RULES = yamllint.plugins.get_plugin_rules_map()
|
|
||||||
|
|
||||||
|
def get(id):
|
||||||
|
if id not in _RULES:
|
||||||
|
raise ValueError('no such rule: "%s"' % id)
|
||||||
|
|
||||||
def get(rule_id):
|
return _RULES[id]
|
||||||
if rule_id in _RULES:
|
|
||||||
return _RULES[rule_id]
|
|
||||||
|
|
||||||
if rule_id in _EXTERNAL_RULES:
|
|
||||||
return _EXTERNAL_RULES[rule_id]
|
|
||||||
|
|
||||||
raise ValueError('no such rule: "%s"' % rule_id)
|
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ braces (``{`` and ``}``).
|
|||||||
|
|
||||||
* ``forbid`` is used to forbid the use of flow mappings which are denoted by
|
* ``forbid`` is used to forbid the use of flow mappings which are denoted by
|
||||||
surrounding braces (``{`` and ``}``). Use ``true`` to forbid the use of flow
|
surrounding braces (``{`` and ``}``). Use ``true`` to forbid the use of flow
|
||||||
mappings completely.
|
mappings completely. Use ``non-empty`` to forbid the use of all flow
|
||||||
|
mappings except for empty ones.
|
||||||
* ``min-spaces-inside`` defines the minimal number of spaces required inside
|
* ``min-spaces-inside`` defines the minimal number of spaces required inside
|
||||||
braces.
|
braces.
|
||||||
* ``max-spaces-inside`` defines the maximal number of spaces allowed inside
|
* ``max-spaces-inside`` defines the maximal number of spaces allowed inside
|
||||||
@@ -60,6 +61,18 @@ braces (``{`` and ``}``).
|
|||||||
|
|
||||||
object: { key1: 4, key2: 8 }
|
object: { key1: 4, key2: 8 }
|
||||||
|
|
||||||
|
#. With ``braces: {forbid: non-empty}``
|
||||||
|
|
||||||
|
the following code snippet would **PASS**:
|
||||||
|
::
|
||||||
|
|
||||||
|
object: {}
|
||||||
|
|
||||||
|
the following code snippet would **FAIL**:
|
||||||
|
::
|
||||||
|
|
||||||
|
object: { key1: 4, key2: 8 }
|
||||||
|
|
||||||
#. With ``braces: {min-spaces-inside: 0, max-spaces-inside: 0}``
|
#. With ``braces: {min-spaces-inside: 0, max-spaces-inside: 0}``
|
||||||
|
|
||||||
the following code snippet would **PASS**:
|
the following code snippet would **PASS**:
|
||||||
@@ -128,7 +141,7 @@ from yamllint.rules.common import spaces_after, spaces_before
|
|||||||
|
|
||||||
ID = 'braces'
|
ID = 'braces'
|
||||||
TYPE = 'token'
|
TYPE = 'token'
|
||||||
CONF = {'forbid': bool,
|
CONF = {'forbid': (bool, 'non-empty'),
|
||||||
'min-spaces-inside': int,
|
'min-spaces-inside': int,
|
||||||
'max-spaces-inside': int,
|
'max-spaces-inside': int,
|
||||||
'min-spaces-inside-empty': int,
|
'min-spaces-inside-empty': int,
|
||||||
@@ -141,7 +154,15 @@ DEFAULT = {'forbid': False,
|
|||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
if conf['forbid'] and isinstance(token, yaml.FlowMappingStartToken):
|
if (conf['forbid'] is True and
|
||||||
|
isinstance(token, yaml.FlowMappingStartToken)):
|
||||||
|
yield LintProblem(token.start_mark.line + 1,
|
||||||
|
token.end_mark.column + 1,
|
||||||
|
'forbidden flow mapping')
|
||||||
|
|
||||||
|
elif (conf['forbid'] == 'non-empty' and
|
||||||
|
isinstance(token, yaml.FlowMappingStartToken) and
|
||||||
|
not isinstance(next, yaml.FlowMappingEndToken)):
|
||||||
yield LintProblem(token.start_mark.line + 1,
|
yield LintProblem(token.start_mark.line + 1,
|
||||||
token.end_mark.column + 1,
|
token.end_mark.column + 1,
|
||||||
'forbidden flow mapping')
|
'forbidden flow mapping')
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ inside brackets (``[`` and ``]``).
|
|||||||
|
|
||||||
* ``forbid`` is used to forbid the use of flow sequences which are denoted by
|
* ``forbid`` is used to forbid the use of flow sequences which are denoted by
|
||||||
surrounding brackets (``[`` and ``]``). Use ``true`` to forbid the use of
|
surrounding brackets (``[`` and ``]``). Use ``true`` to forbid the use of
|
||||||
flow sequences completely.
|
flow sequences completely. Use ``non-empty`` to forbid the use of all flow
|
||||||
|
sequences except for empty ones.
|
||||||
* ``min-spaces-inside`` defines the minimal number of spaces required inside
|
* ``min-spaces-inside`` defines the minimal number of spaces required inside
|
||||||
brackets.
|
brackets.
|
||||||
* ``max-spaces-inside`` defines the maximal number of spaces allowed inside
|
* ``max-spaces-inside`` defines the maximal number of spaces allowed inside
|
||||||
@@ -61,6 +62,18 @@ inside brackets (``[`` and ``]``).
|
|||||||
|
|
||||||
object: [ 1, 2, abc ]
|
object: [ 1, 2, abc ]
|
||||||
|
|
||||||
|
#. With ``brackets: {forbid: non-empty}``
|
||||||
|
|
||||||
|
the following code snippet would **PASS**:
|
||||||
|
::
|
||||||
|
|
||||||
|
object: []
|
||||||
|
|
||||||
|
the following code snippet would **FAIL**:
|
||||||
|
::
|
||||||
|
|
||||||
|
object: [ 1, 2, abc ]
|
||||||
|
|
||||||
#. With ``brackets: {min-spaces-inside: 0, max-spaces-inside: 0}``
|
#. With ``brackets: {min-spaces-inside: 0, max-spaces-inside: 0}``
|
||||||
|
|
||||||
the following code snippet would **PASS**:
|
the following code snippet would **PASS**:
|
||||||
@@ -129,7 +142,7 @@ from yamllint.rules.common import spaces_after, spaces_before
|
|||||||
|
|
||||||
ID = 'brackets'
|
ID = 'brackets'
|
||||||
TYPE = 'token'
|
TYPE = 'token'
|
||||||
CONF = {'forbid': bool,
|
CONF = {'forbid': (bool, 'non-empty'),
|
||||||
'min-spaces-inside': int,
|
'min-spaces-inside': int,
|
||||||
'max-spaces-inside': int,
|
'max-spaces-inside': int,
|
||||||
'min-spaces-inside-empty': int,
|
'min-spaces-inside-empty': int,
|
||||||
@@ -142,7 +155,15 @@ DEFAULT = {'forbid': False,
|
|||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
if conf['forbid'] and isinstance(token, yaml.FlowSequenceStartToken):
|
if (conf['forbid'] is True and
|
||||||
|
isinstance(token, yaml.FlowSequenceStartToken)):
|
||||||
|
yield LintProblem(token.start_mark.line + 1,
|
||||||
|
token.end_mark.column + 1,
|
||||||
|
'forbidden flow sequence')
|
||||||
|
|
||||||
|
elif (conf['forbid'] == 'non-empty' and
|
||||||
|
isinstance(token, yaml.FlowSequenceStartToken) and
|
||||||
|
not isinstance(next, yaml.FlowSequenceEndToken)):
|
||||||
yield LintProblem(token.start_mark.line + 1,
|
yield LintProblem(token.start_mark.line + 1,
|
||||||
token.end_mark.column + 1,
|
token.end_mark.column + 1,
|
||||||
'forbidden flow sequence')
|
'forbidden flow sequence')
|
||||||
|
|||||||
@@ -17,10 +17,6 @@
|
|||||||
"""
|
"""
|
||||||
Use this rule to set a limit to lines length.
|
Use this rule to set a limit to lines length.
|
||||||
|
|
||||||
Note: with Python 2, the ``line-length`` rule may not work properly with
|
|
||||||
unicode characters because of the way strings are represented in bytes. We
|
|
||||||
recommend running yamllint with Python 3.
|
|
||||||
|
|
||||||
.. rubric:: Options
|
.. rubric:: Options
|
||||||
|
|
||||||
* ``max`` defines the maximal (inclusive) length of lines.
|
* ``max`` defines the maximal (inclusive) length of lines.
|
||||||
@@ -144,7 +140,11 @@ def check(conf, line):
|
|||||||
start += 1
|
start += 1
|
||||||
|
|
||||||
if start != line.end:
|
if start != line.end:
|
||||||
if line.buffer[start] in ('#', '-'):
|
if line.buffer[start] == '#':
|
||||||
|
while line.buffer[start] == '#':
|
||||||
|
start += 1
|
||||||
|
start += 1
|
||||||
|
elif line.buffer[start] == '-':
|
||||||
start += 2
|
start += 2
|
||||||
|
|
||||||
if line.buffer.find(' ', start, line.end) == -1:
|
if line.buffer.find(' ', start, line.end) == -1:
|
||||||
|
|||||||
@@ -144,6 +144,17 @@ def VALIDATE(conf):
|
|||||||
|
|
||||||
DEFAULT_SCALAR_TAG = u'tag:yaml.org,2002:str'
|
DEFAULT_SCALAR_TAG = u'tag:yaml.org,2002:str'
|
||||||
|
|
||||||
|
# https://stackoverflow.com/a/36514274
|
||||||
|
yaml.resolver.Resolver.add_implicit_resolver(
|
||||||
|
'tag:yaml.org,2002:int',
|
||||||
|
re.compile(r'''^(?:[-+]?0b[0-1_]+
|
||||||
|
|[-+]?0o?[0-7_]+
|
||||||
|
|[-+]?0[0-7_]+
|
||||||
|
|[-+]?(?:0|[1-9][0-9_]*)
|
||||||
|
|[-+]?0x[0-9a-fA-F_]+
|
||||||
|
|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$''', re.X),
|
||||||
|
list('-+0123456789'))
|
||||||
|
|
||||||
|
|
||||||
def _quote_match(quote_type, token_style):
|
def _quote_match(quote_type, token_style):
|
||||||
return ((quote_type == 'any') or
|
return ((quote_type == 'any') or
|
||||||
|
|||||||
Reference in New Issue
Block a user