4646 _get_libraries_to_prepare_for_release ,
4747 _get_new_library_config ,
4848 _get_previous_version ,
49+ _prepare_new_library_config ,
4950 _process_changelog ,
5051 _process_version_file ,
5152 _read_bazel_build_py_rule ,
@@ -267,36 +268,39 @@ def mock_state_file(tmp_path, monkeypatch):
267268 return request_file
268269
269270
270- def test_handle_configure (mock_configure_request_file , mocker ):
271+ def test_handle_configure_success (mock_configure_request_file , mocker ):
271272 """Tests the successful execution path of handle_configure."""
272273 mock_write_json = mocker .patch ("cli._write_json_file" )
274+ mock_prepare_config = mocker .patch (
275+ "cli._prepare_new_library_config" , return_value = {"id" : "prepared" }
276+ )
273277
274278 handle_configure ()
275279
276- # Verify that the correct configuration is written to the response file.
277- expected_config = {
278- "id" : "google-cloud-language" ,
279- "apis" : [{"path" : "google/cloud/language/v1" }],
280- }
280+ mock_prepare_config .assert_called_once ()
281281 mock_write_json .assert_called_once_with (
282- f"{ LIBRARIAN_DIR } /configure-response.json" , expected_config
282+ f"{ LIBRARIAN_DIR } /configure-response.json" , { "id" : "prepared" }
283283 )
284284
285285
286- def test_handle_configure_failure (mocker ):
287- """Tests that handle_configure raises ValueError on failure."""
286+ def test_handle_configure_no_new_library (mocker ):
287+ """Tests that handle_configure fails if no new library is found."""
288+ mocker .patch ("cli._read_json_file" , return_value = {"libraries" : []})
289+ # The call to _prepare_new_library_config with an empty dict will raise a ValueError
290+ # because _get_library_id will fail.
288291 with pytest .raises (ValueError , match = "Configuring a new library failed." ):
289292 handle_configure ()
290293
291294
292- def test_get_new_library_config_new_library_found (mock_configure_request_data ):
295+ def test_get_new_library_config_found (mock_configure_request_data ):
293296 """Tests that the new library configuration is returned when found."""
294297 config = _get_new_library_config (mock_configure_request_data )
295298 assert config ["id" ] == "google-cloud-language"
296- assert "status" not in config ["apis" ][0 ]
299+ # Assert that the config is NOT modified
300+ assert "status" in config ["apis" ][0 ]
297301
298302
299- def test_get_new_library_config_no_new_library ():
303+ def test_get_new_library_config_not_found ():
300304 """Tests that an empty dictionary is returned when no new library is found."""
301305 request_data = {
302306 "libraries" : [
@@ -313,6 +317,49 @@ def test_get_new_library_config_empty_input():
313317 assert config == {}
314318
315319
320+ def test_prepare_new_library_config ():
321+ """Tests the preparation of a new library's configuration."""
322+ raw_config = {
323+ "id" : "google-cloud-language" ,
324+ "apis" : [{"path" : "google/cloud/language/v1" , "status" : "new" }],
325+ "source_roots" : None ,
326+ "preserve_regex" : None ,
327+ "remove_regex" : None ,
328+ }
329+
330+ prepared_config = _prepare_new_library_config (raw_config )
331+
332+ # Check that status is removed
333+ assert "status" not in prepared_config ["apis" ][0 ]
334+ # Check that defaults are added
335+ assert prepared_config ["source_roots" ] == ["packages/google-cloud-language" ]
336+ assert "packages/google-cloud-language/CHANGELOG.md" in prepared_config ["preserve_regex" ]
337+ assert prepared_config ["remove_regex" ] == ["packages/google-cloud-language" ]
338+ assert prepared_config ["tag_format" ] == "{{id}}-v{{version}}"
339+
340+
341+ def test_prepare_new_library_config_preserves_existing_values ():
342+ """Tests that existing values in the config are not overwritten."""
343+ raw_config = {
344+ "id" : "google-cloud-language" ,
345+ "apis" : [{"path" : "google/cloud/language/v1" , "status" : "new" }],
346+ "source_roots" : ["packages/google-cloud-language-custom" ],
347+ "preserve_regex" : ["custom/regex" ],
348+ "remove_regex" : ["custom/remove" ],
349+ "tag_format" : "custom-format-{{version}}" ,
350+ }
351+
352+ prepared_config = _prepare_new_library_config (raw_config )
353+
354+ # Check that status is removed
355+ assert "status" not in prepared_config ["apis" ][0 ]
356+ # Check that existing values are preserved
357+ assert prepared_config ["source_roots" ] == ["packages/google-cloud-language-custom" ]
358+ assert prepared_config ["preserve_regex" ] == ["custom/regex" ]
359+ assert prepared_config ["remove_regex" ] == ["custom/remove" ]
360+ assert prepared_config ["tag_format" ] == "custom-format-{{version}}"
361+
362+
316363def test_get_library_id_success ():
317364 """Tests that _get_library_id returns the correct ID when present."""
318365 request_data = {"id" : "test-library" , "name" : "Test Library" }
0 commit comments