From 75911649f601dd07bcb0c4af5faf1d50ad692410 Mon Sep 17 00:00:00 2001 From: Xiaozhen Liu Date: Wed, 21 Oct 2020 18:20:40 +0000 Subject: [PATCH 1/2] goldens update bazel rules --- rules_bazel/java/integration_test.bzl | 83 +++++++++++++++++++++++++++ test/integration/BUILD.bazel | 7 +++ 2 files changed, 90 insertions(+) diff --git a/rules_bazel/java/integration_test.bzl b/rules_bazel/java/integration_test.bzl index ff00ae983c..8ae2cb829a 100644 --- a/rules_bazel/java/integration_test.bzl +++ b/rules_bazel/java/integration_test.bzl @@ -97,3 +97,86 @@ def integration_test(name, target, data): test_library = "%s_test" % target, srcs = data, ) + +def _overwrite_golden_impl(ctx): + # Extract the Java source files from the generated 3 srcjars from API bazel target, + # and put them in the temporary folder `codegen_tmp`, zip as `goldens_output_zip`. + # Overwrite the goldens folder e.g `test/integration/goldens/redis` with the + # code generation in `goldens_output_zip`. + + gapic_library = ctx.attr.gapic_library + resource_name_library = ctx.attr.resource_name_library + test_library = ctx.attr.test_library + srcs = ctx.files.srcs + # Convert the name of bazel rules e.g. `redis_update` to `redis` + # because we will need to overwrite the goldens files in `redis` folder. + api_name = "_".join(ctx.attr.name.split("_")[:-1]) + goldens_output_zip = ctx.outputs.goldens_output_zip + + script = """ + mkdir codegen_tmp + unzip -j {input} -d codegen_tmp + unzip -j {input_resource_name} -d codegen_tmp + unzip -j {input_test} -d codegen_tmp + cd codegen_tmp + # Remove unneeded non-Java files, like MANIFEST + rm -rf $(find . -type f ! -name "*.java") + zip -r ../{goldens_output_zip} . + """.format( + goldens_output_zip = goldens_output_zip.path, + input = gapic_library[JavaInfo].source_jars[0].path, + input_resource_name = resource_name_library[JavaInfo].source_jars[0].path, + input_test = test_library[JavaInfo].source_jars[0].path, + ) + + ctx.actions.run_shell( + inputs = srcs + [ + gapic_library[JavaInfo].source_jars[0], + resource_name_library[JavaInfo].source_jars[0], + test_library[JavaInfo].source_jars[0], + ], + outputs = [goldens_output_zip], + command = script, + ) + + # Overwrite the goldens. + golden_update_script_content = """ + cd ${{BUILD_WORKSPACE_DIRECTORY}} + unzip -ao {goldens_output_zip} -d test/integration/goldens/redis + """.format( + goldens_output_zip = goldens_output_zip.path, + api_name = api_name, + ) + ctx.actions.write( + output = ctx.outputs.golden_update_script, + content = golden_update_script_content, + is_executable = True, + ) + return [DefaultInfo(executable = ctx.outputs.golden_update_script)] + +overwrite_golden = rule( + attrs = { + "gapic_library": attr.label(), + "resource_name_library": attr.label(), + "test_library": attr.label(), + "srcs": attr.label_list( + allow_files = True, + mandatory = True, + ), + }, + outputs = { + "goldens_output_zip": "%{name}.zip", + "golden_update_script": "%{name}.sh", + }, + executable = True, + implementation = _overwrite_golden_impl, +) + +def golden_update(name, target, data): + overwrite_golden( + name = name, + gapic_library = target, + resource_name_library = "%s_resource_name" % target, + test_library = "%s_test" % target, + srcs = data, + ) diff --git a/test/integration/BUILD.bazel b/test/integration/BUILD.bazel index 11f96c0dbe..dbba3521cd 100644 --- a/test/integration/BUILD.bazel +++ b/test/integration/BUILD.bazel @@ -7,6 +7,7 @@ load( load( "//:rules_bazel/java/integration_test.bzl", "integration_test", + "golden_update", ) package(default_visibility = ["//visibility:public"]) @@ -61,3 +62,9 @@ integration_test( target = ":redis_java_gapic", data = ["//test/integration/goldens/redis:goldens_files"], ) + +golden_update( + name = "redis_update", + target = ":redis_java_gapic", + data = ["//test/integration/goldens/redis:goldens_files"], +) \ No newline at end of file From cee013ee9bade7d084903e6c02d429127fcb08bf Mon Sep 17 00:00:00 2001 From: Xiaozhen Liu Date: Wed, 21 Oct 2020 18:29:36 +0000 Subject: [PATCH 2/2] clean up --- rules_bazel/java/integration_test.bzl | 2 +- test/integration/BUILD.bazel | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rules_bazel/java/integration_test.bzl b/rules_bazel/java/integration_test.bzl index 8ae2cb829a..9ab7c917f7 100644 --- a/rules_bazel/java/integration_test.bzl +++ b/rules_bazel/java/integration_test.bzl @@ -142,7 +142,7 @@ def _overwrite_golden_impl(ctx): # Overwrite the goldens. golden_update_script_content = """ cd ${{BUILD_WORKSPACE_DIRECTORY}} - unzip -ao {goldens_output_zip} -d test/integration/goldens/redis + unzip -ao {goldens_output_zip} -d test/integration/goldens/{api_name} """.format( goldens_output_zip = goldens_output_zip.path, api_name = api_name, diff --git a/test/integration/BUILD.bazel b/test/integration/BUILD.bazel index dbba3521cd..e198de1801 100644 --- a/test/integration/BUILD.bazel +++ b/test/integration/BUILD.bazel @@ -67,4 +67,4 @@ golden_update( name = "redis_update", target = ":redis_java_gapic", data = ["//test/integration/goldens/redis:goldens_files"], -) \ No newline at end of file +)