Skip to content

Commit

Permalink
fix: skip generating grpc-* directory if transport is rest (#1979)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JoeWang1127 authored Sep 14, 2023
1 parent 4fc844e commit 9c0316b
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 246 deletions.
21 changes: 12 additions & 9 deletions library_generation/generate_library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,18 @@ download_tools "${gapic_generator_version}" "${protobuf_version}" "${grpc_versio
##################### Section 1 #####################
# generate grpc-*/
#####################################################
"${protoc_path}"/protoc "--plugin=protoc-gen-rpc-plugin=protoc-gen-grpc-java-${grpc_version}-${os_architecture}.exe" \
"--rpc-plugin_out=:${destination_path}/java_grpc.jar" \
${proto_files} # Do not quote because this variable should not be treated as one long string.
# unzip java_grpc.jar to grpc-*/src/main/java
unzip_src_files "grpc"
# remove empty files in grpc-*/src/main/java
remove_empty_files "grpc"
# remove grpc version in *ServiceGrpc.java file so the content is identical with bazel build.
remove_grpc_version
if [[ ! "${transport}" == "rest" ]]; then
# do not need to generate grpc-* if the transport is `rest`.
"${protoc_path}"/protoc "--plugin=protoc-gen-rpc-plugin=protoc-gen-grpc-java-${grpc_version}-${os_architecture}.exe" \
"--rpc-plugin_out=:${destination_path}/java_grpc.jar" \
${proto_files} # Do not quote because this variable should not be treated as one long string.
# unzip java_grpc.jar to grpc-*/src/main/java
unzip_src_files "grpc"
# remove empty files in grpc-*/src/main/java
remove_empty_files "grpc"
# remove grpc version in *ServiceGrpc.java file so the content is identical with bazel build.
remove_grpc_version
fi
###################### Section 2 #####################
## generate gapic-*/, part of proto-*/, samples/
######################################################
Expand Down
21 changes: 3 additions & 18 deletions library_generation/test/generate_library_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,9 @@ grpc_version=$(get_version_from_WORKSPACE "_grpc_version" WORKSPACE "=")
echo "The version of protoc-gen-grpc-java plugin is ${gapic_generator_version}."
# parse GAPIC options from proto_path/BUILD.bazel
proto_build_file_path="${proto_path}/BUILD.bazel"
transport=$(get_config_from_BUILD \
"${proto_build_file_path}" \
"java_gapic_library(" \
"grpc+rest" \
"grpc"
)
rest_numeric_enums=$(get_config_from_BUILD \
"${proto_build_file_path}" \
"java_gapic_library(" \
"rest_numeric_enums = False" \
"true"
)
include_samples=$(get_config_from_BUILD \
"${proto_build_file_path}" \
"java_gapic_assembly_gradle_pkg(" \
"include_samples = True" \
"false"
)
transport=$(get_transport_from_BUILD "${proto_build_file_path}")
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${proto_build_file_path}")
include_samples=$(get_include_samples_from_BUILD "${proto_build_file_path}")
echo "GAPIC options are transport=${transport}, rest_numeric_enums=${rest_numeric_enums}, include_samples=${include_samples}."
# generate GAPIC client library
echo "Generating library from ${proto_path}, to ${destination_path}..."
Expand Down
106 changes: 70 additions & 36 deletions library_generation/test/generate_library_unit_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -230,40 +230,67 @@ generate_library_failed_with_invalid_grpc_version() {
cleanup "${destination}"
}

get_config_from_valid_BUILD_matched_test() {
build_file="${script_dir}/resources/misc/TESTBUILD.bazel"
rule="java_gapic_library("
# the pattern we expect to find in the BUILD file
pattern_should_match="name"
# default value if the pattern was not found
if_matched_return="got-a-match"
if_not_matched_return="no-match"
pattern_matched_result=$(get_config_from_BUILD \
"${build_file}" \
"${rule}" \
"${pattern_should_match}" \
"${if_not_matched_return}" \
"${if_matched_return}"
)
assertEquals "${if_matched_return}" "${pattern_matched_result}"
}

get_config_from_valid_BUILD_not_match_test() {
build_file="${script_dir}/resources/misc/TESTBUILD.bazel"
rule="java_gapic_library("
# the pattern that we should not find in the BUILD file
pattern_should_not_match="should-not-match"
# default value if the pattern was not found
if_matched_return="got-a-match"
if_not_matched_return="no-match"
pattern_not_matched_result=$(get_config_from_BUILD \
"${build_file}" \
"${rule}" \
"${pattern_should_not_match}" \
"${if_not_matched_return}" \
"${if_matched_return}"
)
assertEquals "${if_not_matched_return}" "${pattern_not_matched_result}"
get_transport_from_BUILD_grpc_rest_test() {
local build_file="${script_dir}/resources/misc/BUILD_grpc_rest.bazel"
local transport
transport=$(get_transport_from_BUILD "${build_file}")
assertEquals "grpc+rest" "${transport}"
}

get_transport_from_BUILD_grpc_test() {
local build_file="${script_dir}/resources/misc/BUILD_grpc.bazel"
local transport
transport=$(get_transport_from_BUILD "${build_file}")
assertEquals "grpc" "${transport}"
}

get_transport_from_BUILD_rest_test() {
local build_file="${script_dir}/resources/misc/BUILD_rest.bazel"
local transport
transport=$(get_transport_from_BUILD "${build_file}")
assertEquals "rest" "${transport}"
}

get_rest_numeric_enums_from_BUILD_true_test() {
local build_file="${script_dir}/resources/misc/BUILD_rest_numeric_enums_true.bazel"
local rest_numeric_enums
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${build_file}")
assertEquals "true" "${rest_numeric_enums}"
}

get_rest_numeric_enums_from_BUILD_false_test() {
local build_file="${script_dir}/resources/misc/BUILD_rest_numeric_enums_false.bazel"
local rest_numeric_enums
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${build_file}")
assertEquals "false" "${rest_numeric_enums}"
}

get_rest_numeric_enums_from_BUILD_empty_test() {
local build_file="${script_dir}/resources/misc/BUILD_rest_numeric_enums_empty.bazel"
local rest_numeric_enums
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${build_file}")
assertEquals "false" "${rest_numeric_enums}"
}

get_include_samples_from_BUILD_true_test() {
local build_file="${script_dir}/resources/misc/BUILD_include_samples_true.bazel"
local include_samples
include_samples=$(get_include_samples_from_BUILD "${build_file}")
assertEquals "true" "${include_samples}"
}

get_include_samples_from_BUILD_false_test() {
local build_file="${script_dir}/resources/misc/BUILD_include_samples_false.bazel"
local include_samples
include_samples=$(get_include_samples_from_BUILD "${build_file}")
assertEquals "false" "${include_samples}"
}

get_include_samples_from_BUILD_empty_test() {
local build_file="${script_dir}/resources/misc/BUILD_include_samples_empty.bazel"
local include_samples
include_samples=$(get_include_samples_from_BUILD "${build_file}")
assertEquals "false" "${include_samples}"
}

get_version_from_valid_WORKSPACE_test() {
Expand Down Expand Up @@ -312,8 +339,15 @@ test_list=(
generate_library_failed_with_invalid_generator_version
generate_library_failed_with_invalid_protobuf_version
generate_library_failed_with_invalid_grpc_version
get_config_from_valid_BUILD_matched_test
get_config_from_valid_BUILD_not_match_test
get_transport_from_BUILD_grpc_rest_test
get_transport_from_BUILD_grpc_test
get_transport_from_BUILD_rest_test
get_rest_numeric_enums_from_BUILD_true_test
get_rest_numeric_enums_from_BUILD_false_test
get_rest_numeric_enums_from_BUILD_empty_test
get_include_samples_from_BUILD_true_test
get_include_samples_from_BUILD_false_test
get_include_samples_from_BUILD_empty_test
get_version_from_valid_WORKSPACE_test
get_generator_version_from_valid_versions_txt_test
get_gax_version_from_valid_versions_txt_test
Expand Down
5 changes: 5 additions & 0 deletions library_generation/test/resources/misc/BUILD_grpc.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# this file is only used in testing `get_transport_from_BUILD` in utilities.sh

java_gapic_library(
transport = "grpc",
)
5 changes: 5 additions & 0 deletions library_generation/test/resources/misc/BUILD_grpc_rest.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# this file is only used in testing `get_transport_from_BUILD` in utilities.sh

java_gapic_library(
transport = "grpc+rest",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# this file is only used in testing `get_include_samples_from_BUILD` in utilities.sh

java_gapic_assembly_gradle_pkg(

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# this file is only used in testing `get_include_samples_from_BUILD` in utilities.sh

java_gapic_assembly_gradle_pkg(
include_samples = False,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# this file is only used in testing `get_include_samples_from_BUILD` in utilities.sh

java_gapic_assembly_gradle_pkg(
include_samples = True,
)
5 changes: 5 additions & 0 deletions library_generation/test/resources/misc/BUILD_rest.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# this file is only used in testing `get_transport_from_BUILD` in utilities.sh

java_gapic_library(
transport = "rest",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# this file is only used in testing `get_rest_numeric_enums_from_BUILD` in utilities.sh

java_gapic_library(

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# this file is only used in testing `get_rest_numeric_enums_from_BUILD` in utilities.sh

java_gapic_library(
rest_numeric_enums = False
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# this file is only used in testing `get_rest_numeric_enums_from_BUILD` in utilities.sh

java_gapic_library(
rest_numeric_enums = True
)
166 changes: 0 additions & 166 deletions library_generation/test/resources/misc/TESTBUILD.bazel

This file was deleted.

Loading

0 comments on commit 9c0316b

Please sign in to comment.