Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not populate repo level change while removing library #2740

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,21 @@ def test_compare_config_library_addition(self):
config_change = result.change_to_libraries[ChangeType.LIBRARIES_ADDITION][0]
self.assertEqual("new_library", config_change.library_name)

def test_compare_config_library_removal_does_not_have_repo_or_library_level_change(
self,
):
self.current_config.libraries = []
result = compare_config(
baseline_config=self.baseline_config,
current_config=self.current_config,
)
self.assertTrue(
len(result.change_to_libraries[ChangeType.REPO_LEVEL_CHANGE]) == 0
)
self.assertTrue(
len(result.change_to_libraries[ChangeType.LIBRARY_LEVEL_CHANGE]) == 0
)

def test_compare_config_api_shortname_update_without_library_name(self):
self.current_config.libraries[0].api_shortname = "new_api_shortname"
result = compare_config(
Expand Down
23 changes: 18 additions & 5 deletions library_generation/utils/generation_config_comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ def compare_config(
:return: a ConfigChange objects.
"""
diff = defaultdict(list[LibraryChange])
baseline_params = __convert_params_to_sorted_list(baseline_config)
current_params = __convert_params_to_sorted_list(current_config)
# Exclude `libraries` because an empty library list (e.g., by library
# removal) will cause this parameter appears in the sorted param list,
# which leads to unequal list of parameters.
excluded_params = {"libraries"}
baseline_params = __convert_params_to_sorted_list(
obj=baseline_config, excluded_params=excluded_params
)
current_params = __convert_params_to_sorted_list(
obj=current_config, excluded_params=excluded_params
)

for baseline_param, current_param in zip(baseline_params, current_params):
if baseline_param == current_param:
continue
Expand Down Expand Up @@ -216,7 +225,7 @@ def __compare_gapic_configs(
diff[ChangeType.GAPIC_ADDITION].append(config_change)


def __convert_params_to_sorted_list(obj: Any) -> List[tuple]:
def __convert_params_to_sorted_list(obj: Any, excluded_params=None) -> List[tuple]:
"""
Convert the parameter and its value of a given object to a sorted list of
tuples.
Expand All @@ -230,13 +239,17 @@ def __convert_params_to_sorted_list(obj: Any) -> List[tuple]:

Note that built-in params, e.g., __str__, and methods will be skipped.

:param obj: an object
:param obj: an object.
:param excluded_params: excluded params.
:return: a sorted list of tuples.
"""
if excluded_params is None:
excluded_params = set()
param_and_values = []
for param, value in vars(obj).items():
if (
param.startswith("__") # skip built-in params
param in excluded_params
or param.startswith("__") # skip built-in params
or callable(getattr(obj, param)) # skip methods
# skip if the type of param is not one of the following types
# 1. str
Expand Down
Loading