Skip to content

Commit

Permalink
Add test providers for bazel java_test
Browse files Browse the repository at this point in the history
This just moves the code to `java_helper.bzl` for sharing between the internal and OSS rules

PiperOrigin-RevId: 517138016
Change-Id: Ic832aa4df0f21375e0d09cb9cb6b890a2949b78a
  • Loading branch information
hvadehra authored and copybara-github committed Mar 16, 2023
1 parent cfef67d commit db167a7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ load(":common/rule_util.bzl", "merge_attrs")
load(":common/java/java_util.bzl", "shell_quote")
load(":common/java/java_semantics.bzl", "semantics")
load(":common/cc/cc_helper.bzl", "cc_helper")
load(":common/cc/semantics.bzl", cc_semantics = "semantics")
load(":common/java/java_helper.bzl", helper = "util")
load(":common/java/java_binary.bzl", "BASIC_JAVA_BINARY_ATTRIBUTES", "basic_java_binary")
load(":common/paths.bzl", "paths")
Expand Down Expand Up @@ -83,6 +84,9 @@ def _bazel_java_binary_impl(ctx):

return providers.values()

def _bazel_java_test_impl(ctx):
return _bazel_java_binary_impl(ctx) + helper.test_providers(ctx)

def _collect_all_targets_as_deps(ctx, classpath_type = "all"):
deps = helper.collect_all_targets_as_deps(ctx, classpath_type = classpath_type)

Expand Down Expand Up @@ -302,7 +306,7 @@ java_binary = make_java_binary(executable = True, resolve_launcher_flag = True)

def make_java_test(resolve_launcher_flag):
return _make_binary_rule(
_bazel_java_binary_impl,
_bazel_java_test_impl,
merge_attrs(
_BASE_BINARY_ATTRS,
{
Expand All @@ -325,6 +329,14 @@ def make_java_test(resolve_launcher_flag):
allow_single_file = True,
default = "@bazel_tools//tools/test:collect_cc_coverage",
),
"_apple_constraints": attr.label_list(
default = [
"@" + paths.join(cc_semantics.get_platforms_root(), "os:ios"),
"@" + paths.join(cc_semantics.get_platforms_root(), "os:macos"),
"@" + paths.join(cc_semantics.get_platforms_root(), "os:tvos"),
"@" + paths.join(cc_semantics.get_platforms_root(), "os:watchos"),
],
),
},
override_attrs = {
"use_testrunner": attr.bool(default = True),
Expand Down
21 changes: 21 additions & 0 deletions src/main/starlark/builtins_bzl/common/java/java_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ load(":common/cc/cc_helper.bzl", "cc_helper")
load(":common/paths.bzl", "paths")
load(":common/cc/cc_common.bzl", "cc_common")

testing = _builtins.toplevel.testing

def _collect_all_targets_as_deps(ctx, classpath_type = "all"):
deps = []
if not classpath_type == "compile_only":
Expand Down Expand Up @@ -265,6 +267,24 @@ def _get_test_support(ctx):
return ctx.attr._test_support
return None

def _test_providers(ctx):
test_providers = []
if cc_helper.has_target_constraints(ctx, ctx.attr._apple_constraints):
test_providers.append(testing.ExecutionInfo({"requires-darwin": ""}))

test_env = {}
test_env.update(cc_helper.get_expanded_env(ctx, {}))

coverage_config = _get_coverage_config(ctx)
if coverage_config:
test_env.update(coverage_config.env)
test_providers.append(testing.TestEnvironment(
environment = test_env,
inherited_environment = ctx.attr.env_inherit,
))

return test_providers

util = struct(
collect_all_targets_as_deps = _collect_all_targets_as_deps,
filter_launcher_for_target = _filter_launcher_for_target,
Expand All @@ -284,4 +304,5 @@ util = struct(
is_windows = _is_windows,
runfiles_enabled = _runfiles_enabled,
get_test_support = _get_test_support,
test_providers = _test_providers,
)

0 comments on commit db167a7

Please sign in to comment.