Skip to content

Commit 60a223f

Browse files
committed
YAMLLintBear: Make document-start rule disabled
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. Closes #923
1 parent a404eba commit 60a223f

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

bears/yml/YAMLLintBear.py

+24-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,27 @@ 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 = {
33+
'extends': 'default',
34+
'rules': {
35+
'document-start': {
36+
'present': False
37+
}
38+
}
39+
}
40+
if document_start:
41+
yamllint_configs['rules']['document-start']['present'] = True
42+
43+
return yaml.dump(yamllint_configs)
44+
2445
@staticmethod
2546
def create_arguments(filename, file, config_file, yamllint_config: str=''):
2647
"""
@@ -29,4 +50,6 @@ def create_arguments(filename, file, config_file, yamllint_config: str=''):
2950
args = ('-f', 'parsable', filename)
3051
if yamllint_config:
3152
args += ('--config=' + yamllint_config,)
53+
else:
54+
args += ('--config-file=' + config_file,)
3255
return args

tests/yml/YAMLLintBearTest.py

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

21+
no_start_yml_file = """receipt: Oz-Ware Purchase Invoice
22+
date: 2012-08-06
23+
customer:
24+
first_name: Dorothy
25+
family_name: Gale
26+
27+
items:
28+
- part_no: A4786
29+
descrip: Water Bucket (Filled)
30+
price: 1.47
31+
quantity: 4"""
32+
33+
with_start_yml_file = """---
34+
receipt: Oz-Ware Purchase Invoice
35+
date: 2012-08-06
36+
customer:
37+
first_name: Dorothy
38+
family_name: Gale
39+
40+
items:
41+
- part_no: A4786
42+
descrip: Water Bucket (Filled)
43+
price: 1.47
44+
quantity: 4
45+
..."""
46+
2147
config_file = """
2248
extends:
2349
default
@@ -42,3 +68,13 @@
4268
invalid_files=(),
4369
settings={
4470
'yamllint_config': conf_file})
71+
72+
YAMLLintBear3Test = verify_local_bear(YAMLLintBear,
73+
valid_files=(no_start_yml_file,),
74+
invalid_files=(with_start_yml_file,))
75+
76+
YAMLLintBear4Test = verify_local_bear(YAMLLintBear,
77+
valid_files=(with_start_yml_file,),
78+
invalid_files=(no_start_yml_file,),
79+
settings={
80+
'document_start': True})

0 commit comments

Comments
 (0)