Skip to content

Commit

Permalink
Add tests for using the cfg parameter to rule().
Browse files Browse the repository at this point in the history
Also split StarlarkRuleClassTests into its own target.

Part of bazelbuild#22248.

PiperOrigin-RevId: 632182116
Change-Id: Ia6dcf68f45d1fd8b48026ee94c827bdc9ad992ce
  • Loading branch information
katre authored and Kila2 committed May 13, 2024
1 parent 58bdd2b commit cd53b9f
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 15 deletions.
52 changes: 43 additions & 9 deletions src/test/java/com/google/devtools/build/lib/starlark/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ java_test(
timeout = "long",
srcs = glob(
["*.java"],
exclude = ["BindTest.java"],
exclude = [
"StarlarkRuleClassFunctionsTest.java",
"BindTest.java",
],
),
shard_count = 25,
test_class = "com.google.devtools.build.lib.AllTests",
Expand All @@ -40,16 +43,12 @@ java_test(
"//src/main/java/com/google/devtools/build/lib/analysis:actions/template_expansion_action",
"//src/main/java/com/google/devtools/build/lib/analysis:actions_provider",
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
"//src/main/java/com/google/devtools/build/lib/analysis:config/build_configuration",
"//src/main/java/com/google/devtools/build/lib/analysis:config/starlark_defined_config_transition",
"//src/main/java/com/google/devtools/build/lib/analysis:config/toolchain_type_requirement",
"//src/main/java/com/google/devtools/build/lib/analysis:config/transitions/no_transition",
"//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
"//src/main/java/com/google/devtools/build/lib/analysis:file_provider",
"//src/main/java/com/google/devtools/build/lib/analysis:resolved_toolchain_context",
"//src/main/java/com/google/devtools/build/lib/analysis:run_environment_info",
"//src/main/java/com/google/devtools/build/lib/analysis:starlark/args",
"//src/main/java/com/google/devtools/build/lib/analysis:starlark/starlark_config",
"//src/main/java/com/google/devtools/build/lib/analysis:starlark/starlark_exec_group_collection",
"//src/main/java/com/google/devtools/build/lib/analysis:test/analysis_test_result_info",
"//src/main/java/com/google/devtools/build/lib/analysis:test/instrumented_files_info",
Expand Down Expand Up @@ -77,10 +76,8 @@ java_test(
"//src/main/java/com/google/devtools/common/options",
"//src/main/java/net/starlark/java/annot",
"//src/main/java/net/starlark/java/eval",
"//src/main/java/net/starlark/java/syntax",
"//src/main/protobuf:failure_details_java_proto",
"//src/test/java/com/google/devtools/build/lib/actions/util",
"//src/test/java/com/google/devtools/build/lib/analysis/testing",
"//src/test/java/com/google/devtools/build/lib/analysis/util",
"//src/test/java/com/google/devtools/build/lib/bazel/bzlmod:util",
"//src/test/java/com/google/devtools/build/lib/exec/util",
Expand All @@ -90,8 +87,6 @@ java_test(
"//src/test/java/com/google/devtools/build/lib/testutil:JunitUtils",
"//src/test/java/com/google/devtools/build/lib/testutil:TestConstants",
"//third_party:guava",
"//third_party:guava-testlib",
"//third_party:jsr305",
"//third_party:junit4",
"//third_party:mockito",
"//third_party:truth",
Expand All @@ -118,3 +113,42 @@ java_test(
"@bazel_tools//tools/java/runfiles",
],
)

java_test(
name = "StarlarkRuleClassFunctionsTest",
srcs = ["StarlarkRuleClassFunctionsTest.java"],
shard_count = 5,
deps = [
"//src/main/java/com/google/devtools/build/lib/actions:artifacts",
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
"//src/main/java/com/google/devtools/build/lib/analysis:config/build_configuration",
"//src/main/java/com/google/devtools/build/lib/analysis:config/toolchain_type_requirement",
"//src/main/java/com/google/devtools/build/lib/analysis:config/transitions/no_transition",
"//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
"//src/main/java/com/google/devtools/build/lib/analysis:starlark/starlark_config",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/collect/nestedset",
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/packages",
"//src/main/java/com/google/devtools/build/lib/packages:exec_group",
"//src/main/java/com/google/devtools/build/lib/rules/cpp",
"//src/main/java/com/google/devtools/build/lib/skyframe:bzl_load_value",
"//src/main/java/com/google/devtools/build/lib/util:filetype",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/net/starlark/java/eval",
"//src/main/java/net/starlark/java/syntax",
"//src/test/java/com/google/devtools/build/lib/analysis/testing",
"//src/test/java/com/google/devtools/build/lib/analysis/util",
"//src/test/java/com/google/devtools/build/lib/starlark/util",
"//src/test/java/com/google/devtools/build/lib/testutil",
"//src/test/java/com/google/devtools/build/lib/testutil:JunitUtils",
"//src/test/java/com/google/devtools/build/lib/testutil:TestConstants",
"//third_party:guava",
"//third_party:guava-testlib",
"//third_party:jsr305",
"//third_party:junit4",
"//third_party:mockito",
"//third_party:truth",
"@maven//:com_google_testparameterinjector_test_parameter_injector",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,85 @@ public void testAttrCfgTarget() throws Exception {
assertThat(NoTransition.isInstance(attr.getTransitionFactory())).isTrue();
}

private void writeRuleCfgTestRule(String cfg) throws Exception {
scratch.file(
"rule_testing/rule.bzl",
"""
def _impl(ctx):
pass
demo_rule = rule(
implementation = _impl,
cfg = %s,
)
"""
.formatted(cfg));
scratch.file(
"rule_testing/BUILD",
"""
load(":rule.bzl", "demo_rule")
demo_rule(name = "my_target")
""");
}

@Test
public void testRuleCfg_exec_string_fails() throws Exception {
writeRuleCfgTestRule("'exec'");

reporter.removeHandler(failFastHandler);
reporter.addHandler(ev.getEventCollector());
getConfiguredTarget("//rule_testing:my_target");

ev.assertContainsError("`cfg` must be set to a transition object");
}

@Test
public void testRuleCfg_exec_obj_fails() throws Exception {
writeRuleCfgTestRule("config.exec()");

reporter.removeHandler(failFastHandler);
reporter.addHandler(ev.getEventCollector());
getConfiguredTarget("//rule_testing:my_target");

ev.assertContainsError("`cfg` must be set to a transition object");
}

@Test
public void testRuleCfg_starlark() throws Exception {
scratchStarlarkTransition();
scratch.file(
"rule_testing/rule.bzl",
"""
load("//test:transitions.bzl", "parent_transition")
def _impl(ctx):
pass
demo_rule = rule(
implementation = _impl,
cfg = parent_transition,
)
""");
scratch.file(
"rule_testing/BUILD",
"""
load(":rule.bzl", "demo_rule")
demo_rule(name = "my_target")
""");

BuildConfigurationValue configuration =
getConfiguration(getConfiguredTarget("//rule_testing:my_target"));

var options = configuration.getOptions().getStarlarkOptions();
assertThat(options.get(Label.parseCanonicalUnchecked("//test:parent-flag")))
.isEqualTo("parent-changed");
assertThat(options.get(Label.parseCanonicalUnchecked("//test:parent-child-flag")))
.isEqualTo("parent-child-changed-in-parent");
assertThat(options.get(Label.parseCanonicalUnchecked("//test:child-flag"))).isNull();
}

@Test
public void incompatibleDataTransition() {
EvalException expected =
Expand Down Expand Up @@ -5712,12 +5791,15 @@ private void scratchStarlarkTransition() throws IOException {
scratch.overwriteFile(
TestConstants.TOOLS_REPOSITORY_SCRATCH
+ "tools/allowlists/function_transition_allowlist/BUILD",
"package_group(",
" name = 'function_transition_allowlist',",
" packages = [",
" '//extend_rule_testing/...',",
" ],",
")");
"""
package_group(
name = "function_transition_allowlist",
packages = [
# Allow all packages for testing.
"//...",
],
)
""");
}
scratch.file(
"test/build_settings.bzl",
Expand Down

0 comments on commit cd53b9f

Please sign in to comment.