diff --git a/.generator/cli.py b/.generator/cli.py index 48ed4efb369d..06d06366ad3f 100644 --- a/.generator/cli.py +++ b/.generator/cli.py @@ -187,6 +187,18 @@ def _get_new_library_config(request_data: Dict) -> Dict: return {} +def _add_new_library_version( + library_config: Dict +) -> None: + """Adds the library version to the configuration if it's not present. + + Args: + library_config(Dict): The library configuration. + """ + if "version" not in library_config or not library_config["version"]: + library_config["version"] = "0.0.0" + + def _prepare_new_library_config(library_config: Dict) -> Dict: """ Prepares the new library's configuration by removing temporary keys and @@ -209,6 +221,7 @@ def _prepare_new_library_config(library_config: Dict) -> Dict: _add_new_library_preserve_regex(library_config, library_id) _add_new_library_remove_regex(library_config, library_id) _add_new_library_tag_format(library_config) + _add_new_library_version(library_config) return library_config diff --git a/.generator/test_cli.py b/.generator/test_cli.py index 3904d8e72879..f113e6a326d7 100644 --- a/.generator/test_cli.py +++ b/.generator/test_cli.py @@ -46,6 +46,7 @@ _get_libraries_to_prepare_for_release, _get_new_library_config, _get_previous_version, + _add_new_library_version, _prepare_new_library_config, _process_changelog, _process_version_file, @@ -171,6 +172,7 @@ def mock_configure_request_data(): { "id": "google-cloud-language", "apis": [{"path": "google/cloud/language/v1", "status": "new"}], + "version": "", } ] } @@ -317,7 +319,7 @@ def test_get_new_library_config_empty_input(): assert config == {} -def test_prepare_new_library_config(): +def test_prepare_new_library_config(mocker): """Tests the preparation of a new library's configuration.""" raw_config = { "id": "google-cloud-language", @@ -325,6 +327,7 @@ def test_prepare_new_library_config(): "source_roots": None, "preserve_regex": None, "remove_regex": None, + "version": "", } prepared_config = _prepare_new_library_config(raw_config) @@ -336,9 +339,10 @@ def test_prepare_new_library_config(): assert "packages/google-cloud-language/CHANGELOG.md" in prepared_config["preserve_regex"] assert prepared_config["remove_regex"] == ["packages/google-cloud-language"] assert prepared_config["tag_format"] == "{{id}}-v{{version}}" + assert prepared_config["version"] == "0.0.0" -def test_prepare_new_library_config_preserves_existing_values(): +def test_prepare_new_library_config_preserves_existing_values(mocker): """Tests that existing values in the config are not overwritten.""" raw_config = { "id": "google-cloud-language", @@ -347,6 +351,7 @@ def test_prepare_new_library_config_preserves_existing_values(): "preserve_regex": ["custom/regex"], "remove_regex": ["custom/remove"], "tag_format": "custom-format-{{version}}", + "version": "4.5.6", } prepared_config = _prepare_new_library_config(raw_config) @@ -358,6 +363,21 @@ def test_prepare_new_library_config_preserves_existing_values(): assert prepared_config["preserve_regex"] == ["custom/regex"] assert prepared_config["remove_regex"] == ["custom/remove"] assert prepared_config["tag_format"] == "custom-format-{{version}}" + assert prepared_config["version"] == "4.5.6" + + +def test_add_new_library_version_populates_version(mocker): + """Tests that the version is populated if it's missing.""" + config = {"version": ""} + _add_new_library_version(config) + assert config["version"] == "0.0.0" + + +def test_add_new_library_version_preserves_version(): + """Tests that an existing version is preserved.""" + config = {"version": "4.5.6"} + _add_new_library_version(config) + assert config["version"] == "4.5.6" def test_get_library_id_success():