Squashed 'libs/protobuf/' content from commit fcd3b9a85
git-subtree-dir: libs/protobuf git-subtree-split: fcd3b9a85ef36e46643dc30176cea1a7ad62e02b
This commit is contained in:
24
java/internal/BUILD.bazel
Normal file
24
java/internal/BUILD.bazel
Normal file
@@ -0,0 +1,24 @@
|
||||
load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
|
||||
|
||||
package(default_visibility = ["//java:__subpackages__"])
|
||||
|
||||
pkg_files(
|
||||
name = "dist_files",
|
||||
srcs = [
|
||||
"BUILD.bazel",
|
||||
"JavaVersionTest.java",
|
||||
"testing.bzl",
|
||||
],
|
||||
strip_prefix = strip_prefix.from_root(""),
|
||||
visibility = ["//java:__pkg__"],
|
||||
)
|
||||
|
||||
java_test(
|
||||
name = "java_version",
|
||||
srcs = ["JavaVersionTest.java"],
|
||||
test_class = "JavaVersionTest",
|
||||
deps = [
|
||||
"@maven//:com_google_truth_truth",
|
||||
"@maven//:junit_junit",
|
||||
],
|
||||
)
|
||||
22
java/internal/JavaVersionTest.java
Normal file
22
java/internal/JavaVersionTest.java
Normal file
@@ -0,0 +1,22 @@
|
||||
// Test that Kokoro is using the expected version of Java.
|
||||
import static com.google.common.truth.Truth.assertWithMessage;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class JavaVersionTest {
|
||||
@Test
|
||||
public void testJavaVersion() throws Exception {
|
||||
String exp = System.getenv("KOKORO_JAVA_VERSION");
|
||||
if(exp == null || exp.isEmpty()) {
|
||||
System.err.println("No kokoro java version found, skipping check");
|
||||
return;
|
||||
}
|
||||
String version = System.getProperty("java.version");
|
||||
assertWithMessage("Expected Python " + exp + " but found Python " + version)
|
||||
.that(version.startsWith(exp))
|
||||
.isTrue();
|
||||
}
|
||||
}
|
||||
72
java/internal/testing.bzl
Normal file
72
java/internal/testing.bzl
Normal file
@@ -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,
|
||||
)
|
||||
Reference in New Issue
Block a user