Skip to content

Commit 1f4aa27

Browse files
authored
chore: refactor test utilities (#2013)
1 parent ed16ac7 commit 1f4aa27

File tree

5 files changed

+159
-175
lines changed

5 files changed

+159
-175
lines changed

library_generation/test/generate_library_integration_test.sh

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ set -xeo pipefail
1616
googleapis_gen_url="[email protected]:googleapis/googleapis-gen.git"
1717
script_dir=$(dirname "$(readlink -f "$0")")
1818
proto_path_list="${script_dir}/resources/proto_path_list.txt"
19-
source "${script_dir}/../utilities.sh"
2019
library_generation_dir="${script_dir}"/..
21-
output_folder="$(get_output_folder)"
20+
source "${script_dir}/test_utilities.sh"
21+
output_folder="$(pwd)/output"
2222

2323
while [[ $# -gt 0 ]]; do
2424
key="$1"
@@ -39,8 +39,6 @@ esac
3939
shift # past argument or value
4040
done
4141

42-
script_dir=$(dirname "$(readlink -f "$0")")
43-
source "${script_dir}/../utilities.sh"
4442
library_generation_dir="${script_dir}"/..
4543
mkdir -p "${output_folder}"
4644
pushd "${output_folder}"

library_generation/test/generate_library_unit_tests.sh

-14
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,6 @@ get_version_from_valid_WORKSPACE_test() {
290290
assertEquals '2.25.1-SNAPSHOT' "${obtained_ggj_version}"
291291
}
292292

293-
get_generator_version_from_valid_versions_txt_test() {
294-
versions_file="${script_dir}/resources/misc/testversions.txt"
295-
obtained_ggj_version=$(get_version_from_versions_txt "${versions_file}" "gapic-generator-java")
296-
assertEquals '2.25.1-SNAPSHOT' "${obtained_ggj_version}"
297-
}
298-
299-
get_gax_version_from_valid_versions_txt_test() {
300-
versions_file="${script_dir}/resources/misc/testversions.txt"
301-
obtained_gax_version=$(get_version_from_versions_txt "${versions_file}" "gax")
302-
assertEquals '2.33.1-SNAPSHOT' "${obtained_gax_version}"
303-
}
304-
305293
# Execute tests.
306294
# One line per test.
307295
test_list=(
@@ -340,8 +328,6 @@ test_list=(
340328
get_include_samples_from_BUILD_false_test
341329
get_include_samples_from_BUILD_empty_test
342330
get_version_from_valid_WORKSPACE_test
343-
get_generator_version_from_valid_versions_txt_test
344-
get_gax_version_from_valid_versions_txt_test
345331
)
346332

347333
pushd "${script_dir}"

library_generation/test/test_utilities.sh

+147-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ succeed_num=0
1010
failed_num=0
1111
failed_tests=""
1212

13-
# Helper functions, they shouldn't be called outside this file.
13+
############# Helper functions, they shouldn't be called outside this file #############
1414
__test_executed() {
1515
total_num=$((1 + total_num))
1616
}
@@ -25,6 +25,53 @@ __test_failed() {
2525
failed_tests="${failed_tests} ${failed_test}"
2626
}
2727

28+
# Used to obtain configuration values from a bazel BUILD file
29+
#
30+
# inspects a $build_file for a certain $rule (e.g. java_gapic_library). If the
31+
# first 15 lines after the declaration of the rule contain $pattern, then
32+
# it will return $if_match if $pattern is found, otherwise $default
33+
__get_config_from_BUILD() {
34+
build_file=$1
35+
rule=$2
36+
pattern=$3
37+
default=$4
38+
if_match=$5
39+
40+
result="${default}"
41+
if grep -A 20 "${rule}" "${build_file}" | grep -q "${pattern}"; then
42+
result="${if_match}"
43+
fi
44+
echo "${result}"
45+
}
46+
47+
__get_iam_policy_from_BUILD() {
48+
local build_file=$1
49+
local contains_iam_policy
50+
contains_iam_policy=$(__get_config_from_BUILD \
51+
"${build_file}" \
52+
"proto_library_with_info(" \
53+
"//google/iam/v1:iam_policy_proto" \
54+
"false" \
55+
"true"
56+
)
57+
echo "${contains_iam_policy}"
58+
}
59+
60+
__get_locations_from_BUILD() {
61+
local build_file=$1
62+
local contains_locations
63+
contains_locations=$(__get_config_from_BUILD \
64+
"${build_file}" \
65+
"proto_library_with_info(" \
66+
"//google/cloud/location:location_proto" \
67+
"false" \
68+
"true"
69+
)
70+
echo "${contains_locations}"
71+
}
72+
73+
############# Functions used in test execution #############
74+
2875
assertEquals() {
2976
local expected=$1
3077
local actual=$2
@@ -77,3 +124,102 @@ execute_tests() {
77124
echo "Failed test(s): ${failed_tests}."
78125
exit 1
79126
}
127+
128+
############# Utility functions used in `generate_library_integration_tests.sh` #############
129+
130+
# Apart from proto files in proto_path, additional protos are needed in order
131+
# to generate GAPIC client libraries.
132+
# In most cases, these protos should be within google/ directory, which is
133+
# pulled from googleapis as a prerequisite.
134+
# Get additional protos in BUILD.bazel.
135+
get_gapic_additional_protos_from_BUILD() {
136+
local build_file=$1
137+
local gapic_additional_protos="google/cloud/common_resources.proto"
138+
if [[ $(__get_iam_policy_from_BUILD "${build_file}") == "true" ]]; then
139+
gapic_additional_protos="${gapic_additional_protos} google/iam/v1/iam_policy.proto"
140+
fi
141+
if [[ $(__get_locations_from_BUILD "${build_file}") == "true" ]]; then
142+
gapic_additional_protos="${gapic_additional_protos} google/cloud/location/locations.proto"
143+
fi
144+
echo "${gapic_additional_protos}"
145+
}
146+
147+
get_transport_from_BUILD() {
148+
local build_file=$1
149+
local transport
150+
transport=$(__get_config_from_BUILD \
151+
"${build_file}" \
152+
"java_gapic_library(" \
153+
"grpc+rest" \
154+
"grpc" \
155+
"grpc+rest"
156+
)
157+
# search again because the transport maybe `rest`.
158+
transport=$(__get_config_from_BUILD \
159+
"${build_file}" \
160+
"java_gapic_library(" \
161+
"transport = \"rest\"" \
162+
"${transport}" \
163+
"rest"
164+
)
165+
echo "${transport}"
166+
}
167+
168+
get_rest_numeric_enums_from_BUILD() {
169+
local build_file=$1
170+
local rest_numeric_enums
171+
rest_numeric_enums=$(__get_config_from_BUILD \
172+
"${build_file}" \
173+
"java_gapic_library(" \
174+
"rest_numeric_enums = True" \
175+
"false" \
176+
"true"
177+
)
178+
echo "${rest_numeric_enums}"
179+
}
180+
181+
get_include_samples_from_BUILD() {
182+
local build_file=$1
183+
local include_samples
184+
include_samples=$(__get_config_from_BUILD \
185+
"${build_file}" \
186+
"java_gapic_assembly_gradle_pkg(" \
187+
"include_samples = True" \
188+
"false" \
189+
"true"
190+
)
191+
echo "${include_samples}"
192+
}
193+
194+
# Obtains a version from a bazel WORKSPACE file
195+
#
196+
# versions look like "_ggj_version="1.2.3"
197+
# It will return 1.2.3 for such example
198+
get_version_from_WORKSPACE() {
199+
version_key_word=$1
200+
workspace=$2
201+
version=$(\
202+
grep "${version_key_word}" "${workspace}" |\
203+
head -n 1 |\
204+
sed 's/\(.*\) = "\(.*\)"\(.*\)/\2/' |\
205+
sed 's/[a-zA-Z-]*//'
206+
)
207+
echo "${version}"
208+
}
209+
210+
# Convenience function to clone only the necessary folders from a git repository
211+
sparse_clone() {
212+
repo_url=$1
213+
paths=$2
214+
commitish=$3
215+
clone_dir=$(basename "${repo_url%.*}")
216+
rm -rf "${clone_dir}"
217+
git clone -n --depth=1 --no-single-branch --filter=tree:0 "${repo_url}"
218+
pushd "${clone_dir}"
219+
if [ -n "${commitish}" ]; then
220+
git checkout "${commitish}"
221+
fi
222+
git sparse-checkout set --no-cone ${paths}
223+
git checkout
224+
popd
225+
}

library_generation/utilities.sh

+1-154
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,7 @@
22

33
set -xeo pipefail
44

5-
6-
# private functions that should not be called outside this file.
7-
8-
# Used to obtain configuration values from a bazel BUILD file
9-
#
10-
# inspects a $build_file for a certain $rule (e.g. java_gapic_library). If the
11-
# first 15 lines after the declaration of the rule contain $pattern, then
12-
# it will return $if_match if $pattern is found, otherwise $default
13-
__get_config_from_BUILD() {
14-
build_file=$1
15-
rule=$2
16-
pattern=$3
17-
default=$4
18-
if_match=$5
19-
20-
result="${default}"
21-
if grep -A 20 "${rule}" "${build_file}" | grep -q "${pattern}"; then
22-
result="${if_match}"
23-
fi
24-
echo "${result}"
25-
}
26-
27-
__get_iam_policy_from_BUILD() {
28-
local build_file=$1
29-
local contains_iam_policy
30-
contains_iam_policy=$(__get_config_from_BUILD \
31-
"${build_file}" \
32-
"proto_library_with_info(" \
33-
"//google/iam/v1:iam_policy_proto" \
34-
"false" \
35-
"true"
36-
)
37-
echo "${contains_iam_policy}"
38-
}
39-
40-
__get_locations_from_BUILD() {
41-
local build_file=$1
42-
local contains_locations
43-
contains_locations=$(__get_config_from_BUILD \
44-
"${build_file}" \
45-
"proto_library_with_info(" \
46-
"//google/cloud/location:location_proto" \
47-
"false" \
48-
"true"
49-
)
50-
echo "${contains_locations}"
51-
}
52-
53-
# define utility functions
5+
# Utility functions used in `generate_library.sh` and showcase generation.
546
extract_folder_name() {
557
local destination_path=$1
568
local folder_name=${destination_path##*/}
@@ -236,111 +188,6 @@ download_fail() {
236188
exit 1
237189
}
238190

239-
# Obtains a version from a bazel WORKSPACE file
240-
#
241-
# versions look like "_ggj_version="1.2.3"
242-
# It will return 1.2.3 for such example
243-
get_version_from_WORKSPACE() {
244-
version_key_word=$1
245-
workspace=$2
246-
version=$(\
247-
grep "${version_key_word}" "${workspace}" |\
248-
head -n 1 |\
249-
sed 's/\(.*\) = "\(.*\)"\(.*\)/\2/' |\
250-
sed 's/[a-zA-Z-]*//'
251-
)
252-
echo "${version}"
253-
}
254-
255-
# Apart from proto files in proto_path, additional protos are needed in order
256-
# to generate GAPIC client libraries.
257-
# In most cases, these protos should be within google/ directory, which is
258-
# pulled from googleapis as a prerequisite.
259-
# Get additional protos in BUILD.bazel.
260-
get_gapic_additional_protos_from_BUILD() {
261-
local build_file=$1
262-
local gapic_additional_protos="google/cloud/common_resources.proto"
263-
if [[ $(__get_iam_policy_from_BUILD "${build_file}") == "true" ]]; then
264-
gapic_additional_protos="${gapic_additional_protos} google/iam/v1/iam_policy.proto"
265-
fi
266-
if [[ $(__get_locations_from_BUILD "${build_file}") == "true" ]]; then
267-
gapic_additional_protos="${gapic_additional_protos} google/cloud/location/locations.proto"
268-
fi
269-
echo "${gapic_additional_protos}"
270-
}
271-
272-
get_transport_from_BUILD() {
273-
local build_file=$1
274-
local transport
275-
transport=$(__get_config_from_BUILD \
276-
"${build_file}" \
277-
"java_gapic_library(" \
278-
"grpc+rest" \
279-
"grpc" \
280-
"grpc+rest"
281-
)
282-
# search again because the transport maybe `rest`.
283-
transport=$(__get_config_from_BUILD \
284-
"${build_file}" \
285-
"java_gapic_library(" \
286-
"transport = \"rest\"" \
287-
"${transport}" \
288-
"rest"
289-
)
290-
echo "${transport}"
291-
}
292-
293-
get_rest_numeric_enums_from_BUILD() {
294-
local build_file=$1
295-
local rest_numeric_enums
296-
rest_numeric_enums=$(__get_config_from_BUILD \
297-
"${build_file}" \
298-
"java_gapic_library(" \
299-
"rest_numeric_enums = True" \
300-
"false" \
301-
"true"
302-
)
303-
echo "${rest_numeric_enums}"
304-
}
305-
306-
get_include_samples_from_BUILD() {
307-
local build_file=$1
308-
local include_samples
309-
include_samples=$(__get_config_from_BUILD \
310-
"${build_file}" \
311-
"java_gapic_assembly_gradle_pkg(" \
312-
"include_samples = True" \
313-
"false" \
314-
"true"
315-
)
316-
echo "${include_samples}"
317-
}
318-
319-
# Convenience function to clone only the necessary folders from a git repository
320-
sparse_clone() {
321-
repo_url=$1
322-
paths=$2
323-
commitish=$3
324-
clone_dir=$(basename "${repo_url%.*}")
325-
rm -rf "${clone_dir}"
326-
git clone -n --depth=1 --no-single-branch --filter=tree:0 "${repo_url}"
327-
pushd "${clone_dir}"
328-
if [ -n "${commitish}" ]; then
329-
git checkout "${commitish}"
330-
fi
331-
git sparse-checkout set --no-cone ${paths}
332-
git checkout
333-
popd
334-
}
335-
336-
# takes a versions.txt file and returns its version
337-
get_version_from_versions_txt() {
338-
versions=$1
339-
key=$2
340-
version=$(grep "$key:" "${versions}" | cut -d: -f3) # 3rd field is snapshot
341-
echo "${version}"
342-
}
343-
344191
# gets the output folder where all sources and dependencies will be located. It
345192
# relies on utilities_script_dir which points to the same location as
346193
# `generate_library.sh`

0 commit comments

Comments
 (0)