/
test_configupdater.py
1618 lines (1293 loc) 路 39.7 KB
/
test_configupdater.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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import configparser
import copy
import os.path
from configparser import ConfigParser
from io import StringIO
from textwrap import dedent
import pytest
from configupdater import (
AlreadyAttachedError,
AssignMultilineValueError,
Comment,
ConfigUpdater,
DuplicateOptionError,
DuplicateSectionError,
MissingSectionHeaderError,
NoConfigFileReadError,
NoneValueDisallowed,
NoOptionError,
NoSectionError,
NotAttachedError,
Option,
ParsingError,
Section,
Space,
)
def test_read_write_no_changes(setup_cfg_path, setup_cfg):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
assert str(updater) == setup_cfg
def test_read_file_like(setup_cfg_path):
updater = ConfigUpdater()
with open(setup_cfg_path) as fp:
updater.read_file(fp)
fp = StringIO()
updater.read_file(fp)
def test_update_no_changes(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
old_mtime = os.path.getmtime(setup_cfg_path)
updater.update_file()
new_mtime = os.path.getmtime(setup_cfg_path)
assert old_mtime != new_mtime
def test_updater_to_dict(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
parser = ConfigParser()
parser.read(setup_cfg_path)
parser_dict = {sect: dict(parser[sect]) for sect in parser.sections()}
assert updater.to_dict() == parser_dict
def test_update_no_cfg():
updater = ConfigUpdater()
with pytest.raises(NoConfigFileReadError):
updater.update_file()
def test_update_file_issue_68(tmp_path):
# allow_no_value = False
file = tmp_path / "file.cfg"
file.write_text("[section]")
cfg = ConfigUpdater(allow_no_value=False).read_file(open(file))
cfg.set("section", "option")
with pytest.warns(NoneValueDisallowed):
# A warning is issued, but the method will not fail
cfg.update_file()
assert file.read_text().strip() == "[section]"
# allow_no_value = True
file.write_text("[section]")
cfg = ConfigUpdater(allow_no_value=True).read_file(open(file))
cfg.set("section", "option")
cfg.update_file()
assert file.read_text().strip() == "[section]\noption"
def test_str(setup_cfg_path, setup_cfg):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
output = str(updater)
assert output == setup_cfg
def test_has_section(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
assert updater.has_section("metadata")
assert not updater.has_section("nonexistent_section")
def test_contains_section(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
assert "metadata" in updater
def test_sections(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
exp_sections = [
"metadata",
"options",
"options.packages.find",
"options.extras_require",
"test",
"tool:pytest",
"aliases",
"bdist_wheel",
"build_sphinx",
"devpi:upload",
"flake8",
"pyscaffold",
]
assert updater.sections() == exp_sections
def test_len_updater(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
# we test the number of blocks, not sections
assert len(updater) == 14
def test_iter_section(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
# iter_blocks will give us everything, iter_sections just the section objects and
# __iter__ should work as iter_blocks
assert len([block for block in updater.iter_blocks()]) == 14
assert len([block for block in updater.iter_sections()]) == 12
assert len([entry for entry in updater]) == 12
def test_iter_items_section(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
result = list(k for k, v in updater["metadata"].items())
assert result == [
"name",
"description",
"author",
"author-email",
"license",
"url",
"long-description",
"platforms",
"classifiers",
]
def test_get_section(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
section = updater["metadata"]
assert section._structure
with pytest.raises(KeyError):
updater["non_existent_section"]
# `get_section` is equivalent to dict.get
assert updater.get_section("non_existent_section") is None
default = {}
assert updater.get_section("non_existent_section", default) is default
def test_section_to_dict(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
parser = ConfigParser()
parser.read(setup_cfg_path)
updater_dict = updater["metadata"].to_dict()
parser_dict = dict(parser["metadata"])
assert updater_dict == parser_dict
def test_has_option(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
assert updater.has_option("metadata", "author")
assert not updater.has_option("metadata", "coauthor")
assert not updater.has_option("nonexistent_section", "key")
def test_contains_options(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
section = updater["metadata"]
assert "author" in section
assert "wrong_option" not in section
def test_len_section(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
section = updater["metadata"]
# we test the number of blocks in section
assert len(section) == 12
def test_len_option(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
option = updater["metadata"]["classifiers"]
assert len(option.lines) == 3
def test_iter_option(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
section = updater["metadata"]
# iter_blocks will give us everything, iter_options just the option objects and
# __iter__ should work as iter_blocks
assert len([entry for entry in section.iter_blocks()]) == 12
assert len([entry for entry in section.iter_options()]) == 9
assert len([entry for entry in section]) == 9
def test_get_options(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
options = updater.options("options.packages.find")
exp_options = ["where", "exclude"]
assert options == exp_options
with pytest.raises(NoSectionError):
updater.options("non_existent_section")
# Section objects also have a "get" method similar to dict
section = updater["metadata"]
assert section.get("name").value == "configupdater"
assert section.get("non_existent_option") is None
assert section.get("non_existent_option", []) == []
def test_items(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
sect_names, sects = zip(*updater.items())
exp_sect_namess = [
"metadata",
"options",
"options.packages.find",
"options.extras_require",
"test",
"tool:pytest",
"aliases",
"bdist_wheel",
"build_sphinx",
"devpi:upload",
"flake8",
"pyscaffold",
]
assert list(sect_names) == exp_sect_namess
assert all([isinstance(s, Section) for s in sects])
opt_names, opts = zip(*updater.items("devpi:upload"))
exp_opt_names = ["no-vcs", "formats"]
assert list(opt_names) == exp_opt_names
assert all([isinstance(o, Option) for o in opts])
def test_get_method(setup_cfg_path):
updater = ConfigUpdater()
updater.read(setup_cfg_path)
value = updater.get("metadata", "license").value
assert value == "mit"
with pytest.raises(NoSectionError):
updater.get("non_existent_section", "license")
with pytest.raises(NoOptionError):
updater.get("metadata", "wrong_key")
assert updater.get("metadata", "wrong_key", fallback=None) is None
test1_cfg_in = """
[default]
key = 1
"""
test1_cfg_out = """
[default]
key = 2
"""
test1_cfg_out_added = """
[default]
key = 1
other_key = 3
"""
test1_cfg_out_none = """
[default]
key
"""
test1_cfg_out_removed = """
[default]
"""
test1_cfg_out_values = """
[default]
key =
param1
param2
"""
def test_get_option():
updater = ConfigUpdater()
updater.read_string(test1_cfg_in)
updater.read_string(test1_cfg_in)
option = updater["default"]["key"]
assert option.value == "1"
with pytest.raises(KeyError):
updater["default"]["wrong_key"]
def test_value_change():
updater = ConfigUpdater()
updater.read_string(test1_cfg_in)
assert updater["default"]["key"].value == "1"
updater["default"]["key"].value = "2"
assert str(updater) == test1_cfg_out
# try another way
section = updater["default"]
section["key"] = "1"
assert str(updater) == test1_cfg_in
def test_remove_option():
updater = ConfigUpdater()
updater.read_string(test1_cfg_in)
updater.remove_option("default", "key")
assert str(updater) == test1_cfg_out_removed
updater.remove_option("default", "non_existent_key")
updater.read_string(test1_cfg_in)
del updater["default"]["key"]
assert str(updater) == test1_cfg_out_removed
with pytest.raises(NoSectionError):
updater.remove_option("wrong_section", "key")
def test_set_option():
updater = ConfigUpdater(allow_no_value=True)
updater.read_string(test1_cfg_in)
updater.set("default", "key", "1")
assert updater["default"]["key"].value == "1"
updater.set("default", "key", 2)
assert updater["default"]["key"].value == 2
assert str(updater) == test1_cfg_out
updater.set("default", "key")
assert updater["default"]["key"].value is None
assert str(updater) == test1_cfg_out_none
updater.read_string(test1_cfg_in)
updater.set("default", "other_key", 3)
assert str(updater) == test1_cfg_out_added
updater.read_string(test1_cfg_in)
values = ["param1", "param2"]
updater["default"]["key"].set_values(values)
assert str(updater) == test1_cfg_out_values
assert values == ["param1", "param2"] # non destructive operation
with pytest.raises(NoSectionError):
updater.set("wrong_section", "key", "1")
new_option = copy.deepcopy(updater["default"]["key"])
updater["default"]["key"] = new_option
assert updater["default"]["key"] is new_option
new_option = copy.deepcopy(updater["default"]["key"])
new_option.key = "wrong_key"
with pytest.raises(ValueError):
updater["default"]["key"] = new_option
def test_section_set_option():
updater = ConfigUpdater(allow_no_value=True)
updater.read_string(test1_cfg_in)
default_sec = updater["default"]
default_sec.set("key", "1")
assert default_sec["key"].value == "1"
default_sec.set("key", 2)
assert default_sec["key"].value == 2
assert str(default_sec) == test1_cfg_out[1:]
default_sec.set("key")
assert default_sec["key"].value is None
assert str(default_sec) == test1_cfg_out_none[1:]
updater.read_string(test1_cfg_in)
default_sec = updater["default"]
default_sec.set("other_key", 3)
assert str(default_sec) == test1_cfg_out_added[1:]
updater.read_string(test1_cfg_in)
default_sec = updater["default"]
values = ["param1", "param2"]
default_sec["key"].set_values(values)
assert str(default_sec) == test1_cfg_out_values[1:]
assert values == ["param1", "param2"] # non destructive operation
def test_del_option():
updater = ConfigUpdater()
updater.read_string(test1_cfg_in)
del updater["default"]["key"]
assert str(updater) == "\n[default]\n"
with pytest.raises(KeyError):
del updater["default"]["key"]
test2_cfg_in = """
[section1]
key = 1
[section2]
key = 2
"""
test2_cfg_out = """
[section1]
key = 1
"""
def test_del_section():
updater = ConfigUpdater()
updater.read_string(test2_cfg_in)
del updater["section2"]
assert str(updater) == test2_cfg_out
with pytest.raises(KeyError):
del updater["section2"]
test_wrong_cfg = """
[strange section]
a
"""
def test_handle_error():
updater = ConfigUpdater(allow_no_value=False)
with pytest.raises(ParsingError):
updater.read_string(test_wrong_cfg)
def test_validate_format(setup_cfg_path):
updater = ConfigUpdater(allow_no_value=True)
updater.read(setup_cfg_path)
updater.validate_format(allow_no_value=False)
updater.set("metadata", "author")
with pytest.raises(ParsingError):
updater.validate_format(allow_no_value=False)
def test_validate_format_propagates_kwargs(setup_cfg_path):
updater = ConfigUpdater(allow_no_value=True)
updater.read(setup_cfg_path)
updater.set("pyscaffold", "namespace")
assert updater["pyscaffold"]["namespace"].value is None
assert str(updater["pyscaffold"]["namespace"]) == "namespace\n" # no `=` sign
assert updater.validate_format() is True
test3_cfg_in = """
[section]
key = 1
"""
test3_cfg_out = """
# comment of section
[section]
key = 1
# comment after section
"""
def test_add_before_after_comment():
updater = ConfigUpdater()
updater.read_string(test3_cfg_in)
updater["section"].add_before.comment("comment of section")
updater["section"].add_after.comment("# comment after section\n")
assert str(updater) == test3_cfg_out
test4_cfg_in = """
[section]
key1 = 1
"""
test4_cfg_out = """
[section]
key0 = 0
key1 = 1
key2
"""
def test_add_before_after_option():
updater = ConfigUpdater(allow_no_value=True)
updater.read_string(test4_cfg_in)
with pytest.raises(ValueError):
updater["section"].add_before.option("key0", 0)
updater["section"]["key1"].add_before.option("key0", 0)
updater["section"]["key1"].add_after.option("key2")
assert str(updater) == test4_cfg_out
test5_cfg_in = """
[section0]
key0 = 0
[section1]
key1 = 1
key2 = 2
"""
test5_cfg_out = """
[section0]
key0 = 0
[section1]
key1 = 1
key2 = 2
"""
def test_add_before_after_space():
updater = ConfigUpdater()
updater.read_string(test5_cfg_in)
updater["section1"].add_before.space(2)
updater["section1"]["key1"].add_after.space(1)
assert str(updater) == test5_cfg_out
test6_cfg_in = """
[section0]
key0 = 0
[section2]
key1 = 1
key2 = 2
"""
test6_cfg_out = """
[section0]
key0 = 0
[section1]
key1 = 42
[section2]
key1 = 1
key2 = 2
[section3]
"""
test6_cfg_out_new_sect = """
[section0]
key0 = 0
[section2]
key1 = 1
key2 = 2
[new_section]
key0 = 0
"""
def test_add_before_after_section():
updater = ConfigUpdater()
updater.read_string(test6_cfg_in)
with pytest.raises(ValueError):
updater["section2"]["key1"].add_before.section("section1")
updater["section2"].add_before.section("section1")
updater["section1"]["key1"] = 42
updater["section2"].add_after.space(1).section("section3")
assert str(updater) == test6_cfg_out
with pytest.raises(ValueError):
updater["section2"].add_before.section(updater["section2"]["key1"])
updater.read_string(test6_cfg_in)
sect_updater = ConfigUpdater()
sect_updater.read_string(test6_cfg_in)
section = sect_updater["section0"]
section.name = "new_section"
updater["section2"].add_after.section(section.detach())
assert str(updater) == test6_cfg_out_new_sect
with pytest.raises(DuplicateSectionError):
updater["section2"].add_after.section(section)
test7_cfg_in = """
[section0]
key0 = 0
[section1]
key1 = 42
"""
test7_cfg_out = """
[section0]
key0 = 0
[section1]
key1 = 42
[section2]
key1 = 1
"""
def test_add_section():
updater = ConfigUpdater()
updater.read_string(test7_cfg_in)
with pytest.raises(DuplicateSectionError):
updater.add_section("section1")
updater.add_section("section2")
updater["section2"]["key1"] = 1
assert str(updater) == test7_cfg_out
with pytest.raises(ValueError):
updater.add_section(updater["section2"]["key1"])
test6_cfg_out_overwritten = """
[section0]
key0 = 42
[section2]
key1 = 1
key2 = 2
"""
def test_set_item_section():
updater = ConfigUpdater()
sect_updater = ConfigUpdater()
updater.read_string(test6_cfg_in)
with pytest.raises(ValueError):
updater["section"] = "newsection"
sect_updater.read_string(test6_cfg_in)
section = sect_updater["section0"]
with pytest.raises(ValueError):
updater["new_section"] = section
section.name = "new_section"
updater["new_section"] = section.detach()
assert str(updater) == test6_cfg_out_new_sect
# test overwriting an existing section
updater.read_string(test6_cfg_in)
sect_updater.read_string(test6_cfg_in)
exist_section = sect_updater["section0"]
exist_section["key0"] = 42
updater["section0"] = exist_section.detach()
assert str(updater) == test6_cfg_out_overwritten
def test_no_space_around_delim():
updater = ConfigUpdater(space_around_delimiters=False)
updater.read_string(test7_cfg_in)
updater["section0"]["key0"] = 0
del updater["section1"]
assert str(updater) == "\n[section0]\nkey0=0\n\n"
def test_constructor(setup_cfg_path):
updater = ConfigUpdater(delimiters=(":", "="))
updater.read(setup_cfg_path)
updater = ConfigUpdater(delimiters=(":", "="), allow_no_value=True)
updater.read(setup_cfg_path)
test8_inline_prefixes = """
[section] # just a section
key = value # just a value
"""
def test_inline_comments():
updater = ConfigUpdater(inline_comment_prefixes="#")
updater.read_string(test8_inline_prefixes)
assert updater.has_section("section")
assert updater["section"]["key"].value == "value"
test9_dup_section = """
[section]
key = value
[section]
key = value
"""
def test_duplicate_section_error():
updater = ConfigUpdater()
with pytest.raises(DuplicateSectionError):
updater.read_string(test9_dup_section)
def test_missing_section_error():
updater = ConfigUpdater()
with pytest.raises(MissingSectionHeaderError):
updater.read_string("key = value")
test10_dup_option = """
[section]
key = value
key = value
"""
def test_duplicate_option_error():
updater = ConfigUpdater()
with pytest.raises(DuplicateOptionError):
updater.read_string(test10_dup_option)
test11_no_values = """
[section]
key
"""
def test_no_value():
updater = ConfigUpdater(allow_no_value=True)
updater.read_string(test11_no_values)
assert updater["section"]["key"].value is None
def test_eq(setup_cfg_path):
updater1 = ConfigUpdater()
updater1.read(setup_cfg_path)
updater2 = ConfigUpdater()
updater2.read(setup_cfg_path)
assert updater1 == updater2
updater1.remove_section("metadata")
assert updater1 != updater2
assert updater1 != updater2["metadata"]
assert updater2["metadata"] != updater2["metadata"]["author"]
assert not updater1.remove_section("metadata")
test12_cfg_in = """
[section]
opiton = 42
"""
test12_cfg_out = """
[section]
option = 42
"""
def test_rename_option_key():
updater = ConfigUpdater()
updater.read_string(test12_cfg_in)
updater["section"]["opiton"].key = "option"
test13_cfg_in = """
[section]
CAPITAL = 1
"""
def test_set_optionxform():
updater = ConfigUpdater()
updater.read_string(test13_cfg_in)
assert updater["section"]["capital"].value == "1"
assert callable(updater.optionxform)
updater.optionxform = lambda x: x
updater.read_string(test13_cfg_in)
assert updater["section"]["CAPITAL"].value == "1"
test14_cfg_in = """
[section]
option2 = 2
option4 = 4
"""
test14_cfg_out = """
[section]
option0 = 0
option1 = 1
option2 = 2
option3 = 3
option4 = 4
"""
def test_insert_at():
updater = ConfigUpdater()
updater.read_string(test14_cfg_in)
updater["section"].insert_at(0).option("option0", 0).option("option1", 1)
updater["section"].insert_at(3).option("option3", 3)
assert str(updater) == test14_cfg_out
def test_read_file_with_string():
updater = ConfigUpdater()
with pytest.raises(RuntimeError):
updater.read_file("path/to/file.cfg")
test15_cfg_in = """
[section]
OptionA = 2
"""
def test_read_mixed_case_options():
updater = ConfigUpdater()
updater.read_string(test15_cfg_in)
assert updater.has_option("section", "OptionA")
assert updater.has_option("section", "optiona")
assert updater["section"]["OptionA"].value == "2"
assert updater["section"]["optiona"].value == "2"
test16_cfg_in = """
[section]
OptionA = 2
"""
test16_cfg_out = """
[section]
OptionA = 4
OptionB = 6
"""
def test_update_mixed_case_options():
updater = ConfigUpdater()
updater.read_string(test16_cfg_in)
updater["section"]["optiona"].value = "4"
updater["section"]["OptionB"] = "6"
assert str(updater) == test16_cfg_out
test17_cfg_in = """
[section]
key1 = 1
"""
test17_cfg_out = """
[section]
key0 = 0
key1 = 1
key2 = 2
key3 = 3
"""
def test_add_before_then_add_after_option():
updater = ConfigUpdater()
updater.read_string(test17_cfg_in)
updater["section"]["key1"].add_before.option("key0", "0")
updater["section"]["key1"].add_after.option("key2", "2")
updater["section"]["key2"].add_after.option("key3", "3")
assert str(updater) == test17_cfg_out
test18_cfg_in = """
[section]
Key0 = 0
"""
test18_cfg_out = """
[section]
Key0 = 2
"""
def test_assure_no_duplicate_options():
updater = ConfigUpdater()
updater.read_string(test18_cfg_in)
updater["section"]["KEY0"].value = "1"
updater["section"]["keY0"] = "2"
assert str(updater) == test18_cfg_out
test19_cfg_in = """
[section]
Key0 = 0
"""
def test_no_duplicate_blocks_with_blockbuilder():
updater = ConfigUpdater()
updater.read_string(test19_cfg_in)
with pytest.raises(DuplicateOptionError):
updater["section"]["Key0"].add_after.option("key0", "1")
with pytest.raises(DuplicateSectionError):
updater["section"].add_after.section("section")
assert str(updater) == test19_cfg_in
# Taken from issue #14
test20_cfg_in = """
[flake8]
exclude =
# Trash and cache:
.git
__pycache__
.venv
.eggs
*.egg
temp
# Bad code that I write to test things:
ex.py
new = value
per-file-ignores =
# Disable imports in `__init__.py`:
lambdas/__init__.py: WPS226, WPS413
lambdas/contrib/mypy/lambdas_plugin.py: WPS437
# There are multiple assert's in tests:
tests/*.py: S101, WPS226, WPS432, WPS436, WPS450
# We need to write tests to our private class:
tests/test_math_expression/*.py: S101, WPS432, WPS450"""
def test_comments_in_multiline_options():
updater = ConfigUpdater()
updater.read_string(test20_cfg_in)
per_file_ignores = updater["flake8"]["per-file-ignores"].value
exp_val = (
"\n# Disable imports in `__init__.py`:\nlambdas/__init__.py: WPS226, WPS413\n"
"lambdas/contrib/mypy/lambdas_plugin.py: WPS437\n# There are multiple assert's"
" in tests:\ntests/*.py: S101, WPS226, WPS432, WPS436, WPS450\n# We need to"
" write tests to our private class:\ntests/test_math_expression/*.py: S101,"
" WPS432, WPS450"
)
updater.validate_format()
assert per_file_ignores == exp_val
assert test20_cfg_in == str(updater)
test21_cfg_in = """
[main1]
key1 =
a
b
c
d
[main2]
key1 =
a
b
c
d
[main3]
[main4]
key1 =
a
b
c
d
key2 =
# comment
a
b
# comment
c
d
[main5]
key1 =
a
b
c
d
key2 = abcd
[main6]
"""
def test_empty_lines_in_values_support():
updater = ConfigUpdater()
updater.read_string(test21_cfg_in)
parser = ConfigParser()
parser.read_string(test21_cfg_in)
assert updater["main1"]["key1"].value == parser["main1"]["key1"]
assert updater["main2"]["key1"].value == parser["main2"]["key1"]