@@ -47,71 +47,70 @@ def test_yaml_dump(self):
4747 def test_update_yaml_file (self ):
4848 test_file = self ._create_file (
4949 """\
50- a: # comment
51- # comment
50+ a: # comment 1
51+ # comment 2
5252 b:
53- d: 1 # comment
54- c: 2 # comment"""
53+ d: 1 # comment 3
54+ c: 2 # comment 4
55+ e:
56+ - f: 3 # comment 5
57+ g: 4 # comment 6
58+ - [hello, world] # comment 7
59+ - foo: # comment 8
60+ bar # comment 9"""
5561 )
5662
57- updated = update_yaml_file (test_file , "a.b.c" , "2" )
58- self .assertTrue ( updated )
63+ self . assertTrue ( update_yaml_file (test_file , "a.b.c" , "2" ) )
64+ self .assertFalse ( update_yaml_file ( test_file , "a.b.c" , "2" )) # already updated
5965
60- updated = update_yaml_file (test_file , "a.b.c " , "2" )
61- self .assertFalse (updated ) # already updated
66+ self . assertTrue ( update_yaml_file (test_file , "a.e.[0].g " , 42 ) )
67+ self .assertFalse (update_yaml_file ( test_file , "a.e.[0].g" , 42 ) ) # already updated
6268
63- expected = """\
64- a: # comment
65- # comment
66- b:
67- d: 1 # comment
68- c: '2' # comment
69- """
70- actual = self ._read_file (test_file )
71- self .assertEqual (expected , actual )
69+ self .assertTrue (update_yaml_file (test_file , "a.e.[1].[1]" , "tester" ))
70+ self .assertFalse (update_yaml_file (test_file , "a.e.[1].[1]" , "tester" )) # already updated
7271
73- with pytest .raises (KeyError ):
74- updated = update_yaml_file (test_file , "a.x" , "foo" )
75- updated = update_yaml_file (test_file , "a.x" , "foo" , create_new = True )
76- self .assertTrue (updated )
72+ self .assertTrue (update_yaml_file (test_file , "a.e.[2]" , "replaced object" ))
73+ self .assertFalse (update_yaml_file (test_file , "a.e.[2]" , "replaced object" )) # already updated
7774
7875 expected = """\
79- a: # comment
80- # comment
76+ a: # comment 1
77+ # comment 2
8178 b:
82- d: 1 # comment
83- c: '2' # comment
84- x: foo
79+ d: 1 # comment 3
80+ c: '2' # comment 4
81+ e:
82+ - f: 3 # comment 5
83+ g: 42 # comment 6
84+ - [hello, tester] # comment 7
85+ - replaced object
8586"""
8687 actual = self ._read_file (test_file )
8788 self .assertEqual (expected , actual )
8889
89- with pytest .raises (KeyError ):
90- updated = update_yaml_file (test_file , "a.x.z" , "foo_z" )
91- updated = update_yaml_file (test_file , "a.x.z" , "foo_z" , create_new = True )
92- self .assertTrue (updated )
90+ with pytest .raises (KeyError ) as ex :
91+ update_yaml_file (test_file , "x.y" , "foo" )
92+ self .assertEqual ("\" Key 'x' not found in YAML!\" " , str (ex .value ))
9393
94- with pytest .raises (KeyError ):
95- updated = update_yaml_file (test_file , "a.x.y" , "foo_y" )
96- updated = update_yaml_file (test_file , "a.x.y" , "foo_y" , create_new = True )
97- self .assertTrue (updated )
94+ with pytest .raises (KeyError ) as ex :
95+ update_yaml_file (test_file , "[42].y" , "foo" )
96+ self .assertEqual ("\" Key '[42]' not found in YAML!\" " , str (ex .value ))
9897
99- with pytest .raises (KeyError ):
100- updated = update_yaml_file (test_file , "a.x.y.z" , "foo_y_z" )
101- updated = update_yaml_file (test_file , "a.x.y.z" , "foo_y_z" , create_new = True )
102- self .assertTrue (updated )
98+ with pytest .raises (KeyError ) as ex :
99+ update_yaml_file (test_file , "a.x" , "foo" )
100+ self .assertEqual ("\" Key 'a.x' not found in YAML!\" " , str (ex .value ))
101+
102+ with pytest .raises (KeyError ) as ex :
103+ update_yaml_file (test_file , "a.[42]" , "foo" )
104+ self .assertEqual ("\" Key 'a.[42]' not found in YAML!\" " , str (ex .value ))
105+
106+ with pytest .raises (KeyError ) as ex :
107+ update_yaml_file (test_file , "a.e.[3]" , "foo" )
108+ self .assertEqual ("\" Key 'a.e.[3]' not found in YAML!\" " , str (ex .value ))
109+
110+ with pytest .raises (KeyError ) as ex :
111+ update_yaml_file (test_file , "a.e.[2].[2]" , "foo" )
112+ self .assertEqual ("\" Key 'a.e.[2].[2]' not found in YAML!\" " , str (ex .value ))
103113
104- expected = """\
105- a: # comment
106- # comment
107- b:
108- d: 1 # comment
109- c: '2' # comment
110- x:
111- z: foo_z
112- y:
113- z: foo_y_z
114- """
115114 actual = self ._read_file (test_file )
116115 self .assertEqual (expected , actual )
117116
0 commit comments