/
test_services.py
739 lines (619 loc) · 18.6 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
"""Tests the service layer."""
import logging
from pathlib import Path
from textwrap import dedent
import pytest
from yamlfix import fix_files
from yamlfix.model import YamlfixConfig
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",
]
class TestFixFiles:
"""Test the fix_files function."""
def test_fix_files_can_process_string_arguments(self, tmp_path: Path) -> None:
"""
Given: A file to fix
When: Passing the string with the path to the file to fix_files
Then: The file is fixed
"""
test_file = tmp_path / "source.yaml"
test_file.write_text("program: yamlfix")
fixed_source = dedent(
"""\
---
program: yamlfix
"""
)
fix_files([str(test_file)], False) # act
assert test_file.read_text() == fixed_source
def test_fix_files_issues_warning(self, tmp_path: Path) -> None:
"""
Given: A file to fix
When: Using the old signature
Then: A warning is issued
"""
test_file = tmp_path / "source.yaml"
test_file.write_text("program: yamlfix")
with pytest.warns(UserWarning, match="yamlfix/pull/182"):
fix_files([str(test_file)]) # act
class TestFixCode:
"""Test the fix_code function."""
def test_fix_code_ignore_shebang(self) -> None:
"""Ignores shebang lines if present at the beginning of the source."""
source = dedent(
"""\
#! /this/line/should/be/ignored
---
program: yamlfix
"""
)
result = fix_code(source)
assert result == source
def test_fix_code_ignore_ansible_vaults(self) -> None:
"""Adds the --- at the beginning of the source."""
source = dedent(
"""\
$ANSIBLE_VAULT;1.1;AES256
3036303361343731386530393763...
"""
)
result = fix_code(source)
assert result == source
def test_fix_code_adds_header(self) -> 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(self) -> 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(self) -> None:
"""Use two spaces for indentation of lists."""
source = dedent(
"""\
---
hosts:
- item1
- item2
"""
)
fixed_source = dedent(
"""\
---
hosts:
- item1
- item2
"""
)
config = YamlfixConfig()
config.flow_style_sequence = None
result = fix_code(source, config)
assert result == fixed_source
def test_fix_code_respects_parent_lists(self) -> 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(self) -> 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_preserves_comments_without_start_indication(self) -> None:
"""Don't delete comments without yaml explicit start indictor."""
source = dedent(
"""\
# Keep comments!
program: yamlfix
"""
)
config = YamlfixConfig()
config.explicit_start = False
result = fix_code(source, config)
assert result == source
def test_fix_code_preserves_comment_only_file(self) -> None:
"""Don't delete comments even if the file is only comments."""
source = dedent(
"""\
---
# Keep comments!
"""
)
result = fix_code(source)
assert result == source
def test_fix_code_preserves_comment_only_files_without_start_indication(
self,
) -> None:
"""Don't delete comments even if the file is only comments, without\
start indication."""
source = dedent(
"""\
# Keep comments!
"""
)
config = YamlfixConfig()
config.explicit_start = False
result = fix_code(source, config)
assert result == source
def test_fix_code_respects_parent_lists_with_comments(self) -> 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
@pytest.mark.parametrize(
"code",
[
dedent(
"""\
---
- program:
# Keep comments!
"""
),
dedent(
"""\
---
- name: Setup SONiC Build Servers
hosts: sonic_build_server
vars:
# Keep comments!
build_user: build
"""
),
dedent(
"""\
---
tasks:
- name: Make sure repository is cloned # noqa latest[git]
"""
),
],
)
def test_fix_code_preserves_indented_comments(self, code: str) -> None:
"""Don't remove indentation from comments in the code."""
result = fix_code(code)
assert result == code
def test_fix_code_removes_extra_apostrophes(self) -> 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(self, 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(
self, 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(
self,
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
def test_fix_code_adds_space_in_comment(self) -> None:
"""Correct comments that don't have a space between
the # and the first character.
"""
source = dedent(
"""\
---
#This is a comment
project: yamlfix
"""
)
fixed_source = dedent(
"""\
---
# This is a comment
project: yamlfix
"""
)
result = fix_code(source)
assert result == fixed_source
def test_fix_code_not_add_extra_space_in_comment(self) -> None:
"""Respects comments that already have a space between
the # and the first character.
"""
source = dedent(
"""\
---
# This is a comment
project: yamlfix
"""
)
fixed_source = dedent(
"""\
---
# This is a comment
project: yamlfix
"""
)
result = fix_code(source)
assert result == fixed_source
def test_fix_code_add_space_inline_comment(self) -> None:
"""Fix inline comments that don't have a space between
the # and the first character.
"""
source = dedent(
"""\
---
project: yamlfix #This is a comment
"""
)
fixed_source = dedent(
"""\
---
project: yamlfix # This is a comment
"""
)
result = fix_code(source)
assert result == fixed_source
def test_fix_code_respects_url_anchors(self) -> None:
"""Comments that contain a url with an anchor should be respected."""
source = dedent(
"""\
---
# https://lyz-code.github.io/yamlfix/#usage
foo: bar
"""
)
result = fix_code(source)
assert result == source
def test_fix_code_add_extra_space_inline_comment(self) -> None:
"""Fix inline comments that don't have two spaces before
the #.
"""
source = dedent(
"""\
---
project: yamlfix # This is a comment
"""
)
fixed_source = dedent(
"""\
---
project: yamlfix # This is a comment
"""
)
result = fix_code(source)
assert result == fixed_source
def test_fix_code_doesnt_change_double_exclamation_marks(self) -> None:
"""Lines with starting double exclamation marks should be respected, otherwise
some programs like mkdocs-mermaidjs fail.
"""
source = dedent(
"""\
---
format: !!python/name:mermaid2.fence_mermaid
"""
)
result = fix_code(source)
assert result == source
def test_fix_code_parses_files_with_multiple_documents(self) -> None:
"""Files that contain multiple documents should be parsed as a collection of
separate documents and then dumped together again.
"""
source = dedent(
"""\
---
project: yamlfix
---
project: yamlfix
"""
)
result = fix_code(source)
assert result == source
def test_fix_code_functions_emit_debug_logs(
self,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Each fixer function should emit a log at the debug level in each run."""
caplog.set_level(logging.DEBUG)
fix_code("") # act
expected_logs = [
"Setting up ruamel yaml 'quote simple values' configuration...",
"Setting up ruamel yaml 'sequence flow style' configuration...",
"Running ruamel yaml base configuration...",
"Running source code fixers...",
"Fixing truthy strings...",
"Fixing comments...",
"Fixing jinja2 variables...",
"Running ruamel yaml fixer...",
"Restoring truthy strings...",
"Restoring double exclamations...",
"Restoring jinja2 variables...",
"Fixing top level lists...",
"Fixing flow-style lists...",
]
assert caplog.messages == expected_logs
for record in caplog.records:
assert record.levelname == "DEBUG"
@pytest.mark.parametrize("whitespace", ["", "\n", "\n\n"])
def test_fixed_code_has_exactly_one_newline_at_end_of_file(
self,
whitespace: str,
) -> None:
"""Files should have exactly one newline at the end to comply with the POSIX
standard.
"""
source = dedent(
"""\
---
program: yamlfix"""
)
source += whitespace
fixed_code = dedent(
"""\
---
program: yamlfix
"""
)
result = fix_code(source)
assert result == fixed_code
def test_anchors_and_aliases_with_duplicate_merge_keys(self) -> None:
"""All anchors and aliases should be preserved even with multiple merge keys
and merge keys should be formatted as a list in a single line.
"""
source = dedent(
"""\
---
x-node-volumes: &node-volumes
node3_data:
x-vault-volumes: &vault-volumes
vault_data:
x-mongo-volumes: &mongo-volumes
mongo_data:
x-certmgr-volumes: &certmgr-volumes
cert_data:
volumes:
<<: *node-volumes
<<: *vault-volumes
<<: *mongo-volumes
<<: *certmgr-volumes
"""
)
desired_source = dedent(
"""\
---
x-node-volumes: &node-volumes
node3_data:
x-vault-volumes: &vault-volumes
vault_data:
x-mongo-volumes: &mongo-volumes
mongo_data:
x-certmgr-volumes: &certmgr-volumes
cert_data:
volumes:
<<:
- *node-volumes
- *vault-volumes
- *mongo-volumes
- *certmgr-volumes
"""
)
config = YamlfixConfig()
config.allow_duplicate_keys = True
result = fix_code(source, config)
assert result == desired_source
def test_fix_code_respects_comment_symbol_in_strings_with_simple_quotes(
self,
) -> None:
"""
Given: Code with a string that contains a #
When: fix_code is run
Then: The string is left unchanged
"""
source = dedent(
"""\
---
project: 'Here # is not a comment marker'
"""
)
result = fix_code(source)
assert result == source
def test_fix_code_respects_comment_symbol_in_strings_with_double_quotes(
self,
) -> None:
"""
Given: Code with a string that contains a #
When: fix_code is run
Then: The string is left unchanged
"""
source = dedent(
"""\
---
project: "Here # is not a comment marker"
"""
)
desired_source = dedent(
"""\
---
project: 'Here # is not a comment marker'
"""
)
result = fix_code(source)
assert result == desired_source
def test_fix_code_respects_jinja_variables_with_equals(
self,
) -> None:
"""
Given: Code with a long string that contains a jinja variable after an equal
When: fix_code is run
Then: The jinja string is not broken
"""
source = (
"---\n"
"environment:\n"
" - SUPER_LONG_VARIABLE={{ this_is_a_super_long_long_long_long_long"
"_variable_name }}\n"
)
result = fix_code(source)
assert result == source
def test_fix_code_respects_many_jinja_variables(
self,
) -> None:
"""
Given: Code with a long string that contains two jinja variables
When: fix_code is run
Then: The jinja string is not broken
"""
source = (
"---\n"
"project: This is a long long {{ variable_1 }} line that should not be "
"split on the jinja {{ variable_2 }}"
)
desired_source = (
"---\n"
"project: This is a long long {{ variable_1 }} line that should not be "
"split on the\n"
" jinja {{ variable_2 }}\n"
)
result = fix_code(source)
assert result == desired_source
def test_fix_code_respects_jinja_variables_with_operations(
self,
) -> None:
"""
Given: Code with a long string that contains a jinja variable with operations
When: fix_code is run
Then: The jinja string is not broken
"""
source = (
"---\n"
"project: This is a long long long long line that should not be split on "
"the jinja {{ variable that contains different words }}"
)
desired_source = (
"---\n"
"project: This is a long long long long line that should not be split on "
"the jinja\n"
" {{ variable that contains different words }}\n"
)
result = fix_code(source)
assert result == desired_source
def test_fix_code_doesnt_remove_variable(
self,
) -> None:
"""
Given: Code with a short jinja variable
When: fix_code is run
Then: The jinja string is not removed
"""
source = "---\npath: '{{ item }}'\n"
result = fix_code(source)
assert result == source