Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce0336e430 | ||
|
|
063c854658 | ||
|
|
673bdbd324 | ||
|
|
cb5fe2c050 | ||
|
|
930c8eea94 | ||
|
|
f6a24552d9 | ||
|
|
0ba193331b | ||
|
|
f65553c4f7 | ||
|
|
0fef4c14e7 | ||
|
|
4ef7e05f3a | ||
|
|
43c50379e0 | ||
|
|
fec2c2fba7 | ||
|
|
2a66ec2e5e |
@@ -1,6 +1,21 @@
|
|||||||
Changelog
|
Changelog
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
1.17.0 (2019-08-12)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Simplify installation instructions in the README
|
||||||
|
- Add OpenBSD installation instructions
|
||||||
|
- Make YAML file extensions configurable
|
||||||
|
|
||||||
|
1.16.0 (2019-06-07)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Add FreeBSD installation instructions
|
||||||
|
- Fix the ``line`` rule to correctly handle DOS new lines
|
||||||
|
- Add the ``allowed-values`` option to the ``truthy`` rule
|
||||||
|
- Allow configuration options to be a list of enums
|
||||||
|
|
||||||
1.15.0 (2019-02-11)
|
1.15.0 (2019-02-11)
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
|
|||||||
25
README.rst
25
README.rst
@@ -38,31 +38,16 @@ Screenshot
|
|||||||
Installation
|
Installation
|
||||||
^^^^^^^^^^^^
|
^^^^^^^^^^^^
|
||||||
|
|
||||||
On Fedora / CentOS (note: `EPEL <https://fedoraproject.org/wiki/EPEL>`_ is
|
Using pip, the Python package manager:
|
||||||
required on CentOS):
|
|
||||||
|
|
||||||
.. code:: bash
|
|
||||||
|
|
||||||
sudo dnf install yamllint
|
|
||||||
|
|
||||||
On Debian 8+ / Ubuntu 16.04+:
|
|
||||||
|
|
||||||
.. code:: bash
|
|
||||||
|
|
||||||
sudo apt-get install yamllint
|
|
||||||
|
|
||||||
On Mac OS 10.11+:
|
|
||||||
|
|
||||||
.. code:: bash
|
|
||||||
|
|
||||||
brew install yamllint
|
|
||||||
|
|
||||||
Alternatively using pip, the Python package manager:
|
|
||||||
|
|
||||||
.. code:: bash
|
.. code:: bash
|
||||||
|
|
||||||
pip install --user yamllint
|
pip install --user yamllint
|
||||||
|
|
||||||
|
yamllint is also packaged for all major operating systems, see installation
|
||||||
|
examples (``dnf``, ``apt-get``...) `in the documentation
|
||||||
|
<https://yamllint.readthedocs.io/en/stable/quickstart.html>`_.
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
^^^^^
|
^^^^^
|
||||||
|
|
||||||
|
|||||||
@@ -115,6 +115,21 @@ return code will be:
|
|||||||
* ``1`` if one or more errors occur
|
* ``1`` if one or more errors occur
|
||||||
* ``2`` if no errors occur, but one or more warnings occur
|
* ``2`` if no errors occur, but one or more warnings occur
|
||||||
|
|
||||||
|
YAML files extensions
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
To configure what yamllint should consider as YAML files, set ``yaml-files``
|
||||||
|
configuration option. The default is:
|
||||||
|
|
||||||
|
.. code-block:: yaml
|
||||||
|
|
||||||
|
yaml-files:
|
||||||
|
- '*.yaml'
|
||||||
|
- '*.yml'
|
||||||
|
|
||||||
|
The same rules as for ignoring paths apply (``.gitignore``-style path pattern,
|
||||||
|
see below).
|
||||||
|
|
||||||
Ignoring paths
|
Ignoring paths
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ Quickstart
|
|||||||
Installing yamllint
|
Installing yamllint
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
On Fedora / CentOS:
|
On Fedora / CentOS (note: `EPEL <https://fedoraproject.org/wiki/EPEL>`_ is
|
||||||
|
required on CentOS):
|
||||||
|
|
||||||
.. code:: bash
|
.. code:: bash
|
||||||
|
|
||||||
@@ -22,6 +23,18 @@ On Mac OS 10.11+:
|
|||||||
|
|
||||||
brew install yamllint
|
brew install yamllint
|
||||||
|
|
||||||
|
On FreeBSD:
|
||||||
|
|
||||||
|
.. code:: sh
|
||||||
|
|
||||||
|
pkg install py36-yamllint
|
||||||
|
|
||||||
|
On OpenBSD:
|
||||||
|
|
||||||
|
.. code:: sh
|
||||||
|
|
||||||
|
doas pkg_add py3-yamllint
|
||||||
|
|
||||||
Alternatively using pip, the Python package manager:
|
Alternatively using pip, the Python package manager:
|
||||||
|
|
||||||
.. code:: bash
|
.. code:: bash
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -14,7 +14,7 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from setuptools import setup, find_packages
|
from setuptools import find_packages, setup
|
||||||
|
|
||||||
from yamllint import (__author__, __license__,
|
from yamllint import (__author__, __license__,
|
||||||
APP_NAME, APP_VERSION, APP_DESCRIPTION)
|
APP_NAME, APP_VERSION, APP_DESCRIPTION)
|
||||||
|
|||||||
@@ -78,3 +78,22 @@ class EmptyLinesTestCase(RuleTestCase):
|
|||||||
'document-start: disable\n')
|
'document-start: disable\n')
|
||||||
self.check('non empty\n', conf)
|
self.check('non empty\n', conf)
|
||||||
self.check('non empty\n\n', conf, problem=(2, 1))
|
self.check('non empty\n\n', conf, problem=(2, 1))
|
||||||
|
|
||||||
|
def test_with_dos_newlines(self):
|
||||||
|
conf = ('empty-lines: {max: 2, max-start: 0, max-end: 0}\n'
|
||||||
|
'new-lines: {type: dos}\n'
|
||||||
|
'document-start: disable\n')
|
||||||
|
self.check('---\r\n', conf)
|
||||||
|
self.check('---\r\ntext\r\n\r\ntext\r\n', conf)
|
||||||
|
self.check('\r\n---\r\ntext\r\n\r\ntext\r\n', conf,
|
||||||
|
problem=(1, 1))
|
||||||
|
self.check('\r\n\r\n\r\n---\r\ntext\r\n\r\ntext\r\n', conf,
|
||||||
|
problem=(3, 1))
|
||||||
|
self.check('---\r\ntext\r\n\r\n\r\n\r\ntext\r\n', conf,
|
||||||
|
problem=(5, 1))
|
||||||
|
self.check('---\r\ntext\r\n\r\n\r\n\r\n\r\n\r\n\r\ntext\r\n', conf,
|
||||||
|
problem=(8, 1))
|
||||||
|
self.check('---\r\ntext\r\n\r\ntext\r\n\r\n', conf,
|
||||||
|
problem=(5, 1))
|
||||||
|
self.check('---\r\ntext\r\n\r\ntext\r\n\r\n\r\n\r\n', conf,
|
||||||
|
problem=(7, 1))
|
||||||
|
|||||||
@@ -171,3 +171,12 @@ class LineLengthTestCase(RuleTestCase):
|
|||||||
'# This is a test to check if “line-length” works nice\n'
|
'# This is a test to check if “line-length” works nice\n'
|
||||||
'with: “unicode characters” that span accross bytes! ↺\n',
|
'with: “unicode characters” that span accross bytes! ↺\n',
|
||||||
conf, problem1=(2, 53), problem2=(3, 53))
|
conf, problem1=(2, 53), problem2=(3, 53))
|
||||||
|
|
||||||
|
def test_with_dos_newlines(self):
|
||||||
|
conf = ('line-length: {max: 10}\n'
|
||||||
|
'new-lines: {type: dos}\n'
|
||||||
|
'new-line-at-end-of-file: disable\n')
|
||||||
|
self.check('---\r\nABCD EFGHI', conf)
|
||||||
|
self.check('---\r\nABCD EFGHI\r\n', conf)
|
||||||
|
self.check('---\r\nABCD EFGHIJ', conf, problem=(2, 11))
|
||||||
|
self.check('---\r\nABCD EFGHIJ\r\n', conf, problem=(2, 11))
|
||||||
|
|||||||
@@ -31,16 +31,20 @@ class NewLinesTestCase(RuleTestCase):
|
|||||||
self.check('---\r\ntext\r\n', conf)
|
self.check('---\r\ntext\r\n', conf)
|
||||||
|
|
||||||
def test_unix_type(self):
|
def test_unix_type(self):
|
||||||
conf = 'new-lines: {type: unix}'
|
conf = ('new-line-at-end-of-file: disable\n'
|
||||||
|
'new-lines: {type: unix}\n')
|
||||||
self.check('', conf)
|
self.check('', conf)
|
||||||
|
self.check('\r', conf)
|
||||||
self.check('\n', conf)
|
self.check('\n', conf)
|
||||||
self.check('\r\n', conf, problem=(1, 1))
|
self.check('\r\n', conf, problem=(1, 1))
|
||||||
self.check('---\ntext\n', conf)
|
self.check('---\ntext\n', conf)
|
||||||
self.check('---\r\ntext\r\n', conf, problem=(1, 4))
|
self.check('---\r\ntext\r\n', conf, problem=(1, 4))
|
||||||
|
|
||||||
def test_dos_type(self):
|
def test_dos_type(self):
|
||||||
conf = 'new-lines: {type: dos}\n'
|
conf = ('new-line-at-end-of-file: disable\n'
|
||||||
|
'new-lines: {type: dos}\n')
|
||||||
self.check('', conf)
|
self.check('', conf)
|
||||||
|
self.check('\r', conf)
|
||||||
self.check('\n', conf, problem=(1, 1))
|
self.check('\n', conf, problem=(1, 1))
|
||||||
self.check('\r\n', conf)
|
self.check('\r\n', conf)
|
||||||
self.check('---\ntext\n', conf, problem=(1, 4))
|
self.check('---\ntext\n', conf, problem=(1, 4))
|
||||||
|
|||||||
@@ -49,6 +49,54 @@ class TruthyTestCase(RuleTestCase):
|
|||||||
problem3=(7, 3), problem4=(7, 7),
|
problem3=(7, 3), problem4=(7, 7),
|
||||||
problem5=(8, 3), problem6=(8, 7))
|
problem5=(8, 3), problem6=(8, 7))
|
||||||
|
|
||||||
|
def test_different_allowed_values(self):
|
||||||
|
conf = ('truthy:\n'
|
||||||
|
' allowed-values: ["yes", "no"]\n')
|
||||||
|
self.check('---\n'
|
||||||
|
'key1: foo\n'
|
||||||
|
'key2: yes\n'
|
||||||
|
'key3: bar\n'
|
||||||
|
'key4: no\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'key1: true\n'
|
||||||
|
'key2: Yes\n'
|
||||||
|
'key3: false\n'
|
||||||
|
'key4: no\n'
|
||||||
|
'key5: yes\n',
|
||||||
|
conf,
|
||||||
|
problem1=(2, 7), problem2=(3, 7),
|
||||||
|
problem3=(4, 7))
|
||||||
|
|
||||||
|
def test_combined_allowed_values(self):
|
||||||
|
conf = ('truthy:\n'
|
||||||
|
' allowed-values: ["yes", "no", "true", "false"]\n')
|
||||||
|
self.check('---\n'
|
||||||
|
'key1: foo\n'
|
||||||
|
'key2: yes\n'
|
||||||
|
'key3: bar\n'
|
||||||
|
'key4: no\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'key1: true\n'
|
||||||
|
'key2: Yes\n'
|
||||||
|
'key3: false\n'
|
||||||
|
'key4: no\n'
|
||||||
|
'key5: yes\n',
|
||||||
|
conf, problem1=(3, 7))
|
||||||
|
|
||||||
|
def test_no_allowed_values(self):
|
||||||
|
conf = ('truthy:\n'
|
||||||
|
' allowed-values: []\n')
|
||||||
|
self.check('---\n'
|
||||||
|
'key1: foo\n'
|
||||||
|
'key2: bar\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'key1: true\n'
|
||||||
|
'key2: yes\n'
|
||||||
|
'key3: false\n'
|
||||||
|
'key4: no\n', conf,
|
||||||
|
problem1=(2, 7), problem2=(3, 7),
|
||||||
|
problem3=(4, 7), problem4=(5, 7))
|
||||||
|
|
||||||
def test_explicit_types(self):
|
def test_explicit_types(self):
|
||||||
conf = 'truthy: enable\n'
|
conf = 'truthy: enable\n'
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import unittest
|
|||||||
from tests.common import build_temp_workspace
|
from tests.common import build_temp_workspace
|
||||||
|
|
||||||
from yamllint import cli
|
from yamllint import cli
|
||||||
|
from yamllint import config
|
||||||
|
|
||||||
|
|
||||||
class CommandLineTestCase(unittest.TestCase):
|
class CommandLineTestCase(unittest.TestCase):
|
||||||
@@ -73,8 +74,9 @@ class CommandLineTestCase(unittest.TestCase):
|
|||||||
shutil.rmtree(cls.wd)
|
shutil.rmtree(cls.wd)
|
||||||
|
|
||||||
def test_find_files_recursively(self):
|
def test_find_files_recursively(self):
|
||||||
|
conf = config.YamlLintConfig('extends: default')
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
sorted(cli.find_files_recursively([self.wd])),
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
||||||
[os.path.join(self.wd, 'a.yaml'),
|
[os.path.join(self.wd, 'a.yaml'),
|
||||||
os.path.join(self.wd, 'empty.yml'),
|
os.path.join(self.wd, 'empty.yml'),
|
||||||
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'),
|
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'),
|
||||||
@@ -85,14 +87,14 @@ class CommandLineTestCase(unittest.TestCase):
|
|||||||
items = [os.path.join(self.wd, 'sub/ok.yaml'),
|
items = [os.path.join(self.wd, 'sub/ok.yaml'),
|
||||||
os.path.join(self.wd, 'empty-dir')]
|
os.path.join(self.wd, 'empty-dir')]
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
sorted(cli.find_files_recursively(items)),
|
sorted(cli.find_files_recursively(items, conf)),
|
||||||
[os.path.join(self.wd, 'sub/ok.yaml')],
|
[os.path.join(self.wd, 'sub/ok.yaml')],
|
||||||
)
|
)
|
||||||
|
|
||||||
items = [os.path.join(self.wd, 'empty.yml'),
|
items = [os.path.join(self.wd, 'empty.yml'),
|
||||||
os.path.join(self.wd, 's')]
|
os.path.join(self.wd, 's')]
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
sorted(cli.find_files_recursively(items)),
|
sorted(cli.find_files_recursively(items, conf)),
|
||||||
[os.path.join(self.wd, 'empty.yml'),
|
[os.path.join(self.wd, 'empty.yml'),
|
||||||
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml')],
|
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml')],
|
||||||
)
|
)
|
||||||
@@ -100,11 +102,77 @@ class CommandLineTestCase(unittest.TestCase):
|
|||||||
items = [os.path.join(self.wd, 'sub'),
|
items = [os.path.join(self.wd, 'sub'),
|
||||||
os.path.join(self.wd, '/etc/another/file')]
|
os.path.join(self.wd, '/etc/another/file')]
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
sorted(cli.find_files_recursively(items)),
|
sorted(cli.find_files_recursively(items, conf)),
|
||||||
[os.path.join(self.wd, '/etc/another/file'),
|
[os.path.join(self.wd, '/etc/another/file'),
|
||||||
os.path.join(self.wd, 'sub/ok.yaml')],
|
os.path.join(self.wd, 'sub/ok.yaml')],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
conf = config.YamlLintConfig('extends: default\n'
|
||||||
|
'yaml-files:\n'
|
||||||
|
' - \'*.yaml\' \n')
|
||||||
|
self.assertEqual(
|
||||||
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
||||||
|
[os.path.join(self.wd, 'a.yaml'),
|
||||||
|
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'),
|
||||||
|
os.path.join(self.wd, 'sub/ok.yaml'),
|
||||||
|
os.path.join(self.wd, 'warn.yaml')]
|
||||||
|
)
|
||||||
|
|
||||||
|
conf = config.YamlLintConfig('extends: default\n'
|
||||||
|
'yaml-files:\n'
|
||||||
|
' - \'*.yml\'\n')
|
||||||
|
self.assertEqual(
|
||||||
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
||||||
|
[os.path.join(self.wd, 'empty.yml')]
|
||||||
|
)
|
||||||
|
|
||||||
|
conf = config.YamlLintConfig('extends: default\n'
|
||||||
|
'yaml-files:\n'
|
||||||
|
' - \'*.json\'\n')
|
||||||
|
self.assertEqual(
|
||||||
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
||||||
|
[os.path.join(self.wd, 'no-yaml.json')]
|
||||||
|
)
|
||||||
|
|
||||||
|
conf = config.YamlLintConfig('extends: default\n'
|
||||||
|
'yaml-files:\n'
|
||||||
|
' - \'*\'\n')
|
||||||
|
self.assertEqual(
|
||||||
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
||||||
|
[os.path.join(self.wd, 'a.yaml'),
|
||||||
|
os.path.join(self.wd, 'empty.yml'),
|
||||||
|
os.path.join(self.wd, 'no-yaml.json'),
|
||||||
|
os.path.join(self.wd, 'non-ascii/utf-8'),
|
||||||
|
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'),
|
||||||
|
os.path.join(self.wd, 'sub/ok.yaml'),
|
||||||
|
os.path.join(self.wd, 'warn.yaml')]
|
||||||
|
)
|
||||||
|
|
||||||
|
conf = config.YamlLintConfig('extends: default\n'
|
||||||
|
'yaml-files:\n'
|
||||||
|
' - \'*.yaml\'\n'
|
||||||
|
' - \'*\'\n'
|
||||||
|
' - \'**\'\n')
|
||||||
|
self.assertEqual(
|
||||||
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
||||||
|
[os.path.join(self.wd, 'a.yaml'),
|
||||||
|
os.path.join(self.wd, 'empty.yml'),
|
||||||
|
os.path.join(self.wd, 'no-yaml.json'),
|
||||||
|
os.path.join(self.wd, 'non-ascii/utf-8'),
|
||||||
|
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'),
|
||||||
|
os.path.join(self.wd, 'sub/ok.yaml'),
|
||||||
|
os.path.join(self.wd, 'warn.yaml')]
|
||||||
|
)
|
||||||
|
|
||||||
|
conf = config.YamlLintConfig('extends: default\n'
|
||||||
|
'yaml-files:\n'
|
||||||
|
' - \'s/**\'\n'
|
||||||
|
' - \'**/utf-8\'\n')
|
||||||
|
self.assertEqual(
|
||||||
|
sorted(cli.find_files_recursively([self.wd], conf)),
|
||||||
|
[os.path.join(self.wd, 'non-ascii/utf-8')]
|
||||||
|
)
|
||||||
|
|
||||||
def test_run_with_bad_arguments(self):
|
def test_run_with_bad_arguments(self):
|
||||||
sys.stdout, sys.stderr = StringIO(), StringIO()
|
sys.stdout, sys.stderr = StringIO(), StringIO()
|
||||||
with self.assertRaises(SystemExit) as ctx:
|
with self.assertRaises(SystemExit) as ctx:
|
||||||
|
|||||||
@@ -174,6 +174,25 @@ class SimpleConfigTestCase(unittest.TestCase):
|
|||||||
self.assertRaises(config.YamlLintConfigError,
|
self.assertRaises(config.YamlLintConfigError,
|
||||||
config.validate_rule_conf, Rule, {'choice': 'abc'})
|
config.validate_rule_conf, Rule, {'choice': 'abc'})
|
||||||
|
|
||||||
|
Rule.CONF = {'multiple': ['item1', 'item2', 'item3']}
|
||||||
|
Rule.DEFAULT = {'multiple': ['item1']}
|
||||||
|
config.validate_rule_conf(Rule, {'multiple': []})
|
||||||
|
config.validate_rule_conf(Rule, {'multiple': ['item2']})
|
||||||
|
config.validate_rule_conf(Rule, {'multiple': ['item2', 'item3']})
|
||||||
|
config.validate_rule_conf(Rule, {})
|
||||||
|
self.assertRaises(config.YamlLintConfigError,
|
||||||
|
config.validate_rule_conf, Rule,
|
||||||
|
{'multiple': 'item1'})
|
||||||
|
self.assertRaises(config.YamlLintConfigError,
|
||||||
|
config.validate_rule_conf, Rule,
|
||||||
|
{'multiple': ['']})
|
||||||
|
self.assertRaises(config.YamlLintConfigError,
|
||||||
|
config.validate_rule_conf, Rule,
|
||||||
|
{'multiple': ['item1', 4]})
|
||||||
|
self.assertRaises(config.YamlLintConfigError,
|
||||||
|
config.validate_rule_conf, Rule,
|
||||||
|
{'multiple': ['item4']})
|
||||||
|
|
||||||
|
|
||||||
class ExtendedConfigTestCase(unittest.TestCase):
|
class ExtendedConfigTestCase(unittest.TestCase):
|
||||||
def test_extend_on_object(self):
|
def test_extend_on_object(self):
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ indentation, etc."""
|
|||||||
|
|
||||||
|
|
||||||
APP_NAME = 'yamllint'
|
APP_NAME = 'yamllint'
|
||||||
APP_VERSION = '1.15.0'
|
APP_VERSION = '1.17.0'
|
||||||
APP_DESCRIPTION = __doc__
|
APP_DESCRIPTION = __doc__
|
||||||
|
|
||||||
__author__ = u'Adrien Vergé'
|
__author__ = u'Adrien Vergé'
|
||||||
|
|||||||
@@ -16,24 +16,25 @@
|
|||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import platform
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import os
|
||||||
|
import platform
|
||||||
|
import sys
|
||||||
|
|
||||||
from yamllint import APP_DESCRIPTION, APP_NAME, APP_VERSION
|
from yamllint import APP_DESCRIPTION, APP_NAME, APP_VERSION
|
||||||
|
from yamllint import linter
|
||||||
from yamllint.config import YamlLintConfig, YamlLintConfigError
|
from yamllint.config import YamlLintConfig, YamlLintConfigError
|
||||||
from yamllint.linter import PROBLEM_LEVELS
|
from yamllint.linter import PROBLEM_LEVELS
|
||||||
from yamllint import linter
|
|
||||||
|
|
||||||
|
|
||||||
def find_files_recursively(items):
|
def find_files_recursively(items, conf):
|
||||||
for item in items:
|
for item in items:
|
||||||
if os.path.isdir(item):
|
if os.path.isdir(item):
|
||||||
for root, dirnames, filenames in os.walk(item):
|
for root, dirnames, filenames in os.walk(item):
|
||||||
for filename in [f for f in filenames
|
for f in filenames:
|
||||||
if f.endswith(('.yml', '.yaml'))]:
|
filepath = os.path.join(root, f)
|
||||||
yield os.path.join(root, filename)
|
if conf.is_yaml_file(filepath):
|
||||||
|
yield filepath
|
||||||
else:
|
else:
|
||||||
yield item
|
yield item
|
||||||
|
|
||||||
@@ -163,7 +164,7 @@ def run(argv=None):
|
|||||||
|
|
||||||
max_level = 0
|
max_level = 0
|
||||||
|
|
||||||
for file in find_files_recursively(args.files):
|
for file in find_files_recursively(args.files, conf):
|
||||||
filepath = file[2:] if file.startswith('./') else file
|
filepath = file[2:] if file.startswith('./') else file
|
||||||
try:
|
try:
|
||||||
with open(file) as f:
|
with open(file) as f:
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
|
yaml-files:
|
||||||
|
- '*.yaml'
|
||||||
|
- '*.yml'
|
||||||
|
|
||||||
rules:
|
rules:
|
||||||
braces: enable
|
braces: enable
|
||||||
brackets: enable
|
brackets: enable
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ class YamlLintConfig(object):
|
|||||||
|
|
||||||
self.ignore = None
|
self.ignore = None
|
||||||
|
|
||||||
|
self.yaml_files = pathspec.PathSpec.from_lines(
|
||||||
|
'gitwildmatch', ['*.yaml', '*.yml'])
|
||||||
|
|
||||||
if file is not None:
|
if file is not None:
|
||||||
with open(file) as f:
|
with open(file) as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
@@ -42,6 +45,9 @@ class YamlLintConfig(object):
|
|||||||
def is_file_ignored(self, filepath):
|
def is_file_ignored(self, filepath):
|
||||||
return self.ignore and self.ignore.match_file(filepath)
|
return self.ignore and self.ignore.match_file(filepath)
|
||||||
|
|
||||||
|
def is_yaml_file(self, filepath):
|
||||||
|
return self.yaml_files.match_file(filepath)
|
||||||
|
|
||||||
def enabled_rules(self, filepath):
|
def enabled_rules(self, filepath):
|
||||||
return [yamllint.rules.get(id) for id, val in self.rules.items()
|
return [yamllint.rules.get(id) for id, val in self.rules.items()
|
||||||
if val is not False and (
|
if val is not False and (
|
||||||
@@ -96,6 +102,15 @@ class YamlLintConfig(object):
|
|||||||
self.ignore = pathspec.PathSpec.from_lines(
|
self.ignore = pathspec.PathSpec.from_lines(
|
||||||
'gitwildmatch', conf['ignore'].splitlines())
|
'gitwildmatch', conf['ignore'].splitlines())
|
||||||
|
|
||||||
|
if 'yaml-files' in conf:
|
||||||
|
if not (isinstance(conf['yaml-files'], list)
|
||||||
|
and all(isinstance(i, str) for i in conf['yaml-files'])):
|
||||||
|
raise YamlLintConfigError(
|
||||||
|
'invalid config: yaml-files '
|
||||||
|
'should be a list of file patterns')
|
||||||
|
self.yaml_files = pathspec.PathSpec.from_lines('gitwildmatch',
|
||||||
|
conf['yaml-files'])
|
||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
for id in self.rules:
|
for id in self.rules:
|
||||||
try:
|
try:
|
||||||
@@ -134,12 +149,26 @@ def validate_rule_conf(rule, conf):
|
|||||||
raise YamlLintConfigError(
|
raise YamlLintConfigError(
|
||||||
'invalid config: unknown option "%s" for rule "%s"' %
|
'invalid config: unknown option "%s" for rule "%s"' %
|
||||||
(optkey, rule.ID))
|
(optkey, rule.ID))
|
||||||
|
# Example: CONF = {option: (bool, 'mixed')}
|
||||||
|
# → {option: true} → {option: mixed}
|
||||||
if isinstance(options[optkey], tuple):
|
if isinstance(options[optkey], tuple):
|
||||||
if (conf[optkey] not in options[optkey] and
|
if (conf[optkey] not in options[optkey] and
|
||||||
type(conf[optkey]) not in options[optkey]):
|
type(conf[optkey]) not in options[optkey]):
|
||||||
raise YamlLintConfigError(
|
raise YamlLintConfigError(
|
||||||
'invalid config: option "%s" of "%s" should be in %s'
|
'invalid config: option "%s" of "%s" should be in %s'
|
||||||
% (optkey, rule.ID, options[optkey]))
|
% (optkey, rule.ID, options[optkey]))
|
||||||
|
# Example: CONF = {option: ['flag1', 'flag2']}
|
||||||
|
# → {option: [flag1]} → {option: [flag1, flag2]}
|
||||||
|
elif isinstance(options[optkey], list):
|
||||||
|
if (type(conf[optkey]) is not list or
|
||||||
|
any(flag not in options[optkey]
|
||||||
|
for flag in conf[optkey])):
|
||||||
|
raise YamlLintConfigError(
|
||||||
|
('invalid config: option "%s" of "%s" should only '
|
||||||
|
'contain values in %s')
|
||||||
|
% (optkey, rule.ID, str(options[optkey])))
|
||||||
|
# Example: CONF = {option: int}
|
||||||
|
# → {option: 42}
|
||||||
else:
|
else:
|
||||||
if not isinstance(conf[optkey], options[optkey]):
|
if not isinstance(conf[optkey], options[optkey]):
|
||||||
raise YamlLintConfigError(
|
raise YamlLintConfigError(
|
||||||
|
|||||||
@@ -77,7 +77,10 @@ def line_generator(buffer):
|
|||||||
cur = 0
|
cur = 0
|
||||||
next = buffer.find('\n')
|
next = buffer.find('\n')
|
||||||
while next != -1:
|
while next != -1:
|
||||||
yield Line(line_no, buffer, start=cur, end=next)
|
if next > 0 and buffer[next - 1] == '\r':
|
||||||
|
yield Line(line_no, buffer, start=cur, end=next - 1)
|
||||||
|
else:
|
||||||
|
yield Line(line_no, buffer, start=cur, end=next)
|
||||||
cur = next + 1
|
cur = next + 1
|
||||||
next = buffer.find('\n', cur)
|
next = buffer.find('\n', cur)
|
||||||
line_no += 1
|
line_no += 1
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ Use this rule to control the number of spaces before and after colons (``:``).
|
|||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from yamllint.rules.common import spaces_after, spaces_before, is_explicit_key
|
from yamllint.rules.common import is_explicit_key, spaces_after, spaces_before
|
||||||
|
|
||||||
|
|
||||||
ID = 'colons'
|
ID = 'colons'
|
||||||
|
|||||||
@@ -66,27 +66,37 @@ DEFAULT = {'max': 2,
|
|||||||
def check(conf, line):
|
def check(conf, line):
|
||||||
if line.start == line.end and line.end < len(line.buffer):
|
if line.start == line.end and line.end < len(line.buffer):
|
||||||
# Only alert on the last blank line of a series
|
# Only alert on the last blank line of a series
|
||||||
if (line.end < len(line.buffer) - 1 and
|
if (line.end + 2 <= len(line.buffer) and
|
||||||
line.buffer[line.end + 1] == '\n'):
|
line.buffer[line.end:line.end + 2] == '\n\n'):
|
||||||
|
return
|
||||||
|
elif (line.end + 4 <= len(line.buffer) and
|
||||||
|
line.buffer[line.end:line.end + 4] == '\r\n\r\n'):
|
||||||
return
|
return
|
||||||
|
|
||||||
blank_lines = 0
|
blank_lines = 0
|
||||||
|
|
||||||
while (line.start > blank_lines and
|
start = line.start
|
||||||
line.buffer[line.start - blank_lines - 1] == '\n'):
|
while start >= 2 and line.buffer[start - 2:start] == '\r\n':
|
||||||
blank_lines += 1
|
blank_lines += 1
|
||||||
|
start -= 2
|
||||||
|
while start >= 1 and line.buffer[start - 1] == '\n':
|
||||||
|
blank_lines += 1
|
||||||
|
start -= 1
|
||||||
|
|
||||||
max = conf['max']
|
max = conf['max']
|
||||||
|
|
||||||
# Special case: start of document
|
# Special case: start of document
|
||||||
if line.start - blank_lines == 0:
|
if start == 0:
|
||||||
blank_lines += 1 # first line doesn't have a preceding \n
|
blank_lines += 1 # first line doesn't have a preceding \n
|
||||||
max = conf['max-start']
|
max = conf['max-start']
|
||||||
|
|
||||||
# Special case: end of document
|
# Special case: end of document
|
||||||
# NOTE: The last line of a file is always supposed to end with a new
|
# NOTE: The last line of a file is always supposed to end with a new
|
||||||
# line. See POSIX definition of a line at:
|
# line. See POSIX definition of a line at:
|
||||||
if line.end == len(line.buffer) - 1 and line.buffer[line.end] == '\n':
|
if ((line.end == len(line.buffer) - 1 and
|
||||||
|
line.buffer[line.end] == '\n') or
|
||||||
|
(line.end == len(line.buffer) - 2 and
|
||||||
|
line.buffer[line.end:line.end + 2] == '\r\n')):
|
||||||
# Allow the exception of the one-byte file containing '\n'
|
# Allow the exception of the one-byte file containing '\n'
|
||||||
if line.end == 0:
|
if line.end == 0:
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -193,7 +193,7 @@ Use this rule to control the indentation.
|
|||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from yamllint.linter import LintProblem
|
from yamllint.linter import LintProblem
|
||||||
from yamllint.rules.common import is_explicit_key, get_real_end_line
|
from yamllint.rules.common import get_real_end_line, is_explicit_key
|
||||||
|
|
||||||
|
|
||||||
ID = 'indentation'
|
ID = 'indentation'
|
||||||
|
|||||||
@@ -36,10 +36,11 @@ DEFAULT = {'type': 'unix'}
|
|||||||
def check(conf, line):
|
def check(conf, line):
|
||||||
if line.start == 0 and len(line.buffer) > line.end:
|
if line.start == 0 and len(line.buffer) > line.end:
|
||||||
if conf['type'] == 'dos':
|
if conf['type'] == 'dos':
|
||||||
if line.buffer[line.end - 1:line.end + 1] != '\r\n':
|
if (line.end + 2 > len(line.buffer) or
|
||||||
|
line.buffer[line.end:line.end + 2] != '\r\n'):
|
||||||
yield LintProblem(1, line.end - line.start + 1,
|
yield LintProblem(1, line.end - line.start + 1,
|
||||||
'wrong new line character: expected \\r\\n')
|
'wrong new line character: expected \\r\\n')
|
||||||
else:
|
else:
|
||||||
if line.end > 0 and line.buffer[line.end - 1] == '\r':
|
if line.buffer[line.end] == '\r':
|
||||||
yield LintProblem(1, line.end - line.start,
|
yield LintProblem(1, line.end - line.start + 1,
|
||||||
'wrong new line character: expected \\n')
|
'wrong new line character: expected \\n')
|
||||||
|
|||||||
@@ -15,13 +15,22 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Use this rule to forbid non-explictly typed truthy values other than ``true``
|
Use this rule to forbid non-explictly typed truthy values other than allowed
|
||||||
and ``false``, for example ``YES``, ``False`` and ``off``.
|
ones (by default: ``true`` and ``false``), for example ``YES`` or ``off``.
|
||||||
|
|
||||||
This can be useful to prevent surprises from YAML parsers transforming
|
This can be useful to prevent surprises from YAML parsers transforming
|
||||||
``[yes, FALSE, Off]`` into ``[true, false, false]`` or
|
``[yes, FALSE, Off]`` into ``[true, false, false]`` or
|
||||||
``{y: 1, yes: 2, on: 3, true: 4, True: 5}`` into ``{y: 1, true: 5}``.
|
``{y: 1, yes: 2, on: 3, true: 4, True: 5}`` into ``{y: 1, true: 5}``.
|
||||||
|
|
||||||
|
.. rubric:: Options
|
||||||
|
|
||||||
|
* ``allowed-values`` defines the list of truthy values which will be ignored
|
||||||
|
during linting. The default is ``['true', 'false']``, but can be changed to
|
||||||
|
any list containing: ``'TRUE'``, ``'True'``, ``'true'``, ``'FALSE'``,
|
||||||
|
``'False'``, ``'false'``, ``'YES'``, ``'Yes'``, ``'yes'``, ``'NO'``,
|
||||||
|
``'No'``, ``'no'``, ``'ON'``, ``'On'``, ``'on'``, ``'OFF'``, ``'Off'``,
|
||||||
|
``'off'``.
|
||||||
|
|
||||||
.. rubric:: Examples
|
.. rubric:: Examples
|
||||||
|
|
||||||
#. With ``truthy: {}``
|
#. With ``truthy: {}``
|
||||||
@@ -63,29 +72,55 @@ This can be useful to prevent surprises from YAML parsers transforming
|
|||||||
yes: 1
|
yes: 1
|
||||||
on: 2
|
on: 2
|
||||||
True: 3
|
True: 3
|
||||||
|
|
||||||
|
#. With ``truthy: {allowed-values: ["yes", "no"]}``
|
||||||
|
|
||||||
|
the following code snippet would **PASS**:
|
||||||
|
::
|
||||||
|
|
||||||
|
- yes
|
||||||
|
- no
|
||||||
|
- "true"
|
||||||
|
- 'false'
|
||||||
|
- foo
|
||||||
|
- bar
|
||||||
|
|
||||||
|
the following code snippet would **FAIL**:
|
||||||
|
::
|
||||||
|
|
||||||
|
- true
|
||||||
|
- false
|
||||||
|
- on
|
||||||
|
- off
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from yamllint.linter import LintProblem
|
from yamllint.linter import LintProblem
|
||||||
|
|
||||||
ID = 'truthy'
|
|
||||||
TYPE = 'token'
|
|
||||||
|
|
||||||
TRUTHY = ['YES', 'Yes', 'yes',
|
TRUTHY = ['YES', 'Yes', 'yes',
|
||||||
'NO', 'No', 'no',
|
'NO', 'No', 'no',
|
||||||
'TRUE', 'True', # 'true' is a boolean
|
'TRUE', 'True', 'true',
|
||||||
'FALSE', 'False', # 'false' is a boolean
|
'FALSE', 'False', 'false',
|
||||||
'ON', 'On', 'on',
|
'ON', 'On', 'on',
|
||||||
'OFF', 'Off', 'off']
|
'OFF', 'Off', 'off']
|
||||||
|
|
||||||
|
|
||||||
|
ID = 'truthy'
|
||||||
|
TYPE = 'token'
|
||||||
|
CONF = {'allowed-values': list(TRUTHY)}
|
||||||
|
DEFAULT = {'allowed-values': ['true', 'false']}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
if prev and isinstance(prev, yaml.tokens.TagToken):
|
if prev and isinstance(prev, yaml.tokens.TagToken):
|
||||||
return
|
return
|
||||||
|
|
||||||
if isinstance(token, yaml.tokens.ScalarToken):
|
if isinstance(token, yaml.tokens.ScalarToken):
|
||||||
if token.value in TRUTHY and token.style is None:
|
if (token.value in (set(TRUTHY) - set(conf['allowed-values'])) and
|
||||||
|
token.style is None):
|
||||||
yield LintProblem(token.start_mark.line + 1,
|
yield LintProblem(token.start_mark.line + 1,
|
||||||
token.start_mark.column + 1,
|
token.start_mark.column + 1,
|
||||||
"truthy value should be true or false")
|
"truthy value should be one of [" +
|
||||||
|
", ".join(sorted(conf['allowed-values'])) + "]")
|
||||||
|
|||||||
Reference in New Issue
Block a user