-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to run custom rules included in a project
- Loading branch information
Showing
6 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
Custom Rules | ||
============ | ||
|
||
There are times when you might like to add custom rules to your | ||
project. This could be because the rules you'd like to enforce are | ||
not general enough to consider including in upstream yamllint. | ||
|
||
yamllint will look for custom rules in ``.yamllint/rules``. To enable | ||
a custom rule you need to explicitly reference the rule in your | ||
config. | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
In this example there is a custom rule called ``truthy`` that will | ||
complain if ambiguous truthy values are not quoted. | ||
|
||
This is the directory structure: | ||
|
||
.. code:: plain | ||
. | ||
|-- .yamllint | ||
| |-- config | ||
| `-- rules | ||
| |-- __init__.py | ||
| `-- truthy.py | ||
`-- example.yml | ||
2 directories, 4 files | ||
This is an example yaml file with ambiguous truthy values: | ||
|
||
.. code:: yaml | ||
--- | ||
a: y | ||
b: yes | ||
c: on | ||
d: True | ||
This is an example config file: | ||
|
||
.. code:: yaml | ||
--- | ||
extends: default | ||
rules: | ||
truthy: enable | ||
Lint problems from the custom rule are now included in the yamllint | ||
output: | ||
|
||
.. code:: plain | ||
$ yamllint example.yml | ||
example.yml | ||
2:3 error ambiguous truthy value is not quoted (truthy) | ||
3:3 error ambiguous truthy value is not quoted (truthy) | ||
4:3 error ambiguous truthy value is not quoted (truthy) | ||
5:3 error ambiguous truthy value is not quoted (truthy) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,4 @@ Table of contents | |
disable_with_comments | ||
development | ||
text_editors | ||
custom_rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2016 Adrien Vergé | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import os | ||
import shutil | ||
import tempfile | ||
|
||
from tests.common import RuleTestCase | ||
from yamllint.config import YamlLintConfigError | ||
|
||
|
||
class CustomTestCase(RuleTestCase): | ||
rule_id = 'custom' | ||
|
||
@classmethod | ||
def setUpClass(self): | ||
self.tmpd = tempfile.mkdtemp() | ||
rules = os.path.join(self.tmpd, '.yamllint', 'rules') | ||
os.makedirs(rules) | ||
|
||
with open(os.path.join(rules, '__init__.py'), 'w'): | ||
pass | ||
|
||
with open(os.path.join(rules, 'custom.py'), 'w') as f: | ||
f.write("""ID = 'custom' | ||
TYPE = 'token' | ||
def check(*args, **kwargs): | ||
if 0: | ||
yield | ||
""") | ||
|
||
self.orig_cwd = os.getcwd() | ||
os.chdir(self.tmpd) | ||
|
||
def test_disabled(self): | ||
conf = 'custom: disable\n' | ||
|
||
self.check('---\n', conf) | ||
|
||
def test_enabled(self): | ||
conf = 'custom: enable\n' | ||
|
||
self.check('---\n', conf) | ||
|
||
def test_config_present(self): | ||
conf = 'custom: enable\n' | ||
|
||
self.check('---\n', conf) | ||
|
||
def test_config_missing(self): | ||
conf = '' | ||
|
||
with self.assertRaises(YamlLintConfigError): | ||
self.check('---\n', conf) | ||
|
||
@classmethod | ||
def tearDownClass(self): | ||
os.chdir(self.orig_cwd) | ||
shutil.rmtree(self.tmpd) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters