-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ggj][bazel][goldens] fix: refactor and fix bazel golden_update rules (…
…#363) * fix: use imperative bazel rule names * fix: refactor and fix bazel golden_update rules
- Loading branch information
Showing
6 changed files
with
138 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,84 @@ | ||
def _junit_output_impl(ctx): | ||
test_class_name = ctx.attr.test_class_name | ||
def _overwrite_golden_impl(ctx): | ||
test_class = ctx.attr.test_class | ||
inputs = ctx.files.srcs | ||
output = ctx.outputs.output | ||
goldens_output_zip = ctx.outputs.goldens_output_zip | ||
test_runner = ctx.executable.test_runner | ||
|
||
command = """ | ||
mkdir local_tmp | ||
TEST_OUTPUT_HOME="$(pwd)/local_tmp" \ | ||
{test_runner_path} $@ | ||
cd local_tmp | ||
# Generate the goldens from tests. | ||
generate_goldens_script = """ | ||
mkdir local_tmp | ||
TEST_OUTPUT_HOME="$(pwd)/local_tmp" {test_runner_path} $@ | ||
cd local_tmp | ||
# Zip all files under local_tmp with all nested parent folders except for local_tmp itself. | ||
# Zip files because there are cases that one Junit test can produce multiple goldens. | ||
zip -r ../{output} . | ||
zip -r ../{goldens_output_zip} . | ||
""".format( | ||
test_runner_path = test_runner.path, | ||
output=output.path, | ||
goldens_output_zip = goldens_output_zip.path, | ||
) | ||
|
||
ctx.actions.run_shell( | ||
inputs = inputs, | ||
outputs = [output], | ||
arguments = [test_class_name], | ||
outputs = [goldens_output_zip], | ||
arguments = [test_class], | ||
tools = [test_runner], | ||
command = command, | ||
command = generate_goldens_script, | ||
) | ||
|
||
junit_output_zip = rule( | ||
attrs = { | ||
"test_class_name": attr.string(mandatory=True), | ||
"srcs": attr.label_list( | ||
allow_files = True, | ||
mandatory = True, | ||
), | ||
"test_runner": attr.label( | ||
mandatory = True, | ||
executable = True, | ||
cfg = "host", | ||
), | ||
}, | ||
outputs = { | ||
"output": "%{name}.zip", | ||
}, | ||
implementation = _junit_output_impl, | ||
) | ||
|
||
def _overwrite_golden_impl(ctx): | ||
script_content = """ | ||
# Overwrite the goldens. | ||
golden_update_script_content = """ | ||
#!/bin/bash | ||
cd ${{BUILD_WORKSPACE_DIRECTORY}} | ||
unzip -ao {unit_test_results} -d src/test/java | ||
unzip -ao {goldens_output_zip} -d src/test/java | ||
""".format( | ||
unit_test_results = ctx.file.unit_test_results.path, | ||
goldens_output_zip = goldens_output_zip.path, | ||
) | ||
ctx.actions.write( | ||
output = ctx.outputs.bin, | ||
content = script_content, | ||
output = ctx.outputs.golden_update_script, | ||
content = golden_update_script_content, | ||
is_executable = True, | ||
) | ||
return [DefaultInfo(executable = ctx.outputs.bin)] | ||
|
||
return [DefaultInfo(executable = ctx.outputs.golden_update_script)] | ||
|
||
overwrite_golden = rule( | ||
attrs = { | ||
"unit_test_results": attr.label( | ||
"test_class": attr.string(mandatory = True), | ||
"srcs": attr.label_list( | ||
allow_files = True, | ||
mandatory = True, | ||
allow_single_file = True), | ||
), | ||
"test_runner": attr.label( | ||
mandatory = True, | ||
executable = True, | ||
cfg = "host", | ||
), | ||
}, | ||
outputs = { | ||
"bin": "%{name}.sh", | ||
"goldens_output_zip": "%{name}.zip", | ||
"golden_update_script": "%{name}.sh", | ||
}, | ||
executable = True, | ||
implementation = _overwrite_golden_impl, | ||
) | ||
|
||
def golden_update(name, test_class_name, srcs): | ||
junit_output_name = "%s_output" % name | ||
junit_output_zip( | ||
name = junit_output_name, | ||
test_class_name = test_class_name, | ||
test_runner = "//:golden_update_junit_runner", | ||
def golden_update( | ||
name, | ||
srcs, | ||
test_class, | ||
data = [], | ||
deps = []): | ||
golden_junit_runner_name = "%s_junit_runner" % name | ||
native.java_binary( | ||
name = golden_junit_runner_name, | ||
srcs = srcs, | ||
data = data, | ||
main_class = "com.google.api.generator.test.framework.SingleJUnitTestRunner", | ||
deps = ["//src/test/java/com/google/api/generator/test/framework:junit_runner"] + deps, | ||
) | ||
|
||
overwrite_golden( | ||
name = name, | ||
unit_test_results = ":%s" % junit_output_name | ||
test_class = test_class, | ||
test_runner = ":%s" % golden_junit_runner_name, | ||
srcs = srcs + data, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.