forked from coala/coala-bears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYAMLLintBearTest.py
136 lines (115 loc) · 4.76 KB
/
YAMLLintBearTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import unittest
from queue import Queue
from bears.yaml.YAMLLintBear import YAMLLintBear
from coala_utils.ContextManagers import prepare_file
from coalib.testing.LocalBearTestHelper import verify_local_bear, execute_bear
from coalib.testing.BearTestHelper import generate_skip_decorator
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
from coalib.settings.Section import Section
from coalib.settings.Setting import Setting
test_file = """
---
receipt: Oz-Ware Purchase Invoice
date: 2012-08-06
customer:
first_name: Dorothy
family_name: Gale
items:
- part_no: A4786
# - part_tag: A5679
descrip: Water Bucket (Filled)
price: 1.47
quantity: 4
...
"""
no_start_yaml_file = """receipt: Oz-Ware Purchase Invoice
date: 2012-08-06
customer:
first_name: Dorothy
family_name: Gale
items:
- part_no: A4786
descrip: Water Bucket (Filled)
price: 1.47
quantity: 4"""
with_start_yaml_file = """---
receipt: Oz-Ware Purchase Invoice
date: 2012-08-06
customer:
first_name: Dorothy
family_name: Gale
items:
- part_no: A4786
descrip: Water Bucket (Filled)
price: 1.47
quantity: 4
..."""
bad_max_line_length = """---
receipt: Oz-Ware This line-length is beyond 50 chars
..."""
config_file = """
extends:
default
rules:
colons:
max-spaces-after: -1
indentation: disable
empty-lines: disable
"""
YAMLLintBear1Test = verify_local_bear(YAMLLintBear,
valid_files=(),
invalid_files=(test_file,))
with prepare_file(config_file,
filename=None,
force_linebreaks=True,
create_tempfile=True) as (conf_lines, conf_file):
YAMLLintBear2Test = verify_local_bear(YAMLLintBear,
valid_files=(test_file,),
invalid_files=(),
settings={
'yamllint_config': conf_file})
YAMLLintBear3Test = verify_local_bear(YAMLLintBear,
valid_files=(no_start_yaml_file,
with_start_yaml_file,),
invalid_files=())
YAMLLintBear4Test = verify_local_bear(YAMLLintBear,
valid_files=(no_start_yaml_file,),
invalid_files=(with_start_yaml_file,),
settings={'document_start': False})
YAMLLintBear5Test = verify_local_bear(YAMLLintBear,
valid_files=(with_start_yaml_file,),
invalid_files=(no_start_yaml_file,),
settings={'document_start': True})
@generate_skip_decorator(YAMLLintBear)
class YAMLLintBearSeverityTest(unittest.TestCase):
def setUp(self):
self.section = Section('')
self.uut = YAMLLintBear(self.section, Queue())
def test_warning(self):
content = test_file.splitlines()
with prepare_file(content, None) as (file, fname):
with execute_bear(self.uut, fname, file) as results:
self.assertEqual(results[6].message, 'comment not indented'
' like content (comments-indentation)')
self.assertEqual(results[6].affected_code[0].start.line, 11)
self.assertEqual(results[6].affected_code[0].end.line, 11)
self.assertEqual(results[6].severity, RESULT_SEVERITY.NORMAL)
def test_error(self):
content = test_file.splitlines()
with prepare_file(content, None) as (file, fname):
with execute_bear(self.uut, fname, file) as results:
self.assertEqual(results[0].message, 'too many blank lines'
' (1 > 0) (empty-lines)')
self.assertEqual(results[0].affected_code[0].start.line, 1)
self.assertEqual(results[0].affected_code[0].end.line, 1)
self.assertEqual(results[0].severity, RESULT_SEVERITY.MAJOR)
def test_bad_max_line_length(self):
self.section.append(Setting('max_line_length', 50))
content = bad_max_line_length.splitlines()
with prepare_file(content, None) as (file, fname):
with execute_bear(self.uut, fname, file) as results:
self.assertEqual(results[0].message, 'line too long '
'(52 > 50 characters) (line-length)')
self.assertEqual(results[0].affected_code[0].start.line, 2)
self.assertEqual(results[0].affected_code[0].end.line, 2)
self.assertEqual(results[0].severity, RESULT_SEVERITY.MAJOR)