Skip to content

Commit 0f1a14d

Browse files
authored
chore(librarian): RaiseValue error if current and previous version match (googleapis#14381)
This PR addresses the feedback from googleapis#14353 (comment)
1 parent f2cf04c commit 0f1a14d

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

.generator/cli.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,12 @@ def handle_release_init(
846846
the config.yaml.
847847
output(str): Path to the directory in the container where modified
848848
code should be placed.
849+
850+
Raises:
851+
ValueError: if the version in `release-init-request.json` is
852+
the same as the version in state.yaml
853+
ValueError: if the `release-init-request.json` file in the given
854+
librarian directory cannot be read.
849855
"""
850856

851857
try:
@@ -868,20 +874,26 @@ def handle_release_init(
868874
library_id = library_release_data["id"]
869875
library_changes = library_release_data["changes"]
870876
path_to_library = f"packages/{library_id}"
871-
_update_version_for_library(repo, output, path_to_library, version)
872877

873878
# Get previous version from state.yaml
874879
previous_version = _get_previous_version(library_id, librarian)
875-
if previous_version != version:
876-
_update_changelog_for_library(
877-
repo,
878-
output,
879-
library_changes,
880-
version,
881-
previous_version,
882-
library_id,
880+
if previous_version == version:
881+
raise ValueError(
882+
f"The version in {RELEASE_INIT_REQUEST_FILE} is the same as the version in {STATE_YAML_FILE}\n"
883+
f"{library_id} previous released version: {previous_version}\n"
884+
f"{library_id} current version: {version}\n"
883885
)
884886

887+
_update_version_for_library(repo, output, path_to_library, version)
888+
_update_changelog_for_library(
889+
repo,
890+
output,
891+
library_changes,
892+
version,
893+
previous_version,
894+
library_id,
895+
)
896+
885897
except Exception as e:
886898
raise ValueError(f"Release init failed: {e}") from e
887899

.generator/test_cli.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,14 +602,51 @@ def test_handle_release_init_success(mocker, mock_release_init_request_file):
602602
handle_release_init()
603603

604604

605-
def test_handle_release_init_fail():
605+
def test_handle_release_init_fail_value_error_file():
606606
"""
607607
Tests that handle_release_init fails to read `librarian/release-init-request.json`.
608608
"""
609-
with pytest.raises(ValueError):
609+
with pytest.raises(ValueError, match="No such file or directory"):
610610
handle_release_init()
611611

612612

613+
def test_handle_release_init_fail_value_error_version(mocker):
614+
m = mock_open()
615+
616+
mock_release_init_request_content = {
617+
"libraries": [
618+
{
619+
"id": "google-cloud-another-library",
620+
"apis": [{"path": "google/cloud/another/library/v1"}],
621+
"release_triggered": False,
622+
"version": "1.2.3",
623+
"changes": [],
624+
},
625+
{
626+
"id": "google-cloud-language",
627+
"apis": [{"path": "google/cloud/language/v1"}],
628+
"release_triggered": True,
629+
"version": "1.2.2",
630+
"changes": [],
631+
},
632+
]
633+
}
634+
with unittest.mock.patch("cli.open", m):
635+
mocker.patch(
636+
"cli._get_libraries_to_prepare_for_release",
637+
return_value=mock_release_init_request_content["libraries"],
638+
)
639+
mocker.patch("cli._get_previous_version", return_value="1.2.2")
640+
mocker.patch("cli._process_changelog", return_value=None)
641+
mocker.patch(
642+
"cli._read_json_file", return_value=mock_release_init_request_content
643+
)
644+
with pytest.raises(
645+
ValueError, match="is the same as the version in state.yaml"
646+
):
647+
handle_release_init()
648+
649+
613650
def test_read_valid_text_file(mocker):
614651
"""Tests reading a valid text file."""
615652
mock_content = "some text"

0 commit comments

Comments
 (0)