diff --git a/.generator/cli.py b/.generator/cli.py index d3c5932e54c1..1b1328e474e4 100644 --- a/.generator/cli.py +++ b/.generator/cli.py @@ -223,6 +223,23 @@ def _prepare_new_library_config(library_config: Dict) -> Dict: return library_config +def _create_new_changelog_for_library(library_id: str, output: str): + """Creates a new changelog for the library. + Args: + library_id(str): The id of the library. + output(str): Path to the directory in the container where code + should be generated. + """ + package_changelog_path = f"{output}/packages/{library_id}/CHANGELOG.md" + docs_changelog_path = f"{output}/packages/{library_id}/docs/CHANGELOG.md" + + os.makedirs(os.path.dirname(package_changelog_path), exist_ok=True) + _write_text_file(package_changelog_path, "# Changelog\n") + + os.makedirs(os.path.dirname(docs_changelog_path), exist_ok=True) + _write_text_file(docs_changelog_path, "# Changelog\n") + + def handle_configure( librarian: str = LIBRARIAN_DIR, source: str = SOURCE_DIR, @@ -267,6 +284,10 @@ def handle_configure( ) prepared_config = _prepare_new_library_config(new_library_config) + # Create a `CHANGELOG.md` and `docs/CHANGELOG.md` file for the new library + library_id = _get_library_id(prepared_config) + _create_new_changelog_for_library(library_id, output) + # Write the new library configuration to configure-response.json. _write_json_file(f"{librarian}/configure-response.json", prepared_config) diff --git a/.generator/test_cli.py b/.generator/test_cli.py index 9e1ad578f688..f51946ac57e9 100644 --- a/.generator/test_cli.py +++ b/.generator/test_cli.py @@ -71,6 +71,7 @@ _write_json_file, _write_text_file, _copy_readme_to_docs, + _create_new_changelog_for_library, handle_build, handle_configure, handle_generate, @@ -293,6 +294,7 @@ def test_handle_configure_success(mock_configure_request_file, mocker): mock_prepare_config = mocker.patch( "cli._prepare_new_library_config", return_value={"id": "prepared"} ) + mock_create_changelog = mocker.patch("cli._create_new_changelog_for_library") handle_configure() @@ -310,6 +312,28 @@ def test_handle_configure_no_new_library(mocker): with pytest.raises(ValueError, match="Configuring a new library failed."): handle_configure() +def test_create_new_changelog_for_library(mocker): + """Tests that the changelog files are created correctly.""" + library_id = "google-cloud-language" + output = "output" + mock_makedirs = mocker.patch("os.makedirs") + mock_write_text_file = mocker.patch("cli._write_text_file") + + _create_new_changelog_for_library(library_id, output) + + package_changelog_path = f"{output}/packages/{library_id}/CHANGELOG.md" + docs_changelog_path = f"{output}/packages/{library_id}/docs/CHANGELOG.md" + + # Check that makedirs was called for both parent directories + mock_makedirs.assert_any_call(os.path.dirname(package_changelog_path), exist_ok=True) + mock_makedirs.assert_any_call(os.path.dirname(docs_changelog_path), exist_ok=True) + assert mock_makedirs.call_count == 2 + + # Check that the files were "written" with the correct content + mock_write_text_file.assert_any_call(package_changelog_path, "# Changelog\n") + mock_write_text_file.assert_any_call(docs_changelog_path, "# Changelog\n") + assert mock_write_text_file.call_count == 2 + def test_get_new_library_config_found(mock_configure_request_data): """Tests that the new library configuration is returned when found."""