Skip to content

Commit

Permalink
Add basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jan 19, 2025
1 parent cc2339b commit 2129d34
Showing 1 changed file with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.devtools.build.lib.packages.ExecGroup.DEFAULT_EXEC_GROUP_NAME;
import static com.google.devtools.build.lib.skyframe.BzlLoadValue.keyForBuild;

import com.google.devtools.build.lib.actions.Action;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.cmdline.Label;
Expand Down Expand Up @@ -707,4 +708,124 @@ public void ruleOverridePlatformExecGroupExecProperty() throws Exception {
assertThat(getGeneratingAction(target, "test/out.txt").getOwner().getExecProperties())
.containsExactly("ripeness", "unknown");
}

@Test
public void ruleExecGroupCompatibleWith() throws Exception {
scratch.overwriteFile(
"platform/BUILD",
"""
constraint_setting(
name = "fast_cpu",
default_constraint_value = ":no_fast_cpu",
)
constraint_value(
name = "no_fast_cpu",
constraint_setting = ":fast_cpu",
)
constraint_value(
name = "has_fast_cpu",
constraint_setting = ":fast_cpu",
)
constraint_setting(
name = "gpu",
default_constraint_value = ":no_gpu",
)
constraint_value(
name = "no_gpu",
constraint_setting = ":gpu",
)
constraint_value(
name = "has_gpu",
constraint_setting = ":gpu",
)
platform(
name = "default_platform",
)
platform(
name = "fast_cpu_platform",
constraint_values = [":has_fast_cpu"],
exec_properties = {
"require_fast_cpu": "true",
},
)
platform(
name = "gpu_platform",
constraint_values = [":has_gpu"],
exec_properties = {
"require_gpu": "true",
},
)
""");

scratch.file(
"test/defs.bzl",
"""
def _impl(ctx):
object_file = ctx.actions.declare_file(ctx.label.name + ".o")
ctx.actions.run_shell(
outputs = [object_file],
command = "touch $1",
arguments = [object_file.path],
)
executable = ctx.actions.declare_file(ctx.label.name)
ctx.actions.run_shell(
inputs = [object_file],
outputs = [executable],
command = "touch $1",
arguments = [executable.path],
exec_group = "cpp_link",
)
return [
DefaultInfo(
executable = executable,
files = depset([object_file, executable]),
),
]
my_cc_test = rule(
implementation = _impl,
test = True,
exec_groups = {
"cpp_link": exec_group(),
},
)
""");

scratch.file(
"test/BUILD",
"""
load("//test:defs.bzl", "my_cc_test")
my_cc_test(
name = "my_test",
exec_group_compatible_with = {
"cpp_link": ["//platform:has_fast_cpu"],
"test": ["//platform:has_gpu"],
},
)
""");

useConfiguration(
"--platforms=//platform:default_platform", "--extra_execution_platforms=//platform:all");

ConfiguredTarget target = getConfiguredTarget("//test:my_test");

assertThat(getGeneratingAction(target, "test/my_test.o").getExecProperties()).containsExactly();
assertThat(getGeneratingAction(target, "test/my_test").getExecProperties())
.containsExactly("require_fast_cpu", "true");
Action testAction =
getActions("//test:my_test").stream()
.filter(action -> action.getMnemonic().equals("TestRunner"))
.findFirst()
.orElseThrow();
assertThat(testAction.getExecProperties()).containsExactly("require_gpu", "true");
}
}

0 comments on commit 2129d34

Please sign in to comment.