-
Notifications
You must be signed in to change notification settings - Fork 53
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
[ggj][bazel][goldens] fix: refactor and fix bazel golden_update rules #363
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain a little bit why the
def junit_output_zip
is replaced by thenative.java_binary
and it will make the build running before running overwrite_golden? Thanks