Skip to content

Commit 1114f18

Browse files
authored
fix: prepare to generate grafeas (#2761)
In this PR: - Add suffix to sample source folder - Add unit tests Context: - The package name of grafeas is `io.grafeas.v1` which is not starts with `com`. When copying samples to destination directory, we need to change the suffix of source directory. - grafeas doesn't have a valid api_id (empty string). Before this change, the `api_id` will be populated as `{library.api_shortname}.googleapis.com`, which is not correct. `api_id` should not be populated to `.repo-metadata.json` if set to empty string. With the library entry added in generation configuration: ``` - api_shortname: containeranalysis name_pretty: Grafeas product_documentation: https://grafeas.io api_description: n/a client_documentation: "https://cloud.google.com/java/docs/reference/grafeas/latest/overview" release_level: stable distribution_name: "io.grafeas:grafeas" api_id: "" codeowner_team: "@googleapis/aap-dpes" library_name: grafeas requires_billing: false GAPICs: - proto_path: grafeas/v1 ``` The generated java-grafeas is in googleapis/google-cloud-java#10820
1 parent e1e1fb6 commit 1114f18

File tree

7 files changed

+82
-15
lines changed

7 files changed

+82
-15
lines changed

library_generation/test/resources/test_mv_src/gapic/destination/java_gapic_srcjar/src/main/java/example_main.txt

Whitespace-only changes.

library_generation/test/resources/test_mv_src/gapic/destination/java_gapic_srcjar/src/test/java/example_test.txt

Whitespace-only changes.

library_generation/test/resources/test_mv_src/proto/destination/java_gapic_srcjar/proto/src/main/java/example_proto_main.txt

Whitespace-only changes.

library_generation/test/resources/test_mv_src/samples/destination_com/java_gapic_srcjar/samples/snippets/generated/src/main/java/com/example_com_sample.txt

Whitespace-only changes.

library_generation/test/resources/test_mv_src/samples/destination_io/java_gapic_srcjar/samples/snippets/generated/src/main/java/io/example_io_sample.txt

Whitespace-only changes.

library_generation/test/utilities_unit_tests.py

+73-11
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,65 @@ def test_sh_util_nonexistent_function_fails(self):
102102
with self.assertRaises(RuntimeError):
103103
result = util.sh_util("nonexistent_function")
104104

105+
def test_mv_src_files_gapic_main_succeeds(self):
106+
previous_dir = os.getcwd()
107+
os.chdir(f"{resources_dir}/test_mv_src/gapic")
108+
os.environ["folder_name"] = "example"
109+
util.sh_util("mv_src_files gapic main destination")
110+
self.assertTrue(
111+
os.path.isfile("destination/gapic-example/src/main/java/example_main.txt")
112+
)
113+
shutil.rmtree("destination/gapic-example")
114+
os.chdir(previous_dir)
115+
116+
def test_mv_src_files_gapic_test_succeeds(self):
117+
previous_dir = os.getcwd()
118+
os.chdir(f"{resources_dir}/test_mv_src/gapic")
119+
os.environ["folder_name"] = "example"
120+
util.sh_util("mv_src_files gapic test destination")
121+
self.assertTrue(
122+
os.path.isfile("destination/gapic-example/src/test/java/example_test.txt")
123+
)
124+
shutil.rmtree("destination/gapic-example")
125+
os.chdir(previous_dir)
126+
127+
def test_mv_src_files_proto_main_succeeds(self):
128+
previous_dir = os.getcwd()
129+
os.chdir(f"{resources_dir}/test_mv_src/proto")
130+
os.environ["folder_name"] = "example"
131+
util.sh_util("mv_src_files proto main destination")
132+
self.assertTrue(
133+
os.path.isfile(
134+
"destination/proto-example/src/main/java/example_proto_main.txt"
135+
)
136+
)
137+
shutil.rmtree("destination/proto-example")
138+
os.chdir(previous_dir)
139+
140+
def test_mv_src_files_sample_suffix_io_succeeds(self):
141+
previous_dir = os.getcwd()
142+
os.chdir(f"{resources_dir}/test_mv_src/samples")
143+
util.sh_util("mv_src_files samples main destination_io")
144+
self.assertTrue(
145+
os.path.isfile(
146+
"destination_io/samples/snippets/generated/io/example_io_sample.txt"
147+
)
148+
)
149+
shutil.rmtree("destination_io/samples")
150+
os.chdir(previous_dir)
151+
152+
def test_mv_src_files_sample_suffix_com_succeeds(self):
153+
previous_dir = os.getcwd()
154+
os.chdir(f"{resources_dir}/test_mv_src/samples")
155+
util.sh_util("mv_src_files samples main destination_com")
156+
self.assertTrue(
157+
os.path.isfile(
158+
"destination_com/samples/snippets/generated/com/example_com_sample.txt"
159+
)
160+
)
161+
shutil.rmtree("destination_com/samples")
162+
os.chdir(previous_dir)
163+
105164
def test_eprint_valid_input_succeeds(self):
106165
test_input = "This is some test input"
107166
# create a stdio capture object
@@ -247,7 +306,7 @@ def test_gapic_inputs_parse_no_service_yaml_returns_empty_string(self):
247306

248307
def test_generate_prerequisite_files_non_monorepo_success(self):
249308
library_path = self.__setup_prerequisite_files(
250-
num_libraries=1, library_type="GAPIC_COMBO"
309+
combination=1, library_type="GAPIC_COMBO"
251310
)
252311

253312
file_comparator.compare_files(
@@ -266,7 +325,7 @@ def test_generate_prerequisite_files_non_monorepo_success(self):
266325
self.__remove_prerequisite_files(path=library_path, is_monorepo=False)
267326

268327
def test_generate_prerequisite_files_monorepo_success(self):
269-
library_path = self.__setup_prerequisite_files(num_libraries=2)
328+
library_path = self.__setup_prerequisite_files(combination=2)
270329

271330
file_comparator.compare_files(
272331
f"{library_path}/.repo-metadata.json",
@@ -317,7 +376,10 @@ def test_prepare_repo_split_repo_success(self):
317376
shutil.rmtree(repo_config.output_folder)
318377

319378
def __setup_prerequisite_files(
320-
self, num_libraries: int, library_type: str = "GAPIC_AUTO"
379+
self,
380+
combination: int,
381+
library_type: str = "GAPIC_AUTO",
382+
library: LibraryConfig = library_1,
321383
) -> str:
322384
library_path = f"{resources_dir}/goldens"
323385
files = [
@@ -326,12 +388,12 @@ def __setup_prerequisite_files(
326388
f"{library_path}/owlbot.py",
327389
]
328390
cleanup(files)
329-
config = self.__get_a_gen_config(num_libraries, library_type=library_type)
391+
config = self.__get_a_gen_config(combination, library_type=library_type)
330392
proto_path = "google/cloud/baremetalsolution/v2"
331393
transport = "grpc"
332394
util.generate_prerequisite_files(
333395
config=config,
334-
library=library_1,
396+
library=library,
335397
proto_path=proto_path,
336398
transport=transport,
337399
library_path=library_path,
@@ -340,28 +402,28 @@ def __setup_prerequisite_files(
340402

341403
@staticmethod
342404
def __get_a_gen_config(
343-
num_libraries: int, library_type: str = "GAPIC_AUTO"
405+
combination: int, library_type: str = "GAPIC_AUTO"
344406
) -> GenerationConfig:
345407
"""
346408
Returns an object of GenerationConfig with one to three of
347409
LibraryConfig objects. Other attributes are set to empty str.
348410
349-
:param num_libraries: the number of LibraryConfig objects associated with
411+
:param combination: combination of LibraryConfig objects associated with
350412
the GenerationConfig. Only support 1, 2 or 3.
351413
:return: an object of GenerationConfig
352414
"""
353-
if num_libraries == 2:
415+
if combination == 2:
354416
libraries = [library_1, library_2]
355-
elif num_libraries == 3:
356-
libraries = [library_1, library_2, library_3]
417+
elif combination == 3:
418+
libraries = [library_with_empty_api_id, library_2]
357419
else:
358420
libraries = [library_1]
359421

360422
# update libraries with custom configuration (for now, only
361423
# library_type)
362424
for library in libraries:
363425
library.library_type = library_type
364-
if num_libraries == 1:
426+
if combination == 1:
365427
# treat this as a HW library case to generate a real-life
366428
# repo-metadata
367429
library.extra_versioned_modules = "test-module"

library_generation/utils/utilities.sh

+9-4
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,25 @@ mv_src_files() {
3232
local type=$2 # one of main, test
3333
local destination_path=$3
3434
if [ "${category}" == "samples" ]; then
35-
src_suffix="samples/snippets/generated/src/main/java/com"
35+
src_suffix="samples/snippets/generated/src/main/java"
3636
folder_suffix="samples/snippets/generated"
37+
mkdir -p "${destination_path}/${folder_suffix}"
38+
cp -r "${destination_path}/java_gapic_srcjar/${src_suffix}"/* "${destination_path}/${folder_suffix}"
3739
elif [ "${category}" == "proto" ]; then
3840
src_suffix="${category}/src/${type}/java"
3941
folder_suffix="${category}-${folder_name}/src/${type}"
4042
else
4143
src_suffix="src/${type}"
4244
folder_suffix="${category}-${folder_name}/src"
4345
fi
46+
47+
if [ "${category}" == "samples" ]; then
48+
return
49+
fi
50+
4451
mkdir -p "${destination_path}/${folder_suffix}"
4552
cp -r "${destination_path}/java_gapic_srcjar/${src_suffix}" "${destination_path}/${folder_suffix}"
46-
if [ "${category}" != "samples" ]; then
47-
rm -r -f "${destination_path}/${folder_suffix}/java/META-INF"
48-
fi
53+
rm -r -f "${destination_path}/${folder_suffix}/java/META-INF"
4954
}
5055

5156
# unzip jar file

0 commit comments

Comments
 (0)