From bc45f92262a8e0e6e1ab7302c72a380eb0346f8e Mon Sep 17 00:00:00 2001 From: Derek Perez Date: Tue, 20 Apr 2021 11:36:32 -0700 Subject: [PATCH] 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.bzl --- BUILD | 155 ++++++++++++++++++ WORKSPACE | 65 ++++---- conformance/conformance_test_runner.sh | 57 +++++++ internal.bzl | 29 ++++ java/BUILD | 8 + java/core/BUILD | 208 ++++++++++++++++++++++++- java/internal/BUILD | 1 + java/internal/testing.bzl | 72 +++++++++ java/lite/BUILD | 42 +++++ java/lite/pom.xml | 1 + java/util/BUILD | 37 ++++- protobuf.bzl | 10 +- protobuf_deps.bzl | 8 + 13 files changed, 655 insertions(+), 38 deletions(-) create mode 100755 conformance/conformance_test_runner.sh create mode 100644 internal.bzl create mode 100644 java/BUILD create mode 100644 java/internal/BUILD create mode 100644 java/internal/testing.bzl diff --git a/BUILD b/BUILD index 1788cfc06e..03cde083f9 100644 --- a/BUILD +++ b/BUILD @@ -4,6 +4,7 @@ load("@bazel_skylib//rules:common_settings.bzl", "string_flag") load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test", "objc_library", native_cc_proto_library = "cc_proto_library") load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library") load("@rules_python//python:defs.bzl", "py_library") +load("@rules_java//java:defs.bzl", "java_binary", "java_proto_library", "java_lite_proto_library") load(":cc_proto_blacklist_test.bzl", "cc_proto_blacklist_test") licenses(["notice"]) @@ -458,6 +459,12 @@ cc_binary( # Tests ################################################################################ +filegroup( + name = "testdata", + visibility = ["//:__subpackages__"], + srcs = glob(["src/google/protobuf/testdata/**/*"]), +) + RELATIVE_LITE_TEST_PROTOS = [ # AUTOGEN(lite_test_protos) "google/protobuf/map_lite_unittest.proto", @@ -519,6 +526,57 @@ RELATIVE_TEST_PROTOS = [ TEST_PROTOS = ["src/" + s for s in RELATIVE_TEST_PROTOS] +GENERIC_RELATIVE_TEST_PROTOS = [ + "google/protobuf/unittest.proto", + "google/protobuf/unittest_arena.proto", + "google/protobuf/unittest_custom_options.proto", + "google/protobuf/unittest_drop_unknown_fields.proto", + "google/protobuf/unittest_embed_optimize_for.proto", + "google/protobuf/unittest_empty.proto", + "google/protobuf/unittest_enormous_descriptor.proto", + "google/protobuf/unittest_import.proto", + "google/protobuf/unittest_import_public.proto", + "google/protobuf/unittest_lazy_dependencies.proto", + "google/protobuf/unittest_lazy_dependencies_custom_option.proto", + "google/protobuf/unittest_lazy_dependencies_enum.proto", + "google/protobuf/unittest_lite_imports_nonlite.proto", + "google/protobuf/unittest_mset.proto", + "google/protobuf/unittest_mset_wire_format.proto", + "google/protobuf/unittest_no_field_presence.proto", + "google/protobuf/unittest_no_generic_services.proto", + "google/protobuf/unittest_optimize_for.proto", + "google/protobuf/unittest_preserve_unknown_enum.proto", + "google/protobuf/unittest_preserve_unknown_enum2.proto", + "google/protobuf/unittest_proto3.proto", + "google/protobuf/unittest_proto3_arena.proto", + "google/protobuf/unittest_proto3_arena_lite.proto", + "google/protobuf/unittest_proto3_lite.proto", + "google/protobuf/unittest_proto3_optional.proto", + "google/protobuf/unittest_well_known_types.proto", +] + +GENERIC_TEST_PROTOS = ["src/" + s for s in GENERIC_RELATIVE_TEST_PROTOS] + +proto_library( + name = "generic_test_protos", + visibility = ["//:__subpackages__"], + strip_import_prefix = "src", + srcs = LITE_TEST_PROTOS + GENERIC_TEST_PROTOS, + deps = [ + "//:any_proto", + "//:api_proto", + "//:descriptor_proto", + "//:duration_proto", + "//:empty_proto", + "//:field_mask_proto", + "//:source_context_proto", + "//:struct_proto", + "//:timestamp_proto", + "//:type_proto", + "//:wrappers_proto", + ], +) + cc_proto_library( name = "cc_test_protos", srcs = LITE_TEST_PROTOS + TEST_PROTOS, @@ -685,6 +743,15 @@ internal_gen_well_known_protos_java( ], ) +internal_gen_well_known_protos_java( + name = "gen_well_known_protos_javalite", + deps = [proto + "_proto" for proto in WELL_KNOWN_PROTO_MAP.keys()], + javalite = True, + visibility = [ + "//java:__subpackages__", + ], +) + alias( name = "protobuf_java", actual = "//java/core", @@ -1111,3 +1178,91 @@ sh_test( "update_file_lists.sh", ], ) + +java_proto_library( + name = "test_messages_proto2_java_proto", + visibility = [ + "//java:__subpackages__", + ], + deps = [":test_messages_proto2_proto"], +) + +java_proto_library( + name = "test_messages_proto3_java_proto", + visibility = [ + "//java:__subpackages__", + ], + deps = [":test_messages_proto3_proto"], +) + +java_proto_library( + name = "conformance_java_proto", + visibility = [ + "//java:__subpackages__", + ], + deps = [":conformance_proto"], +) + +java_lite_proto_library( + name = "test_messages_proto2_java_proto_lite", + visibility = [ + "//java:__subpackages__", + ], + deps = [":test_messages_proto2_proto"], +) + +java_lite_proto_library( + name = "conformance_java_proto_lite", + visibility = [ + "//java:__subpackages__", + ], + deps = [":conformance_proto"], +) + +java_lite_proto_library( + name = "test_messages_proto3_java_proto_lite", + visibility = [ + "//java:__subpackages__", + ], + deps = [":test_messages_proto3_proto"], +) + +java_binary( + name = "conformance_java", + srcs = ["conformance/ConformanceJava.java"], + visibility = [ + "//java:__subpackages__", + ], + main_class = "ConformanceJava", + deps = [ + ":conformance_java_proto", + ":test_messages_proto2_java_proto", + ":test_messages_proto3_java_proto", + "//:protobuf_java", + "//:protobuf_java_util", + ], +) + +java_binary( + name = "conformance_java_lite", + srcs = ["conformance/ConformanceJavaLite.java"], + visibility = [ + "//java:__subpackages__", + ], + main_class = "ConformanceJavaLite", + deps = [ + ":conformance_java_proto_lite", + ":test_messages_proto2_java_proto_lite", + ":test_messages_proto3_java_proto_lite", + "//:protobuf_javalite", + "//:protobuf_java_util", + ], +) + +exports_files([ + "conformance/conformance_test_runner.sh", + "conformance/failure_list_java.txt", + "conformance/failure_list_java_lite.txt", + "conformance/text_format_failure_list_java.txt", + "conformance/text_format_failure_list_java_lite.txt", +]) diff --git a/WORKSPACE b/WORKSPACE index 4346dbd906..bf896e09b4 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -26,23 +26,27 @@ http_archive( ], ) -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("//:protobuf_deps.bzl", "protobuf_deps") - # Load common dependencies. +load("//:protobuf_deps.bzl", "protobuf_deps") protobuf_deps() -load("@bazel_tools//tools/build_defs/repo:jvm.bzl", "jvm_maven_import_external") bind( name = "python_headers", actual = "//util/python:python_headers", ) -jvm_maven_import_external( - name = "guava_maven", - artifact = "com.google.guava:guava:18.0", - artifact_sha256 = "d664fbfc03d2e5ce9cab2a44fb01f1d0bf9dfebeccc1a473b1f9ea31f79f6f99", - server_urls = [ +load("@rules_jvm_external//:defs.bzl", "maven_install") +maven_install( + artifacts = [ + "com.google.guava:guava:18.0", + "com.google.code.gson:gson:2.7", + "com.google.errorprone:error_prone_annotations:2.3.2", + "junit:junit:4.12", + "org.easymock:easymock:3.2", + "org.easymock:easymockclassextension:3.2", + "com.google.truth:truth:1.1.2", + ], + repositories = [ "https://jcenter.bintray.com/", "https://repo1.maven.org/maven2", ], @@ -50,40 +54,39 @@ jvm_maven_import_external( bind( name = "guava", - actual = "@guava_maven//jar", + actual = "@maven//:com_google_guava_guava", ) -jvm_maven_import_external( - name = "gson_maven", - artifact = "com.google.code.gson:gson:2.7", - artifact_sha256 = "2d43eb5ea9e133d2ee2405cc14f5ee08951b8361302fdd93494a3a997b508d32", - server_urls = [ - "https://jcenter.bintray.com/", - "https://repo1.maven.org/maven2", - ], +bind( + name = "gson", + actual = "@maven//:com_google_code_gson_gson", ) bind( - name = "gson", - actual = "@gson_maven//jar", + name = "error_prone_annotations", + actual = "@maven//:com_google_errorprone_error_prone_annotations", ) -jvm_maven_import_external( - name = "error_prone_annotations_maven", - artifact = "com.google.errorprone:error_prone_annotations:2.3.2", - artifact_sha256 = "357cd6cfb067c969226c442451502aee13800a24e950fdfde77bcdb4565a668d", - server_urls = [ - "https://jcenter.bintray.com/", - "https://repo1.maven.org/maven2", - ], +bind( + name = "junit", + actual = "@maven//:junit_junit", ) bind( - name = "error_prone_annotations", - actual = "@error_prone_annotations_maven//jar", + name = "easymock", + actual = "@maven//:org_easymock_easymock", +) + +bind( + name = "easymock_classextension", + actual = "@maven//:org_easymock_easymockclassextension", +) + +bind( + name = "truth", + actual = "@maven//:com_google_truth_truth", ) # For `cc_proto_blacklist_test`. load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") - bazel_skylib_workspace() diff --git a/conformance/conformance_test_runner.sh b/conformance/conformance_test_runner.sh new file mode 100755 index 0000000000..e9b2e4fffa --- /dev/null +++ b/conformance/conformance_test_runner.sh @@ -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 diff --git a/internal.bzl b/internal.bzl new file mode 100644 index 0000000000..a281418c9b --- /dev/null +++ b/internal.bzl @@ -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(":", "/") diff --git a/java/BUILD b/java/BUILD new file mode 100644 index 0000000000..8778bf9329 --- /dev/null +++ b/java/BUILD @@ -0,0 +1,8 @@ +test_suite( + name = "tests", + tests = [ + "//java/core:tests", + "//java/lite:tests", + "//java/util:tests", + ], +) diff --git a/java/core/BUILD b/java/core/BUILD index fa074c0608..4d8895a8ef 100644 --- a/java/core/BUILD +++ b/java/core/BUILD @@ -1,5 +1,8 @@ -load("@rules_java//java:defs.bzl", "java_library") -load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain") +load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("@rules_java//java:defs.bzl", "java_library", "java_proto_library", "java_lite_proto_library") +load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library") +load("//:internal.bzl", "conformance_test") +load("//java/internal:testing.bzl", "junit_tests") LITE_SRCS = [ # Keep in sync with `//java/lite:pom.xml`. @@ -95,12 +98,19 @@ LITE_SRCS = [ # Should be used as `//java/lite`. java_library( name = "lite", - srcs = LITE_SRCS, + srcs = LITE_SRCS + [ + "//:gen_well_known_protos_javalite" + ], visibility = [ "//java/lite:__pkg__", ], ) +java_library( + name = "lite_runtime_only", + srcs = LITE_SRCS, +) + java_library( name = "core", srcs = glob( @@ -113,10 +123,10 @@ java_library( ], visibility = ["//visibility:public"], exports = [ - "//java/lite", + ":lite_runtime_only", ], deps = [ - "//java/lite", + ":lite_runtime_only", ], ) @@ -126,3 +136,191 @@ proto_lang_toolchain( runtime = ":core", visibility = ["//visibility:public"], ) + +proto_library( + name = "java_test_protos", + srcs = glob(["src/test/proto/**/*.proto"]), + strip_import_prefix = "src/test/proto", + deps = [ + "//:any_proto", + "//:descriptor_proto", + "//:generic_test_protos", + "//:wrappers_proto", + ], +) + +java_proto_library( + name = "generic_test_protos_java_proto", + visibility = [ + "//java:__subpackages__", + ], + deps = ["//:generic_test_protos"], +) + +java_proto_library( + name = "java_test_protos_java_proto", + deps = [":java_test_protos"], +) + +java_library( + name = "test_util", + srcs = [ + "src/test/java/com/google/protobuf/TestUtil.java", + "src/test/java/com/google/protobuf/TestUtilLite.java" + ], + deps = [ + ":core", + ":generic_test_protos_java_proto", + ":java_test_protos_java_proto", + "//external:guava", + "//external:junit", + ], +) + +test_suite( + name = "tests", + tests = [ + "core_build_test", + "conformance_test", + "core_tests", + ], +) + +build_test( + name = "core_build_test", + targets = [ + ":core", + ], +) + +conformance_test( + name = "conformance_test", + testee = "//:conformance_java", + failure_list = "//:conformance/failure_list_java.txt", + text_format_failure_list = "//:conformance/text_format_failure_list_java.txt", +) + +junit_tests( + name = "core_tests", + srcs = glob(["src/test/java/**/*.java"], exclude = [ + "src/test/java/com/google/protobuf/TestUtil.java", + "src/test/java/com/google/protobuf/TestUtilLite.java", + ]), + data = ["//:testdata"], + size = "large", + deps = [ + ":core", + ":generic_test_protos_java_proto", + ":java_test_protos_java_proto", + ":test_util", + "//external:easymock", + "//external:easymock_classextension", + "//external:junit", + "//external:truth", + ] +) + +java_lite_proto_library( + name = "generic_test_protos_java_proto_lite", + visibility = [ + "//java/lite:__pkg__", + ], + deps = ["//:generic_test_protos"], +) + +java_lite_proto_library( + name = "java_test_protos_java_proto_lite", + visibility = [ + "//java/lite:__pkg__", + ], + deps = [":java_test_protos"], +) + +genrule( + name = "rewrite_javalite_test_util", + srcs = [ + "//java/lite:lite.awk", + "src/test/java/com/google/protobuf/TestUtil.java" + ], + outs = ["TestUtil.java"], + cmd = "awk -f $(location //java/lite:lite.awk) $(location src/test/java/com/google/protobuf/TestUtil.java) > $@" +) + +java_library( + name = "test_util_lite", + srcs = [ + ":rewrite_javalite_test_util", + "src/test/java/com/google/protobuf/TestUtilLite.java" + ], + visibility = [ + "//java/lite:__pkg__", + ], + deps = [ + ":generic_test_protos_java_proto_lite", + ":java_test_protos_java_proto_lite", + ":lite_runtime_only", + "//external:guava", + "//external:junit", + ], +) + +LITE_TEST_EXCLUSIONS = [ + # Keep in sync with //java/lite:pom.xml id=copy-test-source-files execution. + "src/test/java/com/google/protobuf/AbstractMessageTest.java", + "src/test/java/com/google/protobuf/AbstractProto2SchemaTest.java", + "src/test/java/com/google/protobuf/AnyTest.java", + "src/test/java/com/google/protobuf/CodedInputStreamTest.java", + "src/test/java/com/google/protobuf/DeprecatedFieldTest.java", + "src/test/java/com/google/protobuf/DescriptorsTest.java", + "src/test/java/com/google/protobuf/DiscardUnknownFieldsTest.java", + "src/test/java/com/google/protobuf/DynamicMessageTest.java", + "src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java", + "src/test/java/com/google/protobuf/FieldPresenceTest.java", + "src/test/java/com/google/protobuf/ForceFieldBuildersPreRun.java", + "src/test/java/com/google/protobuf/GeneratedMessageTest.java", + "src/test/java/com/google/protobuf/LazyFieldTest.java", + "src/test/java/com/google/protobuf/LazyStringEndToEndTest.java", + "src/test/java/com/google/protobuf/MapForProto2Test.java", + "src/test/java/com/google/protobuf/MapTest.java", + "src/test/java/com/google/protobuf/MessageTest.java", + "src/test/java/com/google/protobuf/NestedBuildersTest.java", + "src/test/java/com/google/protobuf/PackedFieldTest.java", + "src/test/java/com/google/protobuf/ParserTest.java", + "src/test/java/com/google/protobuf/ParseExceptionsTest.java", + "src/test/java/com/google/protobuf/Proto2ExtensionLookupSchemaTest.java", + "src/test/java/com/google/protobuf/Proto2SchemaTest.java", + "src/test/java/com/google/protobuf/Proto2UnknownEnumValueTest.java", + "src/test/java/com/google/protobuf/RepeatedFieldBuilderV3Test.java", + "src/test/java/com/google/protobuf/ServiceTest.java", + "src/test/java/com/google/protobuf/SingleFieldBuilderV3Test.java", + "src/test/java/com/google/protobuf/TestBadIdentifiers.java", + "src/test/java/com/google/protobuf/TextFormatParseInfoTreeTest.java", + "src/test/java/com/google/protobuf/TextFormatParseLocationTest.java", + "src/test/java/com/google/protobuf/TextFormatTest.java", + "src/test/java/com/google/protobuf/TestUtil.java", + "src/test/java/com/google/protobuf/TestUtilLite.java", + "src/test/java/com/google/protobuf/TypeRegistryTest.java", + "src/test/java/com/google/protobuf/UnknownEnumValueTest.java", + "src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java", + "src/test/java/com/google/protobuf/UnknownFieldSetTest.java", + "src/test/java/com/google/protobuf/WellKnownTypesTest.java", + "src/test/java/com/google/protobuf/WireFormatTest.java", +] + +junit_tests( + name = "lite_tests", + srcs = glob(["src/test/java/**/*.java"], exclude = LITE_TEST_EXCLUSIONS), + data = ["//:testdata"], + test_prefix = "Lite", + size = "large", + deps = [ + ":lite", + ":generic_test_protos_java_proto_lite", + ":java_test_protos_java_proto_lite", + ":test_util_lite", + "//external:easymock", + "//external:easymock_classextension", + "//external:junit", + "//external:truth", + ] +) diff --git a/java/internal/BUILD b/java/internal/BUILD new file mode 100644 index 0000000000..4f542ecee1 --- /dev/null +++ b/java/internal/BUILD @@ -0,0 +1 @@ +package(default_visibility = ["//java:__subpackages__"]) \ No newline at end of file diff --git a/java/internal/testing.bzl b/java/internal/testing.bzl new file mode 100644 index 0000000000..76e1e45170 --- /dev/null +++ b/java/internal/testing.bzl @@ -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, + ) diff --git a/java/lite/BUILD b/java/lite/BUILD index 22840ec48c..639f30879f 100644 --- a/java/lite/BUILD +++ b/java/lite/BUILD @@ -1,4 +1,9 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain") +load("//:internal.bzl", "conformance_test") +load("//java/internal:testing.bzl", "junit_tests") + +exports_files(["lite.awk"], visibility = ["//java/core:__pkg__"]) alias( name = "lite", @@ -12,3 +17,40 @@ proto_lang_toolchain( runtime = ":lite", visibility = ["//visibility:public"], ) + +test_suite( + name = "tests", + tests = [ + "lite_build_test", + "conformance_test", + "lite_tests", + "//java/core:lite_tests", + ], +) + +build_test( + name = "lite_build_test", + targets = [ + ":lite", + ], +) + +conformance_test( + name = "conformance_test", + testee = "//:conformance_java_lite", + failure_list = "//:conformance/failure_list_java_lite.txt", + text_format_failure_list = "//:conformance/text_format_failure_list_java_lite.txt", +) + +junit_tests( + name = "lite_tests", + srcs = glob(["src/test/**/*.java"]), + size = "small", + deps = [ + ":lite", + "//external:junit", + "//java/core:generic_test_protos_java_proto_lite", + "//java/core:java_test_protos_java_proto_lite", + "//java/core:test_util_lite", + ], +) diff --git a/java/lite/pom.xml b/java/lite/pom.xml index 5f03845038..cd1a3eb1f0 100644 --- a/java/lite/pom.xml +++ b/java/lite/pom.xml @@ -199,6 +199,7 @@ and exclude only the full runtime exclusive tests so we don't accidentally miss any test. --> + AbstractMessageTest.java AbstractProto2SchemaTest.java AnyTest.java diff --git a/java/util/BUILD b/java/util/BUILD index 71f73bab10..25853f7167 100644 --- a/java/util/BUILD +++ b/java/util/BUILD @@ -1,4 +1,6 @@ -load("@rules_java//java:defs.bzl", "java_library") +load("@rules_java//java:defs.bzl", "java_library", "java_proto_library") +load("@rules_proto//proto:defs.bzl", "proto_library") +load("//java/internal:testing.bzl", "junit_tests") java_library( name = "util", @@ -14,3 +16,36 @@ java_library( "//java/lite", ], ) + +proto_library( + name = "test_protos", + srcs = glob(["src/test/proto/**/*.proto"]), + deps = [ + "//:any_proto", + "//:duration_proto", + "//:field_mask_proto", + "//:struct_proto", + "//:timestamp_proto", + "//:wrappers_proto", + ], +) + +java_proto_library( + name = "test_protos_java_proto", + deps = [":test_protos"], +) + +junit_tests( + name = "tests", + srcs = glob(["src/test/java/**/*.java"]), + package_name = "com.google.protobuf.util", + deps = [ + ":test_protos_java_proto", + ":util", + "//external:guava", + "//external:junit", + "//external:truth", + "//java/core", + "//java/core:generic_test_protos_java_proto", + ], +) diff --git a/protobuf.bzl b/protobuf.bzl index 12d3edb947..a47711c7f4 100644 --- a/protobuf.bzl +++ b/protobuf.bzl @@ -328,7 +328,12 @@ def _internal_gen_well_known_protos_java_impl(ctx): deps = [d[ProtoInfo] for d in ctx.attr.deps] srcjar = ctx.actions.declare_file("{}.srcjar".format(ctx.attr.name)) - args.add("--java_out", srcjar) + if ctx.attr.javalite: + java_out = "lite:%s" % srcjar.path + else: + java_out = srcjar + + args.add("--java_out", java_out) descriptors = depset( transitive = [dep.transitive_descriptor_sets for dep in deps], @@ -368,6 +373,9 @@ internal_gen_well_known_protos_java = rule( mandatory = True, providers = [ProtoInfo], ), + "javalite": attr.bool( + default = False, + ), "_protoc": attr.label( executable = True, cfg = "host", diff --git a/protobuf_deps.bzl b/protobuf_deps.bzl index a4945b358a..bacee11644 100644 --- a/protobuf_deps.bzl +++ b/protobuf_deps.bzl @@ -63,3 +63,11 @@ def protobuf_deps(): strip_prefix = "rules_python-4b84ad270387a7c439ebdccfd530e2339601ef27", urls = ["https://github.com/bazelbuild/rules_python/archive/4b84ad270387a7c439ebdccfd530e2339601ef27.tar.gz"], ) + + if not native.existing_rule("rules_jvm_external"): + http_archive( + name = "rules_jvm_external", + sha256 = "31701ad93dbfe544d597dbe62c9a1fdd76d81d8a9150c2bf1ecf928ecdf97169", + strip_prefix = "rules_jvm_external-4.0", + urls = ["https://github.com/bazelbuild/rules_jvm_external/archive/4.0.zip"], + )