diff --git a/.generator/cli.py b/.generator/cli.py index e053019765ab..a20b3baeab05 100644 --- a/.generator/cli.py +++ b/.generator/cli.py @@ -842,6 +842,12 @@ def handle_release_init( the config.yaml. output(str): Path to the directory in the container where modified code should be placed. + + Raises: + ValueError: if the version in `release-init-request.json` is + the same as the version in state.yaml + ValueError: if the `release-init-request.json` file in the given + librarian directory cannot be read. """ try: @@ -864,20 +870,26 @@ def handle_release_init( library_id = library_release_data["id"] library_changes = library_release_data["changes"] path_to_library = f"packages/{library_id}" - _update_version_for_library(repo, output, path_to_library, version) # Get previous version from state.yaml previous_version = _get_previous_version(library_id, librarian) - if previous_version != version: - _update_changelog_for_library( - repo, - output, - library_changes, - version, - previous_version, - library_id, + if previous_version == version: + raise ValueError( + f"The version in {RELEASE_INIT_REQUEST_FILE} is the same as the version in {STATE_YAML_FILE}\n" + f"{library_id} previous released version: {previous_version}\n" + f"{library_id} current version: {version}\n" ) + _update_version_for_library(repo, output, path_to_library, version) + _update_changelog_for_library( + repo, + output, + library_changes, + version, + previous_version, + library_id, + ) + except Exception as e: raise ValueError(f"Release init failed: {e}") from e diff --git a/.generator/test_cli.py b/.generator/test_cli.py index 7fbf316a150d..3c5b522f3c39 100644 --- a/.generator/test_cli.py +++ b/.generator/test_cli.py @@ -604,14 +604,51 @@ def test_handle_release_init_success(mocker, mock_release_init_request_file): handle_release_init() -def test_handle_release_init_fail(): +def test_handle_release_init_fail_value_error_file(): """ Tests that handle_release_init fails to read `librarian/release-init-request.json`. """ - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="No such file or directory"): handle_release_init() +def test_handle_release_init_fail_value_error_version(mocker): + m = mock_open() + + mock_release_init_request_content = { + "libraries": [ + { + "id": "google-cloud-another-library", + "apis": [{"path": "google/cloud/another/library/v1"}], + "release_triggered": False, + "version": "1.2.3", + "changes": [], + }, + { + "id": "google-cloud-language", + "apis": [{"path": "google/cloud/language/v1"}], + "release_triggered": True, + "version": "1.2.2", + "changes": [], + }, + ] + } + with unittest.mock.patch("cli.open", m): + mocker.patch( + "cli._get_libraries_to_prepare_for_release", + return_value=mock_release_init_request_content["libraries"], + ) + mocker.patch("cli._get_previous_version", return_value="1.2.2") + mocker.patch("cli._process_changelog", return_value=None) + mocker.patch( + "cli._read_json_file", return_value=mock_release_init_request_content + ) + with pytest.raises( + ValueError, match="is the same as the version in state.yaml" + ): + handle_release_init() + + def test_read_valid_text_file(mocker): """Tests reading a valid text file.""" mock_content = "some text"