Skip to content

Commit

Permalink
Bazel powered Java testing (#8506)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
perezd committed Apr 20, 2021
1 parent fecf7e9 commit bc45f92
Show file tree
Hide file tree
Showing 13 changed files with 655 additions and 38 deletions.
155 changes: 155 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
])
65 changes: 34 additions & 31 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,64 +26,67 @@ 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",
],
)

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()
57 changes: 57 additions & 0 deletions conformance/conformance_test_runner.sh
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions internal.bzl
Original file line number Diff line number Diff line change
@@ -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(":", "/")
8 changes: 8 additions & 0 deletions java/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test_suite(
name = "tests",
tests = [
"//java/core:tests",
"//java/lite:tests",
"//java/util:tests",
],
)

0 comments on commit bc45f92

Please sign in to comment.