Skip to content

Commit 007cd5d

Browse files
committed
Provide a unit test for targets_compatible_with feature
1 parent fb3ac83 commit 007cd5d

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

MODULE.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ dev_maven.install(
328328
"https://repo1.maven.org/maven2",
329329
"https://maven.google.com",
330330
],
331+
targets_compatible_with = [
332+
"@platforms//os:android",
333+
],
331334
)
332335
dev_maven.install(
333336
name = "m2local_testing",

WORKSPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ maven_install(
534534
repositories = [
535535
"https://repo1.maven.org/maven2",
536536
],
537+
targets_compatible_with = [
538+
"@platforms//os:android",
539+
],
537540
)
538541

539542
maven_install(

tests/unit/jvm_import/BUILD

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
load("@bazel_skylib//rules:build_test.bzl", "build_test")
1616
load("@rules_java//java:defs.bzl", "java_import")
1717
load(":jvm_import_test.bzl", "jvm_import_test_suite")
18+
load(":platform_transition_jar.bzl", "platform_transition_jar")
1819

1920
java_import(
2021
name = "java_import_that_consumes_the_downloaded_file_directly",
@@ -29,4 +30,41 @@ build_test(
2930
],
3031
)
3132

33+
platform(
34+
name = "android",
35+
constraint_values = ["@platforms//os:android"],
36+
)
37+
38+
platform(
39+
name = "linux",
40+
constraint_values = ["@platforms//os:linux"],
41+
)
42+
43+
# Platform constraint is valid on Android but invalid on Linux:
44+
#
45+
# maven_install(
46+
# name = "jvm_import_test",
47+
# # ...
48+
# targets_compatible_with = ["@platforms//os:android"],
49+
# # ...
50+
# )
51+
platform_transition_jar(
52+
name = "findbugs_for_android",
53+
src = "@jvm_import_test//:com_google_code_findbugs_jsr305_3_0_2",
54+
platform = ":android",
55+
)
56+
57+
platform_transition_jar(
58+
name = "findbugs_for_linux",
59+
src = "@jvm_import_test//:com_google_code_findbugs_jsr305_3_0_2",
60+
platform = ":linux",
61+
)
62+
63+
build_test(
64+
name = "test_does_jar_artifact_work_with_matching_platform_constraint",
65+
targets = [
66+
":findbugs_for_android",
67+
],
68+
)
69+
3270
jvm_import_test_suite(name = "jvm_import_tests")

tests/unit/jvm_import/jvm_import_test.bzl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ does_jvm_import_export_a_package_provider_test = analysistest.make(
8888
},
8989
)
9090

91+
def _does_jvm_import_enforce_platform_constraints_impl(ctx):
92+
env = analysistest.begin(ctx)
93+
asserts.expect_failure(env, expected_failure_msg = "didn't satisfy constraint")
94+
return analysistest.end(env)
95+
96+
does_jvm_import_enforce_platform_constraints_test = analysistest.make(
97+
_does_jvm_import_enforce_platform_constraints_impl,
98+
expect_failure = True,
99+
)
100+
91101
def _does_non_jvm_import_target_carry_metadata(ctx):
92102
env = analysistest.begin(ctx)
93103

@@ -121,6 +131,10 @@ def jvm_import_test_suite(name):
121131
target_under_test = "@jvm_import_test//:com_google_code_findbugs_jsr305",
122132
src = "@jvm_import_test//:com_google_code_findbugs_jsr305",
123133
)
134+
does_jvm_import_enforce_platform_constraints_test(
135+
name = "does_jvm_import_enforce_platform_constraints_test",
136+
target_under_test = ":findbugs_for_linux",
137+
)
124138

125139
# TODO: restore once https://github.com/bazelbuild/rules_license/issues/154 is resolved
126140
# does_non_jvm_import_target_carry_metadata_test(
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def _xsitn_impl(settings, attr):
2+
return {"//command_line_option:platforms": str(attr.platform)}
3+
4+
_transition = transition(
5+
implementation = _xsitn_impl,
6+
inputs = [],
7+
outputs = ["//command_line_option:platforms"],
8+
)
9+
10+
def _rule_impl(ctx):
11+
return [
12+
DefaultInfo(files = depset(transitive = [ctx.attr.src[0][DefaultInfo].files])),
13+
]
14+
15+
platform_transition_jar = rule(
16+
implementation = _rule_impl,
17+
attrs = {
18+
"src": attr.label(
19+
cfg = _transition,
20+
),
21+
"platform": attr.label(),
22+
},
23+
doc = """
24+
Depend on a JAR for a specified platform.
25+
This isn't typical usage - for the most part, Java builds should use a consistent config.
26+
Using a transition enables modelling multi-platform builds within the build system for the purposes of testing.
27+
""",
28+
)

0 commit comments

Comments
 (0)