Skip to content

Commit 0b53f8e

Browse files
authored
chore(librarian): create changelog.md file for a new library generation (#14773)
This PR creates the `CHANGELOG.md` and `docs/CHANGELOG.md` for a new package during the container configure command.
1 parent 8af04df commit 0b53f8e

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

.generator/cli.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,23 @@ def _prepare_new_library_config(library_config: Dict) -> Dict:
223223
return library_config
224224

225225

226+
def _create_new_changelog_for_library(library_id: str, output: str):
227+
"""Creates a new changelog for the library.
228+
Args:
229+
library_id(str): The id of the library.
230+
output(str): Path to the directory in the container where code
231+
should be generated.
232+
"""
233+
package_changelog_path = f"{output}/packages/{library_id}/CHANGELOG.md"
234+
docs_changelog_path = f"{output}/packages/{library_id}/docs/CHANGELOG.md"
235+
236+
os.makedirs(os.path.dirname(package_changelog_path), exist_ok=True)
237+
_write_text_file(package_changelog_path, "# Changelog\n")
238+
239+
os.makedirs(os.path.dirname(docs_changelog_path), exist_ok=True)
240+
_write_text_file(docs_changelog_path, "# Changelog\n")
241+
242+
226243
def handle_configure(
227244
librarian: str = LIBRARIAN_DIR,
228245
source: str = SOURCE_DIR,
@@ -267,6 +284,10 @@ def handle_configure(
267284
)
268285
prepared_config = _prepare_new_library_config(new_library_config)
269286

287+
# Create a `CHANGELOG.md` and `docs/CHANGELOG.md` file for the new library
288+
library_id = _get_library_id(prepared_config)
289+
_create_new_changelog_for_library(library_id, output)
290+
270291
# Write the new library configuration to configure-response.json.
271292
_write_json_file(f"{librarian}/configure-response.json", prepared_config)
272293

.generator/test_cli.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
_write_json_file,
7272
_write_text_file,
7373
_copy_readme_to_docs,
74+
_create_new_changelog_for_library,
7475
handle_build,
7576
handle_configure,
7677
handle_generate,
@@ -293,6 +294,7 @@ def test_handle_configure_success(mock_configure_request_file, mocker):
293294
mock_prepare_config = mocker.patch(
294295
"cli._prepare_new_library_config", return_value={"id": "prepared"}
295296
)
297+
mock_create_changelog = mocker.patch("cli._create_new_changelog_for_library")
296298

297299
handle_configure()
298300

@@ -310,6 +312,28 @@ def test_handle_configure_no_new_library(mocker):
310312
with pytest.raises(ValueError, match="Configuring a new library failed."):
311313
handle_configure()
312314

315+
def test_create_new_changelog_for_library(mocker):
316+
"""Tests that the changelog files are created correctly."""
317+
library_id = "google-cloud-language"
318+
output = "output"
319+
mock_makedirs = mocker.patch("os.makedirs")
320+
mock_write_text_file = mocker.patch("cli._write_text_file")
321+
322+
_create_new_changelog_for_library(library_id, output)
323+
324+
package_changelog_path = f"{output}/packages/{library_id}/CHANGELOG.md"
325+
docs_changelog_path = f"{output}/packages/{library_id}/docs/CHANGELOG.md"
326+
327+
# Check that makedirs was called for both parent directories
328+
mock_makedirs.assert_any_call(os.path.dirname(package_changelog_path), exist_ok=True)
329+
mock_makedirs.assert_any_call(os.path.dirname(docs_changelog_path), exist_ok=True)
330+
assert mock_makedirs.call_count == 2
331+
332+
# Check that the files were "written" with the correct content
333+
mock_write_text_file.assert_any_call(package_changelog_path, "# Changelog\n")
334+
mock_write_text_file.assert_any_call(docs_changelog_path, "# Changelog\n")
335+
assert mock_write_text_file.call_count == 2
336+
313337

314338
def test_get_new_library_config_found(mock_configure_request_data):
315339
"""Tests that the new library configuration is returned when found."""

0 commit comments

Comments
 (0)