Compare commits
No commits in common. 'master' and 'v1.11.1' have entirely different histories.
@ -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,24 @@
|
|||||||
|
---
|
||||||
|
language: python
|
||||||
|
python:
|
||||||
|
- 2.6
|
||||||
|
- 2.7
|
||||||
|
- 3.3
|
||||||
|
- 3.4
|
||||||
|
- 3.5
|
||||||
|
- 3.6
|
||||||
|
- nightly
|
||||||
|
install:
|
||||||
|
- pip install pyyaml flake8 flake8-import-order coveralls
|
||||||
|
- if [[ $TRAVIS_PYTHON_VERSION == 2.6 ]]; then pip install unittest2; fi
|
||||||
|
- if [[ $TRAVIS_PYTHON_VERSION != 2* ]]; then pip install sphinx; fi
|
||||||
|
- pip install .
|
||||||
|
script:
|
||||||
|
- if [[ $TRAVIS_PYTHON_VERSION != 2.6 ]]; then flake8 .; fi
|
||||||
|
- yamllint --strict $(git ls-files '*.yaml' '*.yml')
|
||||||
|
- coverage run --source=yamllint setup.py test
|
||||||
|
- if [[ $TRAVIS_PYTHON_VERSION != 2* ]]; then
|
||||||
|
python setup.py build_sphinx;
|
||||||
|
fi
|
||||||
|
after_success:
|
||||||
|
coveralls
|
@ -1,48 +0,0 @@
|
|||||||
Contributing
|
|
||||||
============
|
|
||||||
|
|
||||||
Pull requests are the best way to propose changes to the codebase.
|
|
||||||
Contributions are welcome, but they have to meet some criteria.
|
|
||||||
|
|
||||||
Pull Request Process
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
1. Fork this Git repository and create your branch from ``master``.
|
|
||||||
|
|
||||||
2. Make sure the tests pass:
|
|
||||||
|
|
||||||
.. code:: bash
|
|
||||||
|
|
||||||
pip install --user .
|
|
||||||
python -m unittest discover # all tests...
|
|
||||||
python -m unittest tests/rules/test_commas.py # or just some tests (faster)
|
|
||||||
|
|
||||||
3. If you add code that should be tested, add tests.
|
|
||||||
|
|
||||||
4. Make sure the linters pass:
|
|
||||||
|
|
||||||
.. code:: bash
|
|
||||||
|
|
||||||
flake8 .
|
|
||||||
|
|
||||||
If you added/modified documentation:
|
|
||||||
|
|
||||||
.. code:: bash
|
|
||||||
|
|
||||||
doc8 $(git ls-files '*.rst')
|
|
||||||
|
|
||||||
If you touched YAML files:
|
|
||||||
|
|
||||||
.. code:: bash
|
|
||||||
|
|
||||||
yamllint --strict $(git ls-files '*.yaml' '*.yml')
|
|
||||||
|
|
||||||
5. If relevant, update documentation (either in ``docs`` directly or in rules
|
|
||||||
files themselves).
|
|
||||||
|
|
||||||
6. Write a `good commit message
|
|
||||||
<http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html>`_.
|
|
||||||
If the pull request has multiple commits, each must be atomic (single
|
|
||||||
irreducible change that makes sense on its own).
|
|
||||||
|
|
||||||
7. Then, open a pull request.
|
|
@ -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,12 @@
|
|||||||
|
[bdist_wheel]
|
||||||
|
universal = 1
|
||||||
|
|
||||||
|
[flake8]
|
||||||
|
import-order-style = pep8
|
||||||
|
application-import-names = yamllint
|
||||||
|
|
||||||
|
[build_sphinx]
|
||||||
|
all-files = 1
|
||||||
|
source-dir = docs
|
||||||
|
build-dir = docs/_build
|
||||||
|
warning-is-error = 1
|
@ -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))
|
|
@ -1,558 +0,0 @@
|
|||||||
# Copyright (C) 2018 ClearScore
|
|
||||||
#
|
|
||||||
# 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
|
|
||||||
|
|
||||||
from yamllint import config
|
|
||||||
|
|
||||||
|
|
||||||
class QuotedTestCase(RuleTestCase):
|
|
||||||
rule_id = 'quoted-strings'
|
|
||||||
|
|
||||||
def test_disabled(self):
|
|
||||||
conf = 'quoted-strings: disable'
|
|
||||||
|
|
||||||
self.check('---\n'
|
|
||||||
'foo: bar\n', conf)
|
|
||||||
self.check('---\n'
|
|
||||||
'foo: "bar"\n', conf)
|
|
||||||
self.check('---\n'
|
|
||||||
'foo: \'bar\'\n', conf)
|
|
||||||
self.check('---\n'
|
|
||||||
'bar: 123\n', conf)
|
|
||||||
self.check('---\n'
|
|
||||||
'bar: "123"\n', conf)
|
|
||||||
|
|
||||||
def test_quote_type_any(self):
|
|
||||||
conf = 'quoted-strings: {quote-type: any}\n'
|
|
||||||
|
|
||||||
self.check('---\n'
|
|
||||||
'boolean1: true\n'
|
|
||||||
'number1: 123\n'
|
|
||||||
'string1: foo\n' # fails
|
|
||||||
'string2: "foo"\n'
|
|
||||||
'string3: "true"\n'
|
|
||||||
'string4: "123"\n'
|
|
||||||
'string5: \'bar\'\n'
|
|
||||||
'string6: !!str genericstring\n'
|
|
||||||
'string7: !!str 456\n'
|
|
||||||
'string8: !!str "quotedgenericstring"\n'
|
|
||||||
'binary: !!binary binstring\n'
|
|
||||||
'integer: !!int intstring\n'
|
|
||||||
'boolean2: !!bool boolstring\n'
|
|
||||||
'boolean3: !!bool "quotedboolstring"\n'
|
|
||||||
'block-seq:\n'
|
|
||||||
' - foo\n' # fails
|
|
||||||
' - "foo"\n'
|
|
||||||
'flow-seq: [foo, "foo"]\n' # fails
|
|
||||||
'flow-map: {a: foo, b: "foo"}\n', # fails
|
|
||||||
conf, problem1=(4, 10), problem2=(17, 5),
|
|
||||||
problem3=(19, 12), problem4=(20, 15))
|
|
||||||
self.check('---\n'
|
|
||||||
'multiline string 1: |\n'
|
|
||||||
' line 1\n'
|
|
||||||
' line 2\n'
|
|
||||||
'multiline string 2: >\n'
|
|
||||||
' word 1\n'
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 3:\n'
|
|
||||||
' word 1\n' # fails
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 4:\n'
|
|
||||||
' "word 1\\\n'
|
|
||||||
' word 2"\n',
|
|
||||||
conf, problem1=(9, 3))
|
|
||||||
|
|
||||||
def test_quote_type_single(self):
|
|
||||||
conf = 'quoted-strings: {quote-type: single}\n'
|
|
||||||
|
|
||||||
self.check('---\n'
|
|
||||||
'boolean1: true\n'
|
|
||||||
'number1: 123\n'
|
|
||||||
'string1: foo\n' # fails
|
|
||||||
'string2: "foo"\n' # fails
|
|
||||||
'string3: "true"\n' # fails
|
|
||||||
'string4: "123"\n' # fails
|
|
||||||
'string5: \'bar\'\n'
|
|
||||||
'string6: !!str genericstring\n'
|
|
||||||
'string7: !!str 456\n'
|
|
||||||
'string8: !!str "quotedgenericstring"\n'
|
|
||||||
'binary: !!binary binstring\n'
|
|
||||||
'integer: !!int intstring\n'
|
|
||||||
'boolean2: !!bool boolstring\n'
|
|
||||||
'boolean3: !!bool "quotedboolstring"\n'
|
|
||||||
'block-seq:\n'
|
|
||||||
' - foo\n' # fails
|
|
||||||
' - "foo"\n' # fails
|
|
||||||
'flow-seq: [foo, "foo"]\n' # fails
|
|
||||||
'flow-map: {a: foo, b: "foo"}\n', # fails
|
|
||||||
conf, problem1=(4, 10), problem2=(5, 10), problem3=(6, 10),
|
|
||||||
problem4=(7, 10), problem5=(17, 5), problem6=(18, 5),
|
|
||||||
problem7=(19, 12), problem8=(19, 17), problem9=(20, 15),
|
|
||||||
problem10=(20, 23))
|
|
||||||
self.check('---\n'
|
|
||||||
'multiline string 1: |\n'
|
|
||||||
' line 1\n'
|
|
||||||
' line 2\n'
|
|
||||||
'multiline string 2: >\n'
|
|
||||||
' word 1\n'
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 3:\n'
|
|
||||||
' word 1\n' # fails
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 4:\n'
|
|
||||||
' "word 1\\\n'
|
|
||||||
' word 2"\n',
|
|
||||||
conf, problem1=(9, 3), problem2=(12, 3))
|
|
||||||
|
|
||||||
def test_quote_type_double(self):
|
|
||||||
conf = 'quoted-strings: {quote-type: double}\n'
|
|
||||||
|
|
||||||
self.check('---\n'
|
|
||||||
'boolean1: true\n'
|
|
||||||
'number1: 123\n'
|
|
||||||
'string1: foo\n' # fails
|
|
||||||
'string2: "foo"\n'
|
|
||||||
'string3: "true"\n'
|
|
||||||
'string4: "123"\n'
|
|
||||||
'string5: \'bar\'\n' # fails
|
|
||||||
'string6: !!str genericstring\n'
|
|
||||||
'string7: !!str 456\n'
|
|
||||||
'string8: !!str "quotedgenericstring"\n'
|
|
||||||
'binary: !!binary binstring\n'
|
|
||||||
'integer: !!int intstring\n'
|
|
||||||
'boolean2: !!bool boolstring\n'
|
|
||||||
'boolean3: !!bool "quotedboolstring"\n'
|
|
||||||
'block-seq:\n'
|
|
||||||
' - foo\n' # fails
|
|
||||||
' - "foo"\n'
|
|
||||||
'flow-seq: [foo, "foo"]\n' # fails
|
|
||||||
'flow-map: {a: foo, b: "foo"}\n', # fails
|
|
||||||
conf, problem1=(4, 10), problem2=(8, 10), problem3=(17, 5),
|
|
||||||
problem4=(19, 12), problem5=(20, 15))
|
|
||||||
self.check('---\n'
|
|
||||||
'multiline string 1: |\n'
|
|
||||||
' line 1\n'
|
|
||||||
' line 2\n'
|
|
||||||
'multiline string 2: >\n'
|
|
||||||
' word 1\n'
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 3:\n'
|
|
||||||
' word 1\n' # fails
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 4:\n'
|
|
||||||
' "word 1\\\n'
|
|
||||||
' word 2"\n',
|
|
||||||
conf, problem1=(9, 3))
|
|
||||||
|
|
||||||
def test_any_quotes_not_required(self):
|
|
||||||
conf = 'quoted-strings: {quote-type: any, required: false}\n'
|
|
||||||
|
|
||||||
self.check('---\n'
|
|
||||||
'boolean1: true\n'
|
|
||||||
'number1: 123\n'
|
|
||||||
'string1: foo\n'
|
|
||||||
'string2: "foo"\n'
|
|
||||||
'string3: "true"\n'
|
|
||||||
'string4: "123"\n'
|
|
||||||
'string5: \'bar\'\n'
|
|
||||||
'string6: !!str genericstring\n'
|
|
||||||
'string7: !!str 456\n'
|
|
||||||
'string8: !!str "quotedgenericstring"\n'
|
|
||||||
'binary: !!binary binstring\n'
|
|
||||||
'integer: !!int intstring\n'
|
|
||||||
'boolean2: !!bool boolstring\n'
|
|
||||||
'boolean3: !!bool "quotedboolstring"\n'
|
|
||||||
'block-seq:\n'
|
|
||||||
' - foo\n' # fails
|
|
||||||
' - "foo"\n'
|
|
||||||
'flow-seq: [foo, "foo"]\n' # fails
|
|
||||||
'flow-map: {a: foo, b: "foo"}\n', # fails
|
|
||||||
conf)
|
|
||||||
self.check('---\n'
|
|
||||||
'multiline string 1: |\n'
|
|
||||||
' line 1\n'
|
|
||||||
' line 2\n'
|
|
||||||
'multiline string 2: >\n'
|
|
||||||
' word 1\n'
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 3:\n'
|
|
||||||
' word 1\n'
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 4:\n'
|
|
||||||
' "word 1\\\n'
|
|
||||||
' word 2"\n',
|
|
||||||
conf)
|
|
||||||
|
|
||||||
def test_single_quotes_not_required(self):
|
|
||||||
conf = 'quoted-strings: {quote-type: single, required: false}\n'
|
|
||||||
|
|
||||||
self.check('---\n'
|
|
||||||
'boolean1: true\n'
|
|
||||||
'number1: 123\n'
|
|
||||||
'string1: foo\n'
|
|
||||||
'string2: "foo"\n' # fails
|
|
||||||
'string3: "true"\n' # fails
|
|
||||||
'string4: "123"\n' # fails
|
|
||||||
'string5: \'bar\'\n'
|
|
||||||
'string6: !!str genericstring\n'
|
|
||||||
'string7: !!str 456\n'
|
|
||||||
'string8: !!str "quotedgenericstring"\n'
|
|
||||||
'binary: !!binary binstring\n'
|
|
||||||
'integer: !!int intstring\n'
|
|
||||||
'boolean2: !!bool boolstring\n'
|
|
||||||
'boolean3: !!bool "quotedboolstring"\n'
|
|
||||||
'block-seq:\n'
|
|
||||||
' - foo\n' # fails
|
|
||||||
' - "foo"\n'
|
|
||||||
'flow-seq: [foo, "foo"]\n' # fails
|
|
||||||
'flow-map: {a: foo, b: "foo"}\n', # fails
|
|
||||||
conf, problem1=(5, 10), problem2=(6, 10), problem3=(7, 10),
|
|
||||||
problem4=(18, 5), problem5=(19, 17), problem6=(20, 23))
|
|
||||||
self.check('---\n'
|
|
||||||
'multiline string 1: |\n'
|
|
||||||
' line 1\n'
|
|
||||||
' line 2\n'
|
|
||||||
'multiline string 2: >\n'
|
|
||||||
' word 1\n'
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 3:\n'
|
|
||||||
' word 1\n'
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 4:\n'
|
|
||||||
' "word 1\\\n' # fails
|
|
||||||
' word 2"\n',
|
|
||||||
conf, problem1=(12, 3))
|
|
||||||
|
|
||||||
def test_only_when_needed(self):
|
|
||||||
conf = 'quoted-strings: {required: only-when-needed}\n'
|
|
||||||
|
|
||||||
self.check('---\n'
|
|
||||||
'boolean1: true\n'
|
|
||||||
'number1: 123\n'
|
|
||||||
'string1: foo\n'
|
|
||||||
'string2: "foo"\n' # fails
|
|
||||||
'string3: "true"\n'
|
|
||||||
'string4: "123"\n'
|
|
||||||
'string5: \'bar\'\n' # fails
|
|
||||||
'string6: !!str genericstring\n'
|
|
||||||
'string7: !!str 456\n'
|
|
||||||
'string8: !!str "quotedgenericstring"\n'
|
|
||||||
'binary: !!binary binstring\n'
|
|
||||||
'integer: !!int intstring\n'
|
|
||||||
'boolean2: !!bool boolstring\n'
|
|
||||||
'boolean3: !!bool "quotedboolstring"\n'
|
|
||||||
'block-seq:\n'
|
|
||||||
' - foo\n'
|
|
||||||
' - "foo"\n' # fails
|
|
||||||
'flow-seq: [foo, "foo"]\n' # fails
|
|
||||||
'flow-map: {a: foo, b: "foo"}\n', # fails
|
|
||||||
conf, problem1=(5, 10), problem2=(8, 10), problem3=(18, 5),
|
|
||||||
problem4=(19, 17), problem5=(20, 23))
|
|
||||||
self.check('---\n'
|
|
||||||
'multiline string 1: |\n'
|
|
||||||
' line 1\n'
|
|
||||||
' line 2\n'
|
|
||||||
'multiline string 2: >\n'
|
|
||||||
' word 1\n'
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 3:\n'
|
|
||||||
' word 1\n'
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 4:\n'
|
|
||||||
' "word 1\\\n' # fails
|
|
||||||
' word 2"\n',
|
|
||||||
conf, problem1=(12, 3))
|
|
||||||
|
|
||||||
def test_only_when_needed_single_quotes(self):
|
|
||||||
conf = ('quoted-strings: {quote-type: single,\n'
|
|
||||||
' required: only-when-needed}\n')
|
|
||||||
|
|
||||||
self.check('---\n'
|
|
||||||
'boolean1: true\n'
|
|
||||||
'number1: 123\n'
|
|
||||||
'string1: foo\n'
|
|
||||||
'string2: "foo"\n' # fails
|
|
||||||
'string3: "true"\n' # fails
|
|
||||||
'string4: "123"\n' # fails
|
|
||||||
'string5: \'bar\'\n' # fails
|
|
||||||
'string6: !!str genericstring\n'
|
|
||||||
'string7: !!str 456\n'
|
|
||||||
'string8: !!str "quotedgenericstring"\n'
|
|
||||||
'binary: !!binary binstring\n'
|
|
||||||
'integer: !!int intstring\n'
|
|
||||||
'boolean2: !!bool boolstring\n'
|
|
||||||
'boolean3: !!bool "quotedboolstring"\n'
|
|
||||||
'block-seq:\n'
|
|
||||||
' - foo\n'
|
|
||||||
' - "foo"\n' # fails
|
|
||||||
'flow-seq: [foo, "foo"]\n' # fails
|
|
||||||
'flow-map: {a: foo, b: "foo"}\n', # fails
|
|
||||||
conf, problem1=(5, 10), problem2=(6, 10), problem3=(7, 10),
|
|
||||||
problem4=(8, 10), problem5=(18, 5), problem6=(19, 17),
|
|
||||||
problem7=(20, 23))
|
|
||||||
self.check('---\n'
|
|
||||||
'multiline string 1: |\n'
|
|
||||||
' line 1\n'
|
|
||||||
' line 2\n'
|
|
||||||
'multiline string 2: >\n'
|
|
||||||
' word 1\n'
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 3:\n'
|
|
||||||
' word 1\n'
|
|
||||||
' word 2\n'
|
|
||||||
'multiline string 4:\n'
|
|
||||||
' "word 1\\\n' # fails
|
|
||||||
' word 2"\n',
|
|
||||||
conf, problem1=(12, 3))
|
|
||||||
|
|
||||||
def test_only_when_needed_corner_cases(self):
|
|
||||||
conf = 'quoted-strings: {required: only-when-needed}\n'
|
|
||||||
|
|
||||||
self.check('---\n'
|
|
||||||
'- ""\n'
|
|
||||||
'- "- item"\n'
|
|
||||||
'- "key: value"\n'
|
|
||||||
'- "%H:%M:%S"\n'
|
|
||||||
'- "%wheel ALL=(ALL) NOPASSWD: ALL"\n'
|
|
||||||
'- \'"quoted"\'\n'
|
|
||||||
'- "\'foo\' == \'bar\'"\n'
|
|
||||||
'- "\'Mac\' in ansible_facts.product_name"\n'
|
|
||||||
'- \'foo # bar\'\n',
|
|
||||||
conf)
|
|
||||||
self.check('---\n'
|
|
||||||
'k1: ""\n'
|
|
||||||
'k2: "- item"\n'
|
|
||||||
'k3: "key: value"\n'
|
|
||||||
'k4: "%H:%M:%S"\n'
|
|
||||||
'k5: "%wheel ALL=(ALL) NOPASSWD: ALL"\n'
|
|
||||||
'k6: \'"quoted"\'\n'
|
|
||||||
'k7: "\'foo\' == \'bar\'"\n'
|
|
||||||
'k8: "\'Mac\' in ansible_facts.product_name"\n',
|
|
||||||
conf)
|
|
||||||
|
|
||||||
self.check('---\n'
|
|
||||||
'- ---\n'
|
|
||||||
'- "---"\n' # fails
|
|
||||||
'- ----------\n'
|
|
||||||
'- "----------"\n' # fails
|
|
||||||
'- :wq\n'
|
|
||||||
'- ":wq"\n', # fails
|
|
||||||
conf, problem1=(3, 3), problem2=(5, 3), problem3=(7, 3))
|
|
||||||
self.check('---\n'
|
|
||||||
'k1: ---\n'
|
|
||||||
'k2: "---"\n' # fails
|
|
||||||
'k3: ----------\n'
|
|
||||||
'k4: "----------"\n' # fails
|
|
||||||
'k5: :wq\n'
|
|
||||||
'k6: ":wq"\n', # fails
|
|
||||||
conf, problem1=(3, 5), problem2=(5, 5), problem3=(7, 5))
|
|
||||||
|
|
||||||
def test_only_when_needed_extras(self):
|
|
||||||
conf = ('quoted-strings:\n'
|
|
||||||
' required: true\n'
|
|
||||||
' extra-allowed: [^http://]\n')
|
|
||||||
self.assertRaises(config.YamlLintConfigError, self.check, '', conf)
|
|
||||||
|
|
||||||
conf = ('quoted-strings:\n'
|
|
||||||
' required: true\n'
|
|
||||||
' extra-required: [^http://]\n')
|
|
||||||
self.assertRaises(config.YamlLintConfigError, self.check, '', conf)
|
|
||||||
|
|
||||||
conf = ('quoted-strings:\n'
|
|
||||||
' required: false\n'
|
|
||||||
' extra-allowed: [^http://]\n')
|
|
||||||
self.assertRaises(config.YamlLintConfigError, self.check, '', conf)
|
|
||||||
|
|
||||||
conf = ('quoted-strings:\n'
|
|
||||||
' required: true\n')
|
|
||||||
self.check('---\n'
|
|
||||||
'- 123\n'
|
|
||||||
'- "123"\n'
|
|
||||||
'- localhost\n' # fails
|
|
||||||
'- "localhost"\n'
|
|
||||||
'- http://localhost\n' # fails
|
|
||||||
'- "http://localhost"\n'
|
|
||||||
'- ftp://localhost\n' # fails
|
|
||||||
'- "ftp://localhost"\n',
|
|
||||||
conf, problem1=(4, 3), problem2=(6, 3), problem3=(8, 3))
|
|
||||||
|
|
||||||
conf = ('quoted-strings:\n'
|
|
||||||
' required: only-when-needed\n'
|
|
||||||
' extra-allowed: [^ftp://]\n'
|
|
||||||
' extra-required: [^http://]\n')
|
|
||||||
self.check('---\n'
|
|
||||||
'- 123\n'
|
|
||||||
'- "123"\n'
|
|
||||||
'- localhost\n'
|
|
||||||
'- "localhost"\n' # fails
|
|
||||||
'- http://localhost\n' # fails
|
|
||||||
'- "http://localhost"\n'
|
|
||||||
'- ftp://localhost\n'
|
|
||||||
'- "ftp://localhost"\n',
|
|
||||||
conf, problem1=(5, 3), problem2=(6, 3))
|
|
||||||
|
|
||||||
conf = ('quoted-strings:\n'
|
|
||||||
' required: false\n'
|
|
||||||
' extra-required: [^http://, ^ftp://]\n')
|
|
||||||
self.check('---\n'
|
|
||||||
'- 123\n'
|
|
||||||
'- "123"\n'
|
|
||||||
'- localhost\n'
|
|
||||||
'- "localhost"\n'
|
|
||||||
'- http://localhost\n' # fails
|
|
||||||
'- "http://localhost"\n'
|
|
||||||
'- ftp://localhost\n' # fails
|
|
||||||
'- "ftp://localhost"\n',
|
|
||||||
conf, problem1=(6, 3), problem2=(8, 3))
|
|
||||||
|
|
||||||
conf = ('quoted-strings:\n'
|
|
||||||
' required: only-when-needed\n'
|
|
||||||
' extra-allowed: [^ftp://, ";$", " "]\n')
|
|
||||||
self.check('---\n'
|
|
||||||
'- localhost\n'
|
|
||||||
'- "localhost"\n' # fails
|
|
||||||
'- ftp://localhost\n'
|
|
||||||
'- "ftp://localhost"\n'
|
|
||||||
'- i=i+1\n'
|
|
||||||
'- "i=i+1"\n' # fails
|
|
||||||
'- i=i+2;\n'
|
|
||||||
'- "i=i+2;"\n'
|
|
||||||
'- foo\n'
|
|
||||||
'- "foo"\n' # fails
|
|
||||||
'- foo bar\n'
|
|
||||||
'- "foo bar"\n',
|
|
||||||
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))
|
|
||||||
|
|
||||||
def test_allow_quoted_quotes(self):
|
|
||||||
conf = ('quoted-strings: {quote-type: single,\n'
|
|
||||||
' required: false,\n'
|
|
||||||
' allow-quoted-quotes: false}\n')
|
|
||||||
self.check('---\n'
|
|
||||||
'foo1: "[barbaz]"\n' # fails
|
|
||||||
'foo2: "[bar\'baz]"\n', # fails
|
|
||||||
conf, problem1=(2, 7), problem2=(3, 7))
|
|
||||||
|
|
||||||
conf = ('quoted-strings: {quote-type: single,\n'
|
|
||||||
' required: false,\n'
|
|
||||||
' allow-quoted-quotes: true}\n')
|
|
||||||
self.check('---\n'
|
|
||||||
'foo1: "[barbaz]"\n' # fails
|
|
||||||
'foo2: "[bar\'baz]"\n',
|
|
||||||
conf, problem1=(2, 7))
|
|
||||||
|
|
||||||
conf = ('quoted-strings: {quote-type: single,\n'
|
|
||||||
' required: true,\n'
|
|
||||||
' allow-quoted-quotes: false}\n')
|
|
||||||
self.check('---\n'
|
|
||||||
'foo1: "[barbaz]"\n' # fails
|
|
||||||
'foo2: "[bar\'baz]"\n', # fails
|
|
||||||
conf, problem1=(2, 7), problem2=(3, 7))
|
|
||||||
|
|
||||||
conf = ('quoted-strings: {quote-type: single,\n'
|
|
||||||
' required: true,\n'
|
|
||||||
' allow-quoted-quotes: true}\n')
|
|
||||||
self.check('---\n'
|
|
||||||
'foo1: "[barbaz]"\n' # fails
|
|
||||||
'foo2: "[bar\'baz]"\n',
|
|
||||||
conf, problem1=(2, 7))
|
|
||||||
|
|
||||||
conf = ('quoted-strings: {quote-type: single,\n'
|
|
||||||
' required: only-when-needed,\n'
|
|
||||||
' allow-quoted-quotes: false}\n')
|
|
||||||
self.check('---\n'
|
|
||||||
'foo1: "[barbaz]"\n' # fails
|
|
||||||
'foo2: "[bar\'baz]"\n', # fails
|
|
||||||
conf, problem1=(2, 7), problem2=(3, 7))
|
|
||||||
|
|
||||||
conf = ('quoted-strings: {quote-type: single,\n'
|
|
||||||
' required: only-when-needed,\n'
|
|
||||||
' allow-quoted-quotes: true}\n')
|
|
||||||
self.check('---\n'
|
|
||||||
'foo1: "[barbaz]"\n' # fails
|
|
||||||
'foo2: "[bar\'baz]"\n',
|
|
||||||
conf, problem1=(2, 7))
|
|
||||||
|
|
||||||
conf = ('quoted-strings: {quote-type: double,\n'
|
|
||||||
' required: false,\n'
|
|
||||||
' allow-quoted-quotes: false}\n')
|
|
||||||
self.check("---\n"
|
|
||||||
"foo1: '[barbaz]'\n" # fails
|
|
||||||
"foo2: '[bar\"baz]'\n", # fails
|
|
||||||
conf, problem1=(2, 7), problem2=(3, 7))
|
|
||||||
|
|
||||||
conf = ('quoted-strings: {quote-type: double,\n'
|
|
||||||
' required: false,\n'
|
|
||||||
' allow-quoted-quotes: true}\n')
|
|
||||||
self.check("---\n"
|
|
||||||
"foo1: '[barbaz]'\n" # fails
|
|
||||||
"foo2: '[bar\"baz]'\n",
|
|
||||||
conf, problem1=(2, 7))
|
|
||||||
|
|
||||||
conf = ('quoted-strings: {quote-type: double,\n'
|
|
||||||
' required: true,\n'
|
|
||||||
' allow-quoted-quotes: false}\n')
|
|
||||||
self.check("---\n"
|
|
||||||
"foo1: '[barbaz]'\n" # fails
|
|
||||||
"foo2: '[bar\"baz]'\n", # fails
|
|
||||||
conf, problem1=(2, 7), problem2=(3, 7))
|
|
||||||
|
|
||||||
conf = ('quoted-strings: {quote-type: double,\n'
|
|
||||||
' required: true,\n'
|
|
||||||
' allow-quoted-quotes: true}\n')
|
|
||||||
self.check("---\n"
|
|
||||||
"foo1: '[barbaz]'\n" # fails
|
|
||||||
"foo2: '[bar\"baz]'\n",
|
|
||||||
conf, problem1=(2, 7))
|
|
||||||
|
|
||||||
conf = ('quoted-strings: {quote-type: double,\n'
|
|
||||||
' required: only-when-needed,\n'
|
|
||||||
' allow-quoted-quotes: false}\n')
|
|
||||||
self.check("---\n"
|
|
||||||
"foo1: '[barbaz]'\n" # fails
|
|
||||||
"foo2: '[bar\"baz]'\n", # fails
|
|
||||||
conf, problem1=(2, 7), problem2=(3, 7))
|
|
||||||
|
|
||||||
conf = ('quoted-strings: {quote-type: double,\n'
|
|
||||||
' required: only-when-needed,\n'
|
|
||||||
' allow-quoted-quotes: true}\n')
|
|
||||||
self.check("---\n"
|
|
||||||
"foo1: '[barbaz]'\n" # fails
|
|
||||||
"foo2: '[bar\"baz]'\n",
|
|
||||||
conf, problem1=(2, 7))
|
|
||||||
|
|
||||||
conf = ('quoted-strings: {quote-type: any}\n')
|
|
||||||
self.check("---\n"
|
|
||||||
"foo1: '[barbaz]'\n"
|
|
||||||
"foo2: '[bar\"baz]'\n",
|
|
||||||
conf)
|
|
@ -1,35 +1,58 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
yaml-files:
|
|
||||||
- '*.yaml'
|
|
||||||
- '*.yml'
|
|
||||||
- '.yamllint'
|
|
||||||
|
|
||||||
rules:
|
rules:
|
||||||
anchors: enable
|
braces:
|
||||||
braces: enable
|
min-spaces-inside: 0
|
||||||
brackets: enable
|
max-spaces-inside: 0
|
||||||
colons: enable
|
min-spaces-inside-empty: -1
|
||||||
commas: enable
|
max-spaces-inside-empty: -1
|
||||||
|
brackets:
|
||||||
|
min-spaces-inside: 0
|
||||||
|
max-spaces-inside: 0
|
||||||
|
min-spaces-inside-empty: -1
|
||||||
|
max-spaces-inside-empty: -1
|
||||||
|
colons:
|
||||||
|
max-spaces-before: 0
|
||||||
|
max-spaces-after: 1
|
||||||
|
commas:
|
||||||
|
max-spaces-before: 0
|
||||||
|
min-spaces-after: 1
|
||||||
|
max-spaces-after: 1
|
||||||
comments:
|
comments:
|
||||||
level: warning
|
level: warning
|
||||||
|
require-starting-space: true
|
||||||
|
min-spaces-from-content: 2
|
||||||
comments-indentation:
|
comments-indentation:
|
||||||
level: warning
|
level: warning
|
||||||
document-end: disable
|
document-end: disable
|
||||||
document-start:
|
document-start:
|
||||||
level: warning
|
level: warning
|
||||||
empty-lines: enable
|
present: true
|
||||||
empty-values: disable
|
empty-lines:
|
||||||
float-values: disable
|
max: 2
|
||||||
hyphens: enable
|
max-start: 0
|
||||||
indentation: enable
|
max-end: 0
|
||||||
|
empty-values:
|
||||||
|
forbid-in-block-mappings: false
|
||||||
|
forbid-in-flow-mappings: false
|
||||||
|
hyphens:
|
||||||
|
max-spaces-after: 1
|
||||||
|
indentation:
|
||||||
|
spaces: consistent
|
||||||
|
indent-sequences: true
|
||||||
|
check-multi-line-strings: false
|
||||||
key-duplicates: enable
|
key-duplicates: enable
|
||||||
key-ordering: disable
|
key-ordering: disable
|
||||||
line-length: enable
|
line-length:
|
||||||
|
max: 80
|
||||||
|
allow-non-breakable-words: true
|
||||||
|
allow-non-breakable-inline-mappings: false
|
||||||
new-line-at-end-of-file: enable
|
new-line-at-end-of-file: enable
|
||||||
new-lines: enable
|
new-lines:
|
||||||
octal-values: disable
|
type: unix
|
||||||
quoted-strings: disable
|
octal-values:
|
||||||
|
forbid-implicit-octal: false
|
||||||
|
forbid-explicit-octal: false
|
||||||
trailing-spaces: enable
|
trailing-spaces: enable
|
||||||
truthy:
|
truthy:
|
||||||
level: warning
|
level: warning
|
||||||
|
@ -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}"',
|
|
||||||
)
|
|
@ -1,289 +0,0 @@
|
|||||||
# Copyright (C) 2018 ClearScore
|
|
||||||
#
|
|
||||||
# This program is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
"""
|
|
||||||
Use this rule to forbid any string values that are not quoted, or to prevent
|
|
||||||
quoted strings without needing it. You can also enforce the type of the quote
|
|
||||||
used.
|
|
||||||
|
|
||||||
.. rubric:: Options
|
|
||||||
|
|
||||||
* ``quote-type`` defines allowed quotes: ``single``, ``double`` or ``any``
|
|
||||||
(default).
|
|
||||||
* ``required`` defines whether using quotes in string values is required
|
|
||||||
(``true``, default) or not (``false``), or only allowed when really needed
|
|
||||||
(``only-when-needed``).
|
|
||||||
* ``extra-required`` is a list of PCRE regexes to force string values to be
|
|
||||||
quoted, if they match any regex. This option can only be used with
|
|
||||||
``required: false`` and ``required: only-when-needed``.
|
|
||||||
* ``extra-allowed`` is a list of PCRE regexes to allow quoted string values,
|
|
||||||
even if ``required: only-when-needed`` is set.
|
|
||||||
* ``allow-quoted-quotes`` allows (``true``) using disallowed quotes for strings
|
|
||||||
with allowed quotes inside. Default ``false``.
|
|
||||||
|
|
||||||
**Note**: Multi-line strings (with ``|`` or ``>``) will not be checked.
|
|
||||||
|
|
||||||
.. rubric:: Default values (when enabled)
|
|
||||||
|
|
||||||
.. code-block:: yaml
|
|
||||||
|
|
||||||
rules:
|
|
||||||
quoted-strings:
|
|
||||||
quote-type: any
|
|
||||||
required: true
|
|
||||||
extra-required: []
|
|
||||||
extra-allowed: []
|
|
||||||
allow-quoted-quotes: false
|
|
||||||
|
|
||||||
.. rubric:: Examples
|
|
||||||
|
|
||||||
#. With ``quoted-strings: {quote-type: any, required: true}``
|
|
||||||
|
|
||||||
the following code snippet would **PASS**:
|
|
||||||
::
|
|
||||||
|
|
||||||
foo: "bar"
|
|
||||||
bar: 'foo'
|
|
||||||
number: 123
|
|
||||||
boolean: true
|
|
||||||
|
|
||||||
the following code snippet would **FAIL**:
|
|
||||||
::
|
|
||||||
|
|
||||||
foo: bar
|
|
||||||
|
|
||||||
#. With ``quoted-strings: {quote-type: single, required: only-when-needed}``
|
|
||||||
|
|
||||||
the following code snippet would **PASS**:
|
|
||||||
::
|
|
||||||
|
|
||||||
foo: bar
|
|
||||||
bar: foo
|
|
||||||
not_number: '123'
|
|
||||||
not_boolean: 'true'
|
|
||||||
not_comment: '# comment'
|
|
||||||
not_list: '[1, 2, 3]'
|
|
||||||
not_map: '{a: 1, b: 2}'
|
|
||||||
|
|
||||||
the following code snippet would **FAIL**:
|
|
||||||
::
|
|
||||||
|
|
||||||
foo: 'bar'
|
|
||||||
|
|
||||||
#. With ``quoted-strings: {required: false, extra-required: [^http://,
|
|
||||||
^ftp://]}``
|
|
||||||
|
|
||||||
the following code snippet would **PASS**:
|
|
||||||
::
|
|
||||||
|
|
||||||
- localhost
|
|
||||||
- "localhost"
|
|
||||||
- "http://localhost"
|
|
||||||
- "ftp://localhost"
|
|
||||||
|
|
||||||
the following code snippet would **FAIL**:
|
|
||||||
::
|
|
||||||
|
|
||||||
- http://localhost
|
|
||||||
- ftp://localhost
|
|
||||||
|
|
||||||
#. With ``quoted-strings: {required: only-when-needed, extra-allowed:
|
|
||||||
[^http://, ^ftp://], extra-required: [QUOTED]}``
|
|
||||||
|
|
||||||
the following code snippet would **PASS**:
|
|
||||||
::
|
|
||||||
|
|
||||||
- localhost
|
|
||||||
- "http://localhost"
|
|
||||||
- "ftp://localhost"
|
|
||||||
- "this is a string that needs to be QUOTED"
|
|
||||||
|
|
||||||
the following code snippet would **FAIL**:
|
|
||||||
::
|
|
||||||
|
|
||||||
- "localhost"
|
|
||||||
- this is a string that needs to be QUOTED
|
|
||||||
|
|
||||||
#. With ``quoted-strings: {quote-type: double, allow-quoted-quotes: false}``
|
|
||||||
|
|
||||||
the following code snippet would **PASS**:
|
|
||||||
::
|
|
||||||
|
|
||||||
foo: "bar\\"baz"
|
|
||||||
|
|
||||||
the following code snippet would **FAIL**:
|
|
||||||
::
|
|
||||||
|
|
||||||
foo: 'bar"baz'
|
|
||||||
|
|
||||||
#. With ``quoted-strings: {quote-type: double, allow-quoted-quotes: true}``
|
|
||||||
|
|
||||||
the following code snippet would **PASS**:
|
|
||||||
::
|
|
||||||
|
|
||||||
foo: 'bar"baz'
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
import re
|
|
||||||
|
|
||||||
import yaml
|
|
||||||
|
|
||||||
from yamllint.linter import LintProblem
|
|
||||||
|
|
||||||
ID = 'quoted-strings'
|
|
||||||
TYPE = 'token'
|
|
||||||
CONF = {'quote-type': ('any', 'single', 'double'),
|
|
||||||
'required': (True, False, 'only-when-needed'),
|
|
||||||
'extra-required': [str],
|
|
||||||
'extra-allowed': [str],
|
|
||||||
'allow-quoted-quotes': bool}
|
|
||||||
DEFAULT = {'quote-type': 'any',
|
|
||||||
'required': True,
|
|
||||||
'extra-required': [],
|
|
||||||
'extra-allowed': [],
|
|
||||||
'allow-quoted-quotes': False}
|
|
||||||
|
|
||||||
|
|
||||||
def VALIDATE(conf):
|
|
||||||
if conf['required'] is True and len(conf['extra-allowed']) > 0:
|
|
||||||
return 'cannot use both "required: true" and "extra-allowed"'
|
|
||||||
if conf['required'] is True and len(conf['extra-required']) > 0:
|
|
||||||
return 'cannot use both "required: true" and "extra-required"'
|
|
||||||
if conf['required'] is False and len(conf['extra-allowed']) > 0:
|
|
||||||
return 'cannot use both "required: false" and "extra-allowed"'
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_SCALAR_TAG = '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):
|
|
||||||
return ((quote_type == 'any') or
|
|
||||||
(quote_type == 'single' and token_style == "'") or
|
|
||||||
(quote_type == 'double' and token_style == '"'))
|
|
||||||
|
|
||||||
|
|
||||||
def _quotes_are_needed(string):
|
|
||||||
loader = yaml.BaseLoader('key: ' + string)
|
|
||||||
# Remove the 5 first tokens corresponding to 'key: ' (StreamStartToken,
|
|
||||||
# BlockMappingStartToken, KeyToken, ScalarToken(value=key), ValueToken)
|
|
||||||
for _ in range(5):
|
|
||||||
loader.get_token()
|
|
||||||
try:
|
|
||||||
a, b = loader.get_token(), loader.get_token()
|
|
||||||
if (isinstance(a, yaml.ScalarToken) and a.style is None and
|
|
||||||
isinstance(b, yaml.BlockEndToken) and a.value == string):
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
except yaml.scanner.ScannerError:
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def _has_quoted_quotes(token):
|
|
||||||
return ((not token.plain) and
|
|
||||||
((token.style == "'" and '"' in token.value) or
|
|
||||||
(token.style == '"' and "'" in token.value)))
|
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
|
||||||
if not (isinstance(token, yaml.tokens.ScalarToken) and
|
|
||||||
isinstance(prev, (yaml.BlockEntryToken, yaml.FlowEntryToken,
|
|
||||||
yaml.FlowSequenceStartToken, yaml.TagToken,
|
|
||||||
yaml.ValueToken))):
|
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
# Ignore explicit types, e.g. !!str testtest or !!int 42
|
|
||||||
if (prev and isinstance(prev, yaml.tokens.TagToken) and
|
|
||||||
prev.value[0] == '!!'):
|
|
||||||
return
|
|
||||||
|
|
||||||
# Ignore numbers, booleans, etc.
|
|
||||||
resolver = yaml.resolver.Resolver()
|
|
||||||
tag = resolver.resolve(yaml.nodes.ScalarNode, token.value, (True, False))
|
|
||||||
if token.plain and tag != DEFAULT_SCALAR_TAG:
|
|
||||||
return
|
|
||||||
|
|
||||||
# Ignore multi-line strings
|
|
||||||
if not token.plain and token.style in ("|", ">"):
|
|
||||||
return
|
|
||||||
|
|
||||||
quote_type = conf['quote-type']
|
|
||||||
|
|
||||||
msg = None
|
|
||||||
if conf['required'] is True:
|
|
||||||
|
|
||||||
# Quotes are mandatory and need to match config
|
|
||||||
if (token.style is None or
|
|
||||||
not (_quote_match(quote_type, token.style) or
|
|
||||||
(conf['allow-quoted-quotes'] and _has_quoted_quotes(token)))):
|
|
||||||
msg = "string value is not quoted with %s quotes" % quote_type
|
|
||||||
|
|
||||||
elif conf['required'] is False:
|
|
||||||
|
|
||||||
# Quotes are not mandatory but when used need to match config
|
|
||||||
if (token.style and
|
|
||||||
not _quote_match(quote_type, token.style) and
|
|
||||||
not (conf['allow-quoted-quotes'] and
|
|
||||||
_has_quoted_quotes(token))):
|
|
||||||
msg = "string value is not quoted with %s quotes" % quote_type
|
|
||||||
|
|
||||||
elif not token.style:
|
|
||||||
is_extra_required = any(re.search(r, token.value)
|
|
||||||
for r in conf['extra-required'])
|
|
||||||
if is_extra_required:
|
|
||||||
msg = "string value is not quoted"
|
|
||||||
|
|
||||||
elif conf['required'] == 'only-when-needed':
|
|
||||||
|
|
||||||
# Quotes are not strictly needed here
|
|
||||||
if (token.style and tag == DEFAULT_SCALAR_TAG and token.value and
|
|
||||||
not _quotes_are_needed(token.value)):
|
|
||||||
is_extra_required = any(re.search(r, token.value)
|
|
||||||
for r in conf['extra-required'])
|
|
||||||
is_extra_allowed = any(re.search(r, token.value)
|
|
||||||
for r in conf['extra-allowed'])
|
|
||||||
if not (is_extra_required or is_extra_allowed):
|
|
||||||
msg = "string value is redundantly quoted with %s quotes" % (
|
|
||||||
quote_type)
|
|
||||||
|
|
||||||
# But when used need to match config
|
|
||||||
elif (token.style and
|
|
||||||
not _quote_match(quote_type, token.style) and
|
|
||||||
not (conf['allow-quoted-quotes'] and _has_quoted_quotes(token))):
|
|
||||||
msg = "string value is not quoted with %s quotes" % quote_type
|
|
||||||
|
|
||||||
elif not token.style:
|
|
||||||
is_extra_required = len(conf['extra-required']) and any(
|
|
||||||
re.search(r, token.value) for r in conf['extra-required'])
|
|
||||||
if is_extra_required:
|
|
||||||
msg = "string value is not quoted"
|
|
||||||
|
|
||||||
if msg is not None:
|
|
||||||
yield LintProblem(
|
|
||||||
token.start_mark.line + 1,
|
|
||||||
token.start_mark.column + 1,
|
|
||||||
msg)
|
|
Loading…
Reference in New Issue