Skip to content

Commit 9c0316b

Browse files
authored
fix: skip generating grpc-* directory if transport is rest (#1979)
* chore: add proto_path in integration test * exclude gapic_metadata.json and package-info.java * add additional protos * search transport twice * skip grpc-* if transport is rest * refactor utility functions * add utility function * add tests * restore workflow * restore diff command * remove unrelated change
1 parent 4fc844e commit 9c0316b

14 files changed

+195
-246
lines changed

library_generation/generate_library.sh

+12-9
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,18 @@ download_tools "${gapic_generator_version}" "${protobuf_version}" "${grpc_versio
9595
##################### Section 1 #####################
9696
# generate grpc-*/
9797
#####################################################
98-
"${protoc_path}"/protoc "--plugin=protoc-gen-rpc-plugin=protoc-gen-grpc-java-${grpc_version}-${os_architecture}.exe" \
99-
"--rpc-plugin_out=:${destination_path}/java_grpc.jar" \
100-
${proto_files} # Do not quote because this variable should not be treated as one long string.
101-
# unzip java_grpc.jar to grpc-*/src/main/java
102-
unzip_src_files "grpc"
103-
# remove empty files in grpc-*/src/main/java
104-
remove_empty_files "grpc"
105-
# remove grpc version in *ServiceGrpc.java file so the content is identical with bazel build.
106-
remove_grpc_version
98+
if [[ ! "${transport}" == "rest" ]]; then
99+
# do not need to generate grpc-* if the transport is `rest`.
100+
"${protoc_path}"/protoc "--plugin=protoc-gen-rpc-plugin=protoc-gen-grpc-java-${grpc_version}-${os_architecture}.exe" \
101+
"--rpc-plugin_out=:${destination_path}/java_grpc.jar" \
102+
${proto_files} # Do not quote because this variable should not be treated as one long string.
103+
# unzip java_grpc.jar to grpc-*/src/main/java
104+
unzip_src_files "grpc"
105+
# remove empty files in grpc-*/src/main/java
106+
remove_empty_files "grpc"
107+
# remove grpc version in *ServiceGrpc.java file so the content is identical with bazel build.
108+
remove_grpc_version
109+
fi
107110
###################### Section 2 #####################
108111
## generate gapic-*/, part of proto-*/, samples/
109112
######################################################

library_generation/test/generate_library_integration_test.sh

+3-18
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,9 @@ grpc_version=$(get_version_from_WORKSPACE "_grpc_version" WORKSPACE "=")
5555
echo "The version of protoc-gen-grpc-java plugin is ${gapic_generator_version}."
5656
# parse GAPIC options from proto_path/BUILD.bazel
5757
proto_build_file_path="${proto_path}/BUILD.bazel"
58-
transport=$(get_config_from_BUILD \
59-
"${proto_build_file_path}" \
60-
"java_gapic_library(" \
61-
"grpc+rest" \
62-
"grpc"
63-
)
64-
rest_numeric_enums=$(get_config_from_BUILD \
65-
"${proto_build_file_path}" \
66-
"java_gapic_library(" \
67-
"rest_numeric_enums = False" \
68-
"true"
69-
)
70-
include_samples=$(get_config_from_BUILD \
71-
"${proto_build_file_path}" \
72-
"java_gapic_assembly_gradle_pkg(" \
73-
"include_samples = True" \
74-
"false"
75-
)
58+
transport=$(get_transport_from_BUILD "${proto_build_file_path}")
59+
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${proto_build_file_path}")
60+
include_samples=$(get_include_samples_from_BUILD "${proto_build_file_path}")
7661
echo "GAPIC options are transport=${transport}, rest_numeric_enums=${rest_numeric_enums}, include_samples=${include_samples}."
7762
# generate GAPIC client library
7863
echo "Generating library from ${proto_path}, to ${destination_path}..."

library_generation/test/generate_library_unit_tests.sh

+70-36
Original file line numberDiff line numberDiff line change
@@ -230,40 +230,67 @@ generate_library_failed_with_invalid_grpc_version() {
230230
cleanup "${destination}"
231231
}
232232

233-
get_config_from_valid_BUILD_matched_test() {
234-
build_file="${script_dir}/resources/misc/TESTBUILD.bazel"
235-
rule="java_gapic_library("
236-
# the pattern we expect to find in the BUILD file
237-
pattern_should_match="name"
238-
# default value if the pattern was not found
239-
if_matched_return="got-a-match"
240-
if_not_matched_return="no-match"
241-
pattern_matched_result=$(get_config_from_BUILD \
242-
"${build_file}" \
243-
"${rule}" \
244-
"${pattern_should_match}" \
245-
"${if_not_matched_return}" \
246-
"${if_matched_return}"
247-
)
248-
assertEquals "${if_matched_return}" "${pattern_matched_result}"
249-
}
250-
251-
get_config_from_valid_BUILD_not_match_test() {
252-
build_file="${script_dir}/resources/misc/TESTBUILD.bazel"
253-
rule="java_gapic_library("
254-
# the pattern that we should not find in the BUILD file
255-
pattern_should_not_match="should-not-match"
256-
# default value if the pattern was not found
257-
if_matched_return="got-a-match"
258-
if_not_matched_return="no-match"
259-
pattern_not_matched_result=$(get_config_from_BUILD \
260-
"${build_file}" \
261-
"${rule}" \
262-
"${pattern_should_not_match}" \
263-
"${if_not_matched_return}" \
264-
"${if_matched_return}"
265-
)
266-
assertEquals "${if_not_matched_return}" "${pattern_not_matched_result}"
233+
get_transport_from_BUILD_grpc_rest_test() {
234+
local build_file="${script_dir}/resources/misc/BUILD_grpc_rest.bazel"
235+
local transport
236+
transport=$(get_transport_from_BUILD "${build_file}")
237+
assertEquals "grpc+rest" "${transport}"
238+
}
239+
240+
get_transport_from_BUILD_grpc_test() {
241+
local build_file="${script_dir}/resources/misc/BUILD_grpc.bazel"
242+
local transport
243+
transport=$(get_transport_from_BUILD "${build_file}")
244+
assertEquals "grpc" "${transport}"
245+
}
246+
247+
get_transport_from_BUILD_rest_test() {
248+
local build_file="${script_dir}/resources/misc/BUILD_rest.bazel"
249+
local transport
250+
transport=$(get_transport_from_BUILD "${build_file}")
251+
assertEquals "rest" "${transport}"
252+
}
253+
254+
get_rest_numeric_enums_from_BUILD_true_test() {
255+
local build_file="${script_dir}/resources/misc/BUILD_rest_numeric_enums_true.bazel"
256+
local rest_numeric_enums
257+
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${build_file}")
258+
assertEquals "true" "${rest_numeric_enums}"
259+
}
260+
261+
get_rest_numeric_enums_from_BUILD_false_test() {
262+
local build_file="${script_dir}/resources/misc/BUILD_rest_numeric_enums_false.bazel"
263+
local rest_numeric_enums
264+
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${build_file}")
265+
assertEquals "false" "${rest_numeric_enums}"
266+
}
267+
268+
get_rest_numeric_enums_from_BUILD_empty_test() {
269+
local build_file="${script_dir}/resources/misc/BUILD_rest_numeric_enums_empty.bazel"
270+
local rest_numeric_enums
271+
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${build_file}")
272+
assertEquals "false" "${rest_numeric_enums}"
273+
}
274+
275+
get_include_samples_from_BUILD_true_test() {
276+
local build_file="${script_dir}/resources/misc/BUILD_include_samples_true.bazel"
277+
local include_samples
278+
include_samples=$(get_include_samples_from_BUILD "${build_file}")
279+
assertEquals "true" "${include_samples}"
280+
}
281+
282+
get_include_samples_from_BUILD_false_test() {
283+
local build_file="${script_dir}/resources/misc/BUILD_include_samples_false.bazel"
284+
local include_samples
285+
include_samples=$(get_include_samples_from_BUILD "${build_file}")
286+
assertEquals "false" "${include_samples}"
287+
}
288+
289+
get_include_samples_from_BUILD_empty_test() {
290+
local build_file="${script_dir}/resources/misc/BUILD_include_samples_empty.bazel"
291+
local include_samples
292+
include_samples=$(get_include_samples_from_BUILD "${build_file}")
293+
assertEquals "false" "${include_samples}"
267294
}
268295

269296
get_version_from_valid_WORKSPACE_test() {
@@ -312,8 +339,15 @@ test_list=(
312339
generate_library_failed_with_invalid_generator_version
313340
generate_library_failed_with_invalid_protobuf_version
314341
generate_library_failed_with_invalid_grpc_version
315-
get_config_from_valid_BUILD_matched_test
316-
get_config_from_valid_BUILD_not_match_test
342+
get_transport_from_BUILD_grpc_rest_test
343+
get_transport_from_BUILD_grpc_test
344+
get_transport_from_BUILD_rest_test
345+
get_rest_numeric_enums_from_BUILD_true_test
346+
get_rest_numeric_enums_from_BUILD_false_test
347+
get_rest_numeric_enums_from_BUILD_empty_test
348+
get_include_samples_from_BUILD_true_test
349+
get_include_samples_from_BUILD_false_test
350+
get_include_samples_from_BUILD_empty_test
317351
get_version_from_valid_WORKSPACE_test
318352
get_generator_version_from_valid_versions_txt_test
319353
get_gax_version_from_valid_versions_txt_test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# this file is only used in testing `get_transport_from_BUILD` in utilities.sh
2+
3+
java_gapic_library(
4+
transport = "grpc",
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# this file is only used in testing `get_transport_from_BUILD` in utilities.sh
2+
3+
java_gapic_library(
4+
transport = "grpc+rest",
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# this file is only used in testing `get_include_samples_from_BUILD` in utilities.sh
2+
3+
java_gapic_assembly_gradle_pkg(
4+
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# this file is only used in testing `get_include_samples_from_BUILD` in utilities.sh
2+
3+
java_gapic_assembly_gradle_pkg(
4+
include_samples = False,
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# this file is only used in testing `get_include_samples_from_BUILD` in utilities.sh
2+
3+
java_gapic_assembly_gradle_pkg(
4+
include_samples = True,
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# this file is only used in testing `get_transport_from_BUILD` in utilities.sh
2+
3+
java_gapic_library(
4+
transport = "rest",
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# this file is only used in testing `get_rest_numeric_enums_from_BUILD` in utilities.sh
2+
3+
java_gapic_library(
4+
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# this file is only used in testing `get_rest_numeric_enums_from_BUILD` in utilities.sh
2+
3+
java_gapic_library(
4+
rest_numeric_enums = False
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# this file is only used in testing `get_rest_numeric_enums_from_BUILD` in utilities.sh
2+
3+
java_gapic_library(
4+
rest_numeric_enums = True
5+
)

library_generation/test/resources/misc/TESTBUILD.bazel

-166
This file was deleted.

0 commit comments

Comments
 (0)