From 8123f0b15970c76b08086d8988de14e1ed58a77e Mon Sep 17 00:00:00 2001 From: Joe Wang <106995533+JoeWang1127@users.noreply.github.com> Date: Fri, 16 Feb 2024 15:31:30 +0000 Subject: [PATCH] feat: check library_name is unique among libraries (#2490) In this PR: - check whether `library_name` is unique among all libraries in configuration file. --- library_generation/README.md | 43 ++++++++++++++------------- library_generation/test/unit_tests.py | 27 +++++++++++++++-- library_generation/utilities.py | 5 ++++ 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/library_generation/README.md b/library_generation/README.md index 8381422982..75fc32695d 100644 --- a/library_generation/README.md +++ b/library_generation/README.md @@ -101,26 +101,29 @@ The library level parameters define how to generate a (multi-versions) GAPIC library. They are shared by all GAPICs of a library. -| Name | Required | Notes | -|:---------------------|:--------:|:------------------------------------------------------------------| -| api_shortname | Yes | | -| api_description | Yes | | -| name_pretty | Yes | | -| product_docs | Yes | | -| library_type | No | `GAPIC_AUTO` if not specified | -| release_level | No | `preview` if not specified | -| api_id | No | `{api_shortname}.googleapis.com` if not specified | -| api_reference | No | | -| client_documentation | No | | -| distribution_name | No | `{group_id}:google-{cloud_prefix}{library_name}` if not specified | -| googleapis_commitish | No | use repository level `googleapis_commitish` if not specified. | -| group_id | No | `com.google.cloud` if not specified | -| issue_tracker | No | | -| library_name | No | `api_shortname` is not specified | -| rest_documentation | No | | -| rpc_documentation | No | | -| cloud_api | No | `true` if not specified | -| requires-billing | No | `true` if not specified | +| Name | Required | Notes | +|:----------------------|:--------:|:-----------------------------------------------------------------------------------| +| api_shortname | Yes | | +| api_description | Yes | | +| name_pretty | Yes | | +| product_docs | Yes | | +| library_type | No | `GAPIC_AUTO` if not specified | +| release_level | No | `preview` if not specified | +| api_id | No | `{api_shortname}.googleapis.com` if not specified | +| api_reference | No | | +| codeowner_team | No | | +| client_documentation | No | | +| distribution_name | No | `{group_id}:google-{cloud_prefix}{library_name}` if not specified | +| excluded_poms | No | | +| excluded_dependencies | No | | +| googleapis_commitish | No | use repository level `googleapis_commitish` if not specified. | +| group_id | No | `com.google.cloud` if not specified | +| issue_tracker | No | | +| library_name | No | `api_shortname` is not specified. This value should be unique among all libraries. | +| rest_documentation | No | | +| rpc_documentation | No | | +| cloud_api | No | `true` if not specified | +| requires-billing | No | `true` if not specified | Note that `cloud_prefix` is `cloud-` if `cloud_api` is `true`; empty otherwise. diff --git a/library_generation/test/unit_tests.py b/library_generation/test/unit_tests.py index a55cf7f6b5..fc251e2472 100644 --- a/library_generation/test/unit_tests.py +++ b/library_generation/test/unit_tests.py @@ -52,6 +52,14 @@ api_description="allows you to encrypt, store, manage, and audit infrastructure and application-level secrets.", gapic_configs=list(), ) +library_3 = LibraryConfig( + api_shortname="secret", + name_pretty="Secret Management Example", + product_documentation="https://cloud.google.com/solutions/", + api_description="allows you to encrypt, store, and audit infrastructure and application-level secrets.", + library_name="secretmanager", + gapic_configs=list(), +) class UtilitiesTest(unittest.TestCase): @@ -389,6 +397,17 @@ def test_prepare_repo_monorepo_success(self): ["java-bare-metal-solution", "java-secretmanager"], library_path ) + def test_prepare_repo_monorepo_duplicated_library_name_failed(self): + gen_config = self.__get_a_gen_config(3) + self.assertRaisesRegex( + ValueError, + "secretmanager", + util.prepare_repo, + gen_config, + gen_config.libraries, + f"{resources_dir}/misc", + ) + def test_prepare_repo_monorepo_failed(self): gen_config = self.__get_a_gen_config(2) self.assertRaises( @@ -444,15 +463,17 @@ def __compare_files(self, expect: str, actual: str): @staticmethod def __get_a_gen_config(num: int): """ - Returns an object of GenerationConfig with one or two of + Returns an object of GenerationConfig with one to three of LibraryConfig objects. Other attributes are set to empty str. :param num: the number of LibraryConfig objects associated with - the GenerationConfig. Only support one or two. + the GenerationConfig. Only support 1, 2 or 3. :return: an object of GenerationConfig """ - if num > 1: + if num == 2: libraries = [library_1, library_2] + elif num == 3: + libraries = [library_1, library_2, library_3] else: libraries = [library_1] diff --git a/library_generation/utilities.py b/library_generation/utilities.py index d899dd1773..8af797b792 100755 --- a/library_generation/utilities.py +++ b/library_generation/utilities.py @@ -251,6 +251,7 @@ def prepare_repo( :param language: programming language of the library :return: a RepoConfig object contained repository information :raise FileNotFoundError if there's no versions.txt in repo_path + :raise ValueError if two libraries have the same library_name """ output_folder = sh_util("get_output_folder") print(f"output_folder: {output_folder}") @@ -267,6 +268,10 @@ def prepare_repo( # use absolute path because docker requires absolute path # in volume name. absolute_library_path = str(Path(library_path).resolve()) + if absolute_library_path in libraries: + # check whether the java_library is unique among all libraries + # because two libraries should not go to the same destination. + raise ValueError(f"{absolute_library_path} already exists.") libraries[absolute_library_path] = library # remove existing .repo-metadata.json json_name = ".repo-metadata.json"