/
test_services.py
214 lines (166 loc) · 4.11 KB
/
test_services.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""Tests the service layer."""
from textwrap import dedent
import pytest
from yamlfix.services import fix_code
true_strings = [
"TRUE",
"True",
"true",
"YES",
"Yes",
"yes",
"ON",
"On",
"on",
]
false_strings = [
"FALSE",
"False",
"false",
"NO",
"No",
"no",
"OFF",
"Off",
"off",
]
def test_fix_code_adds_header() -> None:
"""Adds the --- at the beginning of the source."""
source = "program: yamlfix"
fixed_source = dedent(
"""\
---
program: yamlfix"""
)
result = fix_code(source)
assert result == fixed_source
def test_fix_code_doesnt_double_the_header() -> None:
"""If source starts with --- don't add another line."""
source = dedent(
"""\
---
program: yamlfix"""
)
result = fix_code(source)
assert result == source
def test_fix_code_corrects_indentation_on_lists() -> None:
"""Use two spaces for indentation of lists."""
source = dedent(
"""\
---
hosts:
- item1
- item2"""
)
fixed_source = dedent(
"""\
---
hosts:
- item1
- item2"""
)
result = fix_code(source)
assert result == fixed_source
def test_fix_code_respects_parent_lists() -> None:
"""Do not indent lists at the first level."""
source = dedent(
"""\
---
- item1
- item2"""
)
result = fix_code(source)
assert result == source
def test_fix_code_preserves_comments() -> None:
"""Don't delete comments in the code."""
source = dedent(
"""\
---
# Keep comments!
program: yamlfix"""
)
result = fix_code(source)
assert result == source
def test_fix_code_respects_parent_lists_with_comments() -> None:
"""Do not indent lists at the first level even if there is a comment."""
source = dedent(
"""\
---
# Comment
- item1
- item2"""
)
result = fix_code(source)
assert result == source
def test_fix_code_removes_extra_apostrophes() -> None:
"""Remove not needed apostrophes."""
source = dedent(
"""\
---
title: 'Why we sleep'"""
)
fixed_source = dedent(
"""\
---
title: Why we sleep"""
)
result = fix_code(source)
assert result == fixed_source
@pytest.mark.parametrize("true_string", true_strings)
def test_fix_code_converts_non_valid_true_booleans(true_string: str) -> None:
"""Convert common strings that refer to true, but aren't the string `true`.
[More
info](https://yamllint.readthedocs.io/en/stable/rules.html#module-yamllint.rules.truthy)
"""
source = dedent(
f"""\
---
True dictionary: {true_string}
True list:
- {true_string}"""
)
fixed_source = dedent(
"""\
---
True dictionary: true
True list:
- true"""
)
result = fix_code(source)
assert result == fixed_source
@pytest.mark.parametrize("false_string", false_strings)
def test_fix_code_converts_non_valid_false_booleans(false_string: str) -> None:
"""Convert common strings that refer to false, but aren't the string `false`.
[More
info](https://yamllint.readthedocs.io/en/stable/rules.html#module-yamllint.rules.truthy)
"""
source = dedent(
f"""\
---
False dictionary: {false_string}
False list:
- {false_string}"""
)
fixed_source = dedent(
"""\
---
False dictionary: false
False list:
- false"""
)
result = fix_code(source)
assert result == fixed_source
@pytest.mark.parametrize("truthy_string", true_strings + false_strings)
def test_fix_code_respects_apostrophes_for_truthy_substitutions(
truthy_string: str,
) -> None:
"""Keep apostrophes for strings like `yes` or `true`.
So they are not converted to booleans.
"""
source = dedent(
f"""\
---
title: '{truthy_string}'"""
)
result = fix_code(source)
assert result == source