Skip to content

Commit

Permalink
Migrate more tests to use Starlark cc_toolchain configuration
Browse files Browse the repository at this point in the history
    Issue #5380
    RELNOTES: None.
    PiperOrigin-RevId: 239053687
  • Loading branch information
Luca Di Grazia committed Sep 4, 2022
1 parent e64c5bc commit 808a67d
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 513 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.ListMultimap;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.util.BazelMockAndroidSupport;
Expand Down Expand Up @@ -123,7 +124,7 @@ public void testTargetNotInWhitelist() throws Exception {

reporter.removeHandler(failFastHandler);
getConfiguredTarget("//test/not_whitelisted:test");
assertContainsEvent("Non-whitelisted use of Starlark transition");
assertContainsEvent("Non-whitelisted use of function-base split transition");
}

private void testSplitTransitionCheckSplitAttrDeps(ConfiguredTarget target) throws Exception {
Expand Down Expand Up @@ -677,35 +678,27 @@ public void testInvalidOutputKey_analysisTest() throws Exception {
}

@Test
public void testCannotTransitionWithoutFlag() throws Exception {
writeBasicTestFiles();
setSkylarkSemanticsOptions("--experimental_starlark_config_transitions=false");

reporter.removeHandler(failFastHandler);
getConfiguredTarget("//test/skylark:test");
assertContainsEvent(
"Starlark-defined transitions on rule attributes is experimental and disabled by default");
}
public void testTransitionOnBuildSetting() throws Exception {
setSkylarkSemanticsOptions(
"--experimental_starlark_config_transitions=true", "--experimental_build_setting_api");
writeWhitelistFile();

private void writeBuildSettingsBzl() throws Exception {
scratch.file(
"test/skylark/build_settings.bzl",
"BuildSettingInfo = provider(fields = ['value'])",
"def _impl(ctx):",
" return [BuildSettingInfo(value = ctx.build_setting_value)]",
"int_flag = rule(implementation = _impl, build_setting = config.int(flag=True))");
}
"string_flag = rule(implementation = _impl, build_setting = config.string(flag=True))");

private void writeRulesWithAttrTransitionBzl() throws Exception {
scratch.file(
"test/skylark/rules.bzl",
"load('//test/skylark:build_settings.bzl', 'BuildSettingInfo')",
"def _transition_impl(settings, attr):",
" return {'//test/skylark:the-answer': 42}",
" return {'//test:cute-animal-fact': 'puffins mate for life'}",
"my_transition = transition(",
" implementation = _transition_impl,",
" inputs = [],",
" outputs = ['//test/skylark:the-answer']",
" outputs = ['//test:cute-animal-fact']",
")",
"def _rule_impl(ctx):",
" return struct(dep = ctx.attr.dep)",
Expand All @@ -722,193 +715,47 @@ private void writeRulesWithAttrTransitionBzl() throws Exception {
"dep_rule_impl = rule(",
" implementation = _dep_rule_impl,",
" attrs = {",
" 'fact': attr.label(default = '//test/skylark:the-answer'),",
" 'fact': attr.label(default = '//test/skylark:cute-animal-fact'),",
" }",
")");
}

@Test
public void testTransitionOnBuildSetting_fromDefault() throws Exception {
setSkylarkSemanticsOptions(
"--experimental_starlark_config_transitions=true", "--experimental_build_setting_api=true");
writeWhitelistFile();
writeBuildSettingsBzl();
writeRulesWithAttrTransitionBzl();
scratch.file(
"test/skylark/BUILD",
"load('//test/skylark:rules.bzl', 'my_rule')",
"load('//test/skylark:build_settings.bzl', 'int_flag')",
"load('//test/skylark:build_settings.bzl', 'string_flag')",
"my_rule(name = 'test', dep = ':dep')",
"my_rule(name = 'dep')",
"int_flag(name = 'the-answer', build_setting_default = 0)");

ConfiguredTarget dep =
Iterables.getOnlyElement(
(List<ConfiguredTarget>) getConfiguredTarget("//test/skylark:test").get("dep"));
assertThat(
getConfiguration(dep)
.getOptions()
.getStarlarkOptions()
.get(Label.parseAbsoluteUnchecked("//test/skylark:the-answer")))
.isEqualTo(42);
}
"string_flag(",
" name = 'cute-animal-fact',",
" build_setting_default = 'cows produce more milk when they listen to soothing music',",
")");

@Test
public void testTransitionOnBuildSetting_fromCommandLine() throws Exception {
setSkylarkSemanticsOptions(
"--experimental_starlark_config_transitions=true", "--experimental_build_setting_api=true");
writeWhitelistFile();
writeBuildSettingsBzl();
writeRulesWithAttrTransitionBzl();
scratch.file(
"test/skylark/BUILD",
"load('//test/skylark:rules.bzl', 'my_rule')",
"load('//test/skylark:build_settings.bzl', 'int_flag')",
"my_rule(name = 'test', dep = ':dep')",
"my_rule(name = 'dep')",
"int_flag(name = 'the-answer', build_setting_default = 0)");
useConfiguration(ImmutableMap.of("//test:cute-animal-fact", "cats can't taste sugar"));
getConfiguredTarget("//test/skylark:test");

useConfiguration(ImmutableMap.of("//test/skylark:the-answer", 7));
ConfiguredTarget test = getConfiguredTarget("//test/skylark:test");
assertThat(
getConfiguration(test)
.getOptions()
.getStarlarkOptions()
.get(Label.parseAbsoluteUnchecked("//test/skylark:the-answer")))
.isEqualTo(7);
@SuppressWarnings("unchecked")
BuildConfiguration depConfiguration =
getConfiguration(
Iterables.getOnlyElement(
(List<ConfiguredTarget>) getConfiguredTarget("//test/skylark:test").get("dep")));

ConfiguredTarget dep = Iterables.getOnlyElement((List<ConfiguredTarget>) test.get("dep"));
assertThat(
getConfiguration(dep)
depConfiguration
.getOptions()
.getStarlarkOptions()
.get(Label.parseAbsoluteUnchecked("//test/skylark:the-answer")))
.isEqualTo(42);
.get(Label.parseAbsoluteUnchecked("//test:cute-animal-fact")))
.isEqualTo("puffins mate for life");
}

@Test
public void testTransitionOnBuildSetting_badValue() throws Exception {
setSkylarkSemanticsOptions(
"--experimental_build_setting_api=true", "--experimental_starlark_config_transitions");
writeWhitelistFile();
writeBuildSettingsBzl();
scratch.file(
"test/skylark/rules.bzl",
"load('//test/skylark:build_settings.bzl', 'BuildSettingInfo')",
"def _transition_impl(settings, attr):",
" return {'//test/skylark:the-answer': 'What do you get if you multiply six by nine?'}",
"my_transition = transition(",
" implementation = _transition_impl,",
" inputs = [],",
" outputs = ['//test/skylark:the-answer']",
")",
"def _rule_impl(ctx):",
" return struct(dep = ctx.attr.dep)",
"my_rule = rule(",
" implementation = _rule_impl,",
" attrs = {",
" 'dep': attr.label(cfg = my_transition),",
" '_whitelist_function_transition': attr.label(",
" default = '//tools/whitelists/function_transition_whitelist'),",
" }",
")",
"def _dep_rule_impl(ctx):",
" return [BuildSettingInfo(value = ctx.attr.fact[BuildSettingInfo].value)]",
"dep_rule_impl = rule(",
" implementation = _dep_rule_impl,",
" attrs = {",
" 'fact': attr.label(default = '//test/skylark:the-answer'),",
" }",
")");

scratch.file(
"test/skylark/BUILD",
"load('//test/skylark:rules.bzl', 'my_rule')",
"load('//test/skylark:build_settings.bzl', 'int_flag')",
"my_rule(name = 'test', dep = ':dep')",
"my_rule(name = 'dep')",
"int_flag(",
" name = 'the-answer',",
" build_setting_default = 0,",
")");

reporter.removeHandler(failFastHandler);
getConfiguredTarget("//test/skylark:test");
assertContainsEvent(
"expected value of type 'int' for //test/skylark:the-answer, "
+ "but got \"What do you get if you multiply six by nine?\" (string)");
}

@Test
public void testTransitionOnBuildSetting_noSuchTarget() throws Exception {
setSkylarkSemanticsOptions(
"--experimental_build_setting_api=true", "--experimental_starlark_config_transitions");
writeWhitelistFile();
writeRulesWithAttrTransitionBzl();
// Still need to write this file in order not to rewrite rules.bzl file (has loads from this
// file)
writeBuildSettingsBzl();
scratch.file(
"test/skylark/BUILD",
"load('//test/skylark:rules.bzl', 'my_rule')",
"my_rule(name = 'test', dep = ':dep')",
"my_rule(name = 'dep')");

reporter.removeHandler(failFastHandler);
getConfiguredTarget("//test/skylark:test");
assertContainsEvent(
"no such target '//test/skylark:the-answer': target "
+ "'the-answer' not declared in package");
}

@Test
public void testTransitionOnBuildSetting_notABuildSetting() throws Exception {
setSkylarkSemanticsOptions(
"--experimental_build_setting_api=true", "--experimental_starlark_config_transitions");
writeWhitelistFile();
writeRulesWithAttrTransitionBzl();
scratch.file(
"test/skylark/build_settings.bzl",
"BuildSettingInfo = provider(fields = ['value'])",
"def _impl(ctx):",
" return [BuildSettingInfo(value = ctx.build_setting_value)]",
"int_flag = rule(implementation = _impl)");
scratch.file(
"test/skylark/BUILD",
"load('//test/skylark:rules.bzl', 'my_rule')",
"load('//test/skylark:build_settings.bzl', 'int_flag')",
"my_rule(name = 'test', dep = ':dep')",
"my_rule(name = 'dep')",
"int_flag(name = 'the-answer')");

reporter.removeHandler(failFastHandler);
getConfiguredTarget("//test/skylark:test");
assertContainsEvent(
"attempting to transition on '//test/skylark:the-answer' which "
+ "is not a build setting");
}

@Test
public void testTransitionOnBuildSetting_aliasedBuildSetting() throws Exception {
setSkylarkSemanticsOptions(
"--experimental_starlark_config_transitions=true", "--experimental_build_setting_api=true");
writeWhitelistFile();
writeBuildSettingsBzl();
writeRulesWithAttrTransitionBzl();
scratch.file(
"test/skylark/BUILD",
"load('//test/skylark:rules.bzl', 'my_rule')",
"load('//test/skylark:build_settings.bzl', 'int_flag')",
"my_rule(name = 'test', dep = ':dep')",
"my_rule(name = 'dep')",
"int_flag(name = 'the-secret-answer', build_setting_default = 0)",
"alias(name = 'the-answer', actual = ':the-secret-answer')");
public void testCannotTransitionWithoutFlag() throws Exception {
writeBasicTestFiles();
setSkylarkSemanticsOptions("--experimental_starlark_config_transitions=false");

reporter.removeHandler(failFastHandler);
getConfiguredTarget("//test/skylark:test");
assertContainsEvent(
"attempting to transition on '//test/skylark:the-answer' which "
+ "is not a build setting");
"Starlark-defined transitions on rule attributes is experimental and disabled by default");
}

@Test
Expand Down
Loading

0 comments on commit 808a67d

Please sign in to comment.