Bazel powered Java testing (#8506)
* Protobuf Java/Core Tests running w/ Bazel. Also integrates rules_jvm_external and migrates existing maven deps in place. * Add test_suite target that maps to rule name. * Lite tests passing in Bazel * util tests passing with Bazel. * Add conformance and build testing to //java:core * Cleanup bzl style and lock down access to failure lists. * Adding Java Lite conformance tests. * rm newline * parameterize conformance_test This makes usage of failure lists more explicit. * restrict visibility more for newly added libs and fix formatting. * fix formatting and visibility. * move testing.bzl to an internal package. * fix file formatting. * moving conformance_test to internal.bzlpull/8519/head
parent
fecf7e9340
commit
bc45f92262
13 changed files with 655 additions and 38 deletions
@ -0,0 +1,57 @@ |
||||
#!/bin/bash |
||||
|
||||
set -x |
||||
echo $@ |
||||
|
||||
set -euo pipefail |
||||
# --- begin runfiles.bash initialization --- |
||||
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then |
||||
if [[ -f "$0.runfiles_manifest" ]]; then |
||||
export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" |
||||
elif [[ -f "$0.runfiles/MANIFEST" ]]; then |
||||
export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" |
||||
elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then |
||||
export RUNFILES_DIR="$0.runfiles" |
||||
fi |
||||
fi |
||||
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then |
||||
source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" |
||||
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then |
||||
source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ |
||||
"$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" |
||||
else |
||||
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" |
||||
exit 1 |
||||
fi |
||||
# --- end runfiles.bash initialization --- |
||||
|
||||
TESTEE=unset |
||||
FAILURE_LIST=unset |
||||
TEXT_FORMAT_FAILURE_LIST=unset |
||||
|
||||
while [[ -n "$@" ]]; do |
||||
arg="$1"; shift |
||||
val="$1"; shift |
||||
case "$arg" in |
||||
"--testee") TESTEE="$val" ;; |
||||
"--failure_list") FAILURE_LIST="$val" ;; |
||||
"--text_format_failure_list") TEXT_FORMAT_FAILURE_LIST="$val" ;; |
||||
*) echo "Flag $arg is not recognized." && exit 1 ;; |
||||
esac |
||||
done |
||||
|
||||
conformance_test_runner=$(rlocation com_google_protobuf/conformance_test_runner) |
||||
conformance_testee=$(rlocation $TESTEE) |
||||
args=(--enforce_recommended) |
||||
|
||||
failure_list=$(rlocation $FAILURE_LIST) |
||||
if [ "$failure_list" != "1" ] ; then |
||||
args+=(--failure_list $failure_list) |
||||
fi |
||||
|
||||
text_format_failure_list=$(rlocation $TEXT_FORMAT_FAILURE_LIST) |
||||
if [ "$text_format_failure_list" != "1" ]; then |
||||
args+=(--text_format_failure_list $text_format_failure_list) |
||||
fi |
||||
|
||||
$conformance_test_runner "${args[@]}" $conformance_testee |
@ -0,0 +1,29 @@ |
||||
# PLEASE DO NOT DEPEND ON THE CONTENTS OF THIS FILE, IT IS UNSTABLE. |
||||
|
||||
def conformance_test(name, testee, failure_list = None, text_format_failure_list = None): |
||||
args = ["--testee %s" % _strip_bazel(testee)] |
||||
failure_lists = [] |
||||
if failure_list: |
||||
args = args + ["--failure_list %s" % _strip_bazel(failure_list)] |
||||
failure_lists = failure_lists + [failure_list] |
||||
if text_format_failure_list: |
||||
args = args + ["--text_format_failure_list %s" % _strip_bazel(text_format_failure_list)] |
||||
failure_lists = failure_lists + [text_format_failure_list] |
||||
|
||||
native.sh_test( |
||||
name = name, |
||||
srcs = ["//:conformance/conformance_test_runner.sh"], |
||||
data = [testee] + failure_lists + [ |
||||
"//:conformance_test_runner", |
||||
], |
||||
args = args, |
||||
deps = [ |
||||
"@bazel_tools//tools/bash/runfiles", |
||||
], |
||||
) |
||||
|
||||
|
||||
def _strip_bazel(testee): |
||||
if testee.startswith("//"): |
||||
testee = testee.replace("//", "com_google_protobuf") |
||||
return testee.replace(":", "/") |
@ -0,0 +1,8 @@ |
||||
test_suite( |
||||
name = "tests", |
||||
tests = [ |
||||
"//java/core:tests", |
||||
"//java/lite:tests", |
||||
"//java/util:tests", |
||||
], |
||||
) |
@ -0,0 +1 @@ |
||||
package(default_visibility = ["//java:__subpackages__"]) |
@ -0,0 +1,72 @@ |
||||
""" |
||||
Generates a side-car JUnit suite test runner class for each |
||||
input src. |
||||
""" |
||||
_template = """import org.junit.runners.Suite; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
@RunWith(Suite.class) |
||||
@Suite.SuiteClasses({%s}) |
||||
public class %s {} |
||||
""" |
||||
|
||||
def _as_classname(fname, pkg): |
||||
path_name = [x.path for x in fname.files.to_list()][0] |
||||
file_name = path_name.split("/")[-1] |
||||
return ".".join([pkg, file_name.split(".")[0]]) + ".class" |
||||
|
||||
def _gen_suite_impl(ctx): |
||||
classes = ",".join( |
||||
[_as_classname(x, ctx.attr.package_name) for x in ctx.attr.srcs], |
||||
) |
||||
ctx.actions.write(output = ctx.outputs.out, content = _template % ( |
||||
classes, |
||||
ctx.attr.outname, |
||||
)) |
||||
|
||||
_gen_suite = rule( |
||||
attrs = { |
||||
"srcs": attr.label_list(allow_files = True), |
||||
"package_name": attr.string(), |
||||
"outname": attr.string(), |
||||
}, |
||||
outputs = {"out": "%{name}.java"}, |
||||
implementation = _gen_suite_impl, |
||||
) |
||||
|
||||
def junit_tests(name, srcs, data = [], deps = [], package_name = "com.google.protobuf", test_prefix = None, **kwargs): |
||||
testlib_name = "%s_lib" % name |
||||
native.java_library( |
||||
name = testlib_name, |
||||
srcs = srcs, |
||||
deps = deps, |
||||
resources = data, |
||||
data = data, |
||||
) |
||||
test_names = [] |
||||
prefix = name.replace("-", "_") + "TestSuite" |
||||
for src in srcs: |
||||
test_name = src.rsplit("/", 1)[1].split(".")[0] |
||||
if not test_name.endswith("Test") or test_name.startswith("Abstract"): |
||||
continue |
||||
if test_prefix: |
||||
test_name = "%s%s" % (test_prefix, test_name) |
||||
test_names = test_names + [test_name] |
||||
suite_name = prefix + '_' + test_name |
||||
_gen_suite( |
||||
name = suite_name, |
||||
srcs = [src], |
||||
package_name = package_name, |
||||
outname = suite_name, |
||||
) |
||||
native.java_test( |
||||
name = test_name, |
||||
test_class = suite_name, |
||||
srcs = [src] + [":" + suite_name], |
||||
deps = deps + [":%s" % testlib_name], |
||||
**kwargs |
||||
) |
||||
native.test_suite( |
||||
name = name, |
||||
tests = test_names, |
||||
) |
Loading…
Reference in new issue