Skip to content

Commit 128039b

Browse files
authored
chore: make integration test infrastructure sane (#904)
The you-can-do-anything-in-bazel mentality can easily lure one to a state where you are programming a build with a build tool instead of configuring a build. I just extracted the shell script portion out of a `.bzl` file into a file. This should also help switching to any build system from Bazel.
1 parent bfb35cd commit 128039b

File tree

5 files changed

+35
-108
lines changed

5 files changed

+35
-108
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Bazel.
22
bazel-*
3+
.gradle/
34

45
# IDE
56
.idea

rules_bazel/java/integration_test.bzl

-100
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,3 @@
1-
def _diff_integration_goldens_impl(ctx):
2-
# Extract the Java source files from the generated 3 srcjars from API bazel target,
3-
# and put them in the temporary folder `codegen_tmp`.
4-
# Compare the `codegen_tmp` with the goldens folder e.g `test/integration/goldens/redis`
5-
# and save the differences in output file `diff_output.txt`.
6-
7-
diff_output = ctx.outputs.diff_output
8-
check_diff_script = ctx.outputs.check_diff_script
9-
gapic_library = ctx.attr.gapic_library
10-
resource_name_library = ctx.attr.resource_name_library
11-
test_library = ctx.attr.test_library
12-
srcs = ctx.files.srcs
13-
api_name = ctx.attr.name
14-
15-
script = """
16-
mkdir codegen_tmp
17-
unzip {input} -d codegen_tmp
18-
unzip {input_resource_name} -d codegen_tmp
19-
unzip {input_test} -d codegen_tmp
20-
cd codegen_tmp
21-
# Remove unneeded non-Java files, like MANIFEST
22-
rm -rf $(find ./ -type f ! -name '*.java' -a ! -name '*gapic_metadata.json')
23-
rm -rf $(find ./ -type f -name 'PlaceholderFile.java')
24-
rm -r $(find ./ -type d -empty)
25-
cd ..
26-
diff -r codegen_tmp test/integration/goldens/{api_name} > {diff_output}
27-
# Bash `diff` command will return exit code 1 when there are differences between the two
28-
# folders. So we explicitly `exit 0` after the diff command to avoid build failure.
29-
exit 0
30-
""".format(
31-
diff_output = diff_output.path,
32-
input = gapic_library[JavaInfo].source_jars[0].path,
33-
input_resource_name = resource_name_library[JavaInfo].source_jars[0].path,
34-
input_test = test_library[JavaInfo].source_jars[0].path,
35-
api_name = api_name,
36-
)
37-
ctx.actions.run_shell(
38-
inputs = srcs + [
39-
gapic_library[JavaInfo].source_jars[0],
40-
resource_name_library[JavaInfo].source_jars[0],
41-
test_library[JavaInfo].source_jars[0],
42-
],
43-
outputs = [diff_output],
44-
command = script,
45-
)
46-
47-
# Check the generated diff_output file, if it is empty, that means there is no difference
48-
# between generated source code and goldens files, test should pass. If it is not empty, then
49-
# test will fail by exiting 1.
50-
51-
check_diff_script_content = """
52-
# This will not print diff_output to the console unless `--test_output=all` option
53-
# is enabled, it only emits the comparison results to the test.log.
54-
# We could not copy the diff_output.txt to the test.log ($XML_OUTPUT_FILE) because that
55-
# file is not existing at the moment. It is generated once test is finished.
56-
cat $PWD/test/integration/{api_name}_diff_output.txt
57-
if [ -s $PWD/test/integration/{api_name}_diff_output.txt ]
58-
then
59-
exit 1
60-
fi
61-
""".format(
62-
api_name = api_name,
63-
)
64-
65-
ctx.actions.write(
66-
output = check_diff_script,
67-
content = check_diff_script_content,
68-
)
69-
runfiles = ctx.runfiles(files = [ctx.outputs.diff_output])
70-
return [DefaultInfo(executable = check_diff_script, runfiles = runfiles)]
71-
72-
diff_integration_goldens_test = rule(
73-
attrs = {
74-
"gapic_library": attr.label(),
75-
"resource_name_library": attr.label(),
76-
"test_library": attr.label(),
77-
"srcs": attr.label_list(
78-
allow_files = True,
79-
mandatory = True,
80-
),
81-
},
82-
outputs = {
83-
"diff_output": "%{name}_diff_output.txt",
84-
"check_diff_script": "%{name}_check_diff_script.sh",
85-
},
86-
implementation = _diff_integration_goldens_impl,
87-
test = True,
88-
)
89-
90-
def integration_test(name, target, data):
91-
# Bazel target `java_gapic_library` will generate 3 source jars including the
92-
# the source Java code of the gapic_library, resource_name_library and test_library.
93-
diff_integration_goldens_test(
94-
name = name,
95-
gapic_library = target,
96-
resource_name_library = "%s_resource_name" % target,
97-
test_library = "%s_test" % target,
98-
srcs = data,
99-
)
100-
1011
def _overwrite_golden_impl(ctx):
1022
# Extract the Java source files from the generated 3 srcjars from API bazel target,
1033
# and put them in the temporary folder `codegen_tmp`, zip as `goldens_output_zip`.

scripts/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package(default_visibility = ["//test/integration:__pkg__"])
2+
3+
exports_files(["diff_gen_and_golden.sh"])

scripts/diff_gen_and_golden.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
5+
API_NAME=$1
6+
RAW_SRCJAR=$( find . -name '*_java_gapic_srcjar_raw.srcjar' )
7+
8+
mkdir unpacked src
9+
cd unpacked
10+
unzip -q -c "../${RAW_SRCJAR}" temp-codegen.srcjar | jar x
11+
cp -rf src/main/java/* ../src
12+
cp -rf src/test/java/* ../src
13+
[ -d proto ] && cp -rf proto/src/main/java/* ../src
14+
cd ..
15+
16+
# Remove unneeded non-Java files, like MANIFEST
17+
find src -type f ! -name '*.java' -a ! -name '*gapic_metadata.json' -delete
18+
find src -type f -name 'PlaceholderFile.java' -delete
19+
find src -type d -empty -delete
20+
21+
# This will not print diff_output to the console unless `--test_output=all` option
22+
# is enabled, it only emits the comparison results to the test.log.
23+
diff -ru src test/integration/goldens/${API_NAME}

test/integration/BUILD.bazel

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ load(
99
"java_gapic_assembly_gradle_pkg",
1010
)
1111
load("@rules_gapic//:gapic.bzl", "proto_library_with_info")
12-
load(
13-
"//:rules_bazel/java/integration_test.bzl",
14-
"golden_update",
15-
"integration_test",
16-
)
12+
load("//:rules_bazel/java/integration_test.bzl", "golden_update")
1713
load("@rules_proto//proto:defs.bzl", "proto_library")
1814

1915
package(default_visibility = ["//visibility:public"])
@@ -49,10 +45,14 @@ API_GAPIC_TARGETS = {
4945
"compute": "@com_google_googleapis_discovery//google/cloud/compute/v1:compute_small_java_gapic",
5046
}
5147

52-
[integration_test(
48+
[sh_test(
5349
name = lib_name,
54-
data = ["//test/integration/goldens/%s:goldens_files" % lib_name],
55-
target = API_GAPIC_TARGETS[lib_name],
50+
srcs = ["//scripts:diff_gen_and_golden.sh"],
51+
args = [lib_name],
52+
data = [
53+
API_GAPIC_TARGETS[lib_name] + "_srcjar_raw.srcjar",
54+
"//test/integration/goldens/%s:goldens_files" % lib_name,
55+
],
5656
) for lib_name in INTEGRATION_TEST_LIBRARIES]
5757

5858
[golden_update(

0 commit comments

Comments
 (0)