Skip to content

Commit 4513c85

Browse files
authored
feat: populate library version in config (googleapis#14534)
Fixes googleapis/librarian#466 🦕
1 parent 2c30726 commit 4513c85

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

.generator/cli.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ def _get_new_library_config(request_data: Dict) -> Dict:
187187
return {}
188188

189189

190+
def _add_new_library_version(
191+
library_config: Dict
192+
) -> None:
193+
"""Adds the library version to the configuration if it's not present.
194+
195+
Args:
196+
library_config(Dict): The library configuration.
197+
"""
198+
if "version" not in library_config or not library_config["version"]:
199+
library_config["version"] = "0.0.0"
200+
201+
190202
def _prepare_new_library_config(library_config: Dict) -> Dict:
191203
"""
192204
Prepares the new library's configuration by removing temporary keys and
@@ -209,6 +221,7 @@ def _prepare_new_library_config(library_config: Dict) -> Dict:
209221
_add_new_library_preserve_regex(library_config, library_id)
210222
_add_new_library_remove_regex(library_config, library_id)
211223
_add_new_library_tag_format(library_config)
224+
_add_new_library_version(library_config)
212225

213226
return library_config
214227

.generator/test_cli.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
_get_libraries_to_prepare_for_release,
4747
_get_new_library_config,
4848
_get_previous_version,
49+
_add_new_library_version,
4950
_prepare_new_library_config,
5051
_process_changelog,
5152
_process_version_file,
@@ -171,6 +172,7 @@ def mock_configure_request_data():
171172
{
172173
"id": "google-cloud-language",
173174
"apis": [{"path": "google/cloud/language/v1", "status": "new"}],
175+
"version": "",
174176
}
175177
]
176178
}
@@ -320,14 +322,15 @@ def test_get_new_library_config_empty_input():
320322
assert config == {}
321323

322324

323-
def test_prepare_new_library_config():
325+
def test_prepare_new_library_config(mocker):
324326
"""Tests the preparation of a new library's configuration."""
325327
raw_config = {
326328
"id": "google-cloud-language",
327329
"apis": [{"path": "google/cloud/language/v1", "status": "new"}],
328330
"source_roots": None,
329331
"preserve_regex": None,
330332
"remove_regex": None,
333+
"version": "",
331334
}
332335

333336
prepared_config = _prepare_new_library_config(raw_config)
@@ -342,9 +345,10 @@ def test_prepare_new_library_config():
342345
)
343346
assert prepared_config["remove_regex"] == ["packages/google-cloud-language"]
344347
assert prepared_config["tag_format"] == "{{id}}-v{{version}}"
348+
assert prepared_config["version"] == "0.0.0"
345349

346350

347-
def test_prepare_new_library_config_preserves_existing_values():
351+
def test_prepare_new_library_config_preserves_existing_values(mocker):
348352
"""Tests that existing values in the config are not overwritten."""
349353
raw_config = {
350354
"id": "google-cloud-language",
@@ -353,6 +357,7 @@ def test_prepare_new_library_config_preserves_existing_values():
353357
"preserve_regex": ["custom/regex"],
354358
"remove_regex": ["custom/remove"],
355359
"tag_format": "custom-format-{{version}}",
360+
"version": "4.5.6",
356361
}
357362

358363
prepared_config = _prepare_new_library_config(raw_config)
@@ -364,6 +369,21 @@ def test_prepare_new_library_config_preserves_existing_values():
364369
assert prepared_config["preserve_regex"] == ["custom/regex"]
365370
assert prepared_config["remove_regex"] == ["custom/remove"]
366371
assert prepared_config["tag_format"] == "custom-format-{{version}}"
372+
assert prepared_config["version"] == "4.5.6"
373+
374+
375+
def test_add_new_library_version_populates_version(mocker):
376+
"""Tests that the version is populated if it's missing."""
377+
config = {"version": ""}
378+
_add_new_library_version(config)
379+
assert config["version"] == "0.0.0"
380+
381+
382+
def test_add_new_library_version_preserves_version():
383+
"""Tests that an existing version is preserved."""
384+
config = {"version": "4.5.6"}
385+
_add_new_library_version(config)
386+
assert config["version"] == "4.5.6"
367387

368388

369389
def test_get_library_id_success():

0 commit comments

Comments
 (0)