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 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 17 | |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 19 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.analysis.config.transitions.ConfigurationTransition; |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 22 | import com.google.devtools.build.lib.analysis.util.DummyTestFragment; |
| 23 | import com.google.devtools.build.lib.analysis.util.DummyTestFragment.DummyTestOptions; |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.cmdline.Label; |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.packages.Rule; |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 26 | import com.google.devtools.build.lib.rules.cpp.CppOptions; |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData; |
gregce | d679546 | 2020-12-14 11:06:45 -0800 | [diff] [blame] | 28 | import com.google.devtools.build.lib.testutil.TestConstants; |
juliexxia | 6778a05 | 2019-04-09 08:25:05 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.testutil.TestRuleClassProvider; |
juliexxia | a13f590 | 2020-12-10 09:19:01 -0800 | [diff] [blame] | 30 | import java.util.List; |
| 31 | import java.util.Map; |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | import org.junit.runners.JUnit4; |
| 35 | |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 36 | /** Tests for StarlarkRuleTransitionProvider. */ |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 37 | @RunWith(JUnit4.class) |
| 38 | public class StarlarkRuleTransitionProviderTest extends BuildViewTestCase { |
juliexxia | 6778a05 | 2019-04-09 08:25:05 -0700 | [diff] [blame] | 39 | @Override |
brandjon | 232a6b8 | 2020-06-08 12:32:49 -0700 | [diff] [blame] | 40 | protected ConfiguredRuleClassProvider createRuleClassProvider() { |
juliexxia | 6778a05 | 2019-04-09 08:25:05 -0700 | [diff] [blame] | 41 | ConfiguredRuleClassProvider.Builder builder = new ConfiguredRuleClassProvider.Builder(); |
| 42 | TestRuleClassProvider.addStandardRules(builder); |
gregce | 4ed65b0 | 2020-11-18 07:06:25 -0800 | [diff] [blame] | 43 | builder.addConfigurationFragment(DummyTestFragment.class); |
juliexxia | 6778a05 | 2019-04-09 08:25:05 -0700 | [diff] [blame] | 44 | return builder.build(); |
| 45 | } |
| 46 | |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 47 | private void writeAllowlistFile() throws Exception { |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 48 | scratch.file( |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 49 | "tools/allowlists/function_transition_allowlist/BUILD", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 50 | "package_group(", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 51 | " name = 'function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 52 | " packages = [", |
| 53 | " '//test/...',", |
| 54 | " ],", |
| 55 | ")"); |
| 56 | } |
| 57 | |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 58 | @Test |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 59 | public void testBadReturnTypeFromTransition() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 60 | writeAllowlistFile(); |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 61 | scratch.file( |
| 62 | "test/transitions.bzl", |
| 63 | "def _impl(settings, attr):", |
| 64 | " return 'cpu=k8'", |
| 65 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 66 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 67 | scratch.file( |
| 68 | "test/rules.bzl", |
| 69 | "load('//test:transitions.bzl', 'my_transition')", |
| 70 | "def _impl(ctx):", |
| 71 | " return []", |
| 72 | "my_rule = rule(", |
| 73 | " implementation = _impl,", |
| 74 | " cfg = my_transition,", |
| 75 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 76 | " '_allowlist_function_transition': attr.label(", |
| 77 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 78 | " ),", |
| 79 | " })"); |
| 80 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 81 | |
| 82 | reporter.removeHandler(failFastHandler); |
| 83 | getConfiguredTarget("//test"); |
adonovan | d07bf13 | 2021-01-08 09:10:22 -0800 | [diff] [blame] | 84 | assertContainsEvent("transition function returned string, want dict or list of dicts"); |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Test |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 88 | public void testOutputOnlyTransition() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 89 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 90 | scratch.file( |
| 91 | "test/transitions.bzl", |
| 92 | "def _impl(settings, attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 93 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 94 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 95 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 96 | scratch.file( |
| 97 | "test/rules.bzl", |
| 98 | "load('//test:transitions.bzl', 'my_transition')", |
| 99 | "def _impl(ctx):", |
| 100 | " return []", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 101 | "my_rule = rule(", |
| 102 | " implementation = _impl,", |
| 103 | " cfg = my_transition,", |
| 104 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 105 | " '_allowlist_function_transition': attr.label(", |
| 106 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 107 | " ),", |
| 108 | " })"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 109 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 110 | |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 111 | useConfiguration("--foo=pre-transition"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 112 | |
| 113 | BuildConfiguration configuration = getConfiguration(getConfiguredTarget("//test")); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 114 | assertThat(configuration.getOptions().get(DummyTestOptions.class).foo) |
| 115 | .isEqualTo("post-transition"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testInputAndOutputTransition() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 120 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 121 | scratch.file( |
| 122 | "test/transitions.bzl", |
| 123 | "def _impl(settings, attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 124 | " return {'//command_line_option:foo': ", |
| 125 | " settings['//command_line_option:foo']+'->post-transition'}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 126 | "my_transition = transition(", |
| 127 | " implementation = _impl,", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 128 | " inputs = ['//command_line_option:foo'],", |
| 129 | " outputs = ['//command_line_option:foo'],", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 130 | ")"); |
| 131 | |
| 132 | scratch.file( |
| 133 | "test/rules.bzl", |
| 134 | "load('//test:transitions.bzl', 'my_transition')", |
| 135 | "def _impl(ctx):", |
| 136 | " return []", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 137 | "my_rule = rule(", |
| 138 | " implementation = _impl,", |
| 139 | " cfg = my_transition,", |
| 140 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 141 | " '_allowlist_function_transition': attr.label(", |
| 142 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 143 | " ),", |
| 144 | " })"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 145 | |
| 146 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 147 | |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 148 | useConfiguration("--foo=pre-transition"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 149 | |
| 150 | BuildConfiguration configuration = getConfiguration(getConfiguredTarget("//test")); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 151 | assertThat(configuration.getOptions().get(DummyTestOptions.class).foo) |
| 152 | .isEqualTo("pre-transition->post-transition"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void testBuildSettingCannotTransition() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 157 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 158 | scratch.file( |
| 159 | "test/transitions.bzl", |
| 160 | "def _impl(settings, attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 161 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 162 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 163 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 164 | scratch.file( |
| 165 | "test/rules.bzl", |
| 166 | "load('//test:transitions.bzl', 'my_transition')", |
| 167 | "def _impl(ctx):", |
| 168 | " return []", |
| 169 | "my_rule = rule(", |
| 170 | " implementation = _impl,", |
| 171 | " cfg = my_transition,", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 172 | " build_setting = config.string(),", |
| 173 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 174 | " '_allowlist_function_transition': attr.label(", |
| 175 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 176 | " ),", |
| 177 | " })"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 178 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 179 | |
| 180 | reporter.removeHandler(failFastHandler); |
| 181 | getConfiguredTarget("//test"); |
| 182 | assertContainsEvent( |
| 183 | "Build setting rules cannot use the `cfg` param to apply transitions to themselves"); |
| 184 | } |
| 185 | |
| 186 | @Test |
| 187 | public void testBadCfgInput() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 188 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 189 | scratch.file( |
| 190 | "test/rules.bzl", |
| 191 | "def _impl(ctx):", |
| 192 | " return []", |
| 193 | "my_rule = rule(", |
| 194 | " implementation = _impl,", |
| 195 | " cfg = 'my_transition',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 196 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 197 | " '_allowlist_function_transition': attr.label(", |
| 198 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 199 | " ),", |
| 200 | " })"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 201 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 202 | |
| 203 | reporter.removeHandler(failFastHandler); |
| 204 | getConfiguredTarget("//test"); |
| 205 | assertContainsEvent( |
| 206 | "`cfg` must be set to a transition object initialized by the transition() function."); |
| 207 | } |
| 208 | |
| 209 | @Test |
| 210 | public void testMultipleReturnConfigs() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 211 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 212 | scratch.file( |
| 213 | "test/transitions.bzl", |
| 214 | "def _impl(settings, attr):", |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 215 | " return [", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 216 | " {'//command_line_option:foo': 'split_one'},", |
| 217 | " {'//command_line_option:foo': 'split_two'},", |
juliexxia | eee87e9 | 2019-04-30 13:21:42 -0700 | [diff] [blame] | 218 | " ]", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 219 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 220 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 221 | scratch.file( |
| 222 | "test/rules.bzl", |
| 223 | "load('//test:transitions.bzl', 'my_transition')", |
| 224 | "def _impl(ctx):", |
| 225 | " return []", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 226 | "my_rule = rule(", |
| 227 | " implementation = _impl,", |
| 228 | " cfg = my_transition,", |
| 229 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 230 | " '_allowlist_function_transition': attr.label(", |
| 231 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 232 | " ),", |
| 233 | " })"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 234 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 235 | |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 236 | reporter.removeHandler(failFastHandler); |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 237 | getConfiguredTarget("//test"); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 238 | assertContainsEvent( |
| 239 | "Rule transition only allowed to return a single transitioned configuration."); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Test |
| 243 | public void testCanDoBadStuffWithParameterizedTransitionsAndSelects() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 244 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 245 | scratch.file( |
| 246 | "test/transitions.bzl", |
| 247 | "def _impl(settings, attr):", |
| 248 | " if (attr.my_configurable_attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 249 | " return {'//command_line_option:foo': 'true'}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 250 | " else:", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 251 | " return {'//command_line_option:foo': 'false'}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 252 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 253 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 254 | scratch.file( |
| 255 | "test/rules.bzl", |
| 256 | "load('//test:transitions.bzl', 'my_transition')", |
| 257 | "def _impl(ctx):", |
| 258 | " return []", |
| 259 | "my_rule = rule(", |
| 260 | " implementation = _impl,", |
| 261 | " cfg = my_transition,", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 262 | " attrs = {", |
| 263 | " 'my_configurable_attr': attr.bool(default = False),", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 264 | " '_allowlist_function_transition': attr.label(", |
| 265 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 266 | " ),", |
| 267 | " },", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 268 | ")"); |
| 269 | scratch.file( |
| 270 | "test/BUILD", |
| 271 | "load('//test:rules.bzl', 'my_rule')", |
| 272 | "my_rule(", |
| 273 | " name = 'test',", |
| 274 | " my_configurable_attr = select({", |
| 275 | " '//conditions:default': False,", |
| 276 | " ':true-config': True,", |
| 277 | " })", |
| 278 | ")", |
| 279 | "config_setting(", |
| 280 | " name = 'true-config',", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 281 | " values = {'foo': 'true'},", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 282 | ")"); |
| 283 | |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 284 | reporter.removeHandler(failFastHandler); |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 285 | getConfiguredTarget("//test"); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 286 | assertContainsEvent( |
| 287 | "No attribute 'my_configurable_attr'. " |
| 288 | + "Either this attribute does not exist for this rule or is set by a select. " |
| 289 | + "Starlark rule transitions currently cannot read attributes behind selects."); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @Test |
| 293 | public void testLabelTypedAttrReturnsLabelNotDep() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 294 | writeAllowlistFile(); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 295 | scratch.file( |
| 296 | "test/transitions.bzl", |
| 297 | "def _impl(settings, attr):", |
| 298 | " if attr.dict_attr[Label('//test:key')] == 'value':", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 299 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 300 | " else:", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 301 | " return {'//command_line_option:foo': 'uh-oh'}", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 302 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 303 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 304 | scratch.file( |
| 305 | "test/rules.bzl", |
| 306 | "load('//test:transitions.bzl', 'my_transition')", |
| 307 | "def _impl(ctx):", |
| 308 | " return []", |
| 309 | "my_rule = rule(", |
| 310 | " implementation = _impl,", |
| 311 | " cfg = my_transition,", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 312 | " attrs = {", |
| 313 | " 'dict_attr': attr.label_keyed_string_dict(),", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 314 | " '_allowlist_function_transition': attr.label(", |
| 315 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 316 | " ),", |
| 317 | " },", |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 318 | ")", |
| 319 | "simple_rule = rule(_impl)"); |
| 320 | scratch.file( |
| 321 | "test/BUILD", |
| 322 | "load('//test:rules.bzl', 'my_rule', 'simple_rule')", |
| 323 | "my_rule(", |
| 324 | " name = 'test',", |
| 325 | " dict_attr = {':key': 'value'},", |
| 326 | ")", |
| 327 | "simple_rule(name = 'key')"); |
| 328 | |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 329 | useConfiguration("--foo=pre-transition"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 330 | |
| 331 | BuildConfiguration configuration = getConfiguration(getConfiguredTarget("//test")); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 332 | assertThat(configuration.getOptions().get(DummyTestOptions.class).foo) |
| 333 | .isEqualTo("post-transition"); |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 334 | } |
| 335 | |
juliexxia | db56678 | 2019-04-12 10:48:07 -0700 | [diff] [blame] | 336 | private static final String CUTE_ANIMAL_DEFAULT = |
| 337 | "cows produce more milk when they listen to soothing music"; |
| 338 | |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 339 | private void writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 340 | writeAllowlistFile(); |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 341 | scratch.file( |
| 342 | "test/rules.bzl", |
| 343 | "load('//test:transitions.bzl', 'my_transition')", |
| 344 | "def _rule_impl(ctx):", |
| 345 | " return []", |
| 346 | "my_rule = rule(", |
| 347 | " implementation = _rule_impl,", |
| 348 | " cfg = my_transition,", |
| 349 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 350 | " '_allowlist_function_transition': attr.label(", |
| 351 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 352 | " ),", |
| 353 | " },", |
| 354 | ")"); |
| 355 | |
| 356 | scratch.file( |
| 357 | "test/build_settings.bzl", |
| 358 | "def _impl(ctx):", |
| 359 | " return []", |
| 360 | "string_flag = rule(implementation = _impl, build_setting = config.string(flag=True))"); |
| 361 | |
| 362 | scratch.file( |
| 363 | "test/BUILD", |
| 364 | "load('//test:rules.bzl', 'my_rule')", |
| 365 | "load('//test:build_settings.bzl', 'string_flag')", |
| 366 | "my_rule(name = 'test')", |
| 367 | "string_flag(", |
| 368 | " name = 'cute-animal-fact',", |
juliexxia | db56678 | 2019-04-12 10:48:07 -0700 | [diff] [blame] | 369 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 370 | ")"); |
| 371 | } |
| 372 | |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 373 | @Test |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 374 | public void testTransitionOnBuildSetting_fromDefault() throws Exception { |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 375 | scratch.file( |
| 376 | "test/transitions.bzl", |
| 377 | "def _transition_impl(settings, attr):", |
| 378 | " return {'//test:cute-animal-fact': 'puffins mate for life'}", |
| 379 | "my_transition = transition(", |
| 380 | " implementation = _transition_impl,", |
| 381 | " inputs = [],", |
| 382 | " outputs = ['//test:cute-animal-fact']", |
| 383 | ")"); |
| 384 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 385 | |
| 386 | BuildConfiguration configuration = getConfiguration(getConfiguredTarget("//test")); |
| 387 | assertThat( |
| 388 | configuration |
| 389 | .getOptions() |
| 390 | .getStarlarkOptions() |
| 391 | .get(Label.parseAbsoluteUnchecked("//test:cute-animal-fact"))) |
| 392 | .isEqualTo("puffins mate for life"); |
| 393 | } |
| 394 | |
| 395 | @Test |
| 396 | public void testTransitionOnBuildSetting_fromCommandLine() throws Exception { |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 397 | scratch.file( |
| 398 | "test/transitions.bzl", |
| 399 | "def _transition_impl(settings, attr):", |
| 400 | " return {'//test:cute-animal-fact': 'puffins mate for life'}", |
| 401 | "my_transition = transition(", |
| 402 | " implementation = _transition_impl,", |
| 403 | " inputs = [],", |
| 404 | " outputs = ['//test:cute-animal-fact']", |
| 405 | ")"); |
| 406 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 407 | |
| 408 | useConfiguration(ImmutableMap.of("//test:cute-animal-fact", "cats can't taste sugar")); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 409 | |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 410 | BuildConfiguration configuration = getConfiguration(getConfiguredTarget("//test")); |
| 411 | assertThat( |
| 412 | configuration |
| 413 | .getOptions() |
| 414 | .getStarlarkOptions() |
| 415 | .get(Label.parseAbsoluteUnchecked("//test:cute-animal-fact"))) |
| 416 | .isEqualTo("puffins mate for life"); |
| 417 | } |
| 418 | |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 419 | @Test |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 420 | public void testTransitionOnBuildSetting_badValue() throws Exception { |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 421 | scratch.file( |
| 422 | "test/transitions.bzl", |
| 423 | "def _transition_impl(settings, attr):", |
| 424 | " return {'//test:cute-animal-fact': 24}", |
| 425 | "my_transition = transition(", |
| 426 | " implementation = _transition_impl,", |
| 427 | " inputs = [],", |
| 428 | " outputs = ['//test:cute-animal-fact']", |
| 429 | ")"); |
| 430 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 431 | |
| 432 | useConfiguration(ImmutableMap.of("//test:cute-animal-fact", "cats can't taste sugar")); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 433 | |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 434 | reporter.removeHandler(failFastHandler); |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 435 | getConfiguredTarget("//test"); |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 436 | assertContainsEvent( |
| 437 | "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] | 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_noSuchTarget() 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:i-am-not-real': 'imaginary-friend'}", |
| 446 | "my_transition = transition(", |
| 447 | " implementation = _transition_impl,", |
| 448 | " inputs = [],", |
| 449 | " outputs = ['//test:i-am-not-real']", |
| 450 | ")"); |
| 451 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 452 | |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 453 | reporter.removeHandler(failFastHandler); |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 454 | getConfiguredTarget("//test"); |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 455 | assertContainsEvent( |
| 456 | "no such target '//test:i-am-not-real': target " |
| 457 | + "'i-am-not-real' not declared in package 'test'"); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | @Test |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 461 | public void testTransitionOnBuildSetting_noSuchPackage() throws Exception { |
juliexxia | b75e4530 | 2019-05-24 07:48:39 -0700 | [diff] [blame] | 462 | scratch.file( |
| 463 | "test/transitions.bzl", |
| 464 | "def _transition_impl(settings, attr):", |
| 465 | " return {'//i-am-not-real': 'imaginary-friend'}", |
| 466 | "my_transition = transition(", |
| 467 | " implementation = _transition_impl,", |
| 468 | " inputs = [],", |
| 469 | " outputs = ['//i-am-not-real']", |
| 470 | ")"); |
| 471 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 472 | |
| 473 | reporter.removeHandler(failFastHandler); |
| 474 | getConfiguredTarget("//test"); |
| 475 | assertContainsEvent("no such package 'i-am-not-real': Unable to find build setting package"); |
| 476 | } |
| 477 | |
| 478 | @Test |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 479 | public void testTransitionOnBuildSetting_notABuildSetting() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 480 | writeAllowlistFile(); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 481 | scratch.file( |
| 482 | "test/transitions.bzl", |
| 483 | "def _transition_impl(settings, attr):", |
| 484 | " return {'//test:cute-animal-fact': 'puffins mate for life'}", |
| 485 | "my_transition = transition(", |
| 486 | " implementation = _transition_impl,", |
| 487 | " inputs = [],", |
| 488 | " outputs = ['//test:cute-animal-fact']", |
| 489 | ")"); |
| 490 | scratch.file( |
| 491 | "test/rules.bzl", |
| 492 | "load('//test:transitions.bzl', 'my_transition')", |
| 493 | "def _rule_impl(ctx):", |
| 494 | " return []", |
| 495 | "my_rule = rule(", |
| 496 | " implementation = _rule_impl,", |
| 497 | " cfg = my_transition,", |
| 498 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 499 | " '_allowlist_function_transition': attr.label(", |
| 500 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 501 | " ),", |
| 502 | " },", |
| 503 | ")"); |
| 504 | scratch.file( |
| 505 | "test/build_settings.bzl", |
| 506 | "def _impl(ctx):", |
| 507 | " return []", |
| 508 | "non_build_setting = rule(implementation = _impl)"); |
| 509 | scratch.file( |
| 510 | "test/BUILD", |
| 511 | "load('//test:rules.bzl', 'my_rule')", |
| 512 | "load('//test:build_settings.bzl', 'non_build_setting')", |
| 513 | "my_rule(name = 'test')", |
| 514 | "non_build_setting(name = 'cute-animal-fact')"); |
| 515 | |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 516 | reporter.removeHandler(failFastHandler); |
| 517 | getConfiguredTarget("//test"); |
| 518 | assertContainsEvent( |
| 519 | "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] | 520 | } |
| 521 | |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 522 | @Test |
juliexxia | db56678 | 2019-04-12 10:48:07 -0700 | [diff] [blame] | 523 | public void testTransitionOnBuildSetting_dontStoreDefault() throws Exception { |
juliexxia | db56678 | 2019-04-12 10:48:07 -0700 | [diff] [blame] | 524 | scratch.file( |
| 525 | "test/transitions.bzl", |
| 526 | "def _transition_impl(settings, attr):", |
| 527 | " return {'//test:cute-animal-fact': '" + CUTE_ANIMAL_DEFAULT + "'}", |
| 528 | "my_transition = transition(", |
| 529 | " implementation = _transition_impl,", |
| 530 | " inputs = [],", |
| 531 | " outputs = ['//test:cute-animal-fact']", |
| 532 | ")"); |
| 533 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 534 | |
| 535 | useConfiguration(ImmutableMap.of("//test:cute-animal-fact", "cats can't taste sugar")); |
| 536 | |
| 537 | BuildConfiguration configuration = getConfiguration(getConfiguredTarget("//test")); |
| 538 | assertThat(configuration.getOptions().getStarlarkOptions()) |
| 539 | .doesNotContainKey(Label.parseAbsoluteUnchecked("//test:cute-animal-fact")); |
| 540 | } |
| 541 | |
| 542 | @Test |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 543 | public void testTransitionReadsBuildSetting_fromDefault() throws Exception { |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 544 | scratch.file( |
| 545 | "test/transitions.bzl", |
| 546 | "def _transition_impl(settings, attr):", |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 547 | " return {'//test:cute-animal-fact': settings['//test:cute-animal-fact']+' <- TRUE'}", |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 548 | "my_transition = transition(", |
| 549 | " implementation = _transition_impl,", |
| 550 | " inputs = ['//test:cute-animal-fact'],", |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 551 | " outputs = ['//test:cute-animal-fact']", |
| 552 | ")"); |
| 553 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 554 | |
| 555 | BuildConfiguration configuration = getConfiguration(getConfiguredTarget("//test")); |
| 556 | assertThat( |
| 557 | configuration |
| 558 | .getOptions() |
| 559 | .getStarlarkOptions() |
| 560 | .get(Label.parseAbsoluteUnchecked("//test:cute-animal-fact"))) |
| 561 | .isEqualTo("cows produce more milk when they listen to soothing music <- TRUE"); |
| 562 | } |
| 563 | |
| 564 | @Test |
| 565 | public void testTransitionReadsBuildSetting_fromCommandLine() throws Exception { |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 566 | scratch.file( |
| 567 | "test/transitions.bzl", |
| 568 | "def _transition_impl(settings, attr):", |
| 569 | " return {'//test:cute-animal-fact': settings['//test:cute-animal-fact']+' <- TRUE'}", |
| 570 | "my_transition = transition(", |
| 571 | " implementation = _transition_impl,", |
| 572 | " inputs = ['//test:cute-animal-fact'],", |
| 573 | " outputs = ['//test:cute-animal-fact']", |
| 574 | ")"); |
| 575 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 576 | |
| 577 | useConfiguration(ImmutableMap.of("//test:cute-animal-fact", "rats are ticklish")); |
| 578 | |
| 579 | BuildConfiguration configuration = getConfiguration(getConfiguredTarget("//test")); |
| 580 | assertThat( |
| 581 | configuration |
| 582 | .getOptions() |
| 583 | .getStarlarkOptions() |
| 584 | .get(Label.parseAbsoluteUnchecked("//test:cute-animal-fact"))) |
| 585 | .isEqualTo("rats are ticklish <- TRUE"); |
| 586 | } |
| 587 | |
| 588 | @Test |
| 589 | public void testTransitionReadsBuildSetting_notABuildSetting() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 590 | writeAllowlistFile(); |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 591 | scratch.file( |
| 592 | "test/transitions.bzl", |
| 593 | "def _transition_impl(settings, attr):", |
| 594 | " return {'//test:cute-animal-fact': 'puffins mate for life'}", |
| 595 | "my_transition = transition(", |
| 596 | " implementation = _transition_impl,", |
| 597 | " inputs = ['//test:cute-animal-fact'],", |
| 598 | " outputs = ['//test:cute-animal-fact']", |
| 599 | ")"); |
| 600 | scratch.file( |
| 601 | "test/rules.bzl", |
| 602 | "load('//test:transitions.bzl', 'my_transition')", |
| 603 | "def _rule_impl(ctx):", |
| 604 | " return []", |
| 605 | "my_rule = rule(", |
| 606 | " implementation = _rule_impl,", |
| 607 | " cfg = my_transition,", |
| 608 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 609 | " '_allowlist_function_transition': attr.label(", |
| 610 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 611 | " ),", |
| 612 | " },", |
| 613 | ")"); |
| 614 | scratch.file( |
| 615 | "test/build_settings.bzl", |
| 616 | "def _impl(ctx):", |
| 617 | " return []", |
| 618 | "non_build_setting = rule(implementation = _impl)"); |
| 619 | scratch.file( |
| 620 | "test/BUILD", |
| 621 | "load('//test:rules.bzl', 'my_rule')", |
| 622 | "load('//test:build_settings.bzl', 'non_build_setting')", |
| 623 | "my_rule(name = 'test')", |
| 624 | "non_build_setting(name = 'cute-animal-fact')"); |
| 625 | |
| 626 | reporter.removeHandler(failFastHandler); |
| 627 | getConfiguredTarget("//test"); |
| 628 | assertContainsEvent( |
| 629 | "attempting to transition on '//test:cute-animal-fact' which is not a build setting"); |
| 630 | } |
| 631 | |
| 632 | @Test |
| 633 | public void testTransitionReadsBuildSetting_noSuchTarget() 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):", |
| 637 | " return {'//test:cute-animal-fact': settings['//test:cute-animal-fact']+' <- TRUE'}", |
| 638 | "my_transition = transition(", |
| 639 | " implementation = _transition_impl,", |
| 640 | " inputs = ['//test:i-am-not-real'],", |
| 641 | " outputs = ['//test:cute-animal-fact']", |
juliexxia | 22de6cd | 2019-01-09 07:25:23 -0800 | [diff] [blame] | 642 | ")"); |
| 643 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 644 | |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 645 | reporter.removeHandler(failFastHandler); |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 646 | getConfiguredTarget("//test"); |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 647 | assertContainsEvent( |
juliexxia | 1a70c27 | 2019-04-09 09:33:23 -0700 | [diff] [blame] | 648 | "no such target '//test:i-am-not-real': target " |
| 649 | + "'i-am-not-real' not declared in package 'test'"); |
juliexxia | 922312f | 2019-03-12 08:44:33 -0700 | [diff] [blame] | 650 | } |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 651 | |
| 652 | @Test |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 653 | public void testAliasedBuildSetting() throws Exception { |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 654 | scratch.file( |
| 655 | "test/transitions.bzl", |
| 656 | "def _transition_impl(settings, attr):", |
| 657 | " return {'//test:fact': 'puffins mate for life'}", |
| 658 | "my_transition = transition(", |
| 659 | " implementation = _transition_impl,", |
| 660 | " inputs = [],", |
| 661 | " outputs = ['//test:fact']", |
| 662 | ")"); |
| 663 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 664 | scratch.overwriteFile( |
| 665 | "test/BUILD", |
| 666 | "load('//test:rules.bzl', 'my_rule')", |
| 667 | "load('//test:build_settings.bzl', 'string_flag')", |
| 668 | "my_rule(name = 'test')", |
| 669 | "alias(name = 'fact', actual = ':cute-animal-fact')", |
| 670 | "string_flag(", |
| 671 | " name = 'cute-animal-fact',", |
| 672 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
| 673 | ")"); |
| 674 | |
| 675 | useConfiguration(ImmutableMap.of("//test:cute-animal-fact", "rats are ticklish")); |
| 676 | |
| 677 | ImmutableMap<Label, Object> starlarkOptions = |
| 678 | getConfiguration(getConfiguredTarget("//test")).getOptions().getStarlarkOptions(); |
| 679 | assertThat(starlarkOptions.get(Label.parseAbsoluteUnchecked("//test:cute-animal-fact"))) |
| 680 | .isEqualTo("puffins mate for life"); |
| 681 | assertThat(starlarkOptions).doesNotContainKey(Label.parseAbsoluteUnchecked("//test:fact")); |
| 682 | assertThat(starlarkOptions.keySet()) |
| 683 | .containsExactly(Label.parseAbsoluteUnchecked("//test:cute-animal-fact")); |
| 684 | } |
| 685 | |
| 686 | @Test |
| 687 | public void testAliasedBuildSetting_chainedAliases() throws Exception { |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 688 | scratch.file( |
| 689 | "test/transitions.bzl", |
| 690 | "def _transition_impl(settings, attr):", |
| 691 | " return {'//test:fact': 'puffins mate for life'}", |
| 692 | "my_transition = transition(", |
| 693 | " implementation = _transition_impl,", |
| 694 | " inputs = [],", |
| 695 | " outputs = ['//test:fact']", |
| 696 | ")"); |
| 697 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 698 | scratch.overwriteFile( |
| 699 | "test/BUILD", |
| 700 | "load('//test:rules.bzl', 'my_rule')", |
| 701 | "load('//test:build_settings.bzl', 'string_flag')", |
| 702 | "my_rule(name = 'test')", |
| 703 | "alias(name = 'fact', actual = ':alias2')", |
| 704 | "alias(name = 'alias2', actual = ':cute-animal-fact')", |
| 705 | "string_flag(", |
| 706 | " name = 'cute-animal-fact',", |
| 707 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
| 708 | ")"); |
| 709 | |
| 710 | useConfiguration(ImmutableMap.of("//test:cute-animal-fact", "rats are ticklish")); |
| 711 | |
| 712 | BuildConfiguration configuration = getConfiguration(getConfiguredTarget("//test")); |
| 713 | assertThat( |
| 714 | configuration |
| 715 | .getOptions() |
| 716 | .getStarlarkOptions() |
| 717 | .get(Label.parseAbsoluteUnchecked("//test:cute-animal-fact"))) |
| 718 | .isEqualTo("puffins mate for life"); |
| 719 | } |
| 720 | |
| 721 | @Test |
| 722 | public void testAliasedBuildSetting_configuredActualValue() 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(", |
| 739 | " name = 'fact',", |
| 740 | " actual = select({", |
| 741 | " '//conditions:default': ':cute-animal-fact',", |
| 742 | " ':true-config': 'other-cute-animal-fact',", |
| 743 | " })", |
| 744 | ")", |
| 745 | "config_setting(", |
| 746 | " name = 'true-config',", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 747 | " values = {'foo': 'true'},", |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 748 | ")", |
| 749 | "string_flag(", |
| 750 | " name = 'cute-animal-fact',", |
| 751 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
| 752 | ")", |
| 753 | "string_flag(", |
| 754 | " name = 'other-cute-animal-fact',", |
| 755 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
| 756 | ")"); |
| 757 | |
| 758 | reporter.removeHandler(failFastHandler); |
| 759 | getConfiguredTarget("//test"); |
| 760 | assertContainsEvent( |
| 761 | "attempting to transition on aliased build setting '//test:fact', the actual value of" |
| 762 | + " which uses select()."); |
| 763 | } |
| 764 | |
| 765 | @Test |
| 766 | public void testAliasedBuildSetting_cyclicalAliases() throws Exception { |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 767 | scratch.file( |
| 768 | "test/transitions.bzl", |
| 769 | "def _transition_impl(settings, attr):", |
| 770 | " return {'//test:alias1': 'puffins mate for life'}", |
| 771 | "my_transition = transition(", |
| 772 | " implementation = _transition_impl,", |
| 773 | " inputs = [],", |
| 774 | " outputs = ['//test:alias1']", |
| 775 | ")"); |
| 776 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 777 | scratch.overwriteFile( |
| 778 | "test/BUILD", |
| 779 | "load('//test:rules.bzl', 'my_rule')", |
| 780 | "load('//test:build_settings.bzl', 'string_flag')", |
| 781 | "my_rule(name = 'test')", |
| 782 | "alias(name = 'alias1', actual = ':alias2')", |
| 783 | "alias(name = 'alias2', actual = ':alias1')"); |
| 784 | |
| 785 | reporter.removeHandler(failFastHandler); |
| 786 | getConfiguredTarget("//test"); |
| 787 | assertContainsEvent("Error with aliased build settings related to '//test:alias1'."); |
| 788 | } |
| 789 | |
| 790 | @Test |
| 791 | public void testAliasedBuildSetting_setAliasAndActual() 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 {", |
| 796 | " '//test:alias': 'puffins mate for life',", |
| 797 | " '//test:actual': 'cats cannot taste sugar',", |
| 798 | "}", |
| 799 | "my_transition = transition(", |
| 800 | " implementation = _transition_impl,", |
| 801 | " inputs = [],", |
| 802 | " outputs = [", |
| 803 | " '//test:alias',", |
| 804 | " '//test:actual',", |
| 805 | " ]", |
| 806 | ")"); |
| 807 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 808 | scratch.overwriteFile( |
| 809 | "test/BUILD", |
| 810 | "load('//test:rules.bzl', 'my_rule')", |
| 811 | "load('//test:build_settings.bzl', 'string_flag')", |
| 812 | "my_rule(name = 'test')", |
| 813 | "alias(name = 'alias', actual = ':actual')", |
| 814 | "string_flag(", |
| 815 | " name = 'actual',", |
| 816 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
| 817 | ")"); |
| 818 | |
| 819 | reporter.removeHandler(failFastHandler); |
| 820 | getConfiguredTarget("//test"); |
| 821 | assertContainsEvent("Error with aliased build settings related to '//test:actual'."); |
| 822 | } |
| 823 | |
| 824 | @Test |
| 825 | public void testAliasedBuildSetting_outputReturnMismatch() throws Exception { |
juliexxia | b63a4a9 | 2019-06-14 12:05:18 -0700 | [diff] [blame] | 826 | scratch.file( |
| 827 | "test/transitions.bzl", |
| 828 | "def _transition_impl(settings, attr):", |
| 829 | " return {", |
| 830 | " '//test:actual': 'cats cannot taste sugar',", |
| 831 | "}", |
| 832 | "my_transition = transition(", |
| 833 | " implementation = _transition_impl,", |
| 834 | " inputs = [],", |
| 835 | " outputs = [", |
| 836 | " '//test:alias',", |
| 837 | " ]", |
| 838 | ")"); |
| 839 | writeRulesBuildSettingsAndBUILDforBuildSettingTransitionTests(); |
| 840 | scratch.overwriteFile( |
| 841 | "test/BUILD", |
| 842 | "load('//test:rules.bzl', 'my_rule')", |
| 843 | "load('//test:build_settings.bzl', 'string_flag')", |
| 844 | "my_rule(name = 'test')", |
| 845 | "alias(name = 'alias', actual = ':actual')", |
| 846 | "string_flag(", |
| 847 | " name = 'actual',", |
| 848 | " build_setting_default = '" + CUTE_ANIMAL_DEFAULT + "',", |
| 849 | ")"); |
| 850 | |
| 851 | reporter.removeHandler(failFastHandler); |
| 852 | getConfiguredTarget("//test"); |
| 853 | assertContainsEvent("transition function returned undeclared output '//test:actual'"); |
| 854 | } |
| 855 | |
| 856 | @Test |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 857 | public void testOneParamTransitionFunctionApiFails() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 858 | writeAllowlistFile(); |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 859 | scratch.file( |
| 860 | "test/transitions.bzl", |
| 861 | "def _impl(settings):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 862 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 863 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 864 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 865 | scratch.file( |
| 866 | "test/rules.bzl", |
| 867 | "load('//test:transitions.bzl', 'my_transition')", |
| 868 | "def _impl(ctx):", |
| 869 | " return []", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 870 | "my_rule = rule(", |
| 871 | " implementation = _impl,", |
| 872 | " cfg = my_transition,", |
| 873 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 874 | " '_allowlist_function_transition': attr.label(", |
| 875 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 876 | " ),", |
| 877 | " },", |
| 878 | ")"); |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 879 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 880 | |
juliexxia | 03ff878 | 2019-03-20 13:03:27 -0700 | [diff] [blame] | 881 | reporter.removeHandler(failFastHandler); |
juliexxia | fb45b60 | 2019-04-01 12:10:30 -0700 | [diff] [blame] | 882 | getConfiguredTarget("//test"); |
adonovan | 40446b8 | 2020-04-22 06:59:42 -0700 | [diff] [blame] | 883 | assertContainsEvent("_impl() accepts no more than 1 positional argument but got 2"); |
juliexxia | 1a56824 | 2019-02-15 09:16:46 -0800 | [diff] [blame] | 884 | } |
juliexxia | ff93b19 | 2019-02-19 08:02:19 -0800 | [diff] [blame] | 885 | |
| 886 | @Test |
| 887 | public void testCannotTransitionOnExperimentalFlag() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 888 | writeAllowlistFile(); |
juliexxia | ff93b19 | 2019-02-19 08:02:19 -0800 | [diff] [blame] | 889 | scratch.file( |
| 890 | "test/transitions.bzl", |
| 891 | "def _impl(settings, attr):", |
juliexxia | d17649a | 2020-05-26 10:48:04 -0700 | [diff] [blame] | 892 | " return {'//command_line_option:experimental_something_something': True}", |
juliexxia | ff93b19 | 2019-02-19 08:02:19 -0800 | [diff] [blame] | 893 | "my_transition = transition(implementation = _impl, inputs = [],", |
juliexxia | d17649a | 2020-05-26 10:48:04 -0700 | [diff] [blame] | 894 | " outputs = ['//command_line_option:experimental_something_something'])"); |
juliexxia | ff93b19 | 2019-02-19 08:02:19 -0800 | [diff] [blame] | 895 | scratch.file( |
| 896 | "test/rules.bzl", |
| 897 | "load('//test:transitions.bzl', 'my_transition')", |
| 898 | "def _impl(ctx):", |
| 899 | " return []", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 900 | "my_rule = rule(", |
| 901 | " implementation = _impl,", |
| 902 | " cfg = my_transition,", |
| 903 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 904 | " '_allowlist_function_transition': attr.label(", |
| 905 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 906 | " ),", |
| 907 | " },", |
| 908 | ")"); |
juliexxia | ff93b19 | 2019-02-19 08:02:19 -0800 | [diff] [blame] | 909 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 910 | |
| 911 | reporter.removeHandler(failFastHandler); |
| 912 | getConfiguredTarget("//test"); |
| 913 | assertContainsEvent("Cannot transition on --experimental_* or --incompatible_* options"); |
| 914 | } |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 915 | |
| 916 | @Test |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 917 | public void testCannotTransitionWithoutAllowlist() throws Exception { |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 918 | scratch.file( |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 919 | "tools/allowlists/function_transition_allowlist/BUILD", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 920 | "package_group(", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 921 | " name = 'function_transition_allowlist',", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 922 | " packages = [],", |
| 923 | ")"); |
| 924 | scratch.file( |
| 925 | "test/transitions.bzl", |
| 926 | "def _impl(settings, attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 927 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 928 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 929 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 930 | scratch.file( |
| 931 | "test/rules.bzl", |
| 932 | "load('//test:transitions.bzl', 'my_transition')", |
| 933 | "def _impl(ctx):", |
| 934 | " return []", |
| 935 | "my_rule = rule(", |
| 936 | " implementation = _impl,", |
| 937 | " cfg = my_transition,", |
| 938 | ")"); |
| 939 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 940 | |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 941 | useConfiguration("--foo=pre-transition"); |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 942 | |
| 943 | reporter.removeHandler(failFastHandler); |
| 944 | getConfiguredTarget("//test"); |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 945 | assertContainsEvent("Use of Starlark transition without allowlist"); |
juliexxia | 30976d8 | 2019-03-12 10:59:26 -0700 | [diff] [blame] | 946 | } |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 947 | |
| 948 | @Test |
| 949 | public void testNoNullOptionValues() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 950 | writeAllowlistFile(); |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 951 | scratch.file( |
| 952 | "test/transitions.bzl", |
| 953 | "def _impl(settings, attr):", |
juliexxia | 6778a05 | 2019-04-09 08:25:05 -0700 | [diff] [blame] | 954 | " if settings['//command_line_option:nullable_option'] == None:", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 955 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 956 | " else:", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 957 | " return {'//command_line_option:foo': settings['//command_line_option:foo']}", |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 958 | "my_transition = transition(implementation = _impl,", |
| 959 | " inputs = [", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 960 | " '//command_line_option:foo',", |
juliexxia | 6778a05 | 2019-04-09 08:25:05 -0700 | [diff] [blame] | 961 | " '//command_line_option:nullable_option'", |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 962 | " ],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 963 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 964 | scratch.file( |
| 965 | "test/rules.bzl", |
| 966 | "load('//test:transitions.bzl', 'my_transition')", |
| 967 | "def _impl(ctx):", |
| 968 | " return []", |
| 969 | "my_rule = rule(", |
| 970 | " implementation = _impl,", |
| 971 | " cfg = my_transition,", |
| 972 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 973 | " '_allowlist_function_transition': attr.label(", |
| 974 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 975 | " ),", |
| 976 | " })"); |
| 977 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 978 | |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 979 | useConfiguration("--nullable_option=", "--foo=pre-transition"); |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 980 | |
| 981 | BuildConfiguration configuration = getConfiguration(getConfiguredTarget("//test")); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 982 | assertThat(configuration.getOptions().get(DummyTestOptions.class).foo) |
| 983 | .isEqualTo("post-transition"); |
juliexxia | c33f0c8 | 2019-03-25 16:37:41 -0700 | [diff] [blame] | 984 | } |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 985 | |
| 986 | @Test |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 987 | public void testAllowlistOnRuleNotTargets() throws Exception { |
| 988 | // allowlists //test/... |
| 989 | writeAllowlistFile(); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 990 | scratch.file( |
| 991 | "test/transitions.bzl", |
| 992 | "def _impl(settings, attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 993 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 994 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 995 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 996 | scratch.file( |
| 997 | "test/rules.bzl", |
| 998 | "load('//test:transitions.bzl', 'my_transition')", |
| 999 | "def _impl(ctx):", |
| 1000 | " return []", |
| 1001 | "my_rule = rule(", |
| 1002 | " implementation = _impl,", |
| 1003 | " cfg = my_transition,", |
| 1004 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1005 | " '_allowlist_function_transition': attr.label(", |
| 1006 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1007 | " ),", |
| 1008 | " })"); |
| 1009 | scratch.file( |
| 1010 | "neverland/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1011 | scratch.file("test/BUILD"); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 1012 | useConfiguration("--foo=pre-transition"); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1013 | |
| 1014 | BuildConfiguration configuration = getConfiguration(getConfiguredTarget("//neverland:test")); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 1015 | assertThat(configuration.getOptions().get(DummyTestOptions.class).foo) |
| 1016 | .isEqualTo("post-transition"); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Googler | fe8275d | 2021-02-05 16:36:38 -0800 | [diff] [blame] | 1019 | // TODO(blaze-configurability): We probably want to eventually turn this off. Flip this test when |
| 1020 | // this isn't allowed anymore. |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1021 | @Test |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1022 | public void testAllowlistOnTargetsStillWorks() throws Exception { |
| 1023 | // allowlists //test/... |
| 1024 | writeAllowlistFile(); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1025 | scratch.file( |
| 1026 | "neverland/transitions.bzl", |
| 1027 | "def _impl(settings, attr):", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 1028 | " return {'//command_line_option:foo': 'post-transition'}", |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1029 | "my_transition = transition(implementation = _impl, inputs = [],", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 1030 | " outputs = ['//command_line_option:foo'])"); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1031 | scratch.file( |
| 1032 | "neverland/rules.bzl", |
| 1033 | "load('//neverland:transitions.bzl', 'my_transition')", |
| 1034 | "def _impl(ctx):", |
| 1035 | " return []", |
| 1036 | "my_rule = rule(", |
| 1037 | " implementation = _impl,", |
| 1038 | " cfg = my_transition,", |
| 1039 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1040 | " '_allowlist_function_transition': attr.label(", |
| 1041 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1042 | " ),", |
| 1043 | " })"); |
| 1044 | scratch.file( |
| 1045 | "test/BUILD", "load('//neverland:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1046 | scratch.file("neverland/BUILD"); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 1047 | useConfiguration("--foo=pre-transition"); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1048 | |
| 1049 | BuildConfiguration configuration = getConfiguration(getConfiguredTarget("//test")); |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 1050 | assertThat(configuration.getOptions().get(DummyTestOptions.class).foo) |
| 1051 | .isEqualTo("post-transition"); |
juliexxia | d21fa42 | 2019-03-28 09:59:26 -0700 | [diff] [blame] | 1052 | } |
juliexxia | bdc6c10 | 2019-05-20 11:58:02 -0700 | [diff] [blame] | 1053 | |
| 1054 | /** |
| 1055 | * Regression test to ensure that an empty dict is not interpreted as a dict of dicts and |
| 1056 | * generates the proper error message. |
| 1057 | */ |
| 1058 | @Test |
| 1059 | public void testEmptyReturnDict() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1060 | writeAllowlistFile(); |
juliexxia | bdc6c10 | 2019-05-20 11:58:02 -0700 | [diff] [blame] | 1061 | scratch.file( |
| 1062 | "test/transitions.bzl", |
| 1063 | "def _impl(settings, attr):", |
| 1064 | " return {}", |
| 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 | bdc6c10 | 2019-05-20 11:58:02 -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 | bdc6c10 | 2019-05-20 11:58:02 -0700 | [diff] [blame] | 1078 | " ),", |
| 1079 | " })"); |
| 1080 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1081 | |
| 1082 | reporter.removeHandler(failFastHandler); |
| 1083 | getConfiguredTarget("//test"); |
| 1084 | assertContainsEvent( |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 1085 | "transition outputs [//command_line_option:foo] were " |
juliexxia | bdc6c10 | 2019-05-20 11:58:02 -0700 | [diff] [blame] | 1086 | + "not defined by transition function"); |
| 1087 | } |
gregce | f0a40ac | 2020-03-31 14:11:30 -0700 | [diff] [blame] | 1088 | |
| 1089 | @Test |
| 1090 | public void composingTransitionReportsAllStarlarkErrors() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1091 | writeAllowlistFile(); |
gregce | f0a40ac | 2020-03-31 14:11:30 -0700 | [diff] [blame] | 1092 | scratch.file( |
| 1093 | "test/build_settings.bzl", |
| 1094 | "def _impl(ctx):", |
| 1095 | " return []", |
| 1096 | "string_flag = rule(implementation = _impl, build_setting = config.string(flag=True))"); |
| 1097 | scratch.file( |
| 1098 | "test/transitions.bzl", |
| 1099 | "def _impl(settings, attr):", |
| 1100 | " return {}", |
| 1101 | "attr_transition = transition(implementation = _impl, inputs = [],", |
| 1102 | " outputs = ['//test:attr_transition_output_flag'])", |
| 1103 | "self_transition = transition(implementation = _impl, inputs = [],", |
| 1104 | " outputs = ['//test:self_transition_output_flag'])"); |
| 1105 | scratch.file( |
| 1106 | "test/rules.bzl", |
| 1107 | "load('//test:transitions.bzl', 'attr_transition', 'self_transition')", |
| 1108 | "def _impl(ctx):", |
| 1109 | " return []", |
| 1110 | "rule_with_attr_transition = rule(", |
| 1111 | " implementation = _impl,", |
| 1112 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1113 | " '_allowlist_function_transition': attr.label(", |
| 1114 | " default = '//tools/allowlists/function_transition_allowlist'),", |
gregce | f0a40ac | 2020-03-31 14:11:30 -0700 | [diff] [blame] | 1115 | " 'deps': attr.label_list(cfg = attr_transition),", |
| 1116 | " })", |
| 1117 | "rule_with_self_transition = rule(", |
| 1118 | " implementation = _impl,", |
| 1119 | " cfg = self_transition,", |
| 1120 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1121 | " '_allowlist_function_transition': attr.label(", |
| 1122 | " default = '//tools/allowlists/function_transition_allowlist'),", |
gregce | f0a40ac | 2020-03-31 14:11:30 -0700 | [diff] [blame] | 1123 | " })"); |
| 1124 | scratch.file( |
| 1125 | "test/BUILD", |
| 1126 | "load('//test:rules.bzl', 'rule_with_attr_transition', 'rule_with_self_transition')", |
| 1127 | "load('//test:build_settings.bzl', 'string_flag')", |
| 1128 | "string_flag(name = 'attr_transition_output_flag', build_setting_default='')", |
| 1129 | "string_flag(name = 'self_transition_output_flag', build_setting_default='')", |
| 1130 | "rule_with_attr_transition(name = 'buildme', deps = [':adep'])", |
| 1131 | "rule_with_self_transition(name = 'adep')"); |
| 1132 | |
| 1133 | reporter.removeHandler(failFastHandler); |
| 1134 | getConfiguredTarget("//test:buildme"); |
| 1135 | assertContainsEvent( |
| 1136 | "transition outputs [//test:attr_transition_output_flag] were not defined by transition " |
| 1137 | + "function"); |
| 1138 | assertContainsEvent( |
| 1139 | "transition outputs [//test:self_transition_output_flag] were not defined by transition " |
| 1140 | + "function"); |
| 1141 | } |
juliexxia | bf57d6b | 2020-04-14 07:36:59 -0700 | [diff] [blame] | 1142 | |
| 1143 | @Test |
| 1144 | public void testTransitionOnDefine() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1145 | writeAllowlistFile(); |
juliexxia | bf57d6b | 2020-04-14 07:36:59 -0700 | [diff] [blame] | 1146 | scratch.file( |
| 1147 | "test/transitions.bzl", |
| 1148 | "def _impl(settings, attr):", |
| 1149 | " return {'//command_line_option:define': 'chonky=true'}", |
| 1150 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1151 | " outputs = ['//command_line_option:define'])"); |
| 1152 | scratch.file( |
| 1153 | "test/rules.bzl", |
| 1154 | "load('//test:transitions.bzl', 'my_transition')", |
| 1155 | "def _impl(ctx):", |
| 1156 | " return []", |
| 1157 | "my_rule = rule(", |
| 1158 | " implementation = _impl,", |
| 1159 | " cfg = my_transition,", |
| 1160 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1161 | " '_allowlist_function_transition': attr.label(", |
| 1162 | " default = '//tools/allowlists/function_transition_allowlist',", |
juliexxia | bf57d6b | 2020-04-14 07:36:59 -0700 | [diff] [blame] | 1163 | " ),", |
| 1164 | " })"); |
| 1165 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1166 | |
| 1167 | reporter.removeHandler(failFastHandler); |
| 1168 | getConfiguredTarget("//test"); |
| 1169 | assertContainsEvent("Starlark transition on --define not supported - try using build settings"); |
| 1170 | } |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1171 | |
| 1172 | @Test |
| 1173 | public void successfulTypeConversionOfNativeListOption() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1174 | writeAllowlistFile(); |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1175 | scratch.file( |
| 1176 | "test/transitions.bzl", |
| 1177 | "def _impl(settings, attr):", |
| 1178 | " return {'//command_line_option:platforms': ['//test:my_platform']}", |
| 1179 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1180 | " outputs = ['//command_line_option:platforms'])"); |
| 1181 | scratch.file( |
| 1182 | "test/rules.bzl", |
| 1183 | "load('//test:transitions.bzl', 'my_transition')", |
| 1184 | "def _impl(ctx):", |
| 1185 | " return []", |
| 1186 | "my_rule = rule(", |
| 1187 | " implementation = _impl,", |
| 1188 | " cfg = my_transition,", |
| 1189 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1190 | " '_allowlist_function_transition': attr.label(", |
| 1191 | " default = '//tools/allowlists/function_transition_allowlist',", |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1192 | " ),", |
| 1193 | " })"); |
| 1194 | scratch.file( |
| 1195 | "test/BUILD", |
| 1196 | "load('//test:rules.bzl', 'my_rule')", |
| 1197 | "platform(name = 'my_platform')", |
| 1198 | "my_rule(name = 'test')"); |
| 1199 | |
| 1200 | getConfiguredTarget("//test"); |
| 1201 | assertNoEvents(); |
| 1202 | } |
| 1203 | |
juliexxia | 1cf3746 | 2020-10-20 14:48:10 -0700 | [diff] [blame] | 1204 | // Regression test for b/170729565 |
| 1205 | @Test |
| 1206 | public void testSetBooleanNativeOptionWithStarlarkBoolean() throws Exception { |
| 1207 | writeAllowlistFile(); |
| 1208 | scratch.file( |
| 1209 | "test/transitions.bzl", |
| 1210 | "def _impl(settings, attr):", |
| 1211 | " return {'//command_line_option:bool': True}", |
| 1212 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1213 | " outputs = ['//command_line_option:bool'])"); |
| 1214 | scratch.file( |
| 1215 | "test/rules.bzl", |
| 1216 | "load('//test:transitions.bzl', 'my_transition')", |
| 1217 | "def _impl(ctx):", |
| 1218 | " return []", |
| 1219 | "my_rule = rule(", |
| 1220 | " implementation = _impl,", |
| 1221 | " cfg = my_transition,", |
| 1222 | " attrs = {", |
| 1223 | " '_allowlist_function_transition': attr.label(", |
| 1224 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1225 | " ),", |
| 1226 | " })"); |
| 1227 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1228 | useConfiguration("--bool=false"); |
| 1229 | ConfiguredTarget ct = getConfiguredTarget("//test"); |
| 1230 | assertNoEvents(); |
| 1231 | assertThat(getConfiguration(ct).getOptions().get(DummyTestOptions.class).bool).isTrue(); |
| 1232 | } |
| 1233 | |
| 1234 | // Regression test for b/170729565 |
| 1235 | @Test |
| 1236 | public void testSetBooleanNativeOptionWithItself() throws Exception { |
| 1237 | writeAllowlistFile(); |
| 1238 | scratch.file( |
| 1239 | "test/transitions.bzl", |
| 1240 | "def _impl(settings, attr):", |
| 1241 | " return {'//command_line_option:bool': settings['//command_line_option:bool']}", |
| 1242 | "my_transition = transition(implementation = _impl,", |
| 1243 | " inputs = ['//command_line_option:bool'],", |
| 1244 | " outputs = ['//command_line_option:bool'])"); |
| 1245 | scratch.file( |
| 1246 | "test/rules.bzl", |
| 1247 | "load('//test:transitions.bzl', 'my_transition')", |
| 1248 | "def _impl(ctx):", |
| 1249 | " return []", |
| 1250 | "my_rule = rule(", |
| 1251 | " implementation = _impl,", |
| 1252 | " cfg = my_transition,", |
| 1253 | " attrs = {", |
| 1254 | " '_allowlist_function_transition': attr.label(", |
| 1255 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1256 | " ),", |
| 1257 | " })"); |
| 1258 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1259 | useConfiguration("--bool=false"); |
| 1260 | ConfiguredTarget ct = getConfiguredTarget("//test"); |
| 1261 | assertNoEvents(); |
| 1262 | assertThat(getConfiguration(ct).getOptions().get(DummyTestOptions.class).bool).isFalse(); |
| 1263 | } |
| 1264 | |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1265 | @Test |
| 1266 | public void failedTypeConversionOfNativeListOption() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1267 | writeAllowlistFile(); |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1268 | scratch.file( |
| 1269 | "test/transitions.bzl", |
| 1270 | "def _impl(settings, attr):", |
| 1271 | " return {'//command_line_option:platforms': ['this is not a valid label::']}", |
| 1272 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1273 | " outputs = ['//command_line_option:platforms'])"); |
| 1274 | scratch.file( |
| 1275 | "test/rules.bzl", |
| 1276 | "load('//test:transitions.bzl', 'my_transition')", |
| 1277 | "def _impl(ctx):", |
| 1278 | " return []", |
| 1279 | "my_rule = rule(", |
| 1280 | " implementation = _impl,", |
| 1281 | " cfg = my_transition,", |
| 1282 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1283 | " '_allowlist_function_transition': attr.label(", |
| 1284 | " default = '//tools/allowlists/function_transition_allowlist',", |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1285 | " ),", |
| 1286 | " })"); |
| 1287 | scratch.file( |
| 1288 | "test/BUILD", |
| 1289 | "load('//test:rules.bzl', 'my_rule')", |
| 1290 | "platform(name = 'my_platform')", |
| 1291 | "my_rule(name = 'test')"); |
| 1292 | |
| 1293 | reporter.removeHandler(failFastHandler); |
| 1294 | getConfiguredTarget("//test"); |
| 1295 | assertContainsEvent("invalid target name ':': target names may not contain ':'"); |
| 1296 | } |
| 1297 | |
| 1298 | @Test |
| 1299 | public void successfulTypeConversionOfNativeListOptionEmptyList() throws Exception { |
aiuto | 50226ac | 2020-06-14 21:28:27 -0700 | [diff] [blame] | 1300 | writeAllowlistFile(); |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1301 | scratch.file( |
| 1302 | "test/transitions.bzl", |
| 1303 | "def _impl(settings, attr):", |
| 1304 | " return {'//command_line_option:fission': []}", |
| 1305 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1306 | " outputs = ['//command_line_option:fission'])"); |
| 1307 | scratch.file( |
| 1308 | "test/rules.bzl", |
| 1309 | "load('//test:transitions.bzl', 'my_transition')", |
| 1310 | "def _impl(ctx):", |
| 1311 | " return []", |
| 1312 | "my_rule = rule(", |
| 1313 | " implementation = _impl,", |
| 1314 | " cfg = my_transition,", |
| 1315 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1316 | " '_allowlist_function_transition': attr.label(", |
| 1317 | " default = '//tools/allowlists/function_transition_allowlist',", |
gregce | 7fc9ae7 | 2020-04-24 10:54:14 -0700 | [diff] [blame] | 1318 | " ),", |
| 1319 | " })"); |
| 1320 | scratch.file( |
| 1321 | "test/BUILD", |
| 1322 | "load('//test:rules.bzl', 'my_rule')", |
| 1323 | "platform(name = 'my_platform')", |
| 1324 | "my_rule(name = 'test')"); |
| 1325 | |
| 1326 | ConfiguredTarget ct = getConfiguredTarget("//test"); |
| 1327 | assertNoEvents(); |
| 1328 | assertThat(getConfiguration(ct).getOptions().get(CppOptions.class).fissionModes).isEmpty(); |
| 1329 | } |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 1330 | |
| 1331 | @Test |
| 1332 | public void starlarkPatchTransitionRequiredFragments() throws Exception { |
| 1333 | // All Starlark rule transitions are patch transitions, while all Starlark attribute transitions |
| 1334 | // are split transitions. |
| 1335 | writeAllowlistFile(); |
| 1336 | scratch.file( |
| 1337 | "test/transitions.bzl", |
| 1338 | "def _impl(settings, attr):", |
| 1339 | " return {'//command_line_option:copt': []}", // --copt is a C++ option. |
| 1340 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1341 | " outputs = ['//command_line_option:copt'])"); |
| 1342 | scratch.file( |
| 1343 | "test/rules.bzl", |
| 1344 | "load('//test:transitions.bzl', 'my_transition')", |
| 1345 | "def _impl(ctx):", |
| 1346 | " return []", |
| 1347 | "my_rule = rule(", |
| 1348 | " implementation = _impl,", |
| 1349 | " cfg = my_transition,", |
| 1350 | " attrs = {", |
cushon | 1812caf | 2020-06-24 10:54:19 -0700 | [diff] [blame] | 1351 | " '_allowlist_function_transition': attr.label(", |
| 1352 | " default = '//tools/allowlists/function_transition_allowlist',", |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 1353 | " ),", |
| 1354 | " })"); |
| 1355 | scratch.file( |
| 1356 | "test/BUILD", |
| 1357 | "load('//test:rules.bzl', 'my_rule')", |
| 1358 | "platform(name = 'my_platform')", |
| 1359 | "my_rule(name = 'test')"); |
| 1360 | |
| 1361 | ConfiguredTargetAndData ct = getConfiguredTargetAndData("//test"); |
| 1362 | assertNoEvents(); |
| 1363 | Rule testTarget = (Rule) ct.getTarget(); |
| 1364 | ConfigurationTransition ruleTransition = |
| 1365 | testTarget.getRuleClassObject().getTransitionFactory().create(testTarget); |
| 1366 | assertThat(ruleTransition.requiresOptionFragments(ct.getConfiguration().getOptions())) |
gregce | 24e09f7 | 2020-07-01 15:18:36 -0700 | [diff] [blame] | 1367 | .containsExactly("CppOptions"); |
gregce | 06b76c9 | 2020-06-19 14:46:56 -0700 | [diff] [blame] | 1368 | } |
janakr | ca6209f | 2020-11-13 19:17:27 -0800 | [diff] [blame] | 1369 | |
| 1370 | /** |
| 1371 | * Unit test for an invalid output directory from a mnemonic via a dep transition. Integration |
| 1372 | * test for top-level transition in //src/test/shell/integration:starlark_configurations_test# |
| 1373 | * test_invalid_mnemonic_from_transition_top_level. Has to be an integration test because the |
| 1374 | * error is emitted in BuildTool. |
| 1375 | */ |
| 1376 | @Test |
| 1377 | public void invalidMnemonicFromDepTransition() throws Exception { |
| 1378 | writeAllowlistFile(); |
| 1379 | scratch.file( |
| 1380 | "test/transitions.bzl", |
| 1381 | "def _impl(settings, attr):", |
| 1382 | " return {'//command_line_option:cpu': '//bad:cpu'}", |
| 1383 | "my_transition = transition(implementation = _impl, inputs = [],", |
| 1384 | " outputs = ['//command_line_option:cpu'])"); |
| 1385 | scratch.file( |
| 1386 | "test/rules.bzl", |
| 1387 | "load('//test:transitions.bzl', 'my_transition')", |
| 1388 | "def _impl(ctx):", |
| 1389 | " return []", |
| 1390 | "my_rule = rule(", |
| 1391 | " implementation = _impl,", |
| 1392 | " cfg = my_transition,", |
| 1393 | " attrs = {", |
| 1394 | " '_allowlist_function_transition': attr.label(", |
| 1395 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1396 | " ),", |
| 1397 | " })"); |
| 1398 | scratch.file( |
| 1399 | "test/BUILD", |
| 1400 | "load('//test:rules.bzl', 'my_rule')", |
| 1401 | "my_rule(name = 'bottom')", |
| 1402 | "genrule(name = 'test', srcs = [':bottom'], outs = ['out'], cmd = 'touch $@')"); |
| 1403 | reporter.removeHandler(failFastHandler); |
| 1404 | assertThat(getConfiguredTarget("//test:test")).isNull(); |
| 1405 | assertContainsEvent( |
| 1406 | "Output directory name '//bad:cpu' specified by CppConfiguration is invalid as part of a " |
| 1407 | + "path: must not contain /"); |
| 1408 | } |
juliexxia | a13f590 | 2020-12-10 09:19:01 -0800 | [diff] [blame] | 1409 | |
| 1410 | @Test |
| 1411 | public void testTransitionOnAllowMultiplesBuildSettingRequiresList() throws Exception { |
juliexxia | a13f590 | 2020-12-10 09:19:01 -0800 | [diff] [blame] | 1412 | scratch.file( |
| 1413 | "test/transitions.bzl", |
| 1414 | "def _transition_impl(settings, attr):", |
| 1415 | " return {'//test:cute-animal-fact': 'puffins mate for life'}", |
| 1416 | "my_transition = transition(", |
| 1417 | " implementation = _transition_impl,", |
| 1418 | " inputs = [],", |
| 1419 | " outputs = ['//test:cute-animal-fact']", |
| 1420 | ")"); |
| 1421 | writeAllowlistFile(); |
| 1422 | scratch.file( |
| 1423 | "test/rules.bzl", |
| 1424 | "load('//test:transitions.bzl', 'my_transition')", |
| 1425 | "def _rule_impl(ctx):", |
| 1426 | " return []", |
| 1427 | "my_rule = rule(", |
| 1428 | " implementation = _rule_impl,", |
| 1429 | " cfg = my_transition,", |
| 1430 | " attrs = {", |
| 1431 | " '_allowlist_function_transition': attr.label(", |
| 1432 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1433 | " ),", |
| 1434 | " },", |
| 1435 | ")"); |
| 1436 | scratch.file( |
| 1437 | "test/build_settings.bzl", |
| 1438 | "def _impl(ctx):", |
| 1439 | " return []", |
| 1440 | "string_flag = rule(implementation = _impl, build_setting = config.string(flag=True," |
| 1441 | + " allow_multiple=True))"); |
| 1442 | scratch.file( |
| 1443 | "test/BUILD", |
| 1444 | "load('//test:rules.bzl', 'my_rule')", |
| 1445 | "load('//test:build_settings.bzl', 'string_flag')", |
| 1446 | "my_rule(name = 'test')", |
| 1447 | "string_flag(", |
| 1448 | " name = 'cute-animal-fact',", |
| 1449 | " build_setting_default = \"cats can't taste sugar\",", |
| 1450 | ")"); |
| 1451 | |
| 1452 | reporter.removeHandler(failFastHandler); |
| 1453 | getConfiguredTarget("//test"); |
| 1454 | assertContainsEvent( |
| 1455 | "'//test:cute-animal-fact' allows multiple values and must be set in transition using a" |
| 1456 | + " starlark list instead of single value"); |
| 1457 | } |
| 1458 | |
| 1459 | @Test |
| 1460 | public void testTransitionOnAllowMultiplesBuildSetting() throws Exception { |
juliexxia | a13f590 | 2020-12-10 09:19:01 -0800 | [diff] [blame] | 1461 | scratch.file( |
| 1462 | "test/transitions.bzl", |
| 1463 | "def _transition_impl(settings, attr):", |
| 1464 | " return {'//test:cute-animal-fact': ['puffins mate for life']}", |
| 1465 | "my_transition = transition(", |
| 1466 | " implementation = _transition_impl,", |
| 1467 | " inputs = [],", |
| 1468 | " outputs = ['//test:cute-animal-fact']", |
| 1469 | ")"); |
| 1470 | writeAllowlistFile(); |
| 1471 | scratch.file( |
| 1472 | "test/rules.bzl", |
| 1473 | "load('//test:transitions.bzl', 'my_transition')", |
| 1474 | "def _rule_impl(ctx):", |
| 1475 | " return []", |
| 1476 | "my_rule = rule(", |
| 1477 | " implementation = _rule_impl,", |
| 1478 | " cfg = my_transition,", |
| 1479 | " attrs = {", |
| 1480 | " '_allowlist_function_transition': attr.label(", |
| 1481 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1482 | " ),", |
| 1483 | " },", |
| 1484 | ")"); |
| 1485 | scratch.file( |
| 1486 | "test/build_settings.bzl", |
| 1487 | "def _impl(ctx):", |
| 1488 | " return []", |
| 1489 | "string_flag = rule(implementation = _impl, build_setting = config.string(flag=True," |
| 1490 | + " allow_multiple=True))"); |
| 1491 | scratch.file( |
| 1492 | "test/BUILD", |
| 1493 | "load('//test:rules.bzl', 'my_rule')", |
| 1494 | "load('//test:build_settings.bzl', 'string_flag')", |
| 1495 | "my_rule(name = 'test')", |
| 1496 | "string_flag(", |
| 1497 | " name = 'cute-animal-fact',", |
| 1498 | " build_setting_default = \"cats can't taste sugar\",", |
| 1499 | ")"); |
| 1500 | |
| 1501 | Map<Label, Object> starlarkOptions = |
| 1502 | getConfiguration(getConfiguredTarget("//test")).getOptions().getStarlarkOptions(); |
| 1503 | assertNoEvents(); |
| 1504 | assertThat( |
| 1505 | (List<?>) starlarkOptions.get(Label.parseAbsoluteUnchecked("//test:cute-animal-fact"))) |
| 1506 | .containsExactly("puffins mate for life"); |
| 1507 | } |
gregce | d679546 | 2020-12-14 11:06:45 -0800 | [diff] [blame] | 1508 | |
| 1509 | /** |
| 1510 | * Changing --cpu implicitly changes the target platform. Test that the old value of --platforms |
| 1511 | * gets cleared out (platform mappings can then kick in to set --platforms correctly). |
| 1512 | */ |
| 1513 | @Test |
| 1514 | public void testImplicitPlatformsChange() throws Exception { |
| 1515 | scratch.file("platforms/BUILD", "platform(name = 'my_platform', constraint_values = [])"); |
| 1516 | scratch.file( |
| 1517 | "test/transitions.bzl", |
| 1518 | "def _transition_impl(settings, attr):", |
| 1519 | " return {'//command_line_option:cpu': 'ppc'}", |
| 1520 | "my_transition = transition(", |
| 1521 | " implementation = _transition_impl,", |
| 1522 | " inputs = [],", |
| 1523 | " outputs = ['//command_line_option:cpu']", |
| 1524 | ")"); |
| 1525 | writeAllowlistFile(); |
| 1526 | scratch.file( |
| 1527 | "test/rules.bzl", |
| 1528 | "load('//test:transitions.bzl', 'my_transition')", |
| 1529 | "def _rule_impl(ctx):", |
| 1530 | " return []", |
| 1531 | "my_rule = rule(", |
| 1532 | " implementation = _rule_impl,", |
| 1533 | " cfg = my_transition,", |
| 1534 | " attrs = {", |
| 1535 | " '_allowlist_function_transition': attr.label(", |
| 1536 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1537 | " ),", |
| 1538 | " },", |
| 1539 | ")"); |
| 1540 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1541 | |
| 1542 | useConfiguration("--platforms=//platforms:my_platform"); |
| 1543 | // When --platforms is empty and no platform mapping triggers, PlatformMappingValue sets |
| 1544 | // --platforms to PlatformOptions.computeTargetPlatform(), which defaults to the host. |
| 1545 | assertThat( |
| 1546 | getConfiguration(getConfiguredTarget("//test:test")) |
| 1547 | .getOptions() |
| 1548 | .get(PlatformOptions.class) |
| 1549 | .platforms) |
| 1550 | .containsExactly( |
| 1551 | Label.parseAbsoluteUnchecked(TestConstants.PLATFORM_PACKAGE_ROOT + ":default_host")); |
| 1552 | } |
| 1553 | |
| 1554 | @Test |
| 1555 | public void testExplicitPlatformsChange() throws Exception { |
| 1556 | scratch.file( |
| 1557 | "platforms/BUILD", |
| 1558 | "platform(name = 'my_platform', constraint_values = [])", |
| 1559 | "platform(name = 'my_other_platform', constraint_values = [])"); |
| 1560 | scratch.file( |
| 1561 | "test/transitions.bzl", |
| 1562 | "def _transition_impl(settings, attr):", |
| 1563 | " return {", |
| 1564 | " '//command_line_option:cpu': 'ppc',", |
| 1565 | " '//command_line_option:platforms': ['//platforms:my_other_platform']", |
| 1566 | " }", |
| 1567 | "my_transition = transition(", |
| 1568 | " implementation = _transition_impl,", |
| 1569 | " inputs = [],", |
| 1570 | " outputs = [", |
| 1571 | " '//command_line_option:cpu',", |
| 1572 | " '//command_line_option:platforms'", |
| 1573 | " ]", |
| 1574 | ")"); |
| 1575 | writeAllowlistFile(); |
| 1576 | scratch.file( |
| 1577 | "test/rules.bzl", |
| 1578 | "load('//test:transitions.bzl', 'my_transition')", |
| 1579 | "def _rule_impl(ctx):", |
| 1580 | " return []", |
| 1581 | "my_rule = rule(", |
| 1582 | " implementation = _rule_impl,", |
| 1583 | " cfg = my_transition,", |
| 1584 | " attrs = {", |
| 1585 | " '_allowlist_function_transition': attr.label(", |
| 1586 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1587 | " ),", |
| 1588 | " },", |
| 1589 | ")"); |
| 1590 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1591 | |
| 1592 | useConfiguration("--platforms=//platforms:my_platform"); |
| 1593 | assertThat( |
| 1594 | getConfiguration(getConfiguredTarget("//test:test")) |
| 1595 | .getOptions() |
| 1596 | .get(PlatformOptions.class) |
| 1597 | .platforms) |
| 1598 | .containsExactly(Label.parseAbsoluteUnchecked("//platforms:my_other_platform")); |
| 1599 | } |
| 1600 | |
| 1601 | /* If the transition doesn't change --cpu, it doesn't constitute a platform change. */ |
| 1602 | @Test |
| 1603 | public void testNoPlatformChange() throws Exception { |
| 1604 | scratch.file("platforms/BUILD", "platform(name = 'my_platform', constraint_values = [])"); |
| 1605 | scratch.file( |
| 1606 | "test/transitions.bzl", |
| 1607 | "def _transition_impl(settings, attr):", |
| 1608 | " return {", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 1609 | " '//command_line_option:foo': 'blah',", |
gregce | d679546 | 2020-12-14 11:06:45 -0800 | [diff] [blame] | 1610 | " }", |
| 1611 | "my_transition = transition(", |
| 1612 | " implementation = _transition_impl,", |
| 1613 | " inputs = [],", |
| 1614 | " outputs = [", |
twigg | 4970e0f | 2021-03-31 15:34:49 -0700 | [diff] [blame^] | 1615 | " '//command_line_option:foo',", |
gregce | d679546 | 2020-12-14 11:06:45 -0800 | [diff] [blame] | 1616 | " ]", |
| 1617 | ")"); |
| 1618 | writeAllowlistFile(); |
| 1619 | scratch.file( |
| 1620 | "test/rules.bzl", |
| 1621 | "load('//test:transitions.bzl', 'my_transition')", |
| 1622 | "def _rule_impl(ctx):", |
| 1623 | " return []", |
| 1624 | "my_rule = rule(", |
| 1625 | " implementation = _rule_impl,", |
| 1626 | " cfg = my_transition,", |
| 1627 | " attrs = {", |
| 1628 | " '_allowlist_function_transition': attr.label(", |
| 1629 | " default = '//tools/allowlists/function_transition_allowlist',", |
| 1630 | " ),", |
| 1631 | " },", |
| 1632 | ")"); |
| 1633 | scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')"); |
| 1634 | |
| 1635 | useConfiguration("--platforms=//platforms:my_platform"); |
| 1636 | assertThat( |
| 1637 | getConfiguration(getConfiguredTarget("//test:test")) |
| 1638 | .getOptions() |
| 1639 | .get(PlatformOptions.class) |
| 1640 | .platforms) |
| 1641 | .containsExactly(Label.parseAbsoluteUnchecked("//platforms:my_platform")); |
| 1642 | } |
juliexxia | deb028e | 2019-01-05 17:19:21 -0800 | [diff] [blame] | 1643 | } |