Skip to content

Commit 1751d74

Browse files
committed
YAMLLintBear: Disable document-start rule by default
Disable YAMLLint rule `document-start` by default because many YAML files have no need for start and end markers, but user still can enable it by using `document_start=true` setting. Add unit tests for YAMLLintBear. Closes #923
1 parent 34b9bd5 commit 1751d74

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

bears/yml/YAMLLintBear.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from coalib.bearlib.abstractions.Linter import linter
22
from coalib.bears.requirements.PipRequirement import PipRequirement
3+
import yaml
34

45

56
@linter(executable='yamllint',
@@ -9,7 +10,6 @@
910
class YAMLLintBear:
1011
"""
1112
Check yaml code for errors and possible problems.
12-
1313
You can read more about capabilities at
1414
<http://yamllint.readthedocs.org/en/latest/rules.html>.
1515
"""
@@ -21,6 +21,25 @@ class YAMLLintBear:
2121
LICENSE = 'AGPL-3.0'
2222
CAN_DETECT = {'Syntax', 'Formatting'}
2323

24+
@staticmethod
25+
def generate_config(filename, file,
26+
document_start: bool=False):
27+
'''
28+
:param document_start:
29+
Use this rule to require or forbid the use of document start
30+
marker (---).
31+
'''
32+
yamllint_configs = {'extends': 'default',
33+
'rules':
34+
{'document-start':
35+
{'present': False}
36+
}
37+
}
38+
if document_start:
39+
yamllint_configs['rules']['document-start']['present'] = True
40+
41+
return yaml.dump(yamllint_configs)
42+
2443
@staticmethod
2544
def create_arguments(filename, file, config_file, yamllint_config: str=''):
2645
"""
@@ -29,4 +48,6 @@ def create_arguments(filename, file, config_file, yamllint_config: str=''):
2948
args = ('-f', 'parsable', filename)
3049
if yamllint_config:
3150
args += ('--config=' + yamllint_config,)
51+
else:
52+
args += ('--config-file=' + config_file,)
3253
return args

tests/yml/YAMLLintBearTest.py

+40
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,36 @@
1818
...
1919
"""
2020

21+
no_start_yml_file = """
22+
receipt: Oz-Ware Purchase Invoice
23+
date: 2012-08-06
24+
customer:
25+
first_name: Dorothy
26+
family_name: Gale
27+
28+
items:
29+
- part_no: A4786
30+
descrip: Water Bucket (Filled)
31+
price: 1.47
32+
quantity: 4
33+
"""
34+
35+
with_start_yml_file = """
36+
---
37+
receipt: Oz-Ware Purchase Invoice
38+
date: 2012-08-06
39+
customer:
40+
first_name: Dorothy
41+
family_name: Gale
42+
43+
items:
44+
- part_no: A4786
45+
descrip: Water Bucket (Filled)
46+
price: 1.47
47+
quantity: 4
48+
...
49+
"""
50+
2151
config_file = """
2252
extends:
2353
default
@@ -42,3 +72,13 @@
4272
invalid_files=(),
4373
settings={
4474
'yamllint_config': conf_file})
75+
76+
YAMLLintBear3Test = verify_local_bear(YAMLLintBear,
77+
valid_files=(no_start_yml_file,),
78+
invalid_files=())
79+
80+
YAMLLintBear4Test = verify_local_bear(YAMLLintBear,
81+
valid_files=(with_start_yml_file,),
82+
invalid_files=(),
83+
settings={
84+
'document_start': True})

0 commit comments

Comments
 (0)