Skip to content

Commit

Permalink
Updating UTPExtensionInfo to represent a list of jars, matching the E…
Browse files Browse the repository at this point in the history
…xtension proto, while maintaining compatibility with legacy usage.

PiperOrigin-RevId: 623945970
Change-Id: I0c8d776fd065126d1a01849198d9f543e6d0c789
  • Loading branch information
A Googler authored and copybara-github committed Apr 11, 2024
1 parent 748aa47 commit 5dcb28a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
14 changes: 10 additions & 4 deletions launcher/extension.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def extension_to_proto(target):
label = target.label.name,
),
class_name = info.java_class,
jar = [absolute_path_struct(info.binary)],
jar = [absolute_path_struct(x) for x in info.binary],
)
if info.text_proto != None:
message["resource"] = struct(
Expand Down Expand Up @@ -85,19 +85,20 @@ def extension_direct_deps(target):
A list of Files.
"""
if utp_provider.UTPExtensionInfo in target:
return [target[utp_provider.UTPExtensionInfo].binary]
return target[utp_provider.UTPExtensionInfo].binary
return []

def _utp_host_plugin_impl(ctx):
deploy = ctx.file.binary
deploy = [x.files.to_list()[0] for x in ctx.attr.shared_jars] + [ctx.file.binary]
deps = depset(direct = [ctx.file.binary], transitive = [x.files for x in ctx.attr.shared_jars])
return [
extension.utp_extension_info(
ctx = ctx,
extension_rule = "{}:{}".format(ctx.label.package, ctx.label.name),
java_class = ctx.attr.class_name,
binary = deploy,
config_struct = struct(),
files = depset([deploy]),
files = deps,
),
]

Expand All @@ -112,6 +113,11 @@ utp_host_plugin = rule(
providers = [[JavaInfo]],
allow_single_file = ["_deploy.jar"],
),
shared_jars = attr.label_list(
doc = "Binary deploy jars shared by this host plugin.",
providers = [[JavaInfo]],
allow_files = ["_deploy.jar"],
),
class_name = attr.string(
doc = "Class name of the host plugin.",
mandatory = True,
Expand Down
17 changes: 10 additions & 7 deletions provider/extension/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
UTPExtensionInfo = provider(
doc = "Unified Test Platform Extension classloading and configuration information.",
fields = {
"extension_rule": "(Label) The target for this extension rule.",
"extension_target": "(Label) The target that configures this extension",
"extension_rule": "(Target) The target for this extension rule.",
"extension_target": "(Target) The target that configures this extension",
"java_class": "(string) The Extension's Java class.",
"binary": "(Label) The Java binary to load the Extension from.",
"binary": "([File]) The Java binaries to load the Extension from.",
"text_proto": "(string) The textproto representation of this config.",
"proto_type": "(string or None) If specified, the type of the text_proto (protobuf package + name, not Java type name).",
"files": "(depset) Data dependencies to be made available at runtime. Deprecated; use DefaultInfo instead.",
Expand All @@ -34,13 +34,15 @@ def utp_extension_info(ctx, extension_rule, java_class, binary, config_struct, f
ctx: Starlark rule context.
extension_rule: The target for this extension rule.
java_class: The Extension's Java class.
binary: The Java binary to load the Extension from.
binary: (File or [File]) The Java binary to load the Extension from.
config_struct: Struct representation of the configuration proto for this extension.
files: (depset) Data dependencies to be made available at runtime.
proto_type: (str) The type of the proto represented in config_struct (protobuf package + name, not Java type name).
Returns:
A UTPExtensionInfo provider containing information needed to create an extension proto.
"""
if type(binary) != type([]):
binary = [binary]
return utp_extension_info_from_textpb(
ctx = ctx,
extension_rule = extension_rule,
Expand All @@ -58,20 +60,21 @@ def utp_extension_info_from_textpb(ctx, extension_rule, java_class, binary, text
ctx: Starlark rule context.
extension_rule: The target for this extension rule.
java_class: The Extension's Java class.
binary: The Java binary to load the Extension from.
binary: (File or [File]) The Java binary to load the Extension from.
text_proto: The string representation of the configuration proto for this extension.
files: (depset) Data dependencies to be made available at runtime.
proto_type: (str) The type of the proto in text_proto (protobuf package + name, not Java type name).
Returns:
A UTPExtensionInfo provider containing information needed to create an extension proto.
"""

if type(binary) != type([]):
binary = [binary]
return UTPExtensionInfo(
extension_rule = extension_rule,
extension_target = str(ctx.label),
java_class = java_class,
binary = binary,
text_proto = text_proto.strip(),
proto_type = proto_type,
files = depset([binary], transitive = [files]),
files = depset(binary, transitive = [files]),
)
6 changes: 2 additions & 4 deletions provider/extension/textproto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ _TEMPLATE = """label {
namespace: "%s"
}
class_name: "%s"
jar {
path: "%s"
}
%s
resource {
encoding: TEXT
raw: '%s'
Expand All @@ -48,7 +46,7 @@ def extension_to_textproto(ctx, extension):
extension.extension_rule,
extension.extension_target,
extension.java_class,
extension.binary.short_path,
"\n".join(['jar {{\n path: "{}"\n}}'.format(x.short_path) for x in extension.binary]),
extension.text_proto.replace("\n", "\\n"),
)
name = extension.extension_target.split(":")[-1] + "_extension.textproto"
Expand Down
5 changes: 5 additions & 0 deletions test/launcher/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ java_binary(
main_class = "EmptyJava",
)

java_binary(
name = "empty_shared_java_binary_deploy",
main_class = "EmptySharedJava",
)

bzl_library(
name = "android_instrumentation_driver_test_bzl",
srcs = ["android_instrumentation_driver_test.bzl"],
Expand Down
16 changes: 14 additions & 2 deletions test/launcher/extension_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@

load("//launcher:extension.bzl", "extension_to_proto", "utp_host_plugin")
load("//provider:provider.bzl", "utp_provider")
load("//tools/utp:constants.bzl", "JAR_PATH_EMPTY_BINARY", "TARGET_EMPTY_BINARY")
load(
"//tools/utp:constants.bzl",
"JAR_PATH_EMPTY_BINARY",
"JAR_PATH_EMPTY_SHARED_BINARY",
"TARGET_EMPTY_BINARY",
"TARGET_EMPTY_SHARED_BINARY",
)
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")

def _extension_to_proto_test(ctx):
Expand All @@ -39,9 +45,14 @@ def _extension_to_proto_test(ctx):
)
asserts.equals(
env,
"@@PWD@@/" + JAR_PATH_EMPTY_BINARY,
"@@PWD@@/" + JAR_PATH_EMPTY_SHARED_BINARY,
message.jar[0].path,
)
asserts.equals(
env,
"@@PWD@@/" + JAR_PATH_EMPTY_BINARY,
message.jar[1].path,
)
return unittest.end(env)

extension_to_proto_test = unittest.make(
Expand All @@ -60,6 +71,7 @@ def extension_test_suite(name):
testonly = True,
class_name = "com.google.testing.platform.plugin.android.AndroidDevicePlugin",
binary = TARGET_EMPTY_BINARY,
shared_jars = [TARGET_EMPTY_SHARED_BINARY],
)

unittest.suite(
Expand Down
5 changes: 4 additions & 1 deletion tools/utp/constants.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ JAR_PATH_LOCAL_ANDROID_DEVICE_PROVIDER = "../rules_jvm_external~~maven~maven/com
JAR_PATH_ANDROID_INSTRUMENTATION_DRIVER="../rules_jvm_external~~maven~maven/com/google/testing/platform/android-driver-instrumentation/0.0.9-alpha01/processed_android-driver-instrumentation-0.0.9-alpha01.jar"

TARGET_EMPTY_BINARY = "//test/launcher:empty_java_binary_deploy.jar"
JAR_PATH_EMPTY_BINARY="test/launcher/empty_java_binary_deploy.jar"
JAR_PATH_EMPTY_BINARY="test/launcher/empty_java_binary_deploy.jar"

TARGET_EMPTY_SHARED_BINARY = "//test/launcher:empty_java_shared_binary_deploy.jar"
JAR_PATH_EMPTY_SHARED_BINARY = "test/launcher/empty_java_shared_binary_deploy.jar"

0 comments on commit 5dcb28a

Please sign in to comment.