-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtest_commands_yaml_set.py
1656 lines (1494 loc) · 43.7 KB
/
test_commands_yaml_set.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 pytest
from tests.conftest import create_temp_yaml_file, requireseyaml, old_eyaml_keys
class Test_yaml_set():
"""Tests for the yaml-set command-line interface."""
command = "yaml-set"
def test_no_options(self, script_runner):
result = script_runner.run([self.command])
assert not result.success, result.stderr
assert "the following arguments are required: -g/--change" in result.stderr
def test_no_input_file(self, script_runner):
result = script_runner.run([self.command, "--nostdin", "--change='/test'"])
assert not result.success, result.stderr
assert "There must be a YAML_FILE or STDIN document" in result.stderr
def test_no_input_param(self, script_runner):
result = script_runner.run([self.command, "--change='/test'", "no-such-file"])
assert not result.success, result.stderr
assert "Exactly one of the following must be set:" in result.stderr
def test_bad_yaml_file(self, script_runner):
result = script_runner.run([self.command, "--change='/test'", "--random=1", "no-such-file"])
assert not result.success, result.stderr
assert "File not found:" in result.stderr
def test_identical_saveto_change_error(self, script_runner):
result = script_runner.run([self.command, "--change='/test'", "--random=1", "--saveto='/test'", "no-such-file"])
assert not result.success, result.stderr
assert "Impossible to save the old value to the same YAML Path as the new value!" in result.stderr
def test_insufficient_randomness(self, script_runner):
result = script_runner.run([self.command, "--change='/test'", "--random=1", "--random-from=A", "no-such-file"])
assert not result.success, result.stderr
assert "The pool of random CHARS must have at least 2 characters" in result.stderr
def test_bad_privatekey(self, script_runner):
result = script_runner.run([self.command, "--change='/test'", "--random=1", "--privatekey=no-such-file", "no-such-file"])
assert not result.success, result.stderr
assert "EYAML private key is not a readable file" in result.stderr
def test_bad_publickey(self, script_runner):
result = script_runner.run([self.command, "--change='/test'", "--random=1", "--publickey=no-such-file", "no-such-file"])
assert not result.success, result.stderr
assert "EYAML public key is not a readable file" in result.stderr
def test_no_dual_stdin(self, script_runner):
result = script_runner.run([self.command, "--change='/test'", "--stdin", "-"])
assert not result.success, result.stderr
assert "Impossible to read both document and replacement value from STDIN" in result.stderr
def test_no_backup_stdin(self, script_runner):
result = script_runner.run([
self.command, "--change='/test'", "--backup", "-"])
assert not result.success, result.stderr
assert "applies only when reading from a file" in result.stderr
def test_bad_data_type_optional(self, script_runner, tmp_path_factory):
yaml_file = create_temp_yaml_file(tmp_path_factory, """---
boolean: false
""")
result = script_runner.run([
self.command, "--change=/boolean", "--value=NOT_BOOLEAN",
"--format=boolean", yaml_file])
assert not result.success, result.stderr
assert "Impossible to write 'NOT_BOOLEAN' as boolean." in result.stderr
def test_bad_data_type_mandatory(self, script_runner, tmp_path_factory):
yaml_file = create_temp_yaml_file(tmp_path_factory, """---
boolean: false
""")
result = script_runner.run([
self.command, "--change=/boolean", "--value=NOT_BOOLEAN",
"--format=boolean", "--mustexist", yaml_file])
assert not result.success, result.stderr
assert "Impossible to write 'NOT_BOOLEAN' as boolean." in result.stderr
def test_input_by_value(self, script_runner, tmp_path_factory):
import re
content = """---
key: value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command, "--change=/key", "--value=abc", yaml_file])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert re.findall(r"^key:\s+abc$", filedat, re.M), filedat
def test_input_by_stdin(self, tmp_path_factory):
import re
import subprocess
content = """---
key: value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = subprocess.run(
[self.command,
"--change=/key",
"--stdin",
yaml_file],
stdout=subprocess.PIPE,
input="abc".encode(),
)
assert 0 == result.returncode, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert re.findall(r"^key:\s+abc$", filedat, re.M), filedat
def test_input_by_file(self, script_runner, tmp_path_factory):
import re
content = """---
key: value
"""
input_file = create_temp_yaml_file(tmp_path_factory, "abc\n")
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=/key",
"--file={}".format(input_file),
yaml_file
])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert re.findall(r"^key:\s+abc$", filedat, re.M), filedat
def test_input_by_random(self, script_runner, tmp_path_factory):
import re
content = """---
key: value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=/key",
"--random=50",
yaml_file
])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert re.findall(r"^key:\s+[A-Za-z0-9]{50}$", filedat, re.M), filedat
def test_yaml_parsing_error(self, script_runner, imparsible_yaml_file):
result = script_runner.run([self.command, "--change=/", "--random=1", imparsible_yaml_file])
assert not result.success, result.stderr
assert "YAML parsing error" in result.stderr
def test_yaml_syntax_error(self, script_runner, badsyntax_yaml_file):
result = script_runner.run([self.command, "--change=/", "--random=1", badsyntax_yaml_file])
assert not result.success, result.stderr
assert "YAML syntax error" in result.stderr
def test_yaml_composition_error(self, script_runner, badcmp_yaml_file):
result = script_runner.run([self.command, "--change=/", "--random=1", badcmp_yaml_file])
assert not result.success, result.stderr
assert "YAML composition error" in result.stderr
def test_bad_yaml_path(self, script_runner, tmp_path_factory):
content = """---
key: value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
# Explicit --mustexist
result = script_runner.run([self.command, "--change=key2", "--random=1", "--mustexist", yaml_file])
assert not result.success, result.stderr
assert "Required YAML Path does not match any nodes" in result.stderr
# Implicit --mustexist via --saveto
result = script_runner.run([self.command, "--change=key3", "--random=1", "--saveto=save_here", yaml_file])
assert not result.success, result.stderr
assert "Required YAML Path does not match any nodes" in result.stderr
def test_checked_replace(self, script_runner, tmp_path_factory):
content = """---
key: value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=/key",
"--value=abc",
"--check=value",
yaml_file
])
assert result.success, result.stderr
@requireseyaml
def test_missing_key(self, script_runner, tmp_path_factory, old_eyaml_keys):
content = """---
encrypted: ENC[PKCS7,MIIx...blahblahblah...==]
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=encrypted",
"--random=1",
"--check=n/a",
"--privatekey={}".format(old_eyaml_keys[0]),
yaml_file
])
assert not result.success, result.stderr
assert "Neither or both private and public EYAML keys must be set" in result.stderr
result = script_runner.run([
self.command,
"--change=encrypted",
"--random=1",
"--check=n/a",
"--publickey={}".format(old_eyaml_keys[1]),
yaml_file
])
assert not result.success, result.stderr
assert "Neither or both private and public EYAML keys must be set" in result.stderr
@requireseyaml
def test_bad_decryption(self, script_runner, tmp_path_factory, old_eyaml_keys):
content = """---
encrypted: ENC[PKCS7,MIIx...broken-on-purpose...==]
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=encrypted",
"--random=1",
"--check=n/a",
"--privatekey={}".format(old_eyaml_keys[0]),
"--publickey={}".format(old_eyaml_keys[1]),
yaml_file
])
assert not result.success, result.stderr
assert "Unable to decrypt value!" in result.stderr
def test_bad_value_check(self, script_runner, tmp_path_factory):
content = """---
key: value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=key",
"--random=1",
"--check=abc",
yaml_file
])
assert not result.success, result.stderr
assert "does not match the check value" in result.stderr
def test_cannot_save_multiple_matches(self, script_runner, tmp_path_factory):
content = """---
key1: value1
key2: value2
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=/[.^key]",
"--random=1",
"--saveto=/backup",
yaml_file
])
assert not result.success, result.stderr
assert "It is impossible to meaningly save more than one" in result.stderr
def test_save_old_plain_value(self, script_runner, tmp_path_factory):
import re
content = """---
key: value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=key",
"--value=new",
"--saveto=backup",
yaml_file
])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert re.findall(r"^backup:\s+value$", filedat, re.M), filedat
def test_save_old_crypt_value(self, script_runner, tmp_path_factory):
import re
content = """---
encrypted: >
ENC[PKCS7,MIIB...]
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=encrypted",
"--value=now_plaintext",
"--saveto=backup",
yaml_file
])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert re.findall(r"^backup:\s+>$", filedat, re.M), filedat
def test_broken_change(self, script_runner, tmp_path_factory):
content = """---
key: value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=[0]",
"--random=1",
yaml_file
])
assert not result.success, result.stderr
assert "Cannot add" in result.stderr
def test_broken_saveto(self, script_runner, tmp_path_factory):
content = """---
key: value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=key",
"--random=1",
"--saveto=[2]",
yaml_file
])
assert not result.success, result.stderr
assert "Cannot add" in result.stderr
@requireseyaml
def test_good_encryption(self, script_runner, tmp_path_factory, old_eyaml_keys):
import re
content = """---
key: >
old
multiline
value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=key",
"--value=now_encrypted",
"--eyamlcrypt",
"--privatekey={}".format(old_eyaml_keys[0]),
"--publickey={}".format(old_eyaml_keys[1]),
yaml_file
])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert re.findall(r"\nkey:\s+>\n\s{2}ENC\[.+\n", filedat), filedat
@requireseyaml
def test_bad_crypt_path(self, script_runner, tmp_path_factory, old_eyaml_keys):
content = """---
key: value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=[0]",
"--random=1",
"--eyamlcrypt",
"--privatekey={}".format(old_eyaml_keys[0]),
"--publickey={}".format(old_eyaml_keys[1]),
yaml_file
])
assert not result.success, result.stderr
assert "Cannot add" in result.stderr
def test_bad_eyaml_command(self, script_runner, tmp_path_factory):
content = """---
key: value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
result = script_runner.run([
self.command,
"--change=key",
"--random=1",
"--eyamlcrypt",
"--eyaml=/does/not/exist/on-most/systems",
yaml_file
])
assert not result.success, result.stderr
assert "The eyaml binary is not executable" in result.stderr
def test_backup_file(self, script_runner, tmp_path_factory):
import os
content = """---
key: value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
backup_file = yaml_file + ".bak"
result = script_runner.run([
self.command,
"--change=key",
"--random=1",
"--backup",
yaml_file
])
assert result.success, result.stderr
assert os.path.isfile(backup_file)
with open(backup_file, 'r') as fhnd:
filedat = fhnd.read()
assert filedat == content
def test_replace_backup_file(self, script_runner, tmp_path_factory):
import os
content = """---
key: value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, content)
backup_file = yaml_file + ".bak"
with open(backup_file, 'w') as fhnd:
fhnd.write(content + "\nkey2: value2")
result = script_runner.run([
self.command,
"--change=key",
"--random=1",
"--backup",
yaml_file
])
assert result.success, result.stderr
assert os.path.isfile(backup_file)
with open(backup_file, 'r') as fhnd:
filedat = fhnd.read()
assert filedat == content
def test_nonref_changes(self, script_runner, tmp_path_factory):
yamlin = """---
somestring:
string:
someotherstring: true
otherstring:
default:
config:
deploy:
me: true
"""
yamlout = """---
somestring:
string:
someotherstring: true
otherstring:
default:
config:
deploy:
me: set_value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, yamlin)
result = script_runner.run([
self.command,
"--change=otherstring.default.config.deploy.me",
"--value=set_value",
yaml_file
])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert filedat == yamlout
@pytest.mark.xfail(strict=True, reason="https://sourceforge.net/p/ruamel-yaml/tickets/351/")
def test_commented_aliased_parent_hash(self, script_runner, tmp_path_factory):
yamlin = """---
aliases:
- &key_alias hash
*key_alias :
# Comment
key: value
"""
yamlout = """---
aliases:
- &key_alias hash
*key_alias :
# Comment
key: new value
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, yamlin)
result = script_runner.run([
self.command,
"--change=/hash/key",
"--value=new value",
"--backup",
yaml_file
])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert filedat == yamlout
def test_set_value_in_empty_file(
self, script_runner, tmp_path_factory
):
yaml_file = create_temp_yaml_file(tmp_path_factory, "")
result_content = """---
some:
key:
to: nowhere
"""
result = script_runner.run([
self.command
, "--change=some.key.to"
, "--value=nowhere"
, yaml_file])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert filedat == result_content
def test_set_value_in_json_file(
self, script_runner, tmp_path_factory
):
yaml_file = create_temp_yaml_file(tmp_path_factory, '{"key": "value"}')
result_content = '{"key": "changed"}'
result = script_runner.run([
self.command
, "--change=key"
, "--value=changed"
, yaml_file])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert filedat == result_content
def test_set_value_in_json_file_nonindented(
self, script_runner, tmp_path_factory
):
yaml_file = create_temp_yaml_file(tmp_path_factory, """{
"people": {
"name": "john",
"age": 30,
"state": {"name": "up"}
}
}""")
result_content = """{"people": {"name": "John Doe", "age": 30, "state": {"name": "up"}}}"""
result = script_runner.run([
self.command
, "--change=people.name"
, "--value=John Doe"
, "--json-indent=-4"
, yaml_file
])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert filedat == result_content
def test_set_value_in_json_file_indented(
self, script_runner, tmp_path_factory
):
yaml_file = create_temp_yaml_file(tmp_path_factory, """{
"people": {
"name": "john",
"age": 30,
"state": {"name": "up"}
}
}""")
result_content = """{
"people": {
"name": "John Doe",
"age": 30,
"state": {
"name": "up"
}
}
}"""
result = script_runner.run([
self.command
, "--change=people.name"
, "--value=John Doe"
, "--json-indent=4"
, yaml_file
])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert filedat == result_content
def test_stdin_to_stdout_yaml(self, script_runner):
import subprocess
stdin_content = """---
hash:
sub_hash:
key1: value 1.1
key2: value 2.1
another_sub_hash:
key1: value 1.2
key2: value 2.2
array:
- element 1
- element 2
"""
stdout_content = """---
hash:
sub_hash:
key1: CHANGE EVERYTHING!
key2: CHANGE EVERYTHING!
another_sub_hash:
key1: CHANGE EVERYTHING!
key2: CHANGE EVERYTHING!
array:
- CHANGE EVERYTHING!
- CHANGE EVERYTHING!
"""
result = subprocess.run(
[self.command
, "--change=**"
, "--value=CHANGE EVERYTHING!"
, "-"]
, stdout=subprocess.PIPE
, input=stdin_content
, universal_newlines=True
)
# DEBUG
# print("Expected:")
# print(merged_yaml_content)
# print("Got:")
# print(result.stdout)
assert 0 == result.returncode, result.stderr
assert stdout_content == result.stdout
def test_stdin_to_stdout_json(self, script_runner):
import subprocess
stdin_content = """{"hash": {"sub_hash": {"key1": "value 1.1", "key2": "value 2.1"}, "another_sub_hash": {"key1": "value 1.2", "key2": "value 2.2"}}, "array": ["element 1", "element 2"]}"""
stdout_content = """{"hash": {"sub_hash": {"key1": "CHANGE EVERYTHING!", "key2": "CHANGE EVERYTHING!"}, "another_sub_hash": {"key1": "CHANGE EVERYTHING!", "key2": "CHANGE EVERYTHING!"}}, "array": ["CHANGE EVERYTHING!", "CHANGE EVERYTHING!"]}"""
result = subprocess.run(
[self.command
, "--change=**"
, "--value=CHANGE EVERYTHING!"]
, stdout=subprocess.PIPE
, input=stdin_content
, universal_newlines=True
)
# DEBUG
# print("Expected:")
# print(merged_yaml_content)
# print("Got:")
# print(result.stdout)
assert 0 == result.returncode, result.stderr
assert stdout_content == result.stdout
def test_stdin_to_stdout_json_indented(self, script_runner):
import subprocess
stdin_content = """{
"people": {
"name": "john",
"age": 30,
"state": {"name": "up"}
}
}"""
stdout_content = """{
"people": {
"name": "John Doe",
"age": 30,
"state": {
"name": "up"
}
}
}"""
result = subprocess.run(
[self.command
, "--change=people.name"
, "--value=John Doe"
, "--json-indent=4"]
, stdout=subprocess.PIPE
, input=stdin_content
, universal_newlines=True
)
# DEBUG
# print("Expected:")
# print(merged_yaml_content)
# print("Got:")
# print(result.stdout)
assert 0 == result.returncode, result.stderr
assert stdout_content == result.stdout
def test_delete_key(self, script_runner, tmp_path_factory):
yamlin = """---
array:
- 0
- 1
- 2
- 3
array_of_arrays:
- - 0.0
- 0.1
- 0.2
- - 1.0
- 1.1
- 1.2
- - 2.0
- 2.1
- 2.2
- - 3.0
- 3.1
- 3.2
array_of_hashes:
- id: 0
name: zero
- id: 1
name: one
- id: 2
name: two
- id: 3
name: three
"""
yamlout = """---
array_of_arrays:
- - 0.0
- 0.1
- 0.2
- - 1.0
- 1.1
- 1.2
- - 2.0
- 2.1
- 2.2
- - 3.0
- 3.1
- 3.2
array_of_hashes:
- id: 0
name: zero
- id: 1
name: one
- id: 2
name: two
- id: 3
name: three
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, yamlin)
result = script_runner.run([
self.command,
"--change=/array",
"--delete",
yaml_file
])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert filedat == yamlout
def test_delete_from_collectors(self, script_runner, tmp_path_factory):
yamlin = """---
array:
- 0
- 1
- 2
- 3
array_of_arrays:
- - 0.0
- 0.1
- 0.2
- - 1.0
- 1.1
- 1.2
- - 2.0
- 2.1
- 2.2
- - 3.0
- 3.1
- 3.2
array_of_hashes:
- id: 0
name: zero
- id: 1
name: one
- id: 2
name: two
- id: 3
name: three
"""
yamlout = """---
array:
- 1
- 2
- 3
array_of_arrays:
- - 0.0
- 0.1
- 0.2
- []
- - 2.0
- 2.1
- 2.2
- - 3.0
- 3.1
- 3.2
array_of_hashes:
- id: 0
name: zero
- id: 1
name: one
- id: 3
name: three
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, yamlin)
result = script_runner.run([
self.command,
"--change=(/array[0])+(/array_of_arrays[1])+(/array_of_hashes[2])",
"--delete",
yaml_file
])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert filedat == yamlout
def test_delete_from_nested_collectors(self, script_runner, tmp_path_factory):
yamlin = """---
array:
- 0
- 1
- 2
- 3
array_of_arrays:
- - 0.0
- 0.1
- 0.2
- - 1.0
- 1.1
- 1.2
- - 2.0
- 2.1
- 2.2
- - 3.0
- 3.1
- 3.2
array_of_hashes:
- id: 0
name: zero
- id: 1
name: one
- id: 2
name: two
- id: 3
name: three
"""
yamlout = """---
array:
- 1
- 2
- 3
array_of_arrays:
- - 0.0
- 0.1
- 0.2
- []
- - 2.0
- 2.1
- 2.2
- - 3.0
- 3.1
- 3.2
array_of_hashes:
- id: 0
name: zero
- id: 1
name: one
- id: 3
name: three
"""
yaml_file = create_temp_yaml_file(tmp_path_factory, yamlin)
result = script_runner.run([
self.command,
"--change=((/array[0])+(/array_of_arrays[1])+(/array_of_hashes[2]))",
"--delete",
yaml_file
])
assert result.success, result.stderr
with open(yaml_file, 'r') as fhnd:
filedat = fhnd.read()
assert filedat == yamlout
def test_delete_from_too_too_nested_collectors(self, script_runner, tmp_path_factory):
yamlin = """---
array:
- 0
- 1
- 2
- 3
array_of_arrays:
- - 0.0
- 0.1
- 0.2
- - 1.0
- 1.1
- 1.2
- - 2.0
- 2.1
- 2.2
- - 3.0
- 3.1
- 3.2
array_of_hashes:
- id: 0
name: zero
- id: 1
name: one
- id: 2
name: two
- id: 3
name: three
"""
yamlout = """---
array:
- 1
- 2
- 3
array_of_arrays:
- - 0.0
- 0.1
- 0.2
- []
- - 2.0
- 2.1
- 2.2
- - 3.0
- 3.1
- 3.2
array_of_hashes:
- id: 0
name: zero
- id: 1
name: one
- id: 3
name: three
"""