Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions .generator/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand All @@ -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."""
Expand Down
Loading