Skip to content

Commit 43e62b9

Browse files
authored
fix: do not populate repo level change while removing library (#2740)
In this PR: - Do not populate repo level change while removing library Context: the [log](https://github.com/googleapis/google-cloud-java/actions/runs/8970167105/job/24633084853?pr=10782) shows that the script went a full generation when a library is removed from the generation config.
1 parent f0653b8 commit 43e62b9

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

library_generation/test/utils/generation_config_comparator_unit_tests.py

+15
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,21 @@ def test_compare_config_library_addition(self):
178178
config_change = result.change_to_libraries[ChangeType.LIBRARIES_ADDITION][0]
179179
self.assertEqual("new_library", config_change.library_name)
180180

181+
def test_compare_config_library_removal_does_not_have_repo_or_library_level_change(
182+
self,
183+
):
184+
self.current_config.libraries = []
185+
result = compare_config(
186+
baseline_config=self.baseline_config,
187+
current_config=self.current_config,
188+
)
189+
self.assertTrue(
190+
len(result.change_to_libraries[ChangeType.REPO_LEVEL_CHANGE]) == 0
191+
)
192+
self.assertTrue(
193+
len(result.change_to_libraries[ChangeType.LIBRARY_LEVEL_CHANGE]) == 0
194+
)
195+
181196
def test_compare_config_api_shortname_update_without_library_name(self):
182197
self.current_config.libraries[0].api_shortname = "new_api_shortname"
183198
result = compare_config(

library_generation/utils/generation_config_comparator.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,17 @@ def compare_config(
3838
:return: a ConfigChange objects.
3939
"""
4040
diff = defaultdict(list[LibraryChange])
41-
baseline_params = __convert_params_to_sorted_list(baseline_config)
42-
current_params = __convert_params_to_sorted_list(current_config)
41+
# Exclude `libraries` because an empty library list (e.g., by library
42+
# removal) will cause this parameter appears in the sorted param list,
43+
# which leads to unequal list of parameters.
44+
excluded_params = {"libraries"}
45+
baseline_params = __convert_params_to_sorted_list(
46+
obj=baseline_config, excluded_params=excluded_params
47+
)
48+
current_params = __convert_params_to_sorted_list(
49+
obj=current_config, excluded_params=excluded_params
50+
)
51+
4352
for baseline_param, current_param in zip(baseline_params, current_params):
4453
if baseline_param == current_param:
4554
continue
@@ -216,7 +225,7 @@ def __compare_gapic_configs(
216225
diff[ChangeType.GAPIC_ADDITION].append(config_change)
217226

218227

219-
def __convert_params_to_sorted_list(obj: Any) -> List[tuple]:
228+
def __convert_params_to_sorted_list(obj: Any, excluded_params=None) -> List[tuple]:
220229
"""
221230
Convert the parameter and its value of a given object to a sorted list of
222231
tuples.
@@ -230,13 +239,17 @@ def __convert_params_to_sorted_list(obj: Any) -> List[tuple]:
230239
231240
Note that built-in params, e.g., __str__, and methods will be skipped.
232241
233-
:param obj: an object
242+
:param obj: an object.
243+
:param excluded_params: excluded params.
234244
:return: a sorted list of tuples.
235245
"""
246+
if excluded_params is None:
247+
excluded_params = set()
236248
param_and_values = []
237249
for param, value in vars(obj).items():
238250
if (
239-
param.startswith("__") # skip built-in params
251+
param in excluded_params
252+
or param.startswith("__") # skip built-in params
240253
or callable(getattr(obj, param)) # skip methods
241254
# skip if the type of param is not one of the following types
242255
# 1. str

0 commit comments

Comments
 (0)