@@ -159,29 +159,89 @@ def test_execute_ensures_lock_release_on_error(base_command, valid_config):
159159
160160
161161def test_save_config_success (base_command ):
162- """Test successful config saving ."""
162+ """Test successful config save ."""
163163 config = HelmValuesConfig ()
164164 config .version = "1.0"
165165 config .release = "test"
166166
167+ # Mock the validate method
168+ config .validate = MagicMock ()
169+
167170 with patch ("builtins.open" , mock_open ()) as mock_file :
168171 base_command .save_config (config )
169172
173+ # Verify validate was called
174+ config .validate .assert_called_once ()
175+
170176 mock_file .assert_called_once_with (base_command .config_file , "w" , encoding = "utf-8" )
171177 handle = mock_file ()
172178
179+ # Verify the written data
173180 written_json = "" .join (call .args [0 ] for call in handle .write .call_args_list )
174181 written_data = json .loads (written_json )
175- assert written_data ["version" ] == config . version
176- assert written_data ["release" ] == config . release
182+ assert written_data ["version" ] == "1.0"
183+ assert written_data ["release" ] == "test"
177184
178185
179186def test_save_config_io_error (base_command ):
180- """Test save_config when IO error occurs ."""
187+ """Test IO error handling when saving config ."""
181188 config = HelmValuesConfig ()
182- error_message = "Test error"
189+ config .version = "1.0"
190+ config .release = "test"
183191
184- with patch ("builtins.open" , mock_open ()) as mock_file :
185- mock_file .return_value .write .side_effect = IOError (error_message )
186- with pytest .raises (IOError , match = error_message ):
192+ # Mock the validate method
193+ config .validate = MagicMock ()
194+
195+ # Simulate an IO error
196+ mock_file = mock_open ()
197+ mock_file .side_effect = IOError ("Test IO Error" )
198+
199+ with patch ("builtins.open" , mock_file ):
200+ with pytest .raises (IOError ) as excinfo :
187201 base_command .save_config (config )
202+
203+ assert "Test IO Error" in str (excinfo .value )
204+
205+ # Verify validate was called
206+ config .validate .assert_called_once ()
207+
208+
209+ def test_save_config_validates_schema (base_command ):
210+ """Test that save_config validates the schema before saving."""
211+ # Create a mock config
212+ config = MagicMock (spec = HelmValuesConfig )
213+
214+ # Make validate method raise an error
215+ config .validate .side_effect = ValueError ("Schema validation failed" )
216+
217+ # Try to save the config
218+ with pytest .raises (ValueError , match = "Schema validation failed" ):
219+ base_command .save_config (config )
220+
221+ # Verify that validate was called
222+ config .validate .assert_called_once ()
223+
224+
225+ def test_save_config_with_empty_description (base_command , tmp_path ):
226+ """Test that save_config handles empty description correctly."""
227+ # Create a real config
228+ config = HelmValuesConfig ()
229+ config .release = "test"
230+
231+ # Add a config path with empty description (default)
232+ config .add_config_path ("test.path" )
233+
234+ # Set a temporary config file
235+ temp_file = tmp_path / "test_config.json"
236+ base_command .config_file = str (temp_file )
237+
238+ # Save the config
239+ base_command .save_config (config )
240+
241+ # Read the saved file
242+ with open (temp_file , "r" ) as f :
243+ data = json .load (f )
244+
245+ # Verify that description is an empty string
246+ assert data ["config" ][0 ]["description" ] == ""
247+ assert isinstance (data ["config" ][0 ]["description" ], str )
0 commit comments