juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 1 | // Copyright 2018 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.analysis; |
| 15 | |
Googler | 35f81b4 | 2023-07-11 08:49:47 -0700 | [diff] [blame] | 16 | import static com.google.common.collect.Iterables.getOnlyElement; |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
Googler | 068d845 | 2022-08-26 03:17:07 -0700 | [diff] [blame] | 18 | import static com.google.devtools.build.lib.bazel.bzlmod.BzlmodTestUtil.createModuleKey; |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 19 | |
Googler | 068d845 | 2022-08-26 03:17:07 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
gregce | 11f2540 | 2021-07-08 07:14:10 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
Googler | 35f81b4 | 2023-07-11 08:49:47 -0700 | [diff] [blame] | 23 | import com.google.common.eventbus.Subscribe; |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue; |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.config.transitions.ConfigurationTransition; |
gregce | 11f2540 | 2021-07-08 07:14:10 -0700 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.test.TestConfiguration.TestOptions; |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 27 | import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.analysis.util.DummyTestFragment; |
| 29 | import com.google.devtools.build.lib.analysis.util.DummyTestFragment.DummyTestOptions; |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 30 | import com.google.devtools.build.lib.cmdline.Label; |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 31 | import com.google.devtools.build.lib.packages.Rule; |
John Cater | 525227e | 2021-07-27 10:53:43 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.packages.RuleTransitionData; |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.rules.cpp.CppOptions; |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 34 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData; |
gregce | d679546 | 2020-12-14 11:06:45 -0800 | [diff] [blame] | 35 | import com.google.devtools.build.lib.testutil.TestConstants; |
juliexxia | 6778a05 | 2019-04-09 08:25:05 -0700 | [diff] [blame] | 36 | import com.google.devtools.build.lib.testutil.TestRuleClassProvider; |
gregce | fe644be | 2021-11-01 12:12:02 -0700 | [diff] [blame] | 37 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
| 38 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 39 | import com.google.devtools.build.lib.vfs.Root; |
gregce | 11f2540 | 2021-07-08 07:14:10 -0700 | [diff] [blame] | 40 | import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
| 41 | import com.google.testing.junit.testparameterinjector.TestParameters; |
Googler | 35f81b4 | 2023-07-11 08:49:47 -0700 | [diff] [blame] | 42 | import java.util.ArrayList; |
juliexxia | a13f590 | 2020-12-10 09:19:01 -0800 | [diff] [blame] | 43 | import java.util.List; |
| 44 | import java.util.Map; |
Googler | 1d4cecf | 2022-07-27 00:04:12 -0700 | [diff] [blame] | 45 | import java.util.regex.Pattern; |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 48 | |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 49 | /** Tests for StarlarkRuleTransitionProvider. */ |
gregce | 11f2540 | 2021-07-08 07:14:10 -0700 | [diff] [blame] | 50 | @RunWith(TestParameterInjector.class) |
jhorvitz | 2dd6566 | 2021-11-04 16:31:24 -0700 | [diff] [blame] | 51 | public final class StarlarkRuleTransitionProviderTest extends BuildViewTestCase { |
jhorvitz | 2dd6566 | 2021-11-04 16:31:24 -0700 | [diff] [blame] | 52 | |
juliexxia | 6778a05 | 2019-04-09 08:25:05 -0700 | [diff] [blame] | 53 | @Override |
brandjon | 232a6b8 | 2020-06-08 12:32:49 -0700 | [diff] [blame] | 54 | protected ConfiguredRuleClassProvider createRuleClassProvider() { |
juliexxia | 6778a05 | 2019-04-09 08:25:05 -0700 | [diff] [blame] | 55 | ConfiguredRuleClassProvider.Builder builder = new ConfiguredRuleClassProvider.Builder(); |
| 56 | TestRuleClassProvider.addStandardRules(builder); |
gregce | 4ed65b0 | 2020-11-18 07:06:25 -0800 | [diff] [blame] | 57 | builder.addConfigurationFragment(DummyTestFragment.class); |
juliexxia | 6778a05 | 2019-04-09 08:25:05 -0700 | [diff] [blame] | 58 | return builder.build(); |
| 59 | } |
| 60 | |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 61 | private void writeAllowlistFile() throws Exception { |
cmita | 8356358 | 2021-06-15 01:57:45 -0700 | [diff] [blame] | 62 | scratch.overwriteFile( |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 63 | "tools/allowlists/function_transition_allowlist/BUILD", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 64 | "package_group(", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 65 | " name = 'function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 66 | " packages = [", |
| 67 | " '//test/...',", |
| 68 | " ],", |
| 69 | ")"); |
| 70 | } |
| 71 | |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 72 | @Test |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 73 | public void testBadReturnTypeFromTransition() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 74 | writeAllowlistFile(); |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 75 | scratch.file( |
| 76 | "test/transitions.bzl", |
| 77 | "def _impl(settings, attr):", |
| 78 | " return 'cpu=k8'", |
| 79 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 80 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 81 | scratch.file( |
| 82 | "test/rules.bzl", |
| 83 | "load('//test:transitions.bzl', 'my_transition')", |
| 84 | "def _impl(ctx):", |
| 85 | " return []", |
| 86 | "my_rule = rule(", |
| 87 | " implementation = _impl,", |
| 88 | " cfg = my_transition,", |
| 89 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 90 | " '_allowlist_function_transition': attr.label(", |
| 91 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 92 | " ),", |
| 93 | " })"); |
| 94 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 95 | |
Googler | 35f81b4 | 2023-07-11 08:49:47 -0700 | [diff] [blame] | 96 | var collector = new AnalysisRootCauseCollector(); |
| 97 | eventBus.register(collector); |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 98 | reporter.removeHandler(failFastHandler); |
| 99 | getConfiguredTarget("//test"); |
adonovan | d07bf13 | 2021-01-08 09:10:22 -0800 | [diff] [blame] | 100 | assertContainsEvent("transition function returned string, want dict or list of dicts"); |
Googler | 35f81b4 | 2023-07-11 08:49:47 -0700 | [diff] [blame] | 101 | |
| 102 | // Verifies that the AnalysisRootCauseEvent has a no associated configuration. In this case, |
| 103 | // the error occurs during a transition, so no configuration has been determined. |
| 104 | AnalysisRootCauseEvent rootCause = getOnlyElement(collector.rootCauses); |
| 105 | assertThat(rootCause.getConfigurations()).isEmpty(); |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Test |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 109 | public void testOutputOnlyTransition() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 110 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 111 | scratch.file( |
| 112 | "test/transitions.bzl", |
| 113 | "def _impl(settings, attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 114 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 115 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 116 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 117 | scratch.file( |
| 118 | "test/rules.bzl", |
| 119 | "load('//test:transitions.bzl', 'my_transition')", |
| 120 | "def _impl(ctx):", |
| 121 | " return []", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 122 | "my_rule = rule(", |
| 123 | " implementation = _impl,", |
| 124 | " cfg = my_transition,", |
| 125 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 126 | " '_allowlist_function_transition': attr.label(", |
| 127 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 128 | " ),", |
| 129 | " })"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 130 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 131 | |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 132 | useConfiguration("--foo=pre-transition"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 133 | |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 134 | BuildConfigurationValue configuration = getConfiguration(getConfiguredTarget("//test")); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 135 | assertThat(configuration.getOptions().get(DummyTestOptions.class).foo) |
| 136 | .isEqualTo("post-transition"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void testInputAndOutputTransition() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 141 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 142 | scratch.file( |
| 143 | "test/transitions.bzl", |
| 144 | "def _impl(settings, attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 145 | " return {'//command_line_option:foo': ", |
Googler | 3d389d4 | 2023-05-02 03:41:03 -0700 | [diff] [blame] | 146 | " settings['//command_line_option:foo'].replace('pre', 'post')}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 147 | "my_transition = transition(", |
| 148 | " implementation = _impl,", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 149 | " inputs = ['//command_line_option:foo'],", |
| 150 | " outputs = ['//command_line_option:foo'],", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 151 | ")"); |
| 152 | |
| 153 | scratch.file( |
| 154 | "test/rules.bzl", |
| 155 | "load('//test:transitions.bzl', 'my_transition')", |
| 156 | "def _impl(ctx):", |
| 157 | " return []", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 158 | "my_rule = rule(", |
| 159 | " implementation = _impl,", |
| 160 | " cfg = my_transition,", |
| 161 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 162 | " '_allowlist_function_transition': attr.label(", |
| 163 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 164 | " ),", |
| 165 | " })"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 166 | |
| 167 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 168 | |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 169 | useConfiguration("--foo=pre-transition"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 170 | |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 171 | BuildConfigurationValue configuration = getConfiguration(getConfiguredTarget("//test")); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 172 | assertThat(configuration.getOptions().get(DummyTestOptions.class).foo) |
Googler | 3d389d4 | 2023-05-02 03:41:03 -0700 | [diff] [blame] | 173 | .isEqualTo("post-transition"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Test |
| 177 | public void testBuildSettingCannotTransition() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 178 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 179 | scratch.file( |
| 180 | "test/transitions.bzl", |
| 181 | "def _impl(settings, attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 182 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 183 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 184 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 185 | scratch.file( |
| 186 | "test/rules.bzl", |
| 187 | "load('//test:transitions.bzl', 'my_transition')", |
| 188 | "def _impl(ctx):", |
| 189 | " return []", |
| 190 | "my_rule = rule(", |
| 191 | " implementation = _impl,", |
| 192 | " cfg = my_transition,", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 193 | " build_setting = config.string(),", |
| 194 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 195 | " '_allowlist_function_transition': attr.label(", |
| 196 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 197 | " ),", |
| 198 | " })"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 199 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 200 | |
| 201 | reporter.removeHandler(failFastHandler); |
| 202 | getConfiguredTarget("//test"); |
| 203 | assertContainsEvent( |
| 204 | "Build setting rules cannot use the `cfg` param to apply transitions to themselves"); |
| 205 | } |
| 206 | |
| 207 | @Test |
| 208 | public void testBadCfgInput() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 209 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 210 | scratch.file( |
| 211 | "test/rules.bzl", |
| 212 | "def _impl(ctx):", |
| 213 | " return []", |
| 214 | "my_rule = rule(", |
| 215 | " implementation = _impl,", |
| 216 | " cfg = 'my_transition',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 217 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 218 | " '_allowlist_function_transition': attr.label(", |
| 219 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 220 | " ),", |
| 221 | " })"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 222 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 223 | |
| 224 | reporter.removeHandler(failFastHandler); |
| 225 | getConfiguredTarget("//test"); |
| 226 | assertContainsEvent( |
| 227 | "`cfg` must be set to a transition object initialized by the transition() function."); |
| 228 | } |
| 229 | |
| 230 | @Test |
| 231 | public void testMultipleReturnConfigs() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 232 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 233 | scratch.file( |
| 234 | "test/transitions.bzl", |
| 235 | "def _impl(settings, attr):", |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 236 | " return [", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 237 | " {'//command_line_option:foo': 'split_one'},", |
| 238 | " {'//command_line_option:foo': 'split_two'},", |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 239 | " ]", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 240 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 241 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 242 | scratch.file( |
| 243 | "test/rules.bzl", |
| 244 | "load('//test:transitions.bzl', 'my_transition')", |
| 245 | "def _impl(ctx):", |
| 246 | " return []", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 247 | "my_rule = rule(", |
| 248 | " implementation = _impl,", |
| 249 | " cfg = my_transition,", |
| 250 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 251 | " '_allowlist_function_transition': attr.label(", |
| 252 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 253 | " ),", |
| 254 | " })"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 255 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 256 | |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 257 | reporter.removeHandler(failFastHandler); |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 258 | getConfiguredTarget("//test"); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 259 | assertContainsEvent( |
| 260 | "Rule transition only allowed to return a single transitioned configuration."); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 261 | } |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 262 | @Test |
| 263 | public void testCanDoBadStuffWithParameterizedTransitionsAndSelects() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 264 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 265 | scratch.file( |
| 266 | "test/transitions.bzl", |
| 267 | "def _impl(settings, attr):", |
| 268 | " if (attr.my_configurable_attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 269 | " return {'//command_line_option:foo': 'true'}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 270 | " else:", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 271 | " return {'//command_line_option:foo': 'false'}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 272 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 273 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 274 | scratch.file( |
| 275 | "test/rules.bzl", |
| 276 | "load('//test:transitions.bzl', 'my_transition')", |
| 277 | "def _impl(ctx):", |
| 278 | " return []", |
| 279 | "my_rule = rule(", |
| 280 | " implementation = _impl,", |
| 281 | " cfg = my_transition,", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 282 | " attrs = {", |
| 283 | " 'my_configurable_attr': attr.bool(default = False),", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 284 | " '_allowlist_function_transition': attr.label(", |
| 285 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 286 | " ),", |
| 287 | " },", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 288 | ")"); |
| 289 | scratch.file( |
| 290 | "test/BUILD", |
| 291 | "load('//test:rules.bzl', 'my_rule')", |
| 292 | "my_rule(", |
| 293 | " name = 'test',", |
| 294 | " my_configurable_attr = select({", |
| 295 | " '//conditions:default': False,", |
| 296 | " ':true-config': True,", |
| 297 | " })", |
| 298 | ")", |
| 299 | "config_setting(", |
| 300 | " name = 'true-config',", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 301 | " values = {'foo': 'true'},", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 302 | ")"); |
| 303 | |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 304 | reporter.removeHandler(failFastHandler); |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 305 | getConfiguredTarget("//test"); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 306 | assertContainsEvent( |
| 307 | "No attribute 'my_configurable_attr'. " |
Googler | ceddfb1 | 2023-08-18 15:25:03 -0700 | [diff] [blame] | 308 | + "Either this attribute does not exist for this rule or the attribute " |
| 309 | + "was not resolved because it is set by a select that reads flags the transition " |
| 310 | + "may set."); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | @Test |
| 314 | public void testLabelTypedAttrReturnsLabelNotDep() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 315 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 316 | scratch.file( |
| 317 | "test/transitions.bzl", |
| 318 | "def _impl(settings, attr):", |
| 319 | " if attr.dict_attr[Label('//test:key')] == 'value':", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 320 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 321 | " else:", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 322 | " return {'//command_line_option:foo': 'uh-oh'}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 323 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 324 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 325 | scratch.file( |
| 326 | "test/rules.bzl", |
| 327 | "load('//test:transitions.bzl', 'my_transition')", |
| 328 | "def _impl(ctx):", |
| 329 | " return []", |
| 330 | "my_rule = rule(", |
| 331 | " implementation = _impl,", |
| 332 | " cfg = my_transition,", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 333 | " attrs = {", |
| 334 | " 'dict_attr': attr.label_keyed_string_dict(),", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 335 | " '_allowlist_function_transition': attr.label(", |
| 336 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 337 | " ),", |
| 338 | " },", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 339 | ")", |
| 340 | "simple_rule = rule(_impl)"); |
| 341 | scratch.file( |
| 342 | "test/BUILD", |
| 343 | "load('//test:rules.bzl', 'my_rule', 'simple_rule')", |
| 344 | "my_rule(", |
| 345 | " name = 'test',", |
| 346 | " dict_attr = {':key': 'value'},", |
| 347 | ")", |
| 348 | "simple_rule(name = 'key')"); |
| 349 | |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 350 | useConfiguration("--foo=pre-transition"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 351 | |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 352 | BuildConfigurationValue configuration = getConfiguration(getConfiguredTarget("//test")); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 353 | assertThat(configuration.getOptions().get(DummyTestOptions.class).foo) |
| 354 | .isEqualTo("post-transition"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 355 | } |
| 356 | |
juliexxia | db56678 | 2019-04-12 10:48:07 -0700 | [diff] [blame] | 357 | private static final String CUTE_ANIMAL_DEFAULT = |
| 358 | "cows produce more milk when they listen to soothing music"; |
| 359 | |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 360 | private void writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 361 | writeAllowlistFile(); |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 362 | scratch.file( |
| 363 | "test/rules.bzl", |
| 364 | "load('//test:transitions.bzl', 'my_transition')", |
| 365 | "def _rule_impl(ctx):", |
| 366 | " return []", |
| 367 | "my_rule = rule(", |
| 368 | " implementation = _rule_impl,", |
| 369 | " cfg = my_transition,", |
| 370 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 371 | " '_allowlist_function_transition': attr.label(", |
| 372 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 373 | " ),", |
| 374 | " },", |
| 375 | ")"); |
| 376 | |
| 377 | scratch.file( |
| 378 | "test/build_settings.bzl", |
| 379 | "def _impl(ctx):", |
| 380 | " return []", |
| 381 | "string_flag = rule(implementation = _impl, build_setting = config.string(flag=True))"); |
| 382 | |
| 383 | scratch.file( |
| 384 | "test/BUILD", |
| 385 | "load('//test:rules.bzl', 'my_rule')", |
| 386 | "load('//test:build_settings.bzl', 'string_flag')", |
| 387 | "my_rule(name = 'test')", |
| 388 | "string_flag(", |
| 389 | " name = 'cute-animal-fact',", |
juliexxia | db56678 | 2019-04-12 10:48:07 -0700 | [diff] [blame] | 390 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 391 | ")"); |
| 392 | } |
| 393 | |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 394 | @Test |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 395 | public void testTransitionOnBuildSetting_fromDefault() throws Exception { |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 396 | scratch.file( |
| 397 | "test/transitions.bzl", |
| 398 | "def _transition_impl(settings, attr):", |
| 399 | " return {'//test:cute-animal-fact': 'puffins mate for life'}", |
| 400 | "my_transition = transition(", |
| 401 | " implementation = _transition_impl,", |
| 402 | " inputs = [],", |
| 403 | " outputs = ['//test:cute-animal-fact']", |
| 404 | ")"); |
| 405 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 406 | |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 407 | BuildConfigurationValue configuration = getConfiguration(getConfiguredTarget("//test")); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 408 | assertThat( |
| 409 | configuration |
| 410 | .getOptions() |
| 411 | .getStarlarkOptions() |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 412 | .get(Label.parseCanonicalUnchecked("//test:cute-animal-fact"))) |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 413 | .isEqualTo("puffins mate for life"); |
| 414 | } |
| 415 | |
| 416 | @Test |
| 417 | public void testTransitionOnBuildSetting_fromCommandLine() throws Exception { |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 418 | scratch.file( |
| 419 | "test/transitions.bzl", |
| 420 | "def _transition_impl(settings, attr):", |
| 421 | " return {'//test:cute-animal-fact': 'puffins mate for life'}", |
| 422 | "my_transition = transition(", |
| 423 | " implementation = _transition_impl,", |
| 424 | " inputs = [],", |
| 425 | " outputs = ['//test:cute-animal-fact']", |
| 426 | ")"); |
| 427 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 428 | |
| 429 | useConfiguration(ImmutableMap.of("//test:cute-animal-fact", "cats can't taste sugar")); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 430 | |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 431 | BuildConfigurationValue configuration = getConfiguration(getConfiguredTarget("//test")); |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 432 | assertThat( |
| 433 | configuration |
| 434 | .getOptions() |
| 435 | .getStarlarkOptions() |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 436 | .get(Label.parseCanonicalUnchecked("//test:cute-animal-fact"))) |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 437 | .isEqualTo("puffins mate for life"); |
| 438 | } |
| 439 | |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 440 | @Test |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 441 | public void testTransitionOnBuildSetting_badValue() throws Exception { |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 442 | scratch.file( |
| 443 | "test/transitions.bzl", |
| 444 | "def _transition_impl(settings, attr):", |
| 445 | " return {'//test:cute-animal-fact': 24}", |
| 446 | "my_transition = transition(", |
| 447 | " implementation = _transition_impl,", |
| 448 | " inputs = [],", |
| 449 | " outputs = ['//test:cute-animal-fact']", |
| 450 | ")"); |
| 451 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 452 | |
| 453 | useConfiguration(ImmutableMap.of("//test:cute-animal-fact", "cats can't taste sugar")); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 454 | |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 455 | reporter.removeHandler(failFastHandler); |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 456 | getConfiguredTarget("//test"); |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 457 | assertContainsEvent( |
| 458 | "expected value of type 'string' for " + "//test:cute-animal-fact, but got 24 (int)"); |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 459 | } |
| 460 | |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 461 | @Test |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 462 | public void testTransitionOnBuildSetting_noSuchTarget() throws Exception { |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 463 | scratch.file( |
| 464 | "test/transitions.bzl", |
| 465 | "def _transition_impl(settings, attr):", |
| 466 | " return {'//test:i-am-not-real': 'imaginary-friend'}", |
| 467 | "my_transition = transition(", |
| 468 | " implementation = _transition_impl,", |
| 469 | " inputs = [],", |
| 470 | " outputs = ['//test:i-am-not-real']", |
| 471 | ")"); |
| 472 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 473 | |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 474 | reporter.removeHandler(failFastHandler); |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 475 | getConfiguredTarget("//test"); |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 476 | assertContainsEvent( |
| 477 | "no such target '//test:i-am-not-real': target " |
| 478 | + "'i-am-not-real' not declared in package 'test'"); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | @Test |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 482 | public void testTransitionOnBuildSetting_noSuchPackage() throws Exception { |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 483 | scratch.file( |
| 484 | "test/transitions.bzl", |
| 485 | "def _transition_impl(settings, attr):", |
| 486 | " return {'//i-am-not-real': 'imaginary-friend'}", |
| 487 | "my_transition = transition(", |
| 488 | " implementation = _transition_impl,", |
| 489 | " inputs = [],", |
| 490 | " outputs = ['//i-am-not-real']", |
| 491 | ")"); |
| 492 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 493 | |
| 494 | reporter.removeHandler(failFastHandler); |
| 495 | getConfiguredTarget("//test"); |
Googler | 2f7d965 | 2022-07-22 18:05:47 -0700 | [diff] [blame] | 496 | assertContainsEvent( |
| 497 | "no such package 'i-am-not-real': BUILD file not found in any of the following" |
| 498 | + " directories"); |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | @Test |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 502 | public void testTransitionOnBuildSetting_notABuildSetting() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 503 | writeAllowlistFile(); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 504 | scratch.file( |
| 505 | "test/transitions.bzl", |
| 506 | "def _transition_impl(settings, attr):", |
| 507 | " return {'//test:cute-animal-fact': 'puffins mate for life'}", |
| 508 | "my_transition = transition(", |
| 509 | " implementation = _transition_impl,", |
| 510 | " inputs = [],", |
| 511 | " outputs = ['//test:cute-animal-fact']", |
| 512 | ")"); |
| 513 | scratch.file( |
| 514 | "test/rules.bzl", |
| 515 | "load('//test:transitions.bzl', 'my_transition')", |
| 516 | "def _rule_impl(ctx):", |
| 517 | " return []", |
| 518 | "my_rule = rule(", |
| 519 | " implementation = _rule_impl,", |
| 520 | " cfg = my_transition,", |
| 521 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 522 | " '_allowlist_function_transition': attr.label(", |
| 523 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 524 | " ),", |
| 525 | " },", |
| 526 | ")"); |
| 527 | scratch.file( |
| 528 | "test/build_settings.bzl", |
| 529 | "def _impl(ctx):", |
| 530 | " return []", |
| 531 | "non_build_setting = rule(implementation = _impl)"); |
| 532 | scratch.file( |
| 533 | "test/BUILD", |
| 534 | "load('//test:rules.bzl', 'my_rule')", |
| 535 | "load('//test:build_settings.bzl', 'non_build_setting')", |
| 536 | "my_rule(name = 'test')", |
| 537 | "non_build_setting(name = 'cute-animal-fact')"); |
| 538 | |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 539 | reporter.removeHandler(failFastHandler); |
| 540 | getConfiguredTarget("//test"); |
| 541 | assertContainsEvent( |
| 542 | "attempting to transition on '//test:cute-animal-fact' which is not a build setting"); |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 543 | } |
| 544 | |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 545 | @Test |
juliexxia | db56678 | 2019-04-12 10:48:07 -0700 | [diff] [blame] | 546 | public void testTransitionOnBuildSetting_dontStoreDefault() throws Exception { |
juliexxia | db56678 | 2019-04-12 10:48:07 -0700 | [diff] [blame] | 547 | scratch.file( |
| 548 | "test/transitions.bzl", |
| 549 | "def _transition_impl(settings, attr):", |
| 550 | " return {'//test:cute-animal-fact': '" + CUTE_ANIMAL_DEFAULT + "'}", |
| 551 | "my_transition = transition(", |
| 552 | " implementation = _transition_impl,", |
| 553 | " inputs = [],", |
| 554 | " outputs = ['//test:cute-animal-fact']", |
| 555 | ")"); |
| 556 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 557 | |
| 558 | useConfiguration(ImmutableMap.of("//test:cute-animal-fact", "cats can't taste sugar")); |
| 559 | |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 560 | BuildConfigurationValue configuration = getConfiguration(getConfiguredTarget("//test")); |
juliexxia | db56678 | 2019-04-12 10:48:07 -0700 | [diff] [blame] | 561 | assertThat(configuration.getOptions().getStarlarkOptions()) |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 562 | .doesNotContainKey(Label.parseCanonicalUnchecked("//test:cute-animal-fact")); |
juliexxia | db56678 | 2019-04-12 10:48:07 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | @Test |
Googler | 1d4cecf | 2022-07-27 00:04:12 -0700 | [diff] [blame] | 566 | public void testTransitionOnBuildSetting_readingUnreadableBuildSetting() throws Exception { |
| 567 | scratch.file( |
| 568 | "test/transitions.bzl", |
| 569 | "def _transition_impl(settings, attr):", |
| 570 | " old_value = settings['//command_line_option:unreadable_by_starlark']", |
| 571 | " fail('This line should be unreachable.')", |
| 572 | "my_transition = transition(implementation = _transition_impl,", |
| 573 | " inputs = ['//command_line_option:unreadable_by_starlark'],", |
| 574 | " outputs = ['//command_line_option:unreadable_by_starlark'],", |
| 575 | ")"); |
| 576 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 577 | |
| 578 | reporter.removeHandler(failFastHandler); |
| 579 | getConfiguredTarget("//test"); |
| 580 | assertContainsEvent( |
| 581 | Pattern.compile( |
| 582 | "test/transitions.bzl:1:5: before calling _transition_impl: Input build setting" |
| 583 | + " //command_line_option:unreadable_by_starlark is of type class" |
| 584 | + " \\S*UnreadableStringBox, which is unreadable in Starlark. Please submit a" |
| 585 | + " feature request.")); |
| 586 | } |
| 587 | |
| 588 | @Test |
| 589 | public void testTransitionOnBuildSetting_writingUnreadableBuildSetting() throws Exception { |
| 590 | scratch.file( |
| 591 | "test/transitions.bzl", |
| 592 | "def _transition_impl(settings, attr):", |
| 593 | " return {", |
| 594 | " '//command_line_option:unreadable_by_starlark': 'post-transition',", |
| 595 | " }", |
| 596 | "my_transition = transition(implementation = _transition_impl,", |
| 597 | " inputs = [],", |
| 598 | " outputs = ['//command_line_option:unreadable_by_starlark'],", |
| 599 | ")"); |
| 600 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 601 | |
| 602 | useConfiguration("--unreadable_by_starlark=pre-transition"); |
| 603 | |
| 604 | BuildConfigurationValue configuration = getConfiguration(getConfiguredTarget("//test")); |
| 605 | assertThat(configuration.getOptions().get(DummyTestOptions.class).unreadableByStarlark) |
| 606 | .isEqualTo(DummyTestOptions.UnreadableStringBox.create("post-transition")); |
| 607 | } |
| 608 | |
| 609 | @Test |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 610 | public void testTransitionReadsBuildSetting_fromDefault() throws Exception { |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 611 | scratch.file( |
| 612 | "test/transitions.bzl", |
| 613 | "def _transition_impl(settings, attr):", |
Googler | 3d389d4 | 2023-05-02 03:41:03 -0700 | [diff] [blame] | 614 | " new_value = settings['//test:cute-animal-fact'].replace('cows', 'platypuses')", |
| 615 | " return {'//test:cute-animal-fact': new_value}", |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 616 | "my_transition = transition(", |
| 617 | " implementation = _transition_impl,", |
| 618 | " inputs = ['//test:cute-animal-fact'],", |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 619 | " outputs = ['//test:cute-animal-fact']", |
| 620 | ")"); |
| 621 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 622 | |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 623 | BuildConfigurationValue configuration = getConfiguration(getConfiguredTarget("//test")); |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 624 | assertThat( |
| 625 | configuration |
| 626 | .getOptions() |
| 627 | .getStarlarkOptions() |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 628 | .get(Label.parseCanonicalUnchecked("//test:cute-animal-fact"))) |
Googler | 3d389d4 | 2023-05-02 03:41:03 -0700 | [diff] [blame] | 629 | .isEqualTo("platypuses produce more milk when they listen to soothing music"); |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | @Test |
| 633 | public void testTransitionReadsBuildSetting_fromCommandLine() throws Exception { |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 634 | scratch.file( |
| 635 | "test/transitions.bzl", |
| 636 | "def _transition_impl(settings, attr):", |
Googler | 3d389d4 | 2023-05-02 03:41:03 -0700 | [diff] [blame] | 637 | " now_true = settings['//test:cute-animal-fact'].replace('FALSE', 'TRUE')", |
| 638 | " return {'//test:cute-animal-fact': now_true}", |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 639 | "my_transition = transition(", |
| 640 | " implementation = _transition_impl,", |
| 641 | " inputs = ['//test:cute-animal-fact'],", |
| 642 | " outputs = ['//test:cute-animal-fact']", |
| 643 | ")"); |
| 644 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 645 | |
Googler | 3d389d4 | 2023-05-02 03:41:03 -0700 | [diff] [blame] | 646 | useConfiguration(ImmutableMap.of("//test:cute-animal-fact", "rats are ticklish <- FALSE")); |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 647 | |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 648 | BuildConfigurationValue configuration = getConfiguration(getConfiguredTarget("//test")); |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 649 | assertThat( |
| 650 | configuration |
| 651 | .getOptions() |
| 652 | .getStarlarkOptions() |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 653 | .get(Label.parseCanonicalUnchecked("//test:cute-animal-fact"))) |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 654 | .isEqualTo("rats are ticklish <- TRUE"); |
| 655 | } |
| 656 | |
| 657 | @Test |
| 658 | public void testTransitionReadsBuildSetting_notABuildSetting() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 659 | writeAllowlistFile(); |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 660 | scratch.file( |
| 661 | "test/transitions.bzl", |
| 662 | "def _transition_impl(settings, attr):", |
| 663 | " return {'//test:cute-animal-fact': 'puffins mate for life'}", |
| 664 | "my_transition = transition(", |
| 665 | " implementation = _transition_impl,", |
| 666 | " inputs = ['//test:cute-animal-fact'],", |
| 667 | " outputs = ['//test:cute-animal-fact']", |
| 668 | ")"); |
| 669 | scratch.file( |
| 670 | "test/rules.bzl", |
| 671 | "load('//test:transitions.bzl', 'my_transition')", |
| 672 | "def _rule_impl(ctx):", |
| 673 | " return []", |
| 674 | "my_rule = rule(", |
| 675 | " implementation = _rule_impl,", |
| 676 | " cfg = my_transition,", |
| 677 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 678 | " '_allowlist_function_transition': attr.label(", |
| 679 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 680 | " ),", |
| 681 | " },", |
| 682 | ")"); |
| 683 | scratch.file( |
| 684 | "test/build_settings.bzl", |
| 685 | "def _impl(ctx):", |
| 686 | " return []", |
| 687 | "non_build_setting = rule(implementation = _impl)"); |
| 688 | scratch.file( |
| 689 | "test/BUILD", |
| 690 | "load('//test:rules.bzl', 'my_rule')", |
| 691 | "load('//test:build_settings.bzl', 'non_build_setting')", |
| 692 | "my_rule(name = 'test')", |
| 693 | "non_build_setting(name = 'cute-animal-fact')"); |
| 694 | |
| 695 | reporter.removeHandler(failFastHandler); |
| 696 | getConfiguredTarget("//test"); |
| 697 | assertContainsEvent( |
| 698 | "attempting to transition on '//test:cute-animal-fact' which is not a build setting"); |
| 699 | } |
| 700 | |
| 701 | @Test |
| 702 | public void testTransitionReadsBuildSetting_noSuchTarget() throws Exception { |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 703 | scratch.file( |
| 704 | "test/transitions.bzl", |
| 705 | "def _transition_impl(settings, attr):", |
| 706 | " return {'//test:cute-animal-fact': settings['//test:cute-animal-fact']+' <- TRUE'}", |
| 707 | "my_transition = transition(", |
| 708 | " implementation = _transition_impl,", |
| 709 | " inputs = ['//test:i-am-not-real'],", |
| 710 | " outputs = ['//test:cute-animal-fact']", |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 711 | ")"); |
| 712 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 713 | |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 714 | reporter.removeHandler(failFastHandler); |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 715 | getConfiguredTarget("//test"); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 716 | assertContainsEvent( |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 717 | "no such target '//test:i-am-not-real': target " |
| 718 | + "'i-am-not-real' not declared in package 'test'"); |
juliexxia | 922312f | 2019-03-12 08:44:33 -0700 | [diff] [blame] | 719 | } |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 720 | |
| 721 | @Test |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 722 | public void testAliasedBuildSetting() throws Exception { |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 723 | scratch.file( |
| 724 | "test/transitions.bzl", |
| 725 | "def _transition_impl(settings, attr):", |
| 726 | " return {'//test:fact': 'puffins mate for life'}", |
| 727 | "my_transition = transition(", |
| 728 | " implementation = _transition_impl,", |
| 729 | " inputs = [],", |
| 730 | " outputs = ['//test:fact']", |
| 731 | ")"); |
| 732 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 733 | scratch.overwriteFile( |
| 734 | "test/BUILD", |
| 735 | "load('//test:rules.bzl', 'my_rule')", |
| 736 | "load('//test:build_settings.bzl', 'string_flag')", |
| 737 | "my_rule(name = 'test')", |
| 738 | "alias(name = 'fact', actual = ':cute-animal-fact')", |
| 739 | "string_flag(", |
| 740 | " name = 'cute-animal-fact',", |
| 741 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
| 742 | ")"); |
| 743 | |
| 744 | useConfiguration(ImmutableMap.of("//test:cute-animal-fact", "rats are ticklish")); |
| 745 | |
| 746 | ImmutableMap<Label, Object> starlarkOptions = |
| 747 | getConfiguration(getConfiguredTarget("//test")).getOptions().getStarlarkOptions(); |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 748 | assertThat(starlarkOptions.get(Label.parseCanonicalUnchecked("//test:cute-animal-fact"))) |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 749 | .isEqualTo("puffins mate for life"); |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 750 | assertThat(starlarkOptions).doesNotContainKey(Label.parseCanonicalUnchecked("//test:fact")); |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 751 | assertThat(starlarkOptions.keySet()) |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 752 | .containsExactly(Label.parseCanonicalUnchecked("//test:cute-animal-fact")); |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | @Test |
| 756 | public void testAliasedBuildSetting_chainedAliases() throws Exception { |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 757 | scratch.file( |
| 758 | "test/transitions.bzl", |
| 759 | "def _transition_impl(settings, attr):", |
| 760 | " return {'//test:fact': 'puffins mate for life'}", |
| 761 | "my_transition = transition(", |
| 762 | " implementation = _transition_impl,", |
| 763 | " inputs = [],", |
| 764 | " outputs = ['//test:fact']", |
| 765 | ")"); |
| 766 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 767 | scratch.overwriteFile( |
| 768 | "test/BUILD", |
| 769 | "load('//test:rules.bzl', 'my_rule')", |
| 770 | "load('//test:build_settings.bzl', 'string_flag')", |
| 771 | "my_rule(name = 'test')", |
| 772 | "alias(name = 'fact', actual = ':alias2')", |
| 773 | "alias(name = 'alias2', actual = ':cute-animal-fact')", |
| 774 | "string_flag(", |
| 775 | " name = 'cute-animal-fact',", |
| 776 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
| 777 | ")"); |
| 778 | |
| 779 | useConfiguration(ImmutableMap.of("//test:cute-animal-fact", "rats are ticklish")); |
| 780 | |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 781 | BuildConfigurationValue configuration = getConfiguration(getConfiguredTarget("//test")); |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 782 | assertThat( |
| 783 | configuration |
| 784 | .getOptions() |
| 785 | .getStarlarkOptions() |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 786 | .get(Label.parseCanonicalUnchecked("//test:cute-animal-fact"))) |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 787 | .isEqualTo("puffins mate for life"); |
| 788 | } |
| 789 | |
| 790 | @Test |
| 791 | public void testAliasedBuildSetting_configuredActualValue() throws Exception { |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 792 | scratch.file( |
| 793 | "test/transitions.bzl", |
| 794 | "def _transition_impl(settings, attr):", |
| 795 | " return {'//test:fact': 'puffins mate for life'}", |
| 796 | "my_transition = transition(", |
| 797 | " implementation = _transition_impl,", |
| 798 | " inputs = [],", |
| 799 | " outputs = ['//test:fact']", |
| 800 | ")"); |
| 801 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 802 | scratch.overwriteFile( |
| 803 | "test/BUILD", |
| 804 | "load('//test:rules.bzl', 'my_rule')", |
| 805 | "load('//test:build_settings.bzl', 'string_flag')", |
| 806 | "my_rule(name = 'test')", |
| 807 | "alias(", |
| 808 | " name = 'fact',", |
| 809 | " actual = select({", |
| 810 | " '//conditions:default': ':cute-animal-fact',", |
| 811 | " ':true-config': 'other-cute-animal-fact',", |
| 812 | " })", |
| 813 | ")", |
| 814 | "config_setting(", |
| 815 | " name = 'true-config',", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 816 | " values = {'foo': 'true'},", |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 817 | ")", |
| 818 | "string_flag(", |
| 819 | " name = 'cute-animal-fact',", |
| 820 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
| 821 | ")", |
| 822 | "string_flag(", |
| 823 | " name = 'other-cute-animal-fact',", |
| 824 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
| 825 | ")"); |
| 826 | |
| 827 | reporter.removeHandler(failFastHandler); |
| 828 | getConfiguredTarget("//test"); |
| 829 | assertContainsEvent( |
| 830 | "attempting to transition on aliased build setting '//test:fact', the actual value of" |
| 831 | + " which uses select()."); |
| 832 | } |
| 833 | |
| 834 | @Test |
| 835 | public void testAliasedBuildSetting_cyclicalAliases() throws Exception { |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 836 | scratch.file( |
| 837 | "test/transitions.bzl", |
| 838 | "def _transition_impl(settings, attr):", |
| 839 | " return {'//test:alias1': 'puffins mate for life'}", |
| 840 | "my_transition = transition(", |
| 841 | " implementation = _transition_impl,", |
| 842 | " inputs = [],", |
| 843 | " outputs = ['//test:alias1']", |
| 844 | ")"); |
| 845 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 846 | scratch.overwriteFile( |
| 847 | "test/BUILD", |
| 848 | "load('//test:rules.bzl', 'my_rule')", |
| 849 | "load('//test:build_settings.bzl', 'string_flag')", |
| 850 | "my_rule(name = 'test')", |
| 851 | "alias(name = 'alias1', actual = ':alias2')", |
| 852 | "alias(name = 'alias2', actual = ':alias1')"); |
| 853 | |
| 854 | reporter.removeHandler(failFastHandler); |
| 855 | getConfiguredTarget("//test"); |
Googler | 2f7d965 | 2022-07-22 18:05:47 -0700 | [diff] [blame] | 856 | assertContainsEvent( |
| 857 | "Dependency cycle involving '//test:alias1' detected in aliased build settings"); |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | @Test |
| 861 | public void testAliasedBuildSetting_setAliasAndActual() throws Exception { |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 862 | scratch.file( |
| 863 | "test/transitions.bzl", |
| 864 | "def _transition_impl(settings, attr):", |
| 865 | " return {", |
| 866 | " '//test:alias': 'puffins mate for life',", |
| 867 | " '//test:actual': 'cats cannot taste sugar',", |
| 868 | "}", |
| 869 | "my_transition = transition(", |
| 870 | " implementation = _transition_impl,", |
| 871 | " inputs = [],", |
| 872 | " outputs = [", |
| 873 | " '//test:alias',", |
| 874 | " '//test:actual',", |
| 875 | " ]", |
| 876 | ")"); |
| 877 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 878 | scratch.overwriteFile( |
| 879 | "test/BUILD", |
| 880 | "load('//test:rules.bzl', 'my_rule')", |
| 881 | "load('//test:build_settings.bzl', 'string_flag')", |
| 882 | "my_rule(name = 'test')", |
| 883 | "alias(name = 'alias', actual = ':actual')", |
| 884 | "string_flag(", |
| 885 | " name = 'actual',", |
| 886 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
| 887 | ")"); |
| 888 | |
| 889 | reporter.removeHandler(failFastHandler); |
| 890 | getConfiguredTarget("//test"); |
Googler | 2f7d965 | 2022-07-22 18:05:47 -0700 | [diff] [blame] | 891 | assertContainsEvent( |
| 892 | "Dependency cycle involving '//test:actual' detected in aliased build settings"); |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | @Test |
| 896 | public void testAliasedBuildSetting_outputReturnMismatch() throws Exception { |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 897 | scratch.file( |
| 898 | "test/transitions.bzl", |
| 899 | "def _transition_impl(settings, attr):", |
| 900 | " return {", |
| 901 | " '//test:actual': 'cats cannot taste sugar',", |
| 902 | "}", |
| 903 | "my_transition = transition(", |
| 904 | " implementation = _transition_impl,", |
| 905 | " inputs = [],", |
| 906 | " outputs = [", |
| 907 | " '//test:alias',", |
| 908 | " ]", |
| 909 | ")"); |
| 910 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 911 | scratch.overwriteFile( |
| 912 | "test/BUILD", |
| 913 | "load('//test:rules.bzl', 'my_rule')", |
| 914 | "load('//test:build_settings.bzl', 'string_flag')", |
| 915 | "my_rule(name = 'test')", |
| 916 | "alias(name = 'alias', actual = ':actual')", |
| 917 | "string_flag(", |
| 918 | " name = 'actual',", |
| 919 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
| 920 | ")"); |
| 921 | |
| 922 | reporter.removeHandler(failFastHandler); |
| 923 | getConfiguredTarget("//test"); |
| 924 | assertContainsEvent("transition function returned undeclared output '//test:actual'"); |
| 925 | } |
| 926 | |
| 927 | @Test |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 928 | public void testOneParamTransitionFunctionApiFails() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 929 | writeAllowlistFile(); |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 930 | scratch.file( |
| 931 | "test/transitions.bzl", |
| 932 | "def _impl(settings):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 933 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 934 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 935 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 936 | scratch.file( |
| 937 | "test/rules.bzl", |
| 938 | "load('//test:transitions.bzl', 'my_transition')", |
| 939 | "def _impl(ctx):", |
| 940 | " return []", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 941 | "my_rule = rule(", |
| 942 | " implementation = _impl,", |
| 943 | " cfg = my_transition,", |
| 944 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 945 | " '_allowlist_function_transition': attr.label(", |
| 946 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 947 | " ),", |
| 948 | " },", |
| 949 | ")"); |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 950 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 951 | |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 952 | reporter.removeHandler(failFastHandler); |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 953 | getConfiguredTarget("//test"); |
adonovan | 40446b8 | 2020-04-22 06:59:42 -0700 | [diff] [blame] | 954 | assertContainsEvent("_impl() accepts no more than 1 positional argument but got 2"); |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 955 | } |
juliexxia | ff93b19 | 2019-02-19 08:02:19 -0800 | [diff] [blame] | 956 | |
| 957 | @Test |
| 958 | public void testCannotTransitionOnExperimentalFlag() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 959 | writeAllowlistFile(); |
juliexxia | ff93b19 | 2019-02-19 08:02:19 -0800 | [diff] [blame] | 960 | scratch.file( |
| 961 | "test/transitions.bzl", |
| 962 | "def _impl(settings, attr):", |
juliexxia | d17649a | 2020-05-26 10:48:04 -0700 | [diff] [blame] | 963 | " return {'//command_line_option:experimental_something_something': True}", |
juliexxia | ff93b19 | 2019-02-19 08:02:19 -0800 | [diff] [blame] | 964 | "my_transition = transition(implementation = _impl, inputs = [],", |
juliexxia | d17649a | 2020-05-26 10:48:04 -0700 | [diff] [blame] | 965 | " outputs = ['//command_line_option:experimental_something_something'])"); |
juliexxia | ff93b19 | 2019-02-19 08:02:19 -0800 | [diff] [blame] | 966 | scratch.file( |
| 967 | "test/rules.bzl", |
| 968 | "load('//test:transitions.bzl', 'my_transition')", |
| 969 | "def _impl(ctx):", |
| 970 | " return []", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 971 | "my_rule = rule(", |
| 972 | " implementation = _impl,", |
| 973 | " cfg = my_transition,", |
| 974 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 975 | " '_allowlist_function_transition': attr.label(", |
| 976 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 977 | " ),", |
| 978 | " },", |
| 979 | ")"); |
juliexxia | ff93b19 | 2019-02-19 08:02:19 -0800 | [diff] [blame] | 980 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 981 | |
| 982 | reporter.removeHandler(failFastHandler); |
| 983 | getConfiguredTarget("//test"); |
| 984 | assertContainsEvent("Cannot transition on --experimental_* or --incompatible_* options"); |
| 985 | } |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 986 | |
| 987 | @Test |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 988 | public void testCannotTransitionWithoutAllowlist() throws Exception { |
cmita | 8356358 | 2021-06-15 01:57:45 -0700 | [diff] [blame] | 989 | scratch.overwriteFile( |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 990 | "tools/allowlists/function_transition_allowlist/BUILD", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 991 | "package_group(", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 992 | " name = 'function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 993 | " packages = [],", |
| 994 | ")"); |
| 995 | scratch.file( |
| 996 | "test/transitions.bzl", |
| 997 | "def _impl(settings, attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 998 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 999 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1000 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 1001 | scratch.file( |
| 1002 | "test/rules.bzl", |
| 1003 | "load('//test:transitions.bzl', 'my_transition')", |
| 1004 | "def _impl(ctx):", |
| 1005 | " return []", |
| 1006 | "my_rule = rule(", |
| 1007 | " implementation = _impl,", |
| 1008 | " cfg = my_transition,", |
| 1009 | ")"); |
| 1010 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1011 | |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1012 | useConfiguration("--foo=pre-transition"); |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 1013 | |
| 1014 | reporter.removeHandler(failFastHandler); |
| 1015 | getConfiguredTarget("//test"); |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1016 | assertContainsEvent("Use of Starlark transition without allowlist"); |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 1017 | } |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 1018 | |
| 1019 | @Test |
| 1020 | public void testNoNullOptionValues() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1021 | writeAllowlistFile(); |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 1022 | scratch.file( |
| 1023 | "test/transitions.bzl", |
| 1024 | "def _impl(settings, attr):", |
juliexxia | 6778a05 | 2019-04-09 08:25:05 -0700 | [diff] [blame] | 1025 | " if settings['//command_line_option:nullable_option'] == None:", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1026 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 1027 | " else:", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1028 | " return {'//command_line_option:foo': settings['//command_line_option:foo']}", |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 1029 | "my_transition = transition(implementation = _impl,", |
| 1030 | " inputs = [", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1031 | " '//command_line_option:foo',", |
juliexxia | 6778a05 | 2019-04-09 08:25:05 -0700 | [diff] [blame] | 1032 | " '//command_line_option:nullable_option'", |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 1033 | " ],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1034 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 1035 | scratch.file( |
| 1036 | "test/rules.bzl", |
| 1037 | "load('//test:transitions.bzl', 'my_transition')", |
| 1038 | "def _impl(ctx):", |
| 1039 | " return []", |
| 1040 | "my_rule = rule(", |
| 1041 | " implementation = _impl,", |
| 1042 | " cfg = my_transition,", |
| 1043 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1044 | " '_allowlist_function_transition': attr.label(", |
| 1045 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 1046 | " ),", |
| 1047 | " })"); |
| 1048 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1049 | |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1050 | useConfiguration("--nullable_option=", "--foo=pre-transition"); |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 1051 | |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 1052 | BuildConfigurationValue configuration = getConfiguration(getConfiguredTarget("//test")); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1053 | assertThat(configuration.getOptions().get(DummyTestOptions.class).foo) |
| 1054 | .isEqualTo("post-transition"); |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 1055 | } |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1056 | |
| 1057 | @Test |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1058 | public void testAllowlistOnRuleNotTargets() throws Exception { |
| 1059 | // allowlists //test/... |
| 1060 | writeAllowlistFile(); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1061 | scratch.file( |
| 1062 | "test/transitions.bzl", |
| 1063 | "def _impl(settings, attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1064 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1065 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1066 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1067 | scratch.file( |
| 1068 | "test/rules.bzl", |
| 1069 | "load('//test:transitions.bzl', 'my_transition')", |
| 1070 | "def _impl(ctx):", |
| 1071 | " return []", |
| 1072 | "my_rule = rule(", |
| 1073 | " implementation = _impl,", |
| 1074 | " cfg = my_transition,", |
| 1075 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1076 | " '_allowlist_function_transition': attr.label(", |
| 1077 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1078 | " ),", |
| 1079 | " })"); |
| 1080 | scratch.file( |
| 1081 | "neverland/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1082 | scratch.file("test/BUILD"); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1083 | useConfiguration("--foo=pre-transition"); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1084 | |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 1085 | BuildConfigurationValue configuration = |
| 1086 | getConfiguration(getConfiguredTarget("//neverland:test")); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1087 | assertThat(configuration.getOptions().get(DummyTestOptions.class).foo) |
| 1088 | .isEqualTo("post-transition"); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Googler | fe8275d | 2021-02-05 16:36:38 -0800 | [diff] [blame] | 1091 | // TODO(blaze-configurability): We probably want to eventually turn this off. Flip this test when |
| 1092 | // this isn't allowed anymore. |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1093 | @Test |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1094 | public void testAllowlistOnTargetsStillWorks() throws Exception { |
| 1095 | // allowlists //test/... |
| 1096 | writeAllowlistFile(); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1097 | scratch.file( |
| 1098 | "neverland/transitions.bzl", |
| 1099 | "def _impl(settings, attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1100 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1101 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1102 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1103 | scratch.file( |
| 1104 | "neverland/rules.bzl", |
| 1105 | "load('//neverland:transitions.bzl', 'my_transition')", |
| 1106 | "def _impl(ctx):", |
| 1107 | " return []", |
| 1108 | "my_rule = rule(", |
| 1109 | " implementation = _impl,", |
| 1110 | " cfg = my_transition,", |
| 1111 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1112 | " '_allowlist_function_transition': attr.label(", |
| 1113 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1114 | " ),", |
| 1115 | " })"); |
| 1116 | scratch.file( |
| 1117 | "test/BUILD", "load('//neverland:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1118 | scratch.file("neverland/BUILD"); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1119 | useConfiguration("--foo=pre-transition"); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1120 | |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 1121 | BuildConfigurationValue configuration = getConfiguration(getConfiguredTarget("//test")); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1122 | assertThat(configuration.getOptions().get(DummyTestOptions.class).foo) |
| 1123 | .isEqualTo("post-transition"); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1124 | } |
juliexxia | bdc6c10 | 2019-05-20 11:58:02 -0700 | [diff] [blame] | 1125 | |
juliexxia | bdc6c10 | 2019-05-20 11:58:02 -0700 | [diff] [blame] | 1126 | @Test |
gregce | 11f2540 | 2021-07-08 07:14:10 -0700 | [diff] [blame] | 1127 | @TestParameters({ |
| 1128 | "{" |
| 1129 | + "returnLine: 'return []'," |
| 1130 | + "returnLine: 'return {}'," |
| 1131 | + "returnLine: 'return None'," |
| 1132 | + "returnLine: 'pass'," |
| 1133 | + "}" |
| 1134 | }) |
| 1135 | public void noopReturnValues(String returnLine) throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1136 | writeAllowlistFile(); |
juliexxia | bdc6c10 | 2019-05-20 11:58:02 -0700 | [diff] [blame] | 1137 | scratch.file( |
| 1138 | "test/transitions.bzl", |
| 1139 | "def _impl(settings, attr):", |
gregce | 11f2540 | 2021-07-08 07:14:10 -0700 | [diff] [blame] | 1140 | " " + returnLine, |
juliexxia | bdc6c10 | 2019-05-20 11:58:02 -0700 | [diff] [blame] | 1141 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1142 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | bdc6c10 | 2019-05-20 11:58:02 -0700 | [diff] [blame] | 1143 | scratch.file( |
| 1144 | "test/rules.bzl", |
| 1145 | "load('//test:transitions.bzl', 'my_transition')", |
| 1146 | "def _impl(ctx):", |
| 1147 | " return []", |
| 1148 | "my_rule = rule(", |
| 1149 | " implementation = _impl,", |
| 1150 | " cfg = my_transition,", |
| 1151 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1152 | " '_allowlist_function_transition': attr.label(", |
| 1153 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | bdc6c10 | 2019-05-20 11:58:02 -0700 | [diff] [blame] | 1154 | " ),", |
| 1155 | " })"); |
| 1156 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
gregce | 11f2540 | 2021-07-08 07:14:10 -0700 | [diff] [blame] | 1157 | // --trim_test_configuration means only the top-level configuration has TestOptions. |
| 1158 | assertConfigurationsEqual( |
| 1159 | getConfiguration(getConfiguredTarget("//test")), |
| 1160 | targetConfig, |
| 1161 | ImmutableSet.of(TestOptions.class)); |
juliexxia | bdc6c10 | 2019-05-20 11:58:02 -0700 | [diff] [blame] | 1162 | } |
gregce | f0a40ac | 2020-03-31 14:11:30 -0700 | [diff] [blame] | 1163 | |
| 1164 | @Test |
| 1165 | public void composingTransitionReportsAllStarlarkErrors() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1166 | writeAllowlistFile(); |
gregce | f0a40ac | 2020-03-31 14:11:30 -0700 | [diff] [blame] | 1167 | scratch.file( |
| 1168 | "test/build_settings.bzl", |
| 1169 | "def _impl(ctx):", |
| 1170 | " return []", |
| 1171 | "string_flag = rule(implementation = _impl, build_setting = config.string(flag=True))"); |
| 1172 | scratch.file( |
| 1173 | "test/transitions.bzl", |
gregce | 11f2540 | 2021-07-08 07:14:10 -0700 | [diff] [blame] | 1174 | "def _attr_impl(settings, attr):", |
| 1175 | " return {'//test:attr_transition_output_flag1': 'not default'}", |
| 1176 | "attr_transition = transition(implementation = _attr_impl, inputs = [],", |
| 1177 | " outputs = [", |
| 1178 | " '//test:attr_transition_output_flag1',", |
| 1179 | " '//test:attr_transition_output_flag2',", |
| 1180 | " ])", |
| 1181 | "def _self_impl(settings, attr):", |
| 1182 | " return {'//test:self_transition_output_flag1': 'not default'}", |
| 1183 | "self_transition = transition(implementation = _self_impl, inputs = [],", |
| 1184 | " outputs = [", |
| 1185 | " '//test:self_transition_output_flag1',", |
| 1186 | " '//test:self_transition_output_flag2',", |
| 1187 | " ])"); |
gregce | f0a40ac | 2020-03-31 14:11:30 -0700 | [diff] [blame] | 1188 | scratch.file( |
| 1189 | "test/rules.bzl", |
| 1190 | "load('//test:transitions.bzl', 'attr_transition', 'self_transition')", |
| 1191 | "def _impl(ctx):", |
| 1192 | " return []", |
| 1193 | "rule_with_attr_transition = rule(", |
| 1194 | " implementation = _impl,", |
| 1195 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1196 | " '_allowlist_function_transition': attr.label(", |
| 1197 | " default = '//tools/allowlists/function_transition_allowlist'),", |
gregce | f0a40ac | 2020-03-31 14:11:30 -0700 | [diff] [blame] | 1198 | " 'deps': attr.label_list(cfg = attr_transition),", |
| 1199 | " })", |
| 1200 | "rule_with_self_transition = rule(", |
| 1201 | " implementation = _impl,", |
| 1202 | " cfg = self_transition,", |
| 1203 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1204 | " '_allowlist_function_transition': attr.label(", |
| 1205 | " default = '//tools/allowlists/function_transition_allowlist'),", |
gregce | f0a40ac | 2020-03-31 14:11:30 -0700 | [diff] [blame] | 1206 | " })"); |
| 1207 | scratch.file( |
| 1208 | "test/BUILD", |
| 1209 | "load('//test:rules.bzl', 'rule_with_attr_transition', 'rule_with_self_transition')", |
| 1210 | "load('//test:build_settings.bzl', 'string_flag')", |
gregce | 11f2540 | 2021-07-08 07:14:10 -0700 | [diff] [blame] | 1211 | "string_flag(name = 'attr_transition_output_flag1', build_setting_default='')", |
| 1212 | "string_flag(name = 'attr_transition_output_flag2', build_setting_default='')", |
| 1213 | "string_flag(name = 'self_transition_output_flag1', build_setting_default='')", |
| 1214 | "string_flag(name = 'self_transition_output_flag2', build_setting_default='')", |
gregce | f0a40ac | 2020-03-31 14:11:30 -0700 | [diff] [blame] | 1215 | "rule_with_attr_transition(name = 'buildme', deps = [':adep'])", |
| 1216 | "rule_with_self_transition(name = 'adep')"); |
| 1217 | |
| 1218 | reporter.removeHandler(failFastHandler); |
| 1219 | getConfiguredTarget("//test:buildme"); |
| 1220 | assertContainsEvent( |
gregce | 11f2540 | 2021-07-08 07:14:10 -0700 | [diff] [blame] | 1221 | "transition outputs [//test:attr_transition_output_flag2] were not defined by transition " |
gregce | f0a40ac | 2020-03-31 14:11:30 -0700 | [diff] [blame] | 1222 | + "function"); |
Googler | fa28cf2 | 2023-07-11 09:50:38 -0700 | [diff] [blame] | 1223 | // While _self_impl is in error as it does not define //test:self_transition_output_flag2, |
| 1224 | // evaluation stops at the faulty _attr_impl definition so it does not cause an error. |
gregce | f0a40ac | 2020-03-31 14:11:30 -0700 | [diff] [blame] | 1225 | } |
juliexxia | bf57d6b | 2020-04-14 07:36:59 -0700 | [diff] [blame] | 1226 | |
| 1227 | @Test |
| 1228 | public void testTransitionOnDefine() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1229 | writeAllowlistFile(); |
juliexxia | bf57d6b | 2020-04-14 07:36:59 -0700 | [diff] [blame] | 1230 | scratch.file( |
| 1231 | "test/transitions.bzl", |
| 1232 | "def _impl(settings, attr):", |
| 1233 | " return {'//command_line_option:define': 'chonky=true'}", |
| 1234 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1235 | " outputs = ['//command_line_option:define'])"); |
| 1236 | scratch.file( |
| 1237 | "test/rules.bzl", |
| 1238 | "load('//test:transitions.bzl', 'my_transition')", |
| 1239 | "def _impl(ctx):", |
| 1240 | " return []", |
| 1241 | "my_rule = rule(", |
| 1242 | " implementation = _impl,", |
| 1243 | " cfg = my_transition,", |
| 1244 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1245 | " '_allowlist_function_transition': attr.label(", |
| 1246 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | bf57d6b | 2020-04-14 07:36:59 -0700 | [diff] [blame] | 1247 | " ),", |
| 1248 | " })"); |
| 1249 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1250 | |
| 1251 | reporter.removeHandler(failFastHandler); |
| 1252 | getConfiguredTarget("//test"); |
| 1253 | assertContainsEvent("Starlark transition on --define not supported - try using build settings"); |
| 1254 | } |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1255 | |
| 1256 | @Test |
| 1257 | public void successfulTypeConversionOfNativeListOption() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1258 | writeAllowlistFile(); |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1259 | scratch.file( |
| 1260 | "test/transitions.bzl", |
| 1261 | "def _impl(settings, attr):", |
| 1262 | " return {'//command_line_option:platforms': ['//test:my_platform']}", |
| 1263 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1264 | " outputs = ['//command_line_option:platforms'])"); |
| 1265 | scratch.file( |
| 1266 | "test/rules.bzl", |
| 1267 | "load('//test:transitions.bzl', 'my_transition')", |
| 1268 | "def _impl(ctx):", |
| 1269 | " return []", |
| 1270 | "my_rule = rule(", |
| 1271 | " implementation = _impl,", |
| 1272 | " cfg = my_transition,", |
| 1273 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1274 | " '_allowlist_function_transition': attr.label(", |
| 1275 | " default = '//tools/allowlists/function_transition_allowlist',", |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1276 | " ),", |
| 1277 | " })"); |
| 1278 | scratch.file( |
| 1279 | "test/BUILD", |
| 1280 | "load('//test:rules.bzl', 'my_rule')", |
| 1281 | "platform(name = 'my_platform')", |
| 1282 | "my_rule(name = 'test')"); |
| 1283 | |
| 1284 | getConfiguredTarget("//test"); |
| 1285 | assertNoEvents(); |
| 1286 | } |
| 1287 | |
Googler | 068d845 | 2022-08-26 03:17:07 -0700 | [diff] [blame] | 1288 | @Test |
| 1289 | public void successfulTypeConversionOfNativeListOption_unambiguousLabels() throws Exception { |
Googler | d51144c | 2023-10-13 03:20:20 -0700 | [diff] [blame^] | 1290 | setBuildLanguageOptions("--incompatible_unambiguous_label_stringification"); |
Googler | 068d845 | 2022-08-26 03:17:07 -0700 | [diff] [blame] | 1291 | |
| 1292 | scratch.overwriteFile("MODULE.bazel", "bazel_dep(name='rules_x',version='1.0')"); |
| 1293 | registry.addModule(createModuleKey("rules_x", "1.0"), "module(name='rules_x', version='1.0')"); |
| 1294 | scratch.file("modules/rules_x~1.0/WORKSPACE"); |
| 1295 | scratch.file("modules/rules_x~1.0/BUILD"); |
| 1296 | scratch.file( |
| 1297 | "modules/rules_x~1.0/defs.bzl", |
| 1298 | "def _tr_impl(settings, attr):", |
| 1299 | " return {'//command_line_option:platforms': [Label('@@//test:my_platform')]}", |
| 1300 | "my_transition = transition(implementation = _tr_impl, inputs = [],", |
| 1301 | " outputs = ['//command_line_option:platforms'])", |
| 1302 | "def _impl(ctx):", |
| 1303 | " pass", |
| 1304 | "my_rule = rule(", |
| 1305 | " implementation = _impl,", |
| 1306 | " cfg = my_transition,", |
| 1307 | " attrs = {", |
| 1308 | " '_allowlist_function_transition': attr.label(", |
| 1309 | " default = '@@//tools/allowlists/function_transition_allowlist',", |
| 1310 | " ),", |
| 1311 | " })"); |
| 1312 | |
| 1313 | scratch.overwriteFile( |
| 1314 | "tools/allowlists/function_transition_allowlist/BUILD", |
| 1315 | "package_group(", |
| 1316 | " name = 'function_transition_allowlist',", |
| 1317 | " packages = [", |
| 1318 | " '//...',", |
| 1319 | " ],", |
| 1320 | ")"); |
| 1321 | |
| 1322 | scratch.file( |
| 1323 | "test/BUILD", |
| 1324 | "load('@rules_x//:defs.bzl', 'my_rule')", |
| 1325 | "platform(name = 'my_platform')", |
| 1326 | "my_rule(name = 'test')"); |
| 1327 | |
Googler | d51144c | 2023-10-13 03:20:20 -0700 | [diff] [blame^] | 1328 | invalidatePackages(); |
| 1329 | |
Googler | 068d845 | 2022-08-26 03:17:07 -0700 | [diff] [blame] | 1330 | getConfiguredTarget("//test"); |
| 1331 | assertNoEvents(); |
| 1332 | } |
| 1333 | |
juliexxia | 1cf3746 | 2020-10-20 14:48:10 -0700 | [diff] [blame] | 1334 | // Regression test for b/170729565 |
| 1335 | @Test |
| 1336 | public void testSetBooleanNativeOptionWithStarlarkBoolean() throws Exception { |
| 1337 | writeAllowlistFile(); |
| 1338 | scratch.file( |
| 1339 | "test/transitions.bzl", |
| 1340 | "def _impl(settings, attr):", |
| 1341 | " return {'//command_line_option:bool': True}", |
| 1342 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1343 | " outputs = ['//command_line_option:bool'])"); |
| 1344 | scratch.file( |
| 1345 | "test/rules.bzl", |
| 1346 | "load('//test:transitions.bzl', 'my_transition')", |
| 1347 | "def _impl(ctx):", |
| 1348 | " return []", |
| 1349 | "my_rule = rule(", |
| 1350 | " implementation = _impl,", |
| 1351 | " cfg = my_transition,", |
| 1352 | " attrs = {", |
| 1353 | " '_allowlist_function_transition': attr.label(", |
| 1354 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1355 | " ),", |
| 1356 | " })"); |
| 1357 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1358 | useConfiguration("--bool=false"); |
| 1359 | ConfiguredTarget ct = getConfiguredTarget("//test"); |
| 1360 | assertNoEvents(); |
| 1361 | assertThat(getConfiguration(ct).getOptions().get(DummyTestOptions.class).bool).isTrue(); |
| 1362 | } |
| 1363 | |
| 1364 | // Regression test for b/170729565 |
| 1365 | @Test |
| 1366 | public void testSetBooleanNativeOptionWithItself() throws Exception { |
| 1367 | writeAllowlistFile(); |
| 1368 | scratch.file( |
| 1369 | "test/transitions.bzl", |
| 1370 | "def _impl(settings, attr):", |
| 1371 | " return {'//command_line_option:bool': settings['//command_line_option:bool']}", |
| 1372 | "my_transition = transition(implementation = _impl,", |
| 1373 | " inputs = ['//command_line_option:bool'],", |
| 1374 | " outputs = ['//command_line_option:bool'])"); |
| 1375 | scratch.file( |
| 1376 | "test/rules.bzl", |
| 1377 | "load('//test:transitions.bzl', 'my_transition')", |
| 1378 | "def _impl(ctx):", |
| 1379 | " return []", |
| 1380 | "my_rule = rule(", |
| 1381 | " implementation = _impl,", |
| 1382 | " cfg = my_transition,", |
| 1383 | " attrs = {", |
| 1384 | " '_allowlist_function_transition': attr.label(", |
| 1385 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1386 | " ),", |
| 1387 | " })"); |
| 1388 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1389 | useConfiguration("--bool=false"); |
| 1390 | ConfiguredTarget ct = getConfiguredTarget("//test"); |
| 1391 | assertNoEvents(); |
| 1392 | assertThat(getConfiguration(ct).getOptions().get(DummyTestOptions.class).bool).isFalse(); |
| 1393 | } |
| 1394 | |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1395 | @Test |
| 1396 | public void failedTypeConversionOfNativeListOption() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1397 | writeAllowlistFile(); |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1398 | scratch.file( |
| 1399 | "test/transitions.bzl", |
| 1400 | "def _impl(settings, attr):", |
| 1401 | " return {'//command_line_option:platforms': ['this is not a valid label::']}", |
| 1402 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1403 | " outputs = ['//command_line_option:platforms'])"); |
| 1404 | scratch.file( |
| 1405 | "test/rules.bzl", |
| 1406 | "load('//test:transitions.bzl', 'my_transition')", |
| 1407 | "def _impl(ctx):", |
| 1408 | " return []", |
| 1409 | "my_rule = rule(", |
| 1410 | " implementation = _impl,", |
| 1411 | " cfg = my_transition,", |
| 1412 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1413 | " '_allowlist_function_transition': attr.label(", |
| 1414 | " default = '//tools/allowlists/function_transition_allowlist',", |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1415 | " ),", |
| 1416 | " })"); |
| 1417 | scratch.file( |
| 1418 | "test/BUILD", |
| 1419 | "load('//test:rules.bzl', 'my_rule')", |
| 1420 | "platform(name = 'my_platform')", |
| 1421 | "my_rule(name = 'test')"); |
| 1422 | |
| 1423 | reporter.removeHandler(failFastHandler); |
| 1424 | getConfiguredTarget("//test"); |
| 1425 | assertContainsEvent("invalid target name ':': target names may not contain ':'"); |
| 1426 | } |
| 1427 | |
| 1428 | @Test |
| 1429 | public void successfulTypeConversionOfNativeListOptionEmptyList() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1430 | writeAllowlistFile(); |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1431 | scratch.file( |
| 1432 | "test/transitions.bzl", |
| 1433 | "def _impl(settings, attr):", |
| 1434 | " return {'//command_line_option:fission': []}", |
| 1435 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1436 | " outputs = ['//command_line_option:fission'])"); |
| 1437 | scratch.file( |
| 1438 | "test/rules.bzl", |
| 1439 | "load('//test:transitions.bzl', 'my_transition')", |
| 1440 | "def _impl(ctx):", |
| 1441 | " return []", |
| 1442 | "my_rule = rule(", |
| 1443 | " implementation = _impl,", |
| 1444 | " cfg = my_transition,", |
| 1445 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1446 | " '_allowlist_function_transition': attr.label(", |
| 1447 | " default = '//tools/allowlists/function_transition_allowlist',", |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1448 | " ),", |
| 1449 | " })"); |
| 1450 | scratch.file( |
| 1451 | "test/BUILD", |
| 1452 | "load('//test:rules.bzl', 'my_rule')", |
| 1453 | "platform(name = 'my_platform')", |
| 1454 | "my_rule(name = 'test')"); |
| 1455 | |
| 1456 | ConfiguredTarget ct = getConfiguredTarget("//test"); |
| 1457 | assertNoEvents(); |
| 1458 | assertThat(getConfiguration(ct).getOptions().get(CppOptions.class).fissionModes).isEmpty(); |
| 1459 | } |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 1460 | |
| 1461 | @Test |
Tobi | ced45af | 2021-05-04 00:50:06 -0700 | [diff] [blame] | 1462 | public void failedTypeConversionOfNativeListOptionNone() throws Exception { |
| 1463 | writeAllowlistFile(); |
| 1464 | scratch.file( |
| 1465 | "test/transitions.bzl", |
| 1466 | "def _impl(settings, attr):", |
| 1467 | " return {'//command_line_option:copt': None}", |
| 1468 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1469 | " outputs = ['//command_line_option:copt'])"); |
| 1470 | scratch.file( |
| 1471 | "test/rules.bzl", |
| 1472 | "load('//test:transitions.bzl', 'my_transition')", |
| 1473 | "def _impl(ctx):", |
| 1474 | " return []", |
| 1475 | "my_rule = rule(", |
| 1476 | " implementation = _impl,", |
| 1477 | " cfg = my_transition,", |
| 1478 | " attrs = {", |
| 1479 | " '_allowlist_function_transition': attr.label(", |
| 1480 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1481 | " ),", |
| 1482 | " })"); |
| 1483 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1484 | |
| 1485 | reporter.removeHandler(failFastHandler); |
| 1486 | getConfiguredTarget("//test"); |
| 1487 | assertContainsEvent( |
| 1488 | "'None' value not allowed for List-type option 'copt'. Please use '[]' instead if trying" |
| 1489 | + " to set option to empty value."); |
| 1490 | } |
| 1491 | |
| 1492 | @Test |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 1493 | public void starlarkPatchTransitionRequiredFragments() throws Exception { |
| 1494 | // All Starlark rule transitions are patch transitions, while all Starlark attribute transitions |
| 1495 | // are split transitions. |
| 1496 | writeAllowlistFile(); |
| 1497 | scratch.file( |
| 1498 | "test/transitions.bzl", |
| 1499 | "def _impl(settings, attr):", |
| 1500 | " return {'//command_line_option:copt': []}", // --copt is a C++ option. |
| 1501 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1502 | " outputs = ['//command_line_option:copt'])"); |
| 1503 | scratch.file( |
| 1504 | "test/rules.bzl", |
| 1505 | "load('//test:transitions.bzl', 'my_transition')", |
| 1506 | "def _impl(ctx):", |
| 1507 | " return []", |
| 1508 | "my_rule = rule(", |
| 1509 | " implementation = _impl,", |
| 1510 | " cfg = my_transition,", |
| 1511 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1512 | " '_allowlist_function_transition': attr.label(", |
| 1513 | " default = '//tools/allowlists/function_transition_allowlist',", |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 1514 | " ),", |
| 1515 | " })"); |
| 1516 | scratch.file( |
| 1517 | "test/BUILD", |
| 1518 | "load('//test:rules.bzl', 'my_rule')", |
| 1519 | "platform(name = 'my_platform')", |
| 1520 | "my_rule(name = 'test')"); |
| 1521 | |
| 1522 | ConfiguredTargetAndData ct = getConfiguredTargetAndData("//test"); |
| 1523 | assertNoEvents(); |
Googler | f592470 | 2022-12-08 14:55:42 -0800 | [diff] [blame] | 1524 | Rule testTarget = (Rule) ct.getTargetForTesting(); |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 1525 | ConfigurationTransition ruleTransition = |
John Cater | 525227e | 2021-07-27 10:53:43 -0700 | [diff] [blame] | 1526 | testTarget |
| 1527 | .getRuleClassObject() |
| 1528 | .getTransitionFactory() |
Googler | ceddfb1 | 2023-08-18 15:25:03 -0700 | [diff] [blame] | 1529 | .create(RuleTransitionData.create(testTarget, null, "")); |
jhorvitz | 115da04 | 2021-08-16 18:50:06 -0700 | [diff] [blame] | 1530 | RequiredConfigFragmentsProvider.Builder requiredFragments = |
| 1531 | RequiredConfigFragmentsProvider.builder(); |
jhorvitz | 2dd6566 | 2021-11-04 16:31:24 -0700 | [diff] [blame] | 1532 | ruleTransition.addRequiredFragments( |
gregce | a9206eb | 2021-11-08 11:26:35 -0800 | [diff] [blame] | 1533 | requiredFragments, ct.getConfiguration().getBuildOptionDetails()); |
jhorvitz | 2e723f2 | 2021-08-20 17:09:30 -0700 | [diff] [blame] | 1534 | assertThat(requiredFragments.build().getOptionsClasses()).containsExactly(CppOptions.class); |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 1535 | } |
janakr | ca6209f | 2020-11-13 19:17:27 -0800 | [diff] [blame] | 1536 | |
| 1537 | /** |
| 1538 | * Unit test for an invalid output directory from a mnemonic via a dep transition. Integration |
| 1539 | * test for top-level transition in //src/test/shell/integration:starlark_configurations_test# |
| 1540 | * test_invalid_mnemonic_from_transition_top_level. Has to be an integration test because the |
| 1541 | * error is emitted in BuildTool. |
| 1542 | */ |
| 1543 | @Test |
| 1544 | public void invalidMnemonicFromDepTransition() throws Exception { |
| 1545 | writeAllowlistFile(); |
| 1546 | scratch.file( |
| 1547 | "test/transitions.bzl", |
| 1548 | "def _impl(settings, attr):", |
| 1549 | " return {'//command_line_option:cpu': '//bad:cpu'}", |
| 1550 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1551 | " outputs = ['//command_line_option:cpu'])"); |
| 1552 | scratch.file( |
| 1553 | "test/rules.bzl", |
| 1554 | "load('//test:transitions.bzl', 'my_transition')", |
| 1555 | "def _impl(ctx):", |
| 1556 | " return []", |
| 1557 | "my_rule = rule(", |
| 1558 | " implementation = _impl,", |
| 1559 | " cfg = my_transition,", |
| 1560 | " attrs = {", |
| 1561 | " '_allowlist_function_transition': attr.label(", |
| 1562 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1563 | " ),", |
| 1564 | " })"); |
| 1565 | scratch.file( |
| 1566 | "test/BUILD", |
| 1567 | "load('//test:rules.bzl', 'my_rule')", |
| 1568 | "my_rule(name = 'bottom')", |
| 1569 | "genrule(name = 'test', srcs = [':bottom'], outs = ['out'], cmd = 'touch $@')"); |
| 1570 | reporter.removeHandler(failFastHandler); |
| 1571 | assertThat(getConfiguredTarget("//test:test")).isNull(); |
Googler | 4dc0d11 | 2023-09-13 14:04:10 -0700 | [diff] [blame] | 1572 | assertContainsEvent("'//bad:cpu' is invalid as part of a path: must not contain /"); |
janakr | ca6209f | 2020-11-13 19:17:27 -0800 | [diff] [blame] | 1573 | } |
juliexxia | a13f590 | 2020-12-10 09:19:01 -0800 | [diff] [blame] | 1574 | |
| 1575 | @Test |
| 1576 | public void testTransitionOnAllowMultiplesBuildSettingRequiresList() throws Exception { |
juliexxia | a13f590 | 2020-12-10 09:19:01 -0800 | [diff] [blame] | 1577 | scratch.file( |
| 1578 | "test/transitions.bzl", |
| 1579 | "def _transition_impl(settings, attr):", |
| 1580 | " return {'//test:cute-animal-fact': 'puffins mate for life'}", |
| 1581 | "my_transition = transition(", |
| 1582 | " implementation = _transition_impl,", |
| 1583 | " inputs = [],", |
| 1584 | " outputs = ['//test:cute-animal-fact']", |
| 1585 | ")"); |
| 1586 | writeAllowlistFile(); |
| 1587 | scratch.file( |
| 1588 | "test/rules.bzl", |
| 1589 | "load('//test:transitions.bzl', 'my_transition')", |
| 1590 | "def _rule_impl(ctx):", |
| 1591 | " return []", |
| 1592 | "my_rule = rule(", |
| 1593 | " implementation = _rule_impl,", |
| 1594 | " cfg = my_transition,", |
| 1595 | " attrs = {", |
| 1596 | " '_allowlist_function_transition': attr.label(", |
| 1597 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1598 | " ),", |
| 1599 | " },", |
| 1600 | ")"); |
| 1601 | scratch.file( |
| 1602 | "test/build_settings.bzl", |
| 1603 | "def _impl(ctx):", |
| 1604 | " return []", |
| 1605 | "string_flag = rule(implementation = _impl, build_setting = config.string(flag=True," |
| 1606 | + " allow_multiple=True))"); |
| 1607 | scratch.file( |
| 1608 | "test/BUILD", |
| 1609 | "load('//test:rules.bzl', 'my_rule')", |
| 1610 | "load('//test:build_settings.bzl', 'string_flag')", |
| 1611 | "my_rule(name = 'test')", |
| 1612 | "string_flag(", |
| 1613 | " name = 'cute-animal-fact',", |
| 1614 | " build_setting_default = \"cats can't taste sugar\",", |
| 1615 | ")"); |
| 1616 | |
| 1617 | reporter.removeHandler(failFastHandler); |
| 1618 | getConfiguredTarget("//test"); |
| 1619 | assertContainsEvent( |
| 1620 | "'//test:cute-animal-fact' allows multiple values and must be set in transition using a" |
| 1621 | + " starlark list instead of single value"); |
| 1622 | } |
| 1623 | |
| 1624 | @Test |
| 1625 | public void testTransitionOnAllowMultiplesBuildSetting() throws Exception { |
juliexxia | a13f590 | 2020-12-10 09:19:01 -0800 | [diff] [blame] | 1626 | scratch.file( |
| 1627 | "test/transitions.bzl", |
| 1628 | "def _transition_impl(settings, attr):", |
| 1629 | " return {'//test:cute-animal-fact': ['puffins mate for life']}", |
| 1630 | "my_transition = transition(", |
| 1631 | " implementation = _transition_impl,", |
| 1632 | " inputs = [],", |
| 1633 | " outputs = ['//test:cute-animal-fact']", |
| 1634 | ")"); |
| 1635 | writeAllowlistFile(); |
| 1636 | scratch.file( |
| 1637 | "test/rules.bzl", |
| 1638 | "load('//test:transitions.bzl', 'my_transition')", |
| 1639 | "def _rule_impl(ctx):", |
| 1640 | " return []", |
| 1641 | "my_rule = rule(", |
| 1642 | " implementation = _rule_impl,", |
| 1643 | " cfg = my_transition,", |
| 1644 | " attrs = {", |
| 1645 | " '_allowlist_function_transition': attr.label(", |
| 1646 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1647 | " ),", |
| 1648 | " },", |
| 1649 | ")"); |
| 1650 | scratch.file( |
| 1651 | "test/build_settings.bzl", |
| 1652 | "def _impl(ctx):", |
| 1653 | " return []", |
| 1654 | "string_flag = rule(implementation = _impl, build_setting = config.string(flag=True," |
| 1655 | + " allow_multiple=True))"); |
| 1656 | scratch.file( |
| 1657 | "test/BUILD", |
| 1658 | "load('//test:rules.bzl', 'my_rule')", |
| 1659 | "load('//test:build_settings.bzl', 'string_flag')", |
| 1660 | "my_rule(name = 'test')", |
| 1661 | "string_flag(", |
| 1662 | " name = 'cute-animal-fact',", |
| 1663 | " build_setting_default = \"cats can't taste sugar\",", |
| 1664 | ")"); |
| 1665 | |
| 1666 | Map<Label, Object> starlarkOptions = |
| 1667 | getConfiguration(getConfiguredTarget("//test")).getOptions().getStarlarkOptions(); |
| 1668 | assertNoEvents(); |
| 1669 | assertThat( |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 1670 | (List<?>) starlarkOptions.get(Label.parseCanonicalUnchecked("//test:cute-animal-fact"))) |
juliexxia | a13f590 | 2020-12-10 09:19:01 -0800 | [diff] [blame] | 1671 | .containsExactly("puffins mate for life"); |
| 1672 | } |
gregce | d679546 | 2020-12-14 11:06:45 -0800 | [diff] [blame] | 1673 | |
Fabian Meumertzheim | 5dcefe1 | 2022-09-23 08:20:21 -0700 | [diff] [blame] | 1674 | @Test |
| 1675 | public void testTransitionOnAllowMultiplesBuildSettingAlwaysSeesListValue() throws Exception { |
| 1676 | scratch.file( |
| 1677 | "test/transitions.bzl", |
| 1678 | "def _transition_impl(settings, attr):", |
| 1679 | " setting_type = type(settings['//test:multiple_flag'])", |
| 1680 | " if setting_type != type([]):", |
| 1681 | " fail('Expected setting to be a list, got %s' % setting_type)", |
| 1682 | " return {}", |
| 1683 | "my_transition = transition(", |
| 1684 | " implementation = _transition_impl,", |
| 1685 | " inputs = ['//test:multiple_flag'],", |
| 1686 | " outputs = ['//test:multiple_flag']", |
| 1687 | ")"); |
| 1688 | writeAllowlistFile(); |
| 1689 | scratch.file( |
| 1690 | "test/rules.bzl", |
| 1691 | "load('//test:transitions.bzl', 'my_transition')", |
| 1692 | "def _rule_impl(ctx):", |
| 1693 | " return []", |
| 1694 | "my_rule = rule(", |
| 1695 | " implementation = _rule_impl,", |
| 1696 | " cfg = my_transition,", |
| 1697 | " attrs = {", |
| 1698 | " '_allowlist_function_transition': attr.label(", |
| 1699 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1700 | " ),", |
| 1701 | " },", |
| 1702 | ")"); |
| 1703 | scratch.file( |
| 1704 | "test/build_settings.bzl", |
| 1705 | "def _impl(ctx):", |
| 1706 | " return []", |
| 1707 | "string_flag = rule(implementation = _impl, build_setting = config.string(flag=True," |
| 1708 | + " allow_multiple=True))"); |
| 1709 | scratch.file( |
| 1710 | "test/BUILD", |
| 1711 | "load('//test:rules.bzl', 'my_rule')", |
| 1712 | "load('//test:build_settings.bzl', 'string_flag')", |
| 1713 | "my_rule(name = 'test')", |
| 1714 | "string_flag(", |
| 1715 | " name = 'multiple_flag',", |
| 1716 | " build_setting_default = '',", |
| 1717 | ")"); |
| 1718 | |
| 1719 | // Starlark option at is default value. |
| 1720 | getConfiguredTarget("//test"); |
| 1721 | |
| 1722 | useConfiguration(ImmutableMap.of("//test:multiple_flag", ImmutableList.of("foo"))); |
| 1723 | getConfiguredTarget("//test"); |
| 1724 | |
| 1725 | useConfiguration(ImmutableMap.of("//test:multiple_flag", ImmutableList.of("foo", "bar"))); |
| 1726 | getConfiguredTarget("//test"); |
| 1727 | } |
| 1728 | |
gregce | d679546 | 2020-12-14 11:06:45 -0800 | [diff] [blame] | 1729 | /** |
| 1730 | * Changing --cpu implicitly changes the target platform. Test that the old value of --platforms |
| 1731 | * gets cleared out (platform mappings can then kick in to set --platforms correctly). |
| 1732 | */ |
| 1733 | @Test |
| 1734 | public void testImplicitPlatformsChange() throws Exception { |
| 1735 | scratch.file("platforms/BUILD", "platform(name = 'my_platform', constraint_values = [])"); |
| 1736 | scratch.file( |
| 1737 | "test/transitions.bzl", |
| 1738 | "def _transition_impl(settings, attr):", |
| 1739 | " return {'//command_line_option:cpu': 'ppc'}", |
| 1740 | "my_transition = transition(", |
| 1741 | " implementation = _transition_impl,", |
| 1742 | " inputs = [],", |
| 1743 | " outputs = ['//command_line_option:cpu']", |
| 1744 | ")"); |
| 1745 | writeAllowlistFile(); |
| 1746 | scratch.file( |
| 1747 | "test/rules.bzl", |
| 1748 | "load('//test:transitions.bzl', 'my_transition')", |
| 1749 | "def _rule_impl(ctx):", |
| 1750 | " return []", |
| 1751 | "my_rule = rule(", |
| 1752 | " implementation = _rule_impl,", |
| 1753 | " cfg = my_transition,", |
| 1754 | " attrs = {", |
| 1755 | " '_allowlist_function_transition': attr.label(", |
| 1756 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1757 | " ),", |
| 1758 | " },", |
| 1759 | ")"); |
| 1760 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1761 | |
| 1762 | useConfiguration("--platforms=//platforms:my_platform"); |
| 1763 | // When --platforms is empty and no platform mapping triggers, PlatformMappingValue sets |
| 1764 | // --platforms to PlatformOptions.computeTargetPlatform(), which defaults to the host. |
| 1765 | assertThat( |
| 1766 | getConfiguration(getConfiguredTarget("//test:test")) |
| 1767 | .getOptions() |
| 1768 | .get(PlatformOptions.class) |
| 1769 | .platforms) |
| 1770 | .containsExactly( |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 1771 | Label.parseCanonicalUnchecked( |
Googler | 3469784 | 2022-08-09 07:04:42 -0700 | [diff] [blame] | 1772 | TestConstants.LOCAL_CONFIG_PLATFORM_PACKAGE_ROOT + ":host")); |
gregce | d679546 | 2020-12-14 11:06:45 -0800 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | @Test |
| 1776 | public void testExplicitPlatformsChange() throws Exception { |
| 1777 | scratch.file( |
| 1778 | "platforms/BUILD", |
| 1779 | "platform(name = 'my_platform', constraint_values = [])", |
| 1780 | "platform(name = 'my_other_platform', constraint_values = [])"); |
| 1781 | scratch.file( |
| 1782 | "test/transitions.bzl", |
| 1783 | "def _transition_impl(settings, attr):", |
| 1784 | " return {", |
| 1785 | " '//command_line_option:cpu': 'ppc',", |
| 1786 | " '//command_line_option:platforms': ['//platforms:my_other_platform']", |
| 1787 | " }", |
| 1788 | "my_transition = transition(", |
| 1789 | " implementation = _transition_impl,", |
| 1790 | " inputs = [],", |
| 1791 | " outputs = [", |
| 1792 | " '//command_line_option:cpu',", |
| 1793 | " '//command_line_option:platforms'", |
| 1794 | " ]", |
| 1795 | ")"); |
| 1796 | writeAllowlistFile(); |
| 1797 | scratch.file( |
| 1798 | "test/rules.bzl", |
| 1799 | "load('//test:transitions.bzl', 'my_transition')", |
| 1800 | "def _rule_impl(ctx):", |
| 1801 | " return []", |
| 1802 | "my_rule = rule(", |
| 1803 | " implementation = _rule_impl,", |
| 1804 | " cfg = my_transition,", |
| 1805 | " attrs = {", |
| 1806 | " '_allowlist_function_transition': attr.label(", |
| 1807 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1808 | " ),", |
| 1809 | " },", |
| 1810 | ")"); |
| 1811 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1812 | |
| 1813 | useConfiguration("--platforms=//platforms:my_platform"); |
| 1814 | assertThat( |
| 1815 | getConfiguration(getConfiguredTarget("//test:test")) |
| 1816 | .getOptions() |
| 1817 | .get(PlatformOptions.class) |
| 1818 | .platforms) |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 1819 | .containsExactly(Label.parseCanonicalUnchecked("//platforms:my_other_platform")); |
gregce | d679546 | 2020-12-14 11:06:45 -0800 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | /* If the transition doesn't change --cpu, it doesn't constitute a platform change. */ |
| 1823 | @Test |
| 1824 | public void testNoPlatformChange() throws Exception { |
| 1825 | scratch.file("platforms/BUILD", "platform(name = 'my_platform', constraint_values = [])"); |
| 1826 | scratch.file( |
| 1827 | "test/transitions.bzl", |
| 1828 | "def _transition_impl(settings, attr):", |
| 1829 | " return {", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1830 | " '//command_line_option:foo': 'blah',", |
gregce | d679546 | 2020-12-14 11:06:45 -0800 | [diff] [blame] | 1831 | " }", |
| 1832 | "my_transition = transition(", |
| 1833 | " implementation = _transition_impl,", |
| 1834 | " inputs = [],", |
| 1835 | " outputs = [", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame] | 1836 | " '//command_line_option:foo',", |
gregce | d679546 | 2020-12-14 11:06:45 -0800 | [diff] [blame] | 1837 | " ]", |
| 1838 | ")"); |
| 1839 | writeAllowlistFile(); |
| 1840 | scratch.file( |
| 1841 | "test/rules.bzl", |
| 1842 | "load('//test:transitions.bzl', 'my_transition')", |
| 1843 | "def _rule_impl(ctx):", |
| 1844 | " return []", |
| 1845 | "my_rule = rule(", |
| 1846 | " implementation = _rule_impl,", |
| 1847 | " cfg = my_transition,", |
| 1848 | " attrs = {", |
| 1849 | " '_allowlist_function_transition': attr.label(", |
| 1850 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1851 | " ),", |
| 1852 | " },", |
| 1853 | ")"); |
| 1854 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1855 | |
| 1856 | useConfiguration("--platforms=//platforms:my_platform"); |
| 1857 | assertThat( |
| 1858 | getConfiguration(getConfiguredTarget("//test:test")) |
| 1859 | .getOptions() |
| 1860 | .get(PlatformOptions.class) |
| 1861 | .platforms) |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 1862 | .containsExactly(Label.parseCanonicalUnchecked("//platforms:my_platform")); |
gregce | d679546 | 2020-12-14 11:06:45 -0800 | [diff] [blame] | 1863 | } |
gregce | fe644be | 2021-11-01 12:12:02 -0700 | [diff] [blame] | 1864 | |
| 1865 | @Test |
| 1866 | public void testTransitionsStillTriggerWhenOnlyRuleAttributesChange() throws Exception { |
| 1867 | scratch.file( |
| 1868 | "test/defs.bzl", |
| 1869 | "def _transition_impl(settings, attr):", |
| 1870 | " return {", |
| 1871 | " '//command_line_option:foo': attr.my_attr,", |
| 1872 | " }", |
| 1873 | "_my_transition = transition(", |
| 1874 | " implementation = _transition_impl,", |
| 1875 | " inputs = [],", |
| 1876 | " outputs = [", |
| 1877 | " '//command_line_option:foo',", |
| 1878 | " ]", |
| 1879 | ")", |
| 1880 | "def _rule_impl(ctx):", |
| 1881 | " return []", |
| 1882 | "my_rule = rule(", |
| 1883 | " implementation = _rule_impl,", |
| 1884 | " cfg = _my_transition,", |
| 1885 | " attrs = {", |
| 1886 | " 'my_attr': attr.string(),", |
| 1887 | " '_allowlist_function_transition': attr.label(", |
| 1888 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1889 | " ),", |
| 1890 | " },", |
| 1891 | ")"); |
| 1892 | writeAllowlistFile(); |
| 1893 | |
| 1894 | scratch.file( |
| 1895 | "test/BUILD", |
| 1896 | "load('//test:defs.bzl', 'my_rule')", |
| 1897 | "my_rule(", |
| 1898 | " name = 'buildme',", |
| 1899 | " my_attr = 'first build',", |
| 1900 | ")"); |
| 1901 | assertThat( |
| 1902 | getConfiguration(getConfiguredTarget("//test:buildme")) |
| 1903 | .getOptions() |
| 1904 | .get(DummyTestOptions.class) |
| 1905 | .foo) |
| 1906 | .isEqualTo("first build"); |
| 1907 | |
| 1908 | scratch.overwriteFile( |
| 1909 | "test/BUILD", |
| 1910 | "load('//test:defs.bzl', 'my_rule')", |
| 1911 | "my_rule(", |
| 1912 | " name = 'buildme',", |
| 1913 | " my_attr = 'second build',", |
| 1914 | ")"); |
| 1915 | skyframeExecutor.invalidateFilesUnderPathForTesting( |
| 1916 | reporter, |
| 1917 | ModifiedFileSet.builder().modify(PathFragment.create("test/BUILD")).build(), |
| 1918 | Root.fromPath(rootDirectory)); |
| 1919 | |
| 1920 | assertThat( |
| 1921 | getConfiguration(getConfiguredTarget("//test:buildme")) |
| 1922 | .getOptions() |
| 1923 | .get(DummyTestOptions.class) |
| 1924 | .foo) |
| 1925 | .isEqualTo("second build"); |
| 1926 | } |
Googler | 35f81b4 | 2023-07-11 08:49:47 -0700 | [diff] [blame] | 1927 | |
| 1928 | private static class AnalysisRootCauseCollector { |
| 1929 | private final ArrayList<AnalysisRootCauseEvent> rootCauses = new ArrayList<>(); |
| 1930 | |
| 1931 | @Subscribe |
| 1932 | public void rootCause(AnalysisRootCauseEvent event) { |
| 1933 | rootCauses.add(event); |
| 1934 | } |
| 1935 | } |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 1936 | } |