Skip to content

Commit 24ebf47

Browse files
committed
fix: add integer attributes to amend_artifact for explicit unset state
1 parent a23c85c commit 24ebf47

File tree

4 files changed

+163
-3
lines changed

4 files changed

+163
-3
lines changed

private/extensions/maven.bzl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,11 @@ amend_artifact = tag_class(
146146
"name": attr.string(default = DEFAULT_NAME),
147147
"coordinates": attr.string(doc = "Coordinates of the artifact to amend. Only `group:artifact` are used for matching.", mandatory = True),
148148
"force_version": attr.bool(default = False),
149+
"force_version_int": attr.int(default = -1),
149150
"neverlink": attr.bool(),
151+
"neverlink_int": attr.int(default = -1),
150152
"testonly": attr.bool(),
153+
"testonly_int": attr.int(default = -1),
151154
"exclusions": attr.string_list(doc = "Maven artifact tuples, in `artifactId:groupId` format", allow_empty = True),
152155
},
153156
)
@@ -308,6 +311,13 @@ def _deduplicate_artifacts_with_root_priority(name, root_artifacts, non_root_art
308311

309312
return root_artifacts + filtered_non_root
310313

314+
def _get_tri_state_bool(amend_val, amend_int_val, original_val):
315+
if amend_int_val != -1:
316+
return amend_int_val == 1
317+
if amend_val:
318+
return True
319+
return original_val
320+
311321
def _amend_artifact(original_artifact, amend):
312322
"""Apply amendments to an artifact struct, returning a new amended struct."""
313323

@@ -319,15 +329,16 @@ def _amend_artifact(original_artifact, amend):
319329
final_exclusions = existing_exclusions + new_exclusions
320330

321331
# Create new struct with amendments applied
332+
322333
return struct(
323334
group = original_artifact.group,
324335
artifact = original_artifact.artifact,
325336
version = getattr(original_artifact, "version", None),
326337
packaging = getattr(original_artifact, "packaging", None),
327338
classifier = getattr(original_artifact, "classifier", None),
328-
force_version = amend.force_version if amend.force_version else getattr(original_artifact, "force_version", None),
329-
neverlink = amend.neverlink if amend.neverlink else getattr(original_artifact, "neverlink", None),
330-
testonly = amend.testonly if amend.testonly else getattr(original_artifact, "testonly", None),
339+
force_version = _get_tri_state_bool(amend.force_version, amend.force_version_int, getattr(original_artifact, "force_version", None)),
340+
neverlink = _get_tri_state_bool(amend.neverlink, amend.neverlink_int, getattr(original_artifact, "neverlink", None)),
341+
testonly = _get_tri_state_bool(amend.testonly, amend.testonly_int, getattr(original_artifact, "testonly", None)),
331342
exclusions = final_exclusions if final_exclusions else None,
332343
)
333344

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
2+
3+
# This test checks if the testonly attribute was correctly applied.
4+
# If testonly=True applied, this target should have testonly=1.
5+
# But build_test doesn't check attribute values directly.
6+
# We can use a rule that requires testonly dependencies?
7+
# or just inspect with genquery.
8+
9+
# 1. Guava: Unset via chaining (1 -> 0). Expect: False (count 0)
10+
genquery(
11+
name = "check_guava_testonly",
12+
expression = "attr(testonly, 1, @maven//:com_google_guava_guava)",
13+
scope = ["@maven//:com_google_guava_guava"],
14+
)
15+
16+
# 2. Commons-Lang3: Set True via Int (1). Expect: True (count 1)
17+
genquery(
18+
name = "check_lang3_testonly",
19+
expression = "attr(testonly, 1, @maven//:org_apache_commons_commons_lang3)",
20+
scope = ["@maven//:org_apache_commons_commons_lang3"],
21+
)
22+
23+
# 3. Commons-Io: Set True via Legacy (True). Expect: True (count 1)
24+
genquery(
25+
name = "check_io_testonly",
26+
expression = "attr(testonly, 1, @maven//:commons_io_commons_io)",
27+
scope = ["@maven//:commons_io_commons_io"],
28+
)
29+
30+
# 4. Slf4j-api: Set Neverlink True via Int (1). Expect: True (count 1)
31+
genquery(
32+
name = "check_slf4j_neverlink",
33+
expression = "attr(neverlink, 1, @maven//:org_slf4j_slf4j_api)",
34+
scope = ["@maven//:org_slf4j_slf4j_api"],
35+
)
36+
37+
sh_test(
38+
name = "verify_testonly",
39+
srcs = ["verify.sh"],
40+
args = [
41+
"$(location :check_guava_testonly)",
42+
"$(location :check_lang3_testonly)",
43+
"$(location :check_io_testonly)",
44+
"$(location :check_slf4j_neverlink)",
45+
],
46+
data = [
47+
":check_guava_testonly",
48+
":check_lang3_testonly",
49+
":check_io_testonly",
50+
":check_slf4j_neverlink",
51+
],
52+
)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module(
2+
name = "amend_artifact_test",
3+
version = "0.0.1",
4+
)
5+
6+
7+
bazel_dep(name = "rules_jvm_external", version = "0.0.1")
8+
bazel_dep(name = "bazel_skylib", version = "1.7.1")
9+
10+
local_path_override(
11+
module_name = "rules_jvm_external",
12+
path = "../../..",
13+
)
14+
15+
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
16+
17+
maven.install(
18+
artifacts = [
19+
"com.google.guava:guava:31.1-jre",
20+
"org.apache.commons:commons-lang3:3.12.0",
21+
"commons-io:commons-io:2.11.0",
22+
"org.slf4j:slf4j-api:1.7.32",
23+
],
24+
repositories = [
25+
"https://repo1.maven.org/maven2",
26+
],
27+
)
28+
29+
# guava is not testonly by default. We try to set it to true.
30+
# 1. Unset via Chaining: Set to 1, then set to 0. Result should be False.
31+
maven.amend_artifact(
32+
name = "maven",
33+
coordinates = "com.google.guava:guava",
34+
testonly_int = 1,
35+
)
36+
maven.amend_artifact(
37+
name = "maven",
38+
coordinates = "com.google.guava:guava",
39+
testonly_int = 0,
40+
)
41+
42+
# 2. Set True via Int
43+
maven.amend_artifact(
44+
name = "maven",
45+
coordinates = "org.apache.commons:commons-lang3",
46+
testonly_int = 1,
47+
)
48+
49+
# 3. Set True via Legacy Bool
50+
maven.amend_artifact(
51+
name = "maven",
52+
coordinates = "commons-io:commons-io",
53+
testonly = True,
54+
)
55+
56+
# 4. Set Neverlink True via Int
57+
maven.amend_artifact(
58+
name = "maven",
59+
coordinates = "org.slf4j:slf4j-api",
60+
neverlink_int = 1,
61+
)
62+
63+
use_repo(maven, "maven")
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
set -e
3+
GUAVA_OUTPUT="$1"
4+
LANG3_OUTPUT="$2"
5+
IO_OUTPUT="$3"
6+
SLF4J_OUTPUT="$4"
7+
8+
# 1. Guava (Unset via chaining): Should be empty (testonly=0)
9+
if [ -s "$GUAVA_OUTPUT" ]; then
10+
echo "Error: Guava output is NOT empty. 'testonly' was not unset."
11+
cat "$GUAVA_OUTPUT"
12+
exit 1
13+
fi
14+
15+
# 2. Lang3 (Set via Int): Should be non-empty (testonly=1)
16+
if [ ! -s "$LANG3_OUTPUT" ]; then
17+
echo "Error: Lang3 output is empty. 'testonly_int=1' failed."
18+
exit 1
19+
fi
20+
21+
# 3. IO (Set via Bool): Should be non-empty (testonly=1)
22+
if [ ! -s "$IO_OUTPUT" ]; then
23+
echo "Error: Commons-IO output is empty. 'testonly=True' failed."
24+
exit 1
25+
fi
26+
27+
# 4. Slf4j (Neverlink via Int): Should be non-empty (neverlink=1)
28+
if [ ! -s "$SLF4J_OUTPUT" ]; then
29+
echo "Error: Slf4j output is empty. 'neverlink_int=1' failed."
30+
exit 1
31+
fi
32+
33+
echo "All verification checks passed!"
34+

0 commit comments

Comments
 (0)