From d32d1f65ad9600d10f9f6c3fab915e25404b139c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Fri, 2 Oct 2020 12:54:37 +0200 Subject: [PATCH] WIP - test by Adrien --- .../yamllint_plugin_example/README.rst | 0 .../plugins/yamllint_plugin_example/setup.cfg | 11 ++++ .../plugins/yamllint_plugin_example/setup.py | 2 + .../yamllint_plugin_example/__init__.py | 27 ++++++++ .../override_comments.py | 63 +++++++++++++++++++ .../yamllint_plugin_example/random_failure.py | 31 +++++++++ yamllint/conf/default.yaml | 2 + yamllint/plugins.py | 1 + 8 files changed, 137 insertions(+) create mode 100644 tests/plugins/yamllint_plugin_example/README.rst create mode 100644 tests/plugins/yamllint_plugin_example/setup.cfg create mode 100644 tests/plugins/yamllint_plugin_example/setup.py create mode 100644 tests/plugins/yamllint_plugin_example/yamllint_plugin_example/__init__.py create mode 100644 tests/plugins/yamllint_plugin_example/yamllint_plugin_example/override_comments.py create mode 100644 tests/plugins/yamllint_plugin_example/yamllint_plugin_example/random_failure.py diff --git a/tests/plugins/yamllint_plugin_example/README.rst b/tests/plugins/yamllint_plugin_example/README.rst new file mode 100644 index 0000000..e69de29 diff --git a/tests/plugins/yamllint_plugin_example/setup.cfg b/tests/plugins/yamllint_plugin_example/setup.cfg new file mode 100644 index 0000000..57b81d1 --- /dev/null +++ b/tests/plugins/yamllint_plugin_example/setup.cfg @@ -0,0 +1,11 @@ +[metadata] +name = yamllint_plugin_example +version = 1.0.0 + +[options] +packages = find: +install_requires = yamllint + +[options.entry_points] +yamllint.plugins.rules = + example = yamllint_plugin_example diff --git a/tests/plugins/yamllint_plugin_example/setup.py b/tests/plugins/yamllint_plugin_example/setup.py new file mode 100644 index 0000000..a4f49f9 --- /dev/null +++ b/tests/plugins/yamllint_plugin_example/setup.py @@ -0,0 +1,2 @@ +import setuptools +setuptools.setup() diff --git a/tests/plugins/yamllint_plugin_example/yamllint_plugin_example/__init__.py b/tests/plugins/yamllint_plugin_example/yamllint_plugin_example/__init__.py new file mode 100644 index 0000000..a5d1086 --- /dev/null +++ b/tests/plugins/yamllint_plugin_example/yamllint_plugin_example/__init__.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2020 Satoru SATOH +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +"""yamllint plugin entry point +""" +from __future__ import absolute_import + +from . import override_comments, random_failure + + +RULES_MAP = { + override_comments.ID: override_comments, + random_failure.ID: random_failure, +} diff --git a/tests/plugins/yamllint_plugin_example/yamllint_plugin_example/override_comments.py b/tests/plugins/yamllint_plugin_example/yamllint_plugin_example/override_comments.py new file mode 100644 index 0000000..64350eb --- /dev/null +++ b/tests/plugins/yamllint_plugin_example/yamllint_plugin_example/override_comments.py @@ -0,0 +1,63 @@ +# +# Copyright (C) 2020 Satoru SATOH +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +""" +Use this rule to override some comments' rules. + +.. rubric:: Options + +* Use ``forbid`` to control comments. Set to ``true`` to forbid comments + completely. + +.. rubric:: Examples + +#. With ``override-comments: {forbid: true}`` + + the following code snippet would **PASS**: + :: + + foo: 1 + + the following code snippet would **FAIL**: + :: + + # baz + foo: 1 + +.. rubric:: Default values (when enabled) + +.. code-block:: yaml + +rules: + override-comments: + forbid: False + +""" +from yamllint.linter import LintProblem + + +ID = 'override-comments' +TYPE = 'comment' +CONF = {'forbid': bool} +DEFAULT = {'forbid': False} + + +def check(conf, comment): + """Check if comments are found. + """ + if conf['forbid']: + yield LintProblem(comment.line_no, comment.column_no, + 'forbidden comment') diff --git a/tests/plugins/yamllint_plugin_example/yamllint_plugin_example/random_failure.py b/tests/plugins/yamllint_plugin_example/yamllint_plugin_example/random_failure.py new file mode 100644 index 0000000..c061c9e --- /dev/null +++ b/tests/plugins/yamllint_plugin_example/yamllint_plugin_example/random_failure.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2020 Adrien Vergé +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +import random + +from yamllint.linter import LintProblem + + +ID = 'random-failure' +TYPE = 'token' + + +def check(conf, token, prev, next, nextnext, context): + if random.random() > 0.9: + yield LintProblem(token.start_mark.line + 1, + token.start_mark.column + 1, + 'random failure') diff --git a/yamllint/conf/default.yaml b/yamllint/conf/default.yaml index 0720ded..9aee263 100644 --- a/yamllint/conf/default.yaml +++ b/yamllint/conf/default.yaml @@ -29,5 +29,7 @@ rules: octal-values: disable quoted-strings: disable trailing-spaces: enable + random-failure: enable + override-comments: {forbid: true} truthy: level: warning diff --git a/yamllint/plugins.py b/yamllint/plugins.py index fa24218..e4be380 100644 --- a/yamllint/plugins.py +++ b/yamllint/plugins.py @@ -46,6 +46,7 @@ def load_plugin_rules_itr(entry_points=None, group=PACKAGE_GROUP): if rule_id in rule_ids or not validate_rule_module(rule_mod): continue + print(rule_id, rule_mod)### yield (rule_id, rule_mod) rule_ids.add(rule_id)