Skip to content

Commit 91d176d

Browse files
agluszakclaude
andcommitted
Fix analysis tests for experimental_prune_transitive_deps
Revive PR bazelbuild#1343 by rbeazleyspot which adds analysis tests for the experimental_prune_transitive_deps feature. The original tests failed because config_settings creates a configuration transition, causing output paths to include a hash suffix (e.g., k8-fastbuild-ST-xxx). Fixed by comparing file basenames instead of full paths using matching.file_basename_equals() predicates. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent e85833b commit 91d176d

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

src/test/starlark/rules/experimental_prune_transitive_deps_tests.bzl

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
2+
load("@rules_testing//lib:truth.bzl", "matching")
23
load("//src/test/starlark:case.bzl", "suite")
34
load(":arrangement.bzl", "arrange")
4-
load(":util.bzl", "abi_jar_of", "values_for_flag_of")
5+
load(":util.bzl", "abi_jar_of", "basename_of", "values_for_flag_of")
56

67
def _classpath_assertions_(env, target):
78
action = env.expect.that_target(target).action_named(env.ctx.attr.on_action_mnemonic)
8-
action.inputs().contains_at_least([abi_jar_of(f.short_path) for f in env.ctx.files.want_inputs if not f.short_path.endswith("jdeps")])
99

10-
values_for_flag_of(action, "--classpath").contains_at_least([abi_jar_of(f.path) for f in env.ctx.files.want_classpath if not f.short_path.endswith("jdeps")])
11-
values_for_flag_of(action, "--classpath").contains_none_of([f.path for f in env.ctx.files.not_want_classpath])
10+
# Use basename comparison to avoid configuration transition path issues
11+
# When config_settings is used, targets are built in a transitioned configuration
12+
# with a different output path (e.g., k8-fastbuild-ST-xxx instead of k8-fastbuild)
13+
14+
# For inputs (File objects), use file_basename_equals matcher
15+
want_input_matchers = [
16+
matching.file_basename_equals(abi_jar_of(f.basename))
17+
for f in env.ctx.files.want_inputs
18+
if not f.basename.endswith("jdeps")
19+
]
20+
action.inputs().contains_at_least_predicates(want_input_matchers)
21+
22+
# For classpath (string paths), use str_endswith matcher to match by basename
23+
want_classpath_basenames = [abi_jar_of(f.basename) for f in env.ctx.files.want_classpath if not f.basename.endswith("jdeps")]
24+
values_for_flag_of(action, "--classpath").transform(desc = "basenames", map_each = basename_of).contains_at_least(want_classpath_basenames)
25+
26+
not_want_basenames = [f.basename for f in env.ctx.files.not_want_classpath]
27+
values_for_flag_of(action, "--classpath").transform(desc = "basenames", map_each = basename_of).contains_none_of(not_want_basenames)
1228

1329
def _test_classpath_experimental_prune_transitive_deps_False(test):
1430
(dependency_a_trans_dep_jar, dependency_a, main_target_library) = arrange(test)
@@ -22,27 +38,27 @@ def _test_classpath_experimental_prune_transitive_deps_False(test):
2238
str(Label("@rules_kotlin//kotlin/settings:experimental_strict_associate_dependencies")): False,
2339
},
2440
attr_values = {
41+
"not_want_classpath": [],
2542
"on_action_mnemonic": "KotlinCompile",
26-
"want_inputs": [
43+
"want_classpath": [
2744
dependency_a,
2845
dependency_a_trans_dep_jar,
2946
],
3047
"want_direct_dependencies": [
3148
dependency_a,
3249
dependency_a_trans_dep_jar,
3350
],
34-
"want_classpath": [
51+
"want_inputs": [
3552
dependency_a,
3653
dependency_a_trans_dep_jar,
3754
],
38-
"not_want_classpath": [],
3955
},
4056
attrs = {
57+
"not_want_classpath": attr.label_list(providers = [DefaultInfo], allow_files = True),
4158
"on_action_mnemonic": attr.string(),
42-
"want_inputs": attr.label_list(providers = [DefaultInfo], allow_files = True),
43-
"want_direct_dependencies": attr.label_list(providers = [DefaultInfo], allow_files = True),
4459
"want_classpath": attr.label_list(providers = [DefaultInfo], allow_files = True),
45-
"not_want_classpath": attr.label_list(providers = [DefaultInfo], allow_files = True),
60+
"want_direct_dependencies": attr.label_list(providers = [DefaultInfo], allow_files = True),
61+
"want_inputs": attr.label_list(providers = [DefaultInfo], allow_files = True),
4662
},
4763
)
4864

@@ -58,26 +74,26 @@ def _test_classpath_experimental_prune_transitive_deps_True(test):
5874
str(Label("@rules_kotlin//kotlin/settings:experimental_strict_associate_dependencies")): False,
5975
},
6076
attr_values = {
77+
"not_want_classpath": [
78+
dependency_a_trans_dep_jar,
79+
],
6180
"on_action_mnemonic": "KotlinCompile",
62-
"want_inputs": [
81+
"want_classpath": [
6382
dependency_a,
6483
],
6584
"want_direct_dependencies": [
6685
dependency_a,
6786
],
68-
"want_classpath": [
87+
"want_inputs": [
6988
dependency_a,
7089
],
71-
"not_want_classpath": [
72-
dependency_a_trans_dep_jar,
73-
],
7490
},
7591
attrs = {
92+
"not_want_classpath": attr.label_list(providers = [DefaultInfo], allow_files = True),
7693
"on_action_mnemonic": attr.string(),
77-
"want_inputs": attr.label_list(providers = [DefaultInfo], allow_files = True),
78-
"want_direct_dependencies": attr.label_list(providers = [DefaultInfo], allow_files = True),
7994
"want_classpath": attr.label_list(providers = [DefaultInfo], allow_files = True),
80-
"not_want_classpath": attr.label_list(providers = [DefaultInfo], allow_files = True),
95+
"want_direct_dependencies": attr.label_list(providers = [DefaultInfo], allow_files = True),
96+
"want_inputs": attr.label_list(providers = [DefaultInfo], allow_files = True),
8197
},
8298
)
8399

src/test/starlark/rules/util.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
def basename_of(path):
2+
"""Extract basename from a path string."""
3+
return path.split("/")[-1]
4+
15
def values_for_flag_of(action_subject, flag):
26
return action_subject.argv().transform(desc = "parsed()", loop = _curry_denormalise_flags_with(flag))
37

0 commit comments

Comments
 (0)