Skip to content

Commit 5c5a6e1

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 4332b32 commit 5c5a6e1

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

bears/yml/YAMLLintBear.py

+24
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',
@@ -21,6 +22,27 @@ class YAMLLintBear:
2122
LICENSE = 'AGPL-3.0'
2223
CAN_DETECT = {'Syntax', 'Formatting'}
2324

25+
@staticmethod
26+
def generate_config(filename, file,
27+
document_start: bool=False):
28+
"""
29+
:param document_start:
30+
Use this rule to require or forbid the use of document start
31+
marker (---).
32+
"""
33+
yamllint_configs = {
34+
'extends': 'default',
35+
'rules': {
36+
'document-start': {
37+
'present': False
38+
}
39+
}
40+
}
41+
if document_start:
42+
yamllint_configs['rules']['document-start']['present'] = True
43+
44+
return yaml.dump(yamllint_configs)
45+
2446
@staticmethod
2547
def create_arguments(filename, file, config_file, yamllint_config: str=''):
2648
"""
@@ -29,4 +51,6 @@ def create_arguments(filename, file, config_file, yamllint_config: str=''):
2951
args = ('-f', 'parsable', filename)
3052
if yamllint_config:
3153
args += ('--config=' + yamllint_config,)
54+
else:
55+
args += ('--config-file=' + config_file,)
3256
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)