Skip to content

Commit

Permalink
Add test when toolchain is not declared but it has been used in action
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 505727217
Change-Id: Ie2ff3331e1c3da008e076c3e94d0597e3f6ee44c
  • Loading branch information
kotlaja authored and copybara-github committed Jan 30, 2023
1 parent 70811b1 commit b2d47a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,13 @@ private void verifyExecGroupExists(String execGroup, RuleContext ctx) throws Eva
}
}

private void verifyAutomaticExecGroupExists(String execGroup, RuleContext ctx)
throws EvalException {
if (!ctx.hasToolchainContext(execGroup)) {
throw Starlark.errorf("Action declared for non-existent toolchain '%s'.", execGroup);
}
}

private void checkValidGroupName(String execGroup) throws EvalException {
if (!StarlarkExecGroupCollection.isValidGroupName(execGroup)) {
throw Starlark.errorf("Invalid name for exec group '%s'.", execGroup);
Expand Down Expand Up @@ -726,7 +733,7 @@ private void registerStarlarkAction(
builder.setExecGroup(execGroup);
} else if (useAutoExecGroups && toolchainUnchecked != Starlark.NONE) {
String toolchain = (String) toolchainUnchecked;
verifyExecGroupExists(toolchain, ruleContext);
verifyAutomaticExecGroupExists(toolchain, ruleContext);
builder.setExecGroup(toolchain);
} else {
builder.setExecGroup(ExecGroup.DEFAULT_EXEC_GROUP_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,26 @@ public void ctxToolchainsPrint_automaticExecGroupsEnabled() throws Exception {
assertContainsEvent("<toolchain_context.resolved_labels: //rule:toolchain_type_1>");
}

@Test
@TestParameters({
"{action: ctx.actions.run}",
"{action: ctx.actions.run_shell}",
})
public void toolchainNotDefinedButUsedInAction(String action) throws Exception {
createCustomRule(
/* action= */ action,
/* actionParameters= */ "toolchain = '//rule:toolchain_type_1',",
/* extraAttributes= */ "",
/* toolchains= */ "[]",
/* execGroups= */ "");
useConfiguration("--incompatible_auto_exec_groups");

reporter.removeHandler(failFastHandler);
getConfiguredTarget("//test:custom_rule_name");

assertContainsEvent("Action declared for non-existent toolchain '//rule:toolchain_type_1'");
}

@Test
@TestParameters({
"{action: ctx.actions.run}",
Expand Down

0 comments on commit b2d47a0

Please sign in to comment.