Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor test utilities #2013

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions library_generation/test/generate_library_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ set -xeo pipefail
googleapis_gen_url="[email protected]:googleapis/googleapis-gen.git"
script_dir=$(dirname "$(readlink -f "$0")")
proto_path_list="${script_dir}/resources/proto_path_list.txt"
source "${script_dir}/../utilities.sh"
library_generation_dir="${script_dir}"/..
output_folder="$(get_output_folder)"
source "${script_dir}/test_utilities.sh"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now integration test script only source test_utilities.sh

output_folder="$(pwd)/output"

while [[ $# -gt 0 ]]; do
key="$1"
Expand All @@ -39,8 +39,6 @@ esac
shift # past argument or value
done

script_dir=$(dirname "$(readlink -f "$0")")
source "${script_dir}/../utilities.sh"
library_generation_dir="${script_dir}"/..
mkdir -p "${output_folder}"
pushd "${output_folder}"
Expand Down
14 changes: 0 additions & 14 deletions library_generation/test/generate_library_unit_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -290,18 +290,6 @@ get_version_from_valid_WORKSPACE_test() {
assertEquals '2.25.1-SNAPSHOT' "${obtained_ggj_version}"
}

get_generator_version_from_valid_versions_txt_test() {
versions_file="${script_dir}/resources/misc/testversions.txt"
obtained_ggj_version=$(get_version_from_versions_txt "${versions_file}" "gapic-generator-java")
assertEquals '2.25.1-SNAPSHOT' "${obtained_ggj_version}"
}

get_gax_version_from_valid_versions_txt_test() {
versions_file="${script_dir}/resources/misc/testversions.txt"
obtained_gax_version=$(get_version_from_versions_txt "${versions_file}" "gax")
assertEquals '2.33.1-SNAPSHOT' "${obtained_gax_version}"
}

# Execute tests.
# One line per test.
test_list=(
Expand Down Expand Up @@ -340,8 +328,6 @@ test_list=(
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
)

pushd "${script_dir}"
Expand Down
148 changes: 147 additions & 1 deletion library_generation/test/test_utilities.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ succeed_num=0
failed_num=0
failed_tests=""

# Helper functions, they shouldn't be called outside this file.
############# Helper functions, they shouldn't be called outside this file #############
__test_executed() {
total_num=$((1 + total_num))
}
Expand All @@ -25,6 +25,53 @@ __test_failed() {
failed_tests="${failed_tests} ${failed_test}"
}

# Used to obtain configuration values from a bazel BUILD file
#
# inspects a $build_file for a certain $rule (e.g. java_gapic_library). If the
# first 15 lines after the declaration of the rule contain $pattern, then
# it will return $if_match if $pattern is found, otherwise $default
__get_config_from_BUILD() {
build_file=$1
rule=$2
pattern=$3
default=$4
if_match=$5

result="${default}"
if grep -A 20 "${rule}" "${build_file}" | grep -q "${pattern}"; then
result="${if_match}"
fi
echo "${result}"
}

__get_iam_policy_from_BUILD() {
local build_file=$1
local contains_iam_policy
contains_iam_policy=$(__get_config_from_BUILD \
"${build_file}" \
"proto_library_with_info(" \
"//google/iam/v1:iam_policy_proto" \
"false" \
"true"
)
echo "${contains_iam_policy}"
}

__get_locations_from_BUILD() {
local build_file=$1
local contains_locations
contains_locations=$(__get_config_from_BUILD \
"${build_file}" \
"proto_library_with_info(" \
"//google/cloud/location:location_proto" \
"false" \
"true"
)
echo "${contains_locations}"
}

############# Functions used in test execution #############

assertEquals() {
local expected=$1
local actual=$2
Expand Down Expand Up @@ -77,3 +124,102 @@ execute_tests() {
echo "Failed test(s): ${failed_tests}."
exit 1
}

############# Utility functions used in `generate_library_integration_tests.sh` #############

# Apart from proto files in proto_path, additional protos are needed in order
# to generate GAPIC client libraries.
# In most cases, these protos should be within google/ directory, which is
# pulled from googleapis as a prerequisite.
# Get additional protos in BUILD.bazel.
get_gapic_additional_protos_from_BUILD() {
local build_file=$1
local gapic_additional_protos="google/cloud/common_resources.proto"
if [[ $(__get_iam_policy_from_BUILD "${build_file}") == "true" ]]; then
gapic_additional_protos="${gapic_additional_protos} google/iam/v1/iam_policy.proto"
fi
if [[ $(__get_locations_from_BUILD "${build_file}") == "true" ]]; then
gapic_additional_protos="${gapic_additional_protos} google/cloud/location/locations.proto"
fi
echo "${gapic_additional_protos}"
}

get_transport_from_BUILD() {
local build_file=$1
local transport
transport=$(__get_config_from_BUILD \
"${build_file}" \
"java_gapic_library(" \
"grpc+rest" \
"grpc" \
"grpc+rest"
)
# search again because the transport maybe `rest`.
transport=$(__get_config_from_BUILD \
"${build_file}" \
"java_gapic_library(" \
"transport = \"rest\"" \
"${transport}" \
"rest"
)
echo "${transport}"
}

get_rest_numeric_enums_from_BUILD() {
local build_file=$1
local rest_numeric_enums
rest_numeric_enums=$(__get_config_from_BUILD \
"${build_file}" \
"java_gapic_library(" \
"rest_numeric_enums = True" \
"false" \
"true"
)
echo "${rest_numeric_enums}"
}

get_include_samples_from_BUILD() {
local build_file=$1
local include_samples
include_samples=$(__get_config_from_BUILD \
"${build_file}" \
"java_gapic_assembly_gradle_pkg(" \
"include_samples = True" \
"false" \
"true"
)
echo "${include_samples}"
}

# Obtains a version from a bazel WORKSPACE file
#
# versions look like "_ggj_version="1.2.3"
# It will return 1.2.3 for such example
get_version_from_WORKSPACE() {
version_key_word=$1
workspace=$2
version=$(\
grep "${version_key_word}" "${workspace}" |\
head -n 1 |\
sed 's/\(.*\) = "\(.*\)"\(.*\)/\2/' |\
sed 's/[a-zA-Z-]*//'
)
echo "${version}"
}

# Convenience function to clone only the necessary folders from a git repository
sparse_clone() {
repo_url=$1
paths=$2
commitish=$3
clone_dir=$(basename "${repo_url%.*}")
rm -rf "${clone_dir}"
git clone -n --depth=1 --no-single-branch --filter=tree:0 "${repo_url}"
pushd "${clone_dir}"
if [ -n "${commitish}" ]; then
git checkout "${commitish}"
fi
git sparse-checkout set --no-cone ${paths}
git checkout
popd
}
155 changes: 1 addition & 154 deletions library_generation/utilities.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,7 @@

set -xeo pipefail


# private functions that should not be called outside this file.

# Used to obtain configuration values from a bazel BUILD file
#
# inspects a $build_file for a certain $rule (e.g. java_gapic_library). If the
# first 15 lines after the declaration of the rule contain $pattern, then
# it will return $if_match if $pattern is found, otherwise $default
__get_config_from_BUILD() {
build_file=$1
rule=$2
pattern=$3
default=$4
if_match=$5

result="${default}"
if grep -A 20 "${rule}" "${build_file}" | grep -q "${pattern}"; then
result="${if_match}"
fi
echo "${result}"
}

__get_iam_policy_from_BUILD() {
local build_file=$1
local contains_iam_policy
contains_iam_policy=$(__get_config_from_BUILD \
"${build_file}" \
"proto_library_with_info(" \
"//google/iam/v1:iam_policy_proto" \
"false" \
"true"
)
echo "${contains_iam_policy}"
}

__get_locations_from_BUILD() {
local build_file=$1
local contains_locations
contains_locations=$(__get_config_from_BUILD \
"${build_file}" \
"proto_library_with_info(" \
"//google/cloud/location:location_proto" \
"false" \
"true"
)
echo "${contains_locations}"
}

# define utility functions
# Utility functions used in `generate_library.sh` and showcase generation.
extract_folder_name() {
local destination_path=$1
local folder_name=${destination_path##*/}
Expand Down Expand Up @@ -236,111 +188,6 @@ download_fail() {
exit 1
}

# Obtains a version from a bazel WORKSPACE file
#
# versions look like "_ggj_version="1.2.3"
# It will return 1.2.3 for such example
get_version_from_WORKSPACE() {
version_key_word=$1
workspace=$2
version=$(\
grep "${version_key_word}" "${workspace}" |\
head -n 1 |\
sed 's/\(.*\) = "\(.*\)"\(.*\)/\2/' |\
sed 's/[a-zA-Z-]*//'
)
echo "${version}"
}

# Apart from proto files in proto_path, additional protos are needed in order
# to generate GAPIC client libraries.
# In most cases, these protos should be within google/ directory, which is
# pulled from googleapis as a prerequisite.
# Get additional protos in BUILD.bazel.
get_gapic_additional_protos_from_BUILD() {
local build_file=$1
local gapic_additional_protos="google/cloud/common_resources.proto"
if [[ $(__get_iam_policy_from_BUILD "${build_file}") == "true" ]]; then
gapic_additional_protos="${gapic_additional_protos} google/iam/v1/iam_policy.proto"
fi
if [[ $(__get_locations_from_BUILD "${build_file}") == "true" ]]; then
gapic_additional_protos="${gapic_additional_protos} google/cloud/location/locations.proto"
fi
echo "${gapic_additional_protos}"
}

get_transport_from_BUILD() {
local build_file=$1
local transport
transport=$(__get_config_from_BUILD \
"${build_file}" \
"java_gapic_library(" \
"grpc+rest" \
"grpc" \
"grpc+rest"
)
# search again because the transport maybe `rest`.
transport=$(__get_config_from_BUILD \
"${build_file}" \
"java_gapic_library(" \
"transport = \"rest\"" \
"${transport}" \
"rest"
)
echo "${transport}"
}

get_rest_numeric_enums_from_BUILD() {
local build_file=$1
local rest_numeric_enums
rest_numeric_enums=$(__get_config_from_BUILD \
"${build_file}" \
"java_gapic_library(" \
"rest_numeric_enums = True" \
"false" \
"true"
)
echo "${rest_numeric_enums}"
}

get_include_samples_from_BUILD() {
local build_file=$1
local include_samples
include_samples=$(__get_config_from_BUILD \
"${build_file}" \
"java_gapic_assembly_gradle_pkg(" \
"include_samples = True" \
"false" \
"true"
)
echo "${include_samples}"
}

# Convenience function to clone only the necessary folders from a git repository
sparse_clone() {
repo_url=$1
paths=$2
commitish=$3
clone_dir=$(basename "${repo_url%.*}")
rm -rf "${clone_dir}"
git clone -n --depth=1 --no-single-branch --filter=tree:0 "${repo_url}"
pushd "${clone_dir}"
if [ -n "${commitish}" ]; then
git checkout "${commitish}"
fi
git sparse-checkout set --no-cone ${paths}
git checkout
popd
}

# takes a versions.txt file and returns its version
get_version_from_versions_txt() {
versions=$1
key=$2
version=$(grep "$key:" "${versions}" | cut -d: -f3) # 3rd field is snapshot
echo "${version}"
}

# gets the output folder where all sources and dependencies will be located. It
# relies on utilities_script_dir which points to the same location as
# `generate_library.sh`
Expand Down
Loading
Loading