Skip to content

Commit 8915733

Browse files
JoeWang1127lqiu96
authored andcommitted
feat: check library_name is unique among libraries (#2490)
In this PR: - check whether `library_name` is unique among all libraries in configuration file.
1 parent fe10bb6 commit 8915733

File tree

3 files changed

+52
-23
lines changed

3 files changed

+52
-23
lines changed

library_generation/README.md

+23-20
Original file line numberDiff line numberDiff line change
@@ -101,26 +101,29 @@ The library level parameters define how to generate a (multi-versions) GAPIC
101101
library.
102102
They are shared by all GAPICs of a library.
103103

104-
| Name | Required | Notes |
105-
|:---------------------|:--------:|:------------------------------------------------------------------|
106-
| api_shortname | Yes | |
107-
| api_description | Yes | |
108-
| name_pretty | Yes | |
109-
| product_docs | Yes | |
110-
| library_type | No | `GAPIC_AUTO` if not specified |
111-
| release_level | No | `preview` if not specified |
112-
| api_id | No | `{api_shortname}.googleapis.com` if not specified |
113-
| api_reference | No | |
114-
| client_documentation | No | |
115-
| distribution_name | No | `{group_id}:google-{cloud_prefix}{library_name}` if not specified |
116-
| googleapis_commitish | No | use repository level `googleapis_commitish` if not specified. |
117-
| group_id | No | `com.google.cloud` if not specified |
118-
| issue_tracker | No | |
119-
| library_name | No | `api_shortname` is not specified |
120-
| rest_documentation | No | |
121-
| rpc_documentation | No | |
122-
| cloud_api | No | `true` if not specified |
123-
| requires-billing | No | `true` if not specified |
104+
| Name | Required | Notes |
105+
|:----------------------|:--------:|:-----------------------------------------------------------------------------------|
106+
| api_shortname | Yes | |
107+
| api_description | Yes | |
108+
| name_pretty | Yes | |
109+
| product_docs | Yes | |
110+
| library_type | No | `GAPIC_AUTO` if not specified |
111+
| release_level | No | `preview` if not specified |
112+
| api_id | No | `{api_shortname}.googleapis.com` if not specified |
113+
| api_reference | No | |
114+
| codeowner_team | No | |
115+
| client_documentation | No | |
116+
| distribution_name | No | `{group_id}:google-{cloud_prefix}{library_name}` if not specified |
117+
| excluded_poms | No | |
118+
| excluded_dependencies | No | |
119+
| googleapis_commitish | No | use repository level `googleapis_commitish` if not specified. |
120+
| group_id | No | `com.google.cloud` if not specified |
121+
| issue_tracker | No | |
122+
| library_name | No | `api_shortname` is not specified. This value should be unique among all libraries. |
123+
| rest_documentation | No | |
124+
| rpc_documentation | No | |
125+
| cloud_api | No | `true` if not specified |
126+
| requires-billing | No | `true` if not specified |
124127

125128
Note that `cloud_prefix` is `cloud-` if `cloud_api` is `true`; empty otherwise.
126129

library_generation/test/unit_tests.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@
5252
api_description="allows you to encrypt, store, manage, and audit infrastructure and application-level secrets.",
5353
gapic_configs=list(),
5454
)
55+
library_3 = LibraryConfig(
56+
api_shortname="secret",
57+
name_pretty="Secret Management Example",
58+
product_documentation="https://cloud.google.com/solutions/",
59+
api_description="allows you to encrypt, store, and audit infrastructure and application-level secrets.",
60+
library_name="secretmanager",
61+
gapic_configs=list(),
62+
)
5563

5664

5765
class UtilitiesTest(unittest.TestCase):
@@ -389,6 +397,17 @@ def test_prepare_repo_monorepo_success(self):
389397
["java-bare-metal-solution", "java-secretmanager"], library_path
390398
)
391399

400+
def test_prepare_repo_monorepo_duplicated_library_name_failed(self):
401+
gen_config = self.__get_a_gen_config(3)
402+
self.assertRaisesRegex(
403+
ValueError,
404+
"secretmanager",
405+
util.prepare_repo,
406+
gen_config,
407+
gen_config.libraries,
408+
f"{resources_dir}/misc",
409+
)
410+
392411
def test_prepare_repo_monorepo_failed(self):
393412
gen_config = self.__get_a_gen_config(2)
394413
self.assertRaises(
@@ -444,15 +463,17 @@ def __compare_files(self, expect: str, actual: str):
444463
@staticmethod
445464
def __get_a_gen_config(num: int):
446465
"""
447-
Returns an object of GenerationConfig with one or two of
466+
Returns an object of GenerationConfig with one to three of
448467
LibraryConfig objects. Other attributes are set to empty str.
449468
450469
:param num: the number of LibraryConfig objects associated with
451-
the GenerationConfig. Only support one or two.
470+
the GenerationConfig. Only support 1, 2 or 3.
452471
:return: an object of GenerationConfig
453472
"""
454-
if num > 1:
473+
if num == 2:
455474
libraries = [library_1, library_2]
475+
elif num == 3:
476+
libraries = [library_1, library_2, library_3]
456477
else:
457478
libraries = [library_1]
458479

library_generation/utilities.py

+5
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def prepare_repo(
251251
:param language: programming language of the library
252252
:return: a RepoConfig object contained repository information
253253
:raise FileNotFoundError if there's no versions.txt in repo_path
254+
:raise ValueError if two libraries have the same library_name
254255
"""
255256
output_folder = sh_util("get_output_folder")
256257
print(f"output_folder: {output_folder}")
@@ -267,6 +268,10 @@ def prepare_repo(
267268
# use absolute path because docker requires absolute path
268269
# in volume name.
269270
absolute_library_path = str(Path(library_path).resolve())
271+
if absolute_library_path in libraries:
272+
# check whether the java_library is unique among all libraries
273+
# because two libraries should not go to the same destination.
274+
raise ValueError(f"{absolute_library_path} already exists.")
270275
libraries[absolute_library_path] = library
271276
# remove existing .repo-metadata.json
272277
json_name = ".repo-metadata.json"

0 commit comments

Comments
 (0)