Skip to content

Commit

Permalink
Add support for custom listeners in instrumentation tests
Browse files Browse the repository at this point in the history
Summary:
This adds the ability for adding custom listeners that allow instrumentation tests to also execute code as part of the host. This is useful for a few cases.
1. Allow the test to run some custom adb commands prior to executing the test.
2. Perform privileged actions such as communicating with WWW as the host doesn't have access to the server.

This only supported on buck2, I do not intend to bring this to buck1.

Differential Revision: D51141856

fbshipit-source-id: 3050b29dfc315d2e1731190c25dffda1ad282f3a
  • Loading branch information
vener91 authored and facebook-github-bot committed Nov 12, 2023
1 parent d111627 commit c2cabc0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions prelude/android/android.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ extra_attributes = {
"_java_toolchain": toolchains_common.java_for_android(),
},
"android_instrumentation_test": {
"instrumentation_test_listener": attrs.option(attrs.source(), default = None),
"instrumentation_test_listener_class": attrs.option(attrs.string(), default = None),
"_android_toolchain": toolchains_common.android(),
"_exec_os_type": buck.exec_os_type_arg(),
"_java_toolchain": toolchains_common.java_for_android(),
Expand Down
9 changes: 7 additions & 2 deletions prelude/android/android_instrumentation_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def android_instrumentation_test_impl(ctx: AnalysisContext):

classpath_args = cmd_args()
classpath_args.add("-classpath")
classpath_args.add(cmd_args(classpath, delimiter = get_path_separator_for_exec_os(ctx)))
extra_classpath = []
if ctx.attrs.instrumentation_test_listener != None:
extra_classpath.append(ctx.attrs.instrumentation_test_listener)
classpath_args.add(cmd_args(classpath + extra_classpath, delimiter = get_path_separator_for_exec_os(ctx)))
classpath_args_file = ctx.actions.write("classpath_args_file", classpath_args)
cmd.append(cmd_args(classpath_args_file, format = "@{}").hidden(classpath_args))

Expand Down Expand Up @@ -52,7 +55,6 @@ def android_instrumentation_test_impl(ctx: AnalysisContext):
test_runner_file.as_output(),
])
ctx.actions.run(manifest_utils_cmd, category = "get_manifest_info")

cmd.extend(
[
"--test-package-name",
Expand All @@ -64,6 +66,9 @@ def android_instrumentation_test_impl(ctx: AnalysisContext):
],
)

if ctx.attrs.instrumentation_test_listener_class != None:
cmd.extend(["--extra-instrumentation-test-listener", ctx.attrs.instrumentation_test_listener_class])

cmd.extend(
[
"--adb-executable-path",
Expand Down

0 comments on commit c2cabc0

Please sign in to comment.