@@ -47,31 +47,72 @@ 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
68+
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
71+
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
6274
6375 expected = """\
64- a: # comment
65- # comment
76+ a: # comment 1
77+ # comment 2
6678 b:
67- d: 1 # comment
68- c: '2' # comment
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
6986"""
7087 actual = self ._read_file (test_file )
7188 self .assertEqual (expected , actual )
7289
73- with pytest .raises (KeyError ):
74- updated = update_yaml_file (test_file , "a.x" , "foo" )
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 ))
93+
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 ))
97+
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 ))
113+
114+ actual = self ._read_file (test_file )
115+ self .assertEqual (expected , actual )
75116
76117 def test_merge_yaml_element (self ):
77118 test_file = self ._create_file (
0 commit comments