Compare commits
4 Commits
master
...
feature/pl
Author | SHA1 | Date |
---|---|---|
Adrien Vergé | d32d1f65ad | 4 years ago |
Satoru SATOH | 9f9e282da5 | 4 years ago |
Satoru SATOH | 6abce4e9a9 | 4 years ago |
Satoru SATOH | 1c15ad1adc | 4 years ago |
@ -1,4 +0,0 @@
|
||||
[flake8]
|
||||
import-order-style = pep8
|
||||
application-import-names = yamllint
|
||||
ignore = W503,W504
|
@ -1,61 +0,0 @@
|
||||
---
|
||||
|
||||
name: CI
|
||||
|
||||
on: # yamllint disable-line rule:truthy
|
||||
push:
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
name: Linters
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v4
|
||||
- run:
|
||||
pip install flake8 flake8-import-order sphinx rstcheck[sphinx] doc8
|
||||
- run: pip install .
|
||||
- run: flake8 .
|
||||
- run: doc8 $(git ls-files '*.rst')
|
||||
- run: rstcheck --ignore-directives automodule $(git ls-files '*.rst')
|
||||
- run: yamllint --strict $(git ls-files '*.yaml' '*.yml')
|
||||
- run: make -C docs html
|
||||
- name: Check for broken links in documentation
|
||||
run: make -C docs linkcheck
|
||||
|
||||
test:
|
||||
name: Tests
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
python-version:
|
||||
- '3.7'
|
||||
- '3.8'
|
||||
- '3.9'
|
||||
- '3.10'
|
||||
- '3.11'
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: Set up Python ${{ matrix.python-version }}
|
||||
uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
- name: Append GitHub Actions system path
|
||||
run: echo "$HOME/.local/bin" >> $GITHUB_PATH
|
||||
- run: pip install coverage
|
||||
- run: pip install .
|
||||
# https://github.com/AndreMiras/coveralls-python-action/issues/18
|
||||
- run: echo -e "[run]\nrelative_files = True" > .coveragerc
|
||||
- run: coverage run -m unittest discover
|
||||
- name: Coveralls
|
||||
uses: AndreMiras/coveralls-python-action@develop
|
@ -0,0 +1,28 @@
|
||||
---
|
||||
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
|
@ -0,0 +1,4 @@
|
||||
include LICENSE
|
||||
include README.rst
|
||||
include docs/*
|
||||
include tests/*.py tests/rules/*.py tests/yaml-1.2-spec-examples/*
|
Binary file not shown.
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 41 KiB |
@ -1,54 +0,0 @@
|
||||
[project]
|
||||
name = "yamllint"
|
||||
description = "A linter for YAML files."
|
||||
readme = {file = "README.rst", content-type = "text/x-rst"}
|
||||
requires-python = ">=3.7"
|
||||
license = {text = "GPL-3.0-or-later"}
|
||||
authors = [{name = "Adrien Vergé"}]
|
||||
keywords = ["yaml", "lint", "linter", "syntax", "checker"]
|
||||
classifiers = [
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
"Environment :: Console",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
|
||||
"Programming Language :: Python",
|
||||
"Topic :: Software Development",
|
||||
"Topic :: Software Development :: Debuggers",
|
||||
"Topic :: Software Development :: Quality Assurance",
|
||||
"Topic :: Software Development :: Testing",
|
||||
]
|
||||
dependencies = [
|
||||
"pathspec >= 0.5.3",
|
||||
"pyyaml",
|
||||
]
|
||||
dynamic = ["version"]
|
||||
|
||||
[project.optional-dependencies]
|
||||
dev = [
|
||||
"doc8",
|
||||
"flake8",
|
||||
"flake8-import-order",
|
||||
"rstcheck[sphinx]",
|
||||
"sphinx",
|
||||
]
|
||||
|
||||
[project.scripts]
|
||||
yamllint = "yamllint.cli:run"
|
||||
|
||||
[project.urls]
|
||||
homepage = "https://github.com/adrienverge/yamllint"
|
||||
repository = "https://github.com/adrienverge/yamllint"
|
||||
documentation = "https://yamllint.readthedocs.io"
|
||||
|
||||
[build-system]
|
||||
build-backend = "setuptools.build_meta"
|
||||
requires = ["setuptools >= 61"]
|
||||
|
||||
[tool.setuptools]
|
||||
packages = ["yamllint", "yamllint.conf", "yamllint.rules"]
|
||||
|
||||
[tool.setuptools.package-data]
|
||||
yamllint = ["conf/*.yaml"]
|
||||
|
||||
[tool.setuptools.dynamic]
|
||||
version = {attr = "yamllint.__version__"}
|
@ -0,0 +1,71 @@
|
||||
[bdist_wheel]
|
||||
universal = 1
|
||||
|
||||
[flake8]
|
||||
import-order-style = pep8
|
||||
application-import-names = yamllint
|
||||
ignore = W503,W504
|
||||
|
||||
[build_sphinx]
|
||||
all-files = 1
|
||||
source-dir = docs
|
||||
build-dir = docs/_build
|
||||
warning-is-error = 1
|
||||
|
||||
[metadata]
|
||||
keywords =
|
||||
yaml
|
||||
lint
|
||||
linter
|
||||
syntax
|
||||
checker
|
||||
|
||||
url = https://github.com/adrienverge/yamllint
|
||||
classifiers =
|
||||
Development Status :: 5 - Production/Stable
|
||||
Environment :: Console
|
||||
Intended Audience :: Developers
|
||||
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.4
|
||||
Programming Language :: Python :: 3.5
|
||||
Programming Language :: Python :: 3.6
|
||||
Programming Language :: Python :: 3.7
|
||||
Programming Language :: Python :: 3.8
|
||||
Topic :: Software Development
|
||||
Topic :: Software Development :: Debuggers
|
||||
Topic :: Software Development :: Quality Assurance
|
||||
Topic :: Software Development :: Testing
|
||||
|
||||
project_urls =
|
||||
Documentation = https://yamllint.readthedocs.io
|
||||
Download = https://pypi.org/project/yamllint/#files
|
||||
Bug Tracker = https://github.com/adrienverge/yamllint/issues
|
||||
Source Code = https://github.com/adrienverge/yamllint
|
||||
|
||||
[options]
|
||||
packages = find:
|
||||
|
||||
python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
|
||||
|
||||
include_package_data = True
|
||||
install_requires =
|
||||
pathspec >= 0.5.3
|
||||
pyyaml
|
||||
setuptools
|
||||
|
||||
test_suite = tests
|
||||
|
||||
[options.packages.find]
|
||||
exclude =
|
||||
tests
|
||||
tests.*
|
||||
|
||||
[options.package_data]
|
||||
yamllint = conf/*.yaml
|
||||
|
||||
[options.entry_points]
|
||||
console_scripts =
|
||||
yamllint = yamllint.cli:run
|
@ -0,0 +1,26 @@
|
||||
# -*- 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
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
#
|
||||
# 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')
|
@ -0,0 +1,11 @@
|
||||
[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
|
@ -0,0 +1,2 @@
|
||||
import setuptools
|
||||
setuptools.setup()
|
@ -0,0 +1,27 @@
|
||||
# -*- 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,
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
#
|
||||
# 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')
|
@ -0,0 +1,31 @@
|
||||
# -*- 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')
|
@ -1,281 +0,0 @@
|
||||
# Copyright (C) 2023 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 AnchorsTestCase(RuleTestCase):
|
||||
rule_id = 'anchors'
|
||||
|
||||
def test_disabled(self):
|
||||
conf = 'anchors: disable'
|
||||
self.check('---\n'
|
||||
'- &b true\n'
|
||||
'- &i 42\n'
|
||||
'- &s hello\n'
|
||||
'- &f_m {k: v}\n'
|
||||
'- &f_s [1, 2]\n'
|
||||
'- *b\n'
|
||||
'- *i\n'
|
||||
'- *s\n'
|
||||
'- *f_m\n'
|
||||
'- *f_s\n'
|
||||
'---\n' # redeclare anchors in a new document
|
||||
'- &b true\n'
|
||||
'- &i 42\n'
|
||||
'- &s hello\n'
|
||||
'- *b\n'
|
||||
'- *i\n'
|
||||
'- *s\n'
|
||||
'---\n'
|
||||
'block mapping: &b_m\n'
|
||||
' key: value\n'
|
||||
'extended:\n'
|
||||
' <<: *b_m\n'
|
||||
' foo: bar\n'
|
||||
'---\n'
|
||||
'{a: 1, &x b: 2, c: &y 3, *x : 4, e: *y}\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'- &i 42\n'
|
||||
'---\n'
|
||||
'- &b true\n'
|
||||
'- &b true\n'
|
||||
'- &b true\n'
|
||||
'- &s hello\n'
|
||||
'- *b\n'
|
||||
'- *i\n' # declared in a previous document
|
||||
'- *f_m\n' # never declared
|
||||
'- *f_m\n'
|
||||
'- *f_m\n'
|
||||
'- *f_s\n' # declared after
|
||||
'- &f_s [1, 2]\n'
|
||||
'---\n'
|
||||
'block mapping: &b_m\n'
|
||||
' key: value\n'
|
||||
'---\n'
|
||||
'block mapping 1: &b_m_bis\n'
|
||||
' key: value\n'
|
||||
'block mapping 2: &b_m_bis\n'
|
||||
' key: value\n'
|
||||
'extended:\n'
|
||||
' <<: *b_m\n'
|
||||
' foo: bar\n'
|
||||
'---\n'
|
||||
'{a: 1, &x b: 2, c: &x 3, *x : 4, e: *y}\n'
|
||||
'...\n', conf)
|
||||
|
||||
def test_forbid_undeclared_aliases(self):
|
||||
conf = ('anchors:\n'
|
||||
' forbid-undeclared-aliases: true\n'
|
||||
' forbid-duplicated-anchors: false\n'
|
||||
' forbid-unused-anchors: false\n')
|
||||
self.check('---\n'
|
||||
'- &b true\n'
|
||||
'- &i 42\n'
|
||||
'- &s hello\n'
|
||||
'- &f_m {k: v}\n'
|
||||
'- &f_s [1, 2]\n'
|
||||
'- *b\n'
|
||||
'- *i\n'
|
||||
'- *s\n'
|
||||
'- *f_m\n'
|
||||
'- *f_s\n'
|
||||
'---\n' # redeclare anchors in a new document
|
||||
'- &b true\n'
|
||||
'- &i 42\n'
|
||||
'- &s hello\n'
|
||||
'- *b\n'
|
||||
'- *i\n'
|
||||
'- *s\n'
|
||||
'---\n'
|
||||
'block mapping: &b_m\n'
|
||||
' key: value\n'
|
||||
'extended:\n'
|
||||
' <<: *b_m\n'
|
||||
' foo: bar\n'
|
||||
'---\n'
|
||||
'{a: 1, &x b: 2, c: &y 3, *x : 4, e: *y}\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'- &i 42\n'
|
||||
'---\n'
|
||||
'- &b true\n'
|
||||
'- &b true\n'
|
||||
'- &b true\n'
|
||||
'- &s hello\n'
|
||||
'- *b\n'
|
||||
'- *i\n' # declared in a previous document
|
||||
'- *f_m\n' # never declared
|
||||
'- *f_m\n'
|
||||
'- *f_m\n'
|
||||
'- *f_s\n' # declared after
|
||||
'- &f_s [1, 2]\n'
|
||||
'...\n'
|
||||
'---\n'
|
||||
'block mapping: &b_m\n'
|
||||
' key: value\n'
|
||||
'---\n'
|
||||
'block mapping 1: &b_m_bis\n'
|
||||
' key: value\n'
|
||||
'block mapping 2: &b_m_bis\n'
|
||||
' key: value\n'
|
||||
'extended:\n'
|
||||
' <<: *b_m\n'
|
||||
' foo: bar\n'
|
||||
'---\n'
|
||||
'{a: 1, &x b: 2, c: &x 3, *x : 4, e: *y}\n'
|
||||
'...\n', conf,
|
||||
problem1=(9, 3),
|
||||
problem2=(10, 3),
|
||||
problem3=(11, 3),
|
||||
problem4=(12, 3),
|
||||
problem5=(13, 3),
|
||||
problem6=(25, 7),
|
||||
problem7=(28, 37))
|
||||
|
||||
def test_forbid_duplicated_anchors(self):
|
||||
conf = ('anchors:\n'
|
||||
' forbid-undeclared-aliases: false\n'
|
||||
' forbid-duplicated-anchors: true\n'
|
||||
' forbid-unused-anchors: false\n')
|
||||
self.check('---\n'
|
||||
'- &b true\n'
|
||||
'- &i 42\n'
|
||||
'- &s hello\n'
|
||||
'- &f_m {k: v}\n'
|
||||
'- &f_s [1, 2]\n'
|
||||
'- *b\n'
|
||||
'- *i\n'
|
||||
'- *s\n'
|
||||
'- *f_m\n'
|
||||
'- *f_s\n'
|
||||
'---\n' # redeclare anchors in a new document
|
||||
'- &b true\n'
|
||||
'- &i 42\n'
|
||||
'- &s hello\n'
|
||||
'- *b\n'
|
||||
'- *i\n'
|
||||
'- *s\n'
|
||||
'---\n'
|
||||
'block mapping: &b_m\n'
|
||||
' key: value\n'
|
||||
'extended:\n'
|
||||
' <<: *b_m\n'
|
||||
' foo: bar\n'
|
||||
'---\n'
|
||||
'{a: 1, &x b: 2, c: &y 3, *x : 4, e: *y}\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'- &i 42\n'
|
||||
'---\n'
|
||||
'- &b true\n'
|
||||
'- &b true\n'
|
||||
'- &b true\n'
|
||||
'- &s hello\n'
|
||||
'- *b\n'
|
||||
'- *i\n' # declared in a previous document
|
||||
'- *f_m\n' # never declared
|
||||
'- *f_m\n'
|
||||
'- *f_m\n'
|
||||
'- *f_s\n' # declared after
|
||||
'- &f_s [1, 2]\n'
|
||||
'...\n'
|
||||
'---\n'
|
||||
'block mapping: &b_m\n'
|
||||
' key: value\n'
|
||||
'---\n'
|
||||
'block mapping 1: &b_m_bis\n'
|
||||
' key: value\n'
|
||||
'block mapping 2: &b_m_bis\n'
|
||||
' key: value\n'
|
||||
'extended:\n'
|
||||
' <<: *b_m\n'
|
||||
' foo: bar\n'
|
||||
'---\n'
|
||||
'{a: 1, &x b: 2, c: &x 3, *x : 4, e: *y}\n'
|
||||
'...\n', conf,
|
||||
problem1=(5, 3),
|
||||
problem2=(6, 3),
|
||||
problem3=(22, 18),
|
||||
problem4=(28, 20))
|
||||
|
||||
def test_forbid_unused_anchors(self):
|
||||
conf = ('anchors:\n'
|
||||
' forbid-undeclared-aliases: false\n'
|
||||
' forbid-duplicated-anchors: false\n'
|
||||
' forbid-unused-anchors: true\n')
|
||||
|
||||
self.check('---\n'
|
||||
'- &b true\n'
|
||||
'- &i 42\n'
|
||||
'- &s hello\n'
|
||||
'- &f_m {k: v}\n'
|
||||
'- &f_s [1, 2]\n'
|
||||
'- *b\n'
|
||||
'- *i\n'
|
||||
'- *s\n'
|
||||
'- *f_m\n'
|
||||
'- *f_s\n'
|
||||
'---\n' # redeclare anchors in a new document
|
||||
'- &b true\n'
|
||||
'- &i 42\n'
|
||||
'- &s hello\n'
|
||||
'- *b\n'
|
||||
'- *i\n'
|
||||
'- *s\n'
|
||||
'---\n'
|
||||
'block mapping: &b_m\n'
|
||||
' key: value\n'
|
||||
'extended:\n'
|
||||
' <<: *b_m\n'
|
||||
' foo: bar\n'
|
||||
'---\n'
|
||||
'{a: 1, &x b: 2, c: &y 3, *x : 4, e: *y}\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'- &i 42\n'
|
||||
'---\n'
|
||||
'- &b true\n'
|
||||
'- &b true\n'
|
||||
'- &b true\n'
|
||||
'- &s hello\n'
|
||||
'- *b\n'
|
||||
'- *i\n' # declared in a previous document
|
||||
'- *f_m\n' # never declared
|
||||
'- *f_m\n'
|
||||
'- *f_m\n'
|
||||
'- *f_s\n' # declared after
|
||||
'- &f_s [1, 2]\n'
|
||||
'...\n'
|
||||
'---\n'
|
||||
'block mapping: &b_m\n'
|
||||
' key: value\n'
|
||||
'---\n'
|
||||
'block mapping 1: &b_m_bis\n'
|
||||
' key: value\n'
|
||||
'block mapping 2: &b_m_bis\n'
|
||||
' key: value\n'
|
||||
'extended:\n'
|
||||
' <<: *b_m\n'
|
||||
' foo: bar\n'
|
||||
'---\n'
|
||||
'{a: 1, &x b: 2, c: &x 3, *x : 4, e: *y}\n'
|
||||
'...\n', conf,
|
||||
problem1=(2, 3),
|
||||
problem2=(7, 3),
|
||||
problem3=(14, 3),
|
||||
problem4=(17, 16),
|
||||
problem5=(22, 18))
|
@ -1,128 +0,0 @@
|
||||
# Copyright (C) 2022 the yamllint contributors
|
||||
#
|
||||
# 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 FloatValuesTestCase(RuleTestCase):
|
||||
rule_id = 'float-values'
|
||||
|
||||
def test_disabled(self):
|
||||
conf = 'float-values: disable\n'
|
||||
self.check('---\n'
|
||||
'- 0.0\n'
|
||||
'- .NaN\n'
|
||||
'- .INF\n'
|
||||
'- .1\n'
|
||||
'- 10e-6\n',
|
||||
conf)
|
||||
|
||||
def test_numeral_before_decimal(self):
|
||||
conf = (
|
||||
'float-values:\n'
|
||||
' require-numeral-before-decimal: true\n'
|
||||
' forbid-scientific-notation: false\n'
|
||||
' forbid-nan: false\n'
|
||||
' forbid-inf: false\n')
|
||||
self.check('---\n'
|
||||
'- 0.0\n'
|
||||
'- .1\n'
|
||||
'- \'.1\'\n'
|
||||
'- string.1\n'
|
||||
'- .1string\n'
|
||||
'- !custom_tag .2\n'
|
||||
'- &angle1 0.0\n'
|
||||
'- *angle1\n'
|
||||
'- &angle2 .3\n'
|
||||
'- *angle2\n',
|
||||
conf,
|
||||
problem1=(3, 3),
|
||||
problem2=(10, 11))
|
||||
|
||||
def test_scientific_notation(self):
|
||||
conf = (
|
||||
'float-values:\n'
|
||||
' require-numeral-before-decimal: false\n'
|
||||
' forbid-scientific-notation: true\n'
|
||||
' forbid-nan: false\n'
|
||||
' forbid-inf: false\n')
|
||||
self.check('---\n'
|
||||
'- 10e6\n'
|
||||
'- 10e-6\n'
|
||||
'- 0.00001\n'
|
||||
'- \'10e-6\'\n'
|
||||
'- string10e-6\n'
|
||||
'- 10e-6string\n'
|
||||
'- !custom_tag 10e-6\n'
|
||||
'- &angle1 0.000001\n'
|
||||
'- *angle1\n'
|
||||
'- &angle2 10e-6\n'
|
||||
'- *angle2\n'
|
||||
'- &angle3 10e6\n'
|
||||
'- *angle3\n',
|
||||
conf,
|
||||
problem1=(2, 3),
|
||||
problem2=(3, 3),
|
||||
problem3=(11, 11),
|
||||
problem4=(13, 11))
|
||||
|
||||
def test_nan(self):
|
||||
conf = (
|
||||
'float-values:\n'
|
||||
' require-numeral-before-decimal: false\n'
|
||||
' forbid-scientific-notation: false\n'
|
||||
' forbid-nan: true\n'
|
||||
' forbid-inf: false\n')
|
||||
self.check('---\n'
|
||||
'- .NaN\n'
|
||||
'- .NAN\n'
|
||||
'- \'.NaN\'\n'
|
||||
'- a.NaN\n'
|
||||
'- .NaNa\n'
|
||||
'- !custom_tag .NaN\n'
|
||||
'- &angle .nan\n'
|
||||
'- *angle\n',
|
||||
conf,
|
||||
problem1=(2, 3),
|
||||
problem2=(3, 3),
|
||||
problem3=(8, 10))
|
||||
|
||||
def test_inf(self):
|
||||
conf = (
|
||||
'float-values:\n'
|
||||
' require-numeral-before-decimal: false\n'
|
||||
' forbid-scientific-notation: false\n'
|
||||
' forbid-nan: false\n'
|
||||
' forbid-inf: true\n')
|
||||
self.check('---\n'
|
||||
'- .inf\n'
|
||||
'- .INF\n'
|
||||
'- -.inf\n'
|
||||
'- -.INF\n'
|
||||
'- \'.inf\'\n'
|
||||
'- ∞.infinity\n'
|
||||
'- .infinity∞\n'
|
||||
'- !custom_tag .inf\n'
|
||||
'- &angle .inf\n'
|
||||
'- *angle\n'
|
||||
'- &angle -.inf\n'
|
||||
'- *angle\n',
|
||||
conf,
|
||||
problem1=(2, 3),
|
||||
problem2=(3, 3),
|
||||
problem3=(4, 3),
|
||||
problem4=(5, 3),
|
||||
problem5=(10, 10),
|
||||
problem6=(12, 10))
|
@ -0,0 +1,80 @@
|
||||
# -*- 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()])), [])
|
@ -0,0 +1,64 @@
|
||||
# -*- 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)
|
@ -0,0 +1,62 @@
|
||||
# -*- 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())
|
@ -1,174 +0,0 @@
|
||||
# Copyright (C) 2023 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/>.
|
||||
|
||||
"""
|
||||
Use this rule to report duplicated anchors and aliases referencing undeclared
|
||||
anchors.
|
||||
|
||||
.. rubric:: Options
|
||||
|
||||
* Set ``forbid-undeclared-aliases`` to ``true`` to avoid aliases that reference
|
||||
an anchor that hasn't been declared (either not declared at all, or declared
|
||||
later in the document).
|
||||
* Set ``forbid-duplicated-anchors`` to ``true`` to avoid duplications of a same
|
||||
anchor.
|
||||
* Set ``forbid-unused-anchors`` to ``true`` to avoid anchors being declared but
|
||||
not used anywhere in the YAML document via alias.
|
||||
|
||||
.. rubric:: Default values (when enabled)
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
rules:
|
||||
anchors:
|
||||
forbid-undeclared-aliases: true
|
||||
forbid-duplicated-anchors: false
|
||||
forbid-unused-anchors: false
|
||||
|
||||
.. rubric:: Examples
|
||||
|
||||
#. With ``anchors: {forbid-undeclared-aliases: true}``
|
||||
|
||||
the following code snippet would **PASS**:
|
||||
::
|
||||
|
||||
---
|
||||
- &anchor
|
||||
foo: bar
|
||||
- *anchor
|
||||
|
||||
the following code snippet would **FAIL**:
|
||||
::
|
||||
|
||||
---
|
||||
- &anchor
|
||||
foo: bar
|
||||
- *unknown
|
||||
|
||||
the following code snippet would **FAIL**:
|
||||
::
|
||||
|
||||
---
|
||||
- &anchor
|
||||
foo: bar
|
||||
- <<: *unknown
|
||||
extra: value
|
||||
|
||||
#. With ``anchors: {forbid-duplicated-anchors: true}``
|
||||
|
||||
the following code snippet would **PASS**:
|
||||
::
|
||||
|
||||
---
|
||||
- &anchor1 Foo Bar
|
||||
- &anchor2 [item 1, item 2]
|
||||
|
||||
the following code snippet would **FAIL**:
|
||||
::
|
||||
|
||||
---
|
||||
- &anchor Foo Bar
|
||||
- &anchor [item 1, item 2]
|
||||
|
||||
#. With ``anchors: {forbid-unused-anchors: true}``
|
||||
|
||||
the following code snippet would **PASS**:
|
||||
::
|
||||
|
||||
---
|
||||
- &anchor
|
||||
foo: bar
|
||||
- *anchor
|
||||
|
||||
the following code snippet would **FAIL**:
|
||||
::
|
||||
|
||||
---
|
||||
- &anchor
|
||||
foo: bar
|
||||
- items:
|
||||
- item1
|
||||
- item2
|
||||
"""
|
||||
|
||||
|
||||
import yaml
|
||||
|
||||
from yamllint.linter import LintProblem
|
||||
|
||||
|
||||
ID = 'anchors'
|
||||
TYPE = 'token'
|
||||
CONF = {'forbid-undeclared-aliases': bool,
|
||||
'forbid-duplicated-anchors': bool,
|
||||
'forbid-unused-anchors': bool}
|
||||
DEFAULT = {'forbid-undeclared-aliases': True,
|
||||
'forbid-duplicated-anchors': False,
|
||||
'forbid-unused-anchors': False}
|
||||
|
||||
|
||||
def check(conf, token, prev, next, nextnext, context):
|
||||
if (conf['forbid-undeclared-aliases'] or
|
||||
conf['forbid-duplicated-anchors'] or
|
||||
conf['forbid-unused-anchors']):
|
||||
if isinstance(token, (
|
||||
yaml.StreamStartToken,
|
||||
yaml.DocumentStartToken,
|
||||
yaml.DocumentEndToken)):
|
||||
context['anchors'] = {}
|
||||
|
||||
if (conf['forbid-undeclared-aliases'] and
|
||||
isinstance(token, yaml.AliasToken) and
|
||||
token.value not in context['anchors']):
|
||||
yield LintProblem(
|
||||
token.start_mark.line + 1, token.start_mark.column + 1,
|
||||
f'found undeclared alias "{token.value}"')
|
||||
|
||||
if (conf['forbid-duplicated-anchors'] and
|
||||
isinstance(token, yaml.AnchorToken) and
|
||||
token.value in context['anchors']):
|
||||
yield LintProblem(
|
||||
token.start_mark.line + 1, token.start_mark.column + 1,
|
||||
f'found duplicated anchor "{token.value}"')
|
||||
|
||||
if conf['forbid-unused-anchors']:
|
||||
# Unused anchors can only be detected at the end of Document.
|
||||
# End of document can be either
|
||||
# - end of stream
|
||||
# - end of document sign '...'
|
||||
# - start of a new document sign '---'
|
||||
# If next token indicates end of document,
|
||||
# check if the anchors have been used or not.
|
||||
# If they haven't been used, report problem on those anchors.
|
||||
if isinstance(next, (yaml.StreamEndToken,
|
||||
yaml.DocumentStartToken,
|
||||
yaml.DocumentEndToken)):
|
||||
for anchor, info in context['anchors'].items():
|
||||
if not info['used']:
|
||||
yield LintProblem(info['line'] + 1,
|
||||
info['column'] + 1,
|
||||
f'found unused anchor "{anchor}"')
|
||||
elif isinstance(token, yaml.AliasToken):
|
||||
context['anchors'].get(token.value, {})['used'] = True
|
||||
|
||||
if (conf['forbid-undeclared-aliases'] or
|
||||
conf['forbid-duplicated-anchors'] or
|
||||
conf['forbid-unused-anchors']):
|
||||
if isinstance(token, yaml.AnchorToken):
|
||||
context['anchors'][token.value] = {
|
||||
'line': token.start_mark.line,
|
||||
'column': token.start_mark.column,
|
||||
'used': False
|
||||
}
|
@ -1,158 +0,0 @@
|
||||
# Copyright (C) 2022 the yamllint contributors
|
||||
|
||||
# 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 limit the permitted values for floating-point numbers.
|
||||
YAML permits three classes of float expressions: approximation to real numbers,
|
||||
positive and negative infinity and "not a number".
|
||||
|
||||
.. rubric:: Options
|
||||
|
||||
* Use ``require-numeral-before-decimal`` to require floats to start
|
||||
with a numeral (ex ``0.0`` instead of ``.0``).
|
||||
* Use ``forbid-scientific-notation`` to forbid scientific notation.
|
||||
* Use ``forbid-nan`` to forbid NaN (not a number) values.
|
||||
* Use ``forbid-inf`` to forbid infinite values.
|
||||
|
||||
.. rubric:: Default values (when enabled)
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
rules:
|
||||
float-values:
|
||||
forbid-inf: false
|
||||
forbid-nan: false
|
||||
forbid-scientific-notation: false
|
||||
require-numeral-before-decimal: false
|
||||
|
||||
.. rubric:: Examples
|
||||
|
||||
#. With ``float-values: {require-numeral-before-decimal: true}``
|
||||
|
||||
the following code snippets would **PASS**:
|
||||
::
|
||||
|
||||
anemometer:
|
||||
angle: 0.0
|
||||
|
||||
the following code snippets would **FAIL**:
|
||||
::
|
||||
|
||||
anemometer:
|
||||
angle: .0
|
||||
|
||||
#. With ``float-values: {forbid-scientific-notation: true}``
|
||||
|
||||
the following code snippets would **PASS**:
|
||||
::
|
||||
|
||||
anemometer:
|
||||
angle: 0.00001
|
||||
|
||||
the following code snippets would **FAIL**:
|
||||
::
|
||||
|
||||
anemometer:
|
||||
angle: 10e-6
|
||||
|
||||
#. With ``float-values: {forbid-nan: true}``
|
||||
|
||||
the following code snippets would **FAIL**:
|
||||
::
|
||||
|
||||
anemometer:
|
||||
angle: .NaN
|
||||
|
||||
#. With ``float-values: {forbid-inf: true}``
|
||||
|
||||
the following code snippets would **FAIL**:
|
||||
::
|
||||
|
||||
anemometer:
|
||||
angle: .inf
|
||||
"""
|
||||
|
||||
import re
|
||||
|
||||
import yaml
|
||||
|
||||
from yamllint.linter import LintProblem
|
||||
|
||||
|
||||
ID = 'float-values'
|
||||
TYPE = 'token'
|
||||
CONF = {
|
||||
'require-numeral-before-decimal': bool,
|
||||
'forbid-scientific-notation': bool,
|
||||
'forbid-nan': bool,
|
||||
'forbid-inf': bool,
|
||||
}
|
||||
DEFAULT = {
|
||||
'require-numeral-before-decimal': False,
|
||||
'forbid-scientific-notation': False,
|
||||
'forbid-nan': False,
|
||||
'forbid-inf': False,
|
||||
}
|
||||
|
||||
IS_NUMERAL_BEFORE_DECIMAL_PATTERN = (
|
||||
re.compile('[-+]?(\\.[0-9]+)([eE][-+]?[0-9]+)?$')
|
||||
)
|
||||
IS_SCIENTIFIC_NOTATION_PATTERN = re.compile(
|
||||
'[-+]?(\\.[0-9]+|[0-9]+(\\.[0-9]*)?)([eE][-+]?[0-9]+)$'
|
||||
)
|
||||
IS_INF_PATTERN = re.compile('[-+]?(\\.inf|\\.Inf|\\.INF)$')
|
||||
IS_NAN_PATTERN = re.compile('(\\.nan|\\.NaN|\\.NAN)$')
|
||||
|
||||
|
||||
def check(conf, token, prev, next, nextnext, context):
|
||||
if prev and isinstance(prev, yaml.tokens.TagToken):
|
||||
return
|
||||
if not isinstance(token, yaml.tokens.ScalarToken):
|
||||
return
|
||||
if token.style:
|
||||
return
|
||||
val = token.value
|
||||
|
||||
if conf['forbid-nan'] and IS_NAN_PATTERN.match(val):
|
||||
yield LintProblem(
|
||||
token.start_mark.line + 1,
|
||||
token.start_mark.column + 1,
|
||||
f'forbidden not a number value "{token.value}"',
|
||||
)
|
||||
|
||||
if conf['forbid-inf'] and IS_INF_PATTERN.match(val):
|
||||
yield LintProblem(
|
||||
token.start_mark.line + 1,
|
||||
token.start_mark.column + 1,
|
||||
f'forbidden infinite value "{token.value}"',
|
||||
)
|
||||
|
||||
if conf[
|
||||
'forbid-scientific-notation'
|
||||
] and IS_SCIENTIFIC_NOTATION_PATTERN.match(val):
|
||||
yield LintProblem(
|
||||
token.start_mark.line + 1,
|
||||
token.start_mark.column + 1,
|
||||
f'forbidden scientific notation "{token.value}"',
|
||||
)
|
||||
|
||||
if conf[
|
||||
'require-numeral-before-decimal'
|
||||
] and IS_NUMERAL_BEFORE_DECIMAL_PATTERN.match(val):
|
||||
yield LintProblem(
|
||||
token.start_mark.line + 1,
|
||||
token.start_mark.column + 1,
|
||||
f'forbidden decimal missing 0 prefix "{token.value}"',
|
||||
)
|
Loading…
Reference in New Issue