From 0b94a8950a3388b2d8c32537d6e3fd525a5bbb05 Mon Sep 17 00:00:00 2001 From: A Googler Date: Tue, 13 Feb 2024 14:33:25 -0800 Subject: [PATCH] Adding hints to the various textprotos emitted by UTP so tools that render textprotos know the specific one they're dealing with. PiperOrigin-RevId: 606749309 Change-Id: I306b60b01c59a0aefe196cbce7c5024bd97655b5 --- launcher/android_instrumentation_driver.bzl | 4 +++ launcher/launcher.bzl | 28 +++++++++++++++++++-- launcher/launcher.sh | 7 +++++- launcher/local_android_device_provider.bzl | 4 +++ provider/extension/providers.bzl | 9 +++++-- 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/launcher/android_instrumentation_driver.bzl b/launcher/android_instrumentation_driver.bzl index 7799f15..f0c66d0 100644 --- a/launcher/android_instrumentation_driver.bzl +++ b/launcher/android_instrumentation_driver.bzl @@ -55,6 +55,7 @@ def _android_instrumentation_driver_impl(ctx): java_class = ctx.attr._class_name, binary = deploy, config_struct = struct(**configuration), + proto_type = ctx.attr._proto_type, files = depset([deploy]), ), ] @@ -65,6 +66,9 @@ android_instrumentation_driver = rule( _class_name = attr.string( default = "com.google.testing.platform.runtime.android.driver.AndroidInstrumentationDriver", ), + _proto_type = attr.string( + default = "google.testing.platform.proto.api.config.AndroidInstrumentationDriver", + ), utp_release = attr.label( default = UTP_RELEASE, providers = [[UTPReleaseInfo]], diff --git a/launcher/launcher.bzl b/launcher/launcher.bzl index df5b641..211aa34 100644 --- a/launcher/launcher.bzl +++ b/launcher/launcher.bzl @@ -217,15 +217,39 @@ def _utp_test_impl(ctx): test_driver_config = ctx.actions.declare_file(ctx.attr.name + "_test_driver_config.proto") direct_deps.append(test_driver_config) ctx.actions.write(test_driver_config, extension_config_proto(ctx.attr.test_driver)) + if ctx.attr.test_driver[utp_provider.UTPExtensionInfo].proto_type: + # If we know the type of the message, put that in the header to provide a hint for tools + # that display the textproto. + extra_setup_commands.append( + "echo '# proto-message: {}' > {}/test_driver_config.textproto".format( + ctx.attr.test_driver[utp_provider.UTPExtensionInfo].proto_type, + _UNDECLARED_OUTPUTS, + ), + ) extra_setup_commands.append( - "{} {} > {}/test_driver_config.proto".format(_SED, test_driver_config.short_path, _UNDECLARED_OUTPUTS), + "{} {} >> {}/test_driver_config.textproto".format( + _SED, + test_driver_config.short_path, + _UNDECLARED_OUTPUTS, + ), ) device_provider_config = ctx.actions.declare_file(ctx.attr.name + "_device_provider_config.proto") direct_deps.append(device_provider_config) ctx.actions.write(device_provider_config, extension_config_proto(ctx.attr.device_provider)) + if ctx.attr.device_provider[utp_provider.UTPExtensionInfo].proto_type: + extra_setup_commands.append( + "echo '# proto-message: {}' > {}/device_provider_config.textproto".format( + ctx.attr.device_provider[utp_provider.UTPExtensionInfo].proto_type, + _UNDECLARED_OUTPUTS, + ), + ) extra_setup_commands.append( - "{} {} > {}/device_provider_config.proto".format(_SED, device_provider_config.short_path, _UNDECLARED_OUTPUTS), + "{} {} >> {}/device_provider_config.textproto".format( + _SED, + device_provider_config.short_path, + _UNDECLARED_OUTPUTS, + ), ) for target in [ctx.attr.device_provider] + ctx.attr.test_fixtures: diff --git a/launcher/launcher.sh b/launcher/launcher.sh index 07f1c0f..12ac132 100644 --- a/launcher/launcher.sh +++ b/launcher/launcher.sh @@ -75,10 +75,15 @@ sed -r \ # not 4.8 from 2020! So use -r instead of -E to indicate extended regexps, # and that -z is not available (which is why I used tr to quote newlines # above). + +# Put the message type in the header of the file, to make it easier for tools +# that display the textproto to know what they're working with. +echo '# proto-message: google.testing.platform.proto.api.config.RunnerConfig' \ + > ${RUNNER_CONFIG} sed -r \ -f ${SED_SCRIPT} \ ${PWD}/%runner_config_short_path% \ - > ${RUNNER_CONFIG} + >> ${RUNNER_CONFIG} # Dump the config with line numbers, to make it easy to diagnose any error # messages. diff --git a/launcher/local_android_device_provider.bzl b/launcher/local_android_device_provider.bzl index 4481687..bf19623 100644 --- a/launcher/local_android_device_provider.bzl +++ b/launcher/local_android_device_provider.bzl @@ -78,6 +78,7 @@ def _local_android_device_provider_impl(ctx): java_class = ctx.attr._class_name, binary = deploy, config_struct = struct(**configuration), + proto_type = ctx.attr._proto_type, files = depset(), ), DefaultInfo( @@ -91,6 +92,9 @@ local_android_device_provider = rule( _class_name = attr.string( default = "com.google.testing.platform.runtime.android.provider.local.LocalAndroidDeviceProvider", ), + _proto_type = attr.string( + default = "google.testing.platform.proto.api.config.LocalAndroidDeviceProvider", + ), utp_release = attr.label( default = UTP_RELEASE, providers = [[UTPReleaseInfo]], diff --git a/provider/extension/providers.bzl b/provider/extension/providers.bzl index 2afc602..71082e1 100644 --- a/provider/extension/providers.bzl +++ b/provider/extension/providers.bzl @@ -22,11 +22,12 @@ UTPExtensionInfo = provider( "java_class": "(string) The Extension's Java class.", "binary": "(Label) The Java binary 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.", }, ) -def utp_extension_info(ctx, extension_rule, java_class, binary, config_struct, files): +def utp_extension_info(ctx, extension_rule, java_class, binary, config_struct, files, proto_type = None): """Wrapper for generating a UTPExtensionInfo provider. Args: @@ -36,6 +37,7 @@ def utp_extension_info(ctx, extension_rule, java_class, binary, config_struct, f binary: 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. """ @@ -45,10 +47,11 @@ def utp_extension_info(ctx, extension_rule, java_class, binary, config_struct, f java_class = java_class, binary = binary, text_proto = proto.encode_text(config_struct), + proto_type = proto_type, files = files, ) -def utp_extension_info_from_textpb(ctx, extension_rule, java_class, binary, text_proto, files): +def utp_extension_info_from_textpb(ctx, extension_rule, java_class, binary, text_proto, files, proto_type = None): """Wrapper for generating a UTPExtensionInfo provider. Args: @@ -58,6 +61,7 @@ def utp_extension_info_from_textpb(ctx, extension_rule, java_class, binary, text binary: 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. """ @@ -68,5 +72,6 @@ def utp_extension_info_from_textpb(ctx, extension_rule, java_class, binary, text java_class = java_class, binary = binary, text_proto = text_proto.strip(), + proto_type = proto_type, files = depset([binary], transitive = [files]), )