Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 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 | |
| 15 | package com.google.devtools.build.lib.skylark; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 18 | import static org.junit.Assert.assertThrows; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 19 | |
| 20 | import com.google.common.base.Joiner; |
michajlo | 0a89cef | 2020-04-06 12:04:12 -0700 | [diff] [blame] | 21 | import com.google.common.base.Strings; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableMap; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 24 | import com.google.common.collect.Iterables; |
gregce | 7fa23ea | 2018-01-18 12:46:04 -0800 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.config.transitions.NoTransition; |
gregce | eefc91c | 2020-06-19 13:33:43 -0700 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.starlark.StarlarkAttrModule; |
| 27 | import com.google.devtools.build.lib.analysis.starlark.StarlarkRuleClassFunctions.StarlarkRuleFunction; |
| 28 | import com.google.devtools.build.lib.analysis.starlark.StarlarkRuleContext; |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.cmdline.Label; |
adonovan | bc74d1d | 2020-04-30 12:08:31 -0700 | [diff] [blame] | 31 | import com.google.devtools.build.lib.collect.nestedset.Depset; |
michajlo | b839a51 | 2020-03-11 10:04:23 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.events.Event; |
| 33 | import com.google.devtools.build.lib.events.EventKind; |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.packages.AdvertisedProviderSet; |
Dmitry Lomov | 0692c7f | 2016-09-30 16:43:30 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.packages.AspectParameters; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.packages.Attribute; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.packages.BuildType; |
juliexxia | 44e2143 | 2020-03-31 08:21:20 -0700 | [diff] [blame] | 38 | import com.google.devtools.build.lib.packages.ExecGroup; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.packages.ImplicitOutputsFunction; |
| 40 | import com.google.devtools.build.lib.packages.PredicateWithMessage; |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.packages.RequiredProviders; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 42 | import com.google.devtools.build.lib.packages.RuleClass; |
| 43 | import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType; |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 44 | import com.google.devtools.build.lib.packages.StarlarkAspectClass; |
gregce | d281df7 | 2020-05-11 12:27:06 -0700 | [diff] [blame] | 45 | import com.google.devtools.build.lib.packages.StarlarkDefinedAspect; |
gregce | d7c1cef | 2020-05-12 07:51:48 -0700 | [diff] [blame] | 46 | import com.google.devtools.build.lib.packages.StarlarkInfo; |
gregce | b6eafee | 2020-04-20 08:04:51 -0700 | [diff] [blame] | 47 | import com.google.devtools.build.lib.packages.StarlarkProvider; |
gregce | 74d84d4 | 2020-04-17 10:02:03 -0700 | [diff] [blame] | 48 | import com.google.devtools.build.lib.packages.StarlarkProviderIdentifier; |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 49 | import com.google.devtools.build.lib.packages.StructImpl; |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 50 | import com.google.devtools.build.lib.packages.StructProvider; |
Googler | c5fcc86 | 2019-09-06 16:17:47 -0700 | [diff] [blame] | 51 | import com.google.devtools.build.lib.packages.Type; |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 52 | import com.google.devtools.build.lib.skyframe.BzlLoadFunction; |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 53 | import com.google.devtools.build.lib.skylark.util.BazelEvaluationTestCase; |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 54 | import com.google.devtools.build.lib.syntax.ClassObject; |
Googler | a9c9363 | 2019-11-13 10:48:07 -0800 | [diff] [blame] | 55 | import com.google.devtools.build.lib.syntax.Dict; |
laurentlb | 707acfe | 2018-04-13 06:09:30 -0700 | [diff] [blame] | 56 | import com.google.devtools.build.lib.syntax.EvalException; |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 57 | import com.google.devtools.build.lib.syntax.EvalUtils; |
adonovan | 034220a | 2020-03-24 10:11:26 -0700 | [diff] [blame] | 58 | import com.google.devtools.build.lib.syntax.FileOptions; |
| 59 | import com.google.devtools.build.lib.syntax.Module; |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 60 | import com.google.devtools.build.lib.syntax.Mutability; |
Googler | 2abde27 | 2019-09-17 12:06:08 -0700 | [diff] [blame] | 61 | import com.google.devtools.build.lib.syntax.ParserInput; |
Googler | 66d099e | 2019-09-26 08:07:06 -0700 | [diff] [blame] | 62 | import com.google.devtools.build.lib.syntax.StarlarkFile; |
Googler | 942e1c4 | 2019-11-12 13:11:44 -0800 | [diff] [blame] | 63 | import com.google.devtools.build.lib.syntax.StarlarkList; |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 64 | import com.google.devtools.build.lib.syntax.SyntaxError; |
Googler | cfd681f | 2019-11-11 07:24:02 -0800 | [diff] [blame] | 65 | import com.google.devtools.build.lib.syntax.Tuple; |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 66 | import com.google.devtools.build.lib.syntax.util.EvaluationTestCase; |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 67 | import com.google.devtools.build.lib.testutil.MoreAsserts; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 68 | import com.google.devtools.build.lib.util.FileTypeSet; |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 69 | import javax.annotation.Nullable; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 70 | import org.junit.Before; |
Vladimir Moskva | f7c552c | 2017-01-12 17:17:15 +0000 | [diff] [blame] | 71 | import org.junit.Rule; |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 72 | import org.junit.Test; |
Vladimir Moskva | f7c552c | 2017-01-12 17:17:15 +0000 | [diff] [blame] | 73 | import org.junit.rules.ExpectedException; |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 74 | import org.junit.runner.RunWith; |
| 75 | import org.junit.runners.JUnit4; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 76 | |
gregce | b100b1d | 2020-05-20 10:22:17 -0700 | [diff] [blame] | 77 | /** Tests for StarlarkRuleClassFunctions. */ |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 78 | @RunWith(JUnit4.class) |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 79 | public final class StarlarkRuleClassFunctionsTest extends BuildViewTestCase { |
| 80 | |
| 81 | private final EvaluationTestCase ev = new BazelEvaluationTestCase(); |
| 82 | |
| 83 | private StarlarkRuleContext createRuleContext(String label) throws Exception { |
| 84 | return new StarlarkRuleContext( |
| 85 | getRuleContextForStarlark(getConfiguredTarget(label)), null, getStarlarkSemantics()); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected void setStarlarkSemanticsOptions(String... options) throws Exception { |
| 90 | super.setStarlarkSemanticsOptions(options); // for BuildViewTestCase |
| 91 | ev.setSemantics(options); // for StarlarkThread |
| 92 | } |
| 93 | |
Vladimir Moskva | f7c552c | 2017-01-12 17:17:15 +0000 | [diff] [blame] | 94 | @Rule public ExpectedException thrown = ExpectedException.none(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 95 | |
| 96 | @Before |
Taras Tsugrii | 7fe7047 | 2018-07-25 13:58:02 -0700 | [diff] [blame] | 97 | public final void createBuildFile() throws Exception { |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 98 | scratch.file( |
| 99 | "foo/BUILD", |
| 100 | "genrule(name = 'foo',", |
| 101 | " cmd = 'dummy_cmd',", |
| 102 | " srcs = ['a.txt', 'b.img'],", |
| 103 | " tools = ['t.exe'],", |
| 104 | " outs = ['c.txt'])", |
| 105 | "genrule(name = 'bar',", |
| 106 | " cmd = 'dummy_cmd',", |
| 107 | " srcs = [':jl', ':gl'],", |
| 108 | " outs = ['d.txt'])", |
| 109 | "java_library(name = 'jl',", |
| 110 | " srcs = ['a.java'])", |
| 111 | "genrule(name = 'gl',", |
| 112 | " cmd = 'touch $(OUTS)',", |
| 113 | " srcs = ['a.go'],", |
| 114 | " outs = [ 'gl.a', 'gl.gcgox', ],", |
| 115 | " output_to_bindir = 1,", |
| 116 | ")"); |
| 117 | } |
| 118 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 119 | @Test |
Florian Weikert | e96b0b8 | 2015-09-25 11:35:11 +0000 | [diff] [blame] | 120 | public void testCannotOverrideBuiltInAttribute() throws Exception { |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 121 | ev.setFailFast(false); |
brandjon | 8bd2016 | 2017-12-28 08:49:54 -0800 | [diff] [blame] | 122 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 123 | ev, |
| 124 | "def impl(ctx):", // |
brandjon | 8bd2016 | 2017-12-28 08:49:54 -0800 | [diff] [blame] | 125 | " return", |
| 126 | "r = rule(impl, attrs = {'tags': attr.string_list()})"); |
| 127 | ev.assertContainsError( |
| 128 | "There is already a built-in attribute 'tags' which cannot be overridden"); |
Florian Weikert | e96b0b8 | 2015-09-25 11:35:11 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 131 | @Test |
Vladimir Moskva | da57492 | 2016-10-05 16:36:49 +0000 | [diff] [blame] | 132 | public void testCannotOverrideBuiltInAttributeName() throws Exception { |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 133 | ev.setFailFast(false); |
brandjon | 8bd2016 | 2017-12-28 08:49:54 -0800 | [diff] [blame] | 134 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 135 | ev, |
| 136 | "def impl(ctx):", // |
brandjon | 8bd2016 | 2017-12-28 08:49:54 -0800 | [diff] [blame] | 137 | " return", |
| 138 | "r = rule(impl, attrs = {'name': attr.string()})"); |
| 139 | ev.assertContainsError( |
| 140 | "There is already a built-in attribute 'name' which cannot be overridden"); |
Vladimir Moskva | da57492 | 2016-10-05 16:36:49 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 144 | public void testImplicitArgsAttribute() throws Exception { |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 145 | ev.setFailFast(false); |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 146 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 147 | ev, |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 148 | "def _impl(ctx):", |
| 149 | " pass", |
| 150 | "exec_rule = rule(implementation = _impl, executable = True)", |
| 151 | "non_exec_rule = rule(implementation = _impl)"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 152 | assertThat(getRuleClass("exec_rule").hasAttr("args", Type.STRING_LIST)).isTrue(); |
| 153 | assertThat(getRuleClass("non_exec_rule").hasAttr("args", Type.STRING_LIST)).isFalse(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | private RuleClass getRuleClass(String name) throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 157 | return ((StarlarkRuleFunction) ev.lookup(name)).getRuleClass(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Googler | 3fcfbe1 | 2019-08-28 08:10:11 -0700 | [diff] [blame] | 160 | private void registerDummyStarlarkFunction() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 161 | ev.exec("def impl():", " pass"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 164 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 165 | public void testAttrWithOnlyType() throws Exception { |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 166 | Attribute attr = buildAttribute("a1", "attr.string_list()"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 167 | assertThat(attr.getType()).isEqualTo(Type.STRING_LIST); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 170 | private Attribute buildAttribute(String name, String... lines) throws Exception { |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 171 | String[] strings = lines.clone(); |
| 172 | strings[strings.length - 1] = String.format("%s = %s", name, strings[strings.length - 1]); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 173 | evalAndExport(ev, strings); |
| 174 | StarlarkAttrModule.Descriptor lookup = (StarlarkAttrModule.Descriptor) ev.lookup(name); |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 175 | return lookup != null ? lookup.build(name) : null; |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 178 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 179 | public void testOutputListAttr() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 180 | Attribute attr = buildAttribute("a1", "attr.output_list()"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 181 | assertThat(attr.getType()).isEqualTo(BuildType.OUTPUT_LIST); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 184 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 185 | public void testIntListAttr() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 186 | Attribute attr = buildAttribute("a1", "attr.int_list()"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 187 | assertThat(attr.getType()).isEqualTo(Type.INTEGER_LIST); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 190 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 191 | public void testOutputAttr() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 192 | Attribute attr = buildAttribute("a1", "attr.output()"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 193 | assertThat(attr.getType()).isEqualTo(BuildType.OUTPUT); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 196 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 197 | public void testStringDictAttr() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 198 | Attribute attr = buildAttribute("a1", "attr.string_dict(default = {'a': 'b'})"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 199 | assertThat(attr.getType()).isEqualTo(Type.STRING_DICT); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 202 | @Test |
Francois-Rene Rideau | 028e319 | 2015-10-29 14:26:59 +0000 | [diff] [blame] | 203 | public void testStringListDictAttr() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 204 | Attribute attr = buildAttribute("a1", "attr.string_list_dict(default = {'a': ['b', 'c']})"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 205 | assertThat(attr.getType()).isEqualTo(Type.STRING_LIST_DICT); |
Francois-Rene Rideau | 028e319 | 2015-10-29 14:26:59 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 209 | public void testAttrAllowedFileTypesAnyFile() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 210 | Attribute attr = buildAttribute("a1", "attr.label_list(allow_files = True)"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 211 | assertThat(attr.getAllowedFileTypesPredicate()).isEqualTo(FileTypeSet.ANY_FILE); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 214 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 215 | public void testAttrAllowedFileTypesWrongType() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 216 | ev.checkEvalErrorContains( |
Googler | ee05663 | 2019-10-10 13:04:49 -0700 | [diff] [blame] | 217 | "allow_files should be a boolean or a string list", "attr.label_list(allow_files = 18)"); |
Laurent Le Brun | 2445df1 | 2016-05-11 14:36:40 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Test |
laurentlb | e6ab3c4 | 2019-07-01 10:43:57 -0700 | [diff] [blame] | 221 | public void testAttrNameSpecialCharactersAreForbidden() throws Exception { |
laurentlb | e6ab3c4 | 2019-07-01 10:43:57 -0700 | [diff] [blame] | 222 | ev.setFailFast(false); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 223 | evalAndExport(ev, "def impl(ctx): return", "r = rule(impl, attrs = {'ab$c': attr.int()})"); |
laurentlb | 0fddb30 | 2019-08-16 14:02:01 -0700 | [diff] [blame] | 224 | ev.assertContainsError("attribute name `ab$c` is not a valid identifier"); |
laurentlb | e6ab3c4 | 2019-07-01 10:43:57 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Test |
| 228 | public void testAttrNameCannotStartWithDigit() throws Exception { |
laurentlb | e6ab3c4 | 2019-07-01 10:43:57 -0700 | [diff] [blame] | 229 | ev.setFailFast(false); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 230 | evalAndExport(ev, "def impl(ctx): return", "r = rule(impl, attrs = {'2_foo': attr.int()})"); |
laurentlb | 0fddb30 | 2019-08-16 14:02:01 -0700 | [diff] [blame] | 231 | ev.assertContainsError("attribute name `2_foo` is not a valid identifier"); |
laurentlb | e6ab3c4 | 2019-07-01 10:43:57 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Test |
michajlo | b839a51 | 2020-03-11 10:04:23 -0700 | [diff] [blame] | 235 | public void testRuleClassTooManyAttributes() throws Exception { |
| 236 | ev.setFailFast(false); |
| 237 | |
| 238 | ImmutableList.Builder<String> linesBuilder = |
| 239 | ImmutableList.<String>builder() |
| 240 | .add("def impl(ctx): return") |
| 241 | .add("r = rule(impl, attrs = {"); |
michajlo | 7475b77 | 2020-03-12 15:10:11 -0700 | [diff] [blame] | 242 | for (int i = 0; i < 200; i++) { |
michajlo | b839a51 | 2020-03-11 10:04:23 -0700 | [diff] [blame] | 243 | linesBuilder.add(" 'attr" + i + "': attr.int(),"); |
| 244 | } |
| 245 | linesBuilder.add("})"); |
| 246 | |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 247 | evalAndExport(ev, linesBuilder.build().toArray(new String[0])); |
michajlo | b839a51 | 2020-03-11 10:04:23 -0700 | [diff] [blame] | 248 | |
| 249 | assertThat(ev.getEventCollector()).hasSize(1); |
| 250 | Event event = ev.getEventCollector().iterator().next(); |
| 251 | assertThat(event.getKind()).isEqualTo(EventKind.ERROR); |
| 252 | assertThat(event.getMessage()).contains("Rule class r declared too many attributes"); |
| 253 | } |
| 254 | |
| 255 | @Test |
michajlo | 0a89cef | 2020-04-06 12:04:12 -0700 | [diff] [blame] | 256 | public void testRuleClassTooLongAttributeName() throws Exception { |
| 257 | ev.setFailFast(false); |
| 258 | |
| 259 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 260 | ev, |
michajlo | 0a89cef | 2020-04-06 12:04:12 -0700 | [diff] [blame] | 261 | "def impl(ctx): return;", |
| 262 | "r = rule(impl, attrs = { '" + Strings.repeat("x", 150) + "': attr.int() })"); |
| 263 | |
| 264 | assertThat(ev.getEventCollector()).hasSize(1); |
| 265 | Event event = ev.getEventCollector().iterator().next(); |
| 266 | assertThat(event.getKind()).isEqualTo(EventKind.ERROR); |
| 267 | assertThat(event.getMessage()) |
| 268 | .matches("Attribute r\\.x{150}'s name is too long \\(150 > 128\\)"); |
| 269 | } |
| 270 | |
| 271 | @Test |
Laurent Le Brun | 50681c1 | 2016-07-05 10:08:54 +0000 | [diff] [blame] | 272 | public void testAttrAllowedSingleFileTypesWrongType() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 273 | ev.checkEvalErrorContains( |
Laurent Le Brun | 50681c1 | 2016-07-05 10:08:54 +0000 | [diff] [blame] | 274 | "allow_single_file should be a boolean or a string list", |
| 275 | "attr.label(allow_single_file = 18)"); |
| 276 | } |
| 277 | |
| 278 | @Test |
Laurent Le Brun | 2445df1 | 2016-05-11 14:36:40 +0000 | [diff] [blame] | 279 | public void testAttrWithList() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 280 | Attribute attr = buildAttribute("a1", "attr.label_list(allow_files = ['.xml'])"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 281 | assertThat(attr.getAllowedFileTypesPredicate().apply("a.xml")).isTrue(); |
| 282 | assertThat(attr.getAllowedFileTypesPredicate().apply("a.txt")).isFalse(); |
| 283 | assertThat(attr.isSingleArtifact()).isFalse(); |
Laurent Le Brun | 50681c1 | 2016-07-05 10:08:54 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | @Test |
| 287 | public void testAttrSingleFileWithList() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 288 | Attribute attr = buildAttribute("a1", "attr.label(allow_single_file = ['.xml'])"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 289 | assertThat(attr.getAllowedFileTypesPredicate().apply("a.xml")).isTrue(); |
| 290 | assertThat(attr.getAllowedFileTypesPredicate().apply("a.txt")).isFalse(); |
| 291 | assertThat(attr.isSingleArtifact()).isTrue(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 292 | } |
| 293 | |
gregce | 74d84d4 | 2020-04-17 10:02:03 -0700 | [diff] [blame] | 294 | private static StarlarkProviderIdentifier legacy(String legacyId) { |
| 295 | return StarlarkProviderIdentifier.forLegacy(legacyId); |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 296 | } |
| 297 | |
gregce | 74d84d4 | 2020-04-17 10:02:03 -0700 | [diff] [blame] | 298 | private static StarlarkProviderIdentifier declared(String exportedName) { |
gregce | b6eafee | 2020-04-20 08:04:51 -0700 | [diff] [blame] | 299 | return StarlarkProviderIdentifier.forKey(new StarlarkProvider.Key(FAKE_LABEL, exportedName)); |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 302 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 303 | public void testAttrWithProviders() throws Exception { |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 304 | Attribute attr = |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 305 | buildAttribute("a1", |
| 306 | "b = provider()", |
| 307 | "attr.label_list(allow_files = True, providers = ['a', b])"); |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 308 | assertThat(attr.getRequiredProviders().isSatisfiedBy(set(legacy("a"), declared("b")))).isTrue(); |
| 309 | assertThat(attr.getRequiredProviders().isSatisfiedBy(set(legacy("a")))).isFalse(); |
| 310 | } |
| 311 | |
| 312 | @Test |
| 313 | public void testAttrWithProvidersOneEmpty() throws Exception { |
| 314 | Attribute attr = |
| 315 | buildAttribute( |
| 316 | "a1", |
| 317 | "b = provider()", |
| 318 | "attr.label_list(allow_files = True, providers = [['a', b],[]])"); |
| 319 | assertThat(attr.getRequiredProviders().acceptsAny()).isTrue(); |
dslomov | c32e1b1 | 2017-07-31 19:23:52 +0200 | [diff] [blame] | 320 | } |
| 321 | |
dslomov | c32e1b1 | 2017-07-31 19:23:52 +0200 | [diff] [blame] | 322 | @Test |
Yun Peng | 83fbb91a | 2016-02-23 18:37:44 +0000 | [diff] [blame] | 323 | public void testAttrWithProvidersList() throws Exception { |
| 324 | Attribute attr = |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 325 | buildAttribute("a1", |
| 326 | "b = provider()", |
| 327 | "attr.label_list(allow_files = True, providers = [['a', b], ['c']])"); |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 328 | assertThat(attr.getRequiredProviders().isSatisfiedBy(set(legacy("a"), declared("b")))).isTrue(); |
| 329 | assertThat(attr.getRequiredProviders().isSatisfiedBy(set(legacy("c")))).isTrue(); |
| 330 | assertThat(attr.getRequiredProviders().isSatisfiedBy(set(legacy("a")))).isFalse(); |
| 331 | } |
| 332 | |
gregce | 74d84d4 | 2020-04-17 10:02:03 -0700 | [diff] [blame] | 333 | private static AdvertisedProviderSet set(StarlarkProviderIdentifier... ids) { |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 334 | AdvertisedProviderSet.Builder builder = AdvertisedProviderSet.builder(); |
gregce | 74d84d4 | 2020-04-17 10:02:03 -0700 | [diff] [blame] | 335 | for (StarlarkProviderIdentifier id : ids) { |
gregce | d7c1cef | 2020-05-12 07:51:48 -0700 | [diff] [blame] | 336 | builder.addStarlark(id); |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 337 | } |
| 338 | return builder.build(); |
Yun Peng | 83fbb91a | 2016-02-23 18:37:44 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 341 | private void checkAttributeError(String expectedMessage, String... lines) throws Exception { |
| 342 | ev.setFailFast(false); |
| 343 | buildAttribute("fakeAttribute", lines); |
| 344 | MoreAsserts.assertContainsEvent(ev.getEventCollector(), expectedMessage); |
| 345 | } |
| 346 | |
Yun Peng | 83fbb91a | 2016-02-23 18:37:44 +0000 | [diff] [blame] | 347 | @Test |
| 348 | public void testAttrWithWrongProvidersList() throws Exception { |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 349 | checkAttributeError( |
| 350 | "element in 'providers' is of unexpected type. Either all elements should be providers," |
| 351 | + " or all elements should be lists of providers," |
| 352 | + " but got list with an element of type int.", |
Yun Peng | da9410c | 2016-03-18 21:14:51 +0000 | [diff] [blame] | 353 | "attr.label_list(allow_files = True, providers = [['a', 1], ['c']])"); |
Yun Peng | 83fbb91a | 2016-02-23 18:37:44 +0000 | [diff] [blame] | 354 | |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 355 | checkAttributeError( |
| 356 | "element in 'providers' is of unexpected type. Either all elements should be providers," |
| 357 | + " or all elements should be lists of providers," |
| 358 | + " but got an element of type string.", |
| 359 | "b = provider()", |
| 360 | "attr.label_list(allow_files = True, providers = [['a', b], 'c'])"); |
| 361 | |
| 362 | checkAttributeError( |
| 363 | "element in 'providers' is of unexpected type. Either all elements should be providers," |
| 364 | + " or all elements should be lists of providers," |
| 365 | + " but got an element of type string.", |
| 366 | "c = provider()", |
| 367 | "attr.label_list(allow_files = True, providers = [['a', b], c])"); |
Yun Peng | 83fbb91a | 2016-02-23 18:37:44 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | @Test |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 371 | public void testLabelListWithAspects() throws Exception { |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 372 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 373 | ev, |
| 374 | "def _impl(target, ctx):", |
| 375 | " pass", |
| 376 | "my_aspect = aspect(implementation = _impl)", |
| 377 | "a = attr.label_list(aspects = [my_aspect])"); |
| 378 | StarlarkAttrModule.Descriptor attr = (StarlarkAttrModule.Descriptor) ev.lookup("a"); |
| 379 | StarlarkDefinedAspect aspect = (StarlarkDefinedAspect) ev.lookup("my_aspect"); |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 380 | assertThat(aspect).isNotNull(); |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 381 | assertThat(attr.build("xxx").getAspectClasses()).containsExactly(aspect.getAspectClass()); |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | @Test |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 385 | public void testLabelWithAspects() throws Exception { |
| 386 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 387 | ev, |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 388 | "def _impl(target, ctx):", |
| 389 | " pass", |
| 390 | "my_aspect = aspect(implementation = _impl)", |
| 391 | "a = attr.label(aspects = [my_aspect])"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 392 | StarlarkAttrModule.Descriptor attr = (StarlarkAttrModule.Descriptor) ev.lookup("a"); |
| 393 | StarlarkDefinedAspect aspect = (StarlarkDefinedAspect) ev.lookup("my_aspect"); |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 394 | assertThat(aspect).isNotNull(); |
| 395 | assertThat(attr.build("xxx").getAspectClasses()).containsExactly(aspect.getAspectClass()); |
| 396 | } |
| 397 | |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 398 | @Test |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 399 | public void testLabelListWithAspectsError() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 400 | ev.checkEvalErrorContains( |
adonovan | 8580390 | 2020-04-16 14:46:57 -0700 | [diff] [blame] | 401 | "at index 0 of aspects, got element of type int, want Aspect", |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 402 | "def _impl(target, ctx):", |
| 403 | " pass", |
| 404 | "my_aspect = aspect(implementation = _impl)", |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 405 | "attr.label_list(aspects = [my_aspect, 123])"); |
Dmitry Lomov | 5a8f1c0 | 2015-11-26 10:49:16 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | @Test |
Dmitry Lomov | c1b1eed | 2015-11-26 11:18:12 +0000 | [diff] [blame] | 409 | public void testAspectExtraDeps() throws Exception { |
| 410 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 411 | ev, |
Dmitry Lomov | c1b1eed | 2015-11-26 11:18:12 +0000 | [diff] [blame] | 412 | "def _impl(target, ctx):", |
| 413 | " pass", |
| 414 | "my_aspect = aspect(_impl,", |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 415 | " attrs = { '_extra_deps' : attr.label(default = Label('//foo/bar:baz')) }", |
| 416 | ")"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 417 | StarlarkDefinedAspect aspect = (StarlarkDefinedAspect) ev.lookup("my_aspect"); |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 418 | Attribute attribute = Iterables.getOnlyElement(aspect.getAttributes()); |
| 419 | assertThat(attribute.getName()).isEqualTo("$extra_deps"); |
| 420 | assertThat(attribute.getDefaultValue(null)) |
dannark | da327bb | 2018-06-22 11:44:27 -0700 | [diff] [blame] | 421 | .isEqualTo( |
| 422 | Label.parseAbsolute( |
| 423 | "//foo/bar:baz", |
| 424 | /* defaultToMain= */ false, |
| 425 | /* repositoryMapping= */ ImmutableMap.of())); |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | @Test |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 429 | public void testAspectParameter() throws Exception { |
| 430 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 431 | ev, |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 432 | "def _impl(target, ctx):", |
| 433 | " pass", |
| 434 | "my_aspect = aspect(_impl,", |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 435 | " attrs = { 'param' : attr.string(values=['a', 'b']) }", |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 436 | ")"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 437 | StarlarkDefinedAspect aspect = (StarlarkDefinedAspect) ev.lookup("my_aspect"); |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 438 | Attribute attribute = Iterables.getOnlyElement(aspect.getAttributes()); |
| 439 | assertThat(attribute.getName()).isEqualTo("param"); |
| 440 | } |
Michajlo Matijkiw | 6d47141 | 2016-08-09 20:35:45 +0000 | [diff] [blame] | 441 | |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 442 | @Test |
| 443 | public void testAspectParameterRequiresValues() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 444 | ev.checkEvalErrorContains( |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 445 | "Aspect parameter attribute 'param' must have type 'string' and use the 'values' " |
Googler | ee05663 | 2019-10-10 13:04:49 -0700 | [diff] [blame] | 446 | + "restriction.", |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 447 | "def _impl(target, ctx):", |
| 448 | " pass", |
| 449 | "my_aspect = aspect(_impl,", |
| 450 | " attrs = { 'param' : attr.string(default = 'c') }", |
| 451 | ")"); |
| 452 | } |
| 453 | |
| 454 | @Test |
| 455 | public void testAspectParameterBadType() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 456 | ev.checkEvalErrorContains( |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 457 | "Aspect parameter attribute 'param' must have type 'string' and use the 'values' " |
Googler | ee05663 | 2019-10-10 13:04:49 -0700 | [diff] [blame] | 458 | + "restriction.", |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 459 | "def _impl(target, ctx):", |
| 460 | " pass", |
| 461 | "my_aspect = aspect(_impl,", |
| 462 | " attrs = { 'param' : attr.label(default = Label('//foo/bar:baz')) }", |
| 463 | ")"); |
| 464 | } |
| 465 | |
| 466 | @Test |
| 467 | public void testAspectParameterAndExtraDeps() throws Exception { |
| 468 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 469 | ev, |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 470 | "def _impl(target, ctx):", |
| 471 | " pass", |
| 472 | "my_aspect = aspect(_impl,", |
| 473 | " attrs = { 'param' : attr.string(values=['a', 'b']),", |
| 474 | " '_extra' : attr.label(default = Label('//foo/bar:baz')) }", |
| 475 | ")"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 476 | StarlarkDefinedAspect aspect = (StarlarkDefinedAspect) ev.lookup("my_aspect"); |
Googler | 74558fc | 2016-05-06 21:47:42 +0000 | [diff] [blame] | 477 | assertThat(aspect.getAttributes()).hasSize(2); |
| 478 | assertThat(aspect.getParamAttributes()).containsExactly("param"); |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | @Test |
| 482 | public void testAspectNoDefaultValueAttribute() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 483 | ev.checkEvalErrorContains( |
Dmitry Lomov | ace678e | 2015-12-16 15:10:20 +0000 | [diff] [blame] | 484 | "Aspect attribute '_extra_deps' has no default value", |
| 485 | "def _impl(target, ctx):", |
| 486 | " pass", |
| 487 | "my_aspect = aspect(_impl,", |
| 488 | " attrs = { '_extra_deps' : attr.label() }", |
| 489 | ")"); |
Dmitry Lomov | c1b1eed | 2015-11-26 11:18:12 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | @Test |
John Cater | 9a8d16e | 2017-07-05 16:12:07 -0400 | [diff] [blame] | 493 | public void testAspectAddToolchain() throws Exception { |
| 494 | scratch.file("test/BUILD", "toolchain_type(name = 'my_toolchain_type')"); |
| 495 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 496 | ev, "def _impl(ctx): pass", "a1 = aspect(_impl, toolchains=['//test:my_toolchain_type'])"); |
| 497 | StarlarkDefinedAspect a = (StarlarkDefinedAspect) ev.lookup("a1"); |
John Cater | 9a8d16e | 2017-07-05 16:12:07 -0400 | [diff] [blame] | 498 | assertThat(a.getRequiredToolchains()).containsExactly(makeLabel("//test:my_toolchain_type")); |
| 499 | } |
| 500 | |
| 501 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 502 | public void testNonLabelAttrWithProviders() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 503 | ev.checkEvalErrorContains( |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 504 | "unexpected keyword argument 'providers'", "attr.string(providers = ['a'])"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 505 | } |
| 506 | |
cparsons | e2d200f | 2018-03-06 16:15:11 -0800 | [diff] [blame] | 507 | private static final RuleClass.ConfiguredTargetFactory<Object, Object, Exception> |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 508 | DUMMY_CONFIGURED_TARGET_FACTORY = |
dslomov | c13bb39 | 2017-08-02 23:29:54 +0200 | [diff] [blame] | 509 | ruleContext -> { |
| 510 | throw new IllegalStateException(); |
| 511 | }; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 512 | |
| 513 | private RuleClass ruleClass(String name) { |
| 514 | return new RuleClass.Builder(name, RuleClassType.NORMAL, false) |
| 515 | .factory(DUMMY_CONFIGURED_TARGET_FACTORY) |
| 516 | .add(Attribute.attr("tags", Type.STRING_LIST)) |
| 517 | .build(); |
| 518 | } |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 519 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 520 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 521 | public void testAttrAllowedRuleClassesSpecificRuleClasses() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 522 | Attribute attr = buildAttribute("a", |
| 523 | "attr.label_list(allow_rules = ['java_binary'], allow_files = True)"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 524 | assertThat(attr.getAllowedRuleClassesPredicate().apply(ruleClass("java_binary"))).isTrue(); |
| 525 | assertThat(attr.getAllowedRuleClassesPredicate().apply(ruleClass("genrule"))).isFalse(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 526 | } |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 527 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 528 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 529 | public void testAttrDefaultValue() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 530 | Attribute attr = buildAttribute("a1", "attr.string(default = 'some value')"); |
juliexxia | 84d1a66 | 2018-12-26 14:07:04 -0800 | [diff] [blame] | 531 | assertThat(attr.getDefaultValueUnchecked()).isEqualTo("some value"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 534 | @Test |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 535 | public void testLabelAttrDefaultValueAsString() throws Exception { |
| 536 | Attribute sligleAttr = buildAttribute("a1", "attr.label(default = '//foo:bar')"); |
juliexxia | 84d1a66 | 2018-12-26 14:07:04 -0800 | [diff] [blame] | 537 | assertThat(sligleAttr.getDefaultValueUnchecked()) |
dannark | da327bb | 2018-06-22 11:44:27 -0700 | [diff] [blame] | 538 | .isEqualTo( |
| 539 | Label.parseAbsolute( |
| 540 | "//foo:bar", |
| 541 | /* defaultToMain= */ false, |
| 542 | /* repositoryMapping= */ ImmutableMap.of())); |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 543 | |
| 544 | Attribute listAttr = |
| 545 | buildAttribute("a2", "attr.label_list(default = ['//foo:bar', '//bar:foo'])"); |
juliexxia | 84d1a66 | 2018-12-26 14:07:04 -0800 | [diff] [blame] | 546 | assertThat(listAttr.getDefaultValueUnchecked()) |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 547 | .isEqualTo( |
| 548 | ImmutableList.of( |
dannark | da327bb | 2018-06-22 11:44:27 -0700 | [diff] [blame] | 549 | Label.parseAbsolute( |
| 550 | "//foo:bar", |
| 551 | /* defaultToMain= */ false, |
| 552 | /* repositoryMapping= */ ImmutableMap.of()), |
| 553 | Label.parseAbsolute( |
| 554 | "//bar:foo", |
| 555 | /* defaultToMain= */ false, |
| 556 | /*repositoryMapping= */ ImmutableMap.of()))); |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 557 | |
| 558 | Attribute dictAttr = |
| 559 | buildAttribute("a3", "attr.label_keyed_string_dict(default = {'//foo:bar': 'my value'})"); |
juliexxia | 84d1a66 | 2018-12-26 14:07:04 -0800 | [diff] [blame] | 560 | assertThat(dictAttr.getDefaultValueUnchecked()) |
dannark | da327bb | 2018-06-22 11:44:27 -0700 | [diff] [blame] | 561 | .isEqualTo( |
| 562 | ImmutableMap.of( |
| 563 | Label.parseAbsolute( |
| 564 | "//foo:bar", |
| 565 | /* defaultToMain= */ false, |
| 566 | /* repositoryMapping= */ ImmutableMap.of()), |
| 567 | "my value")); |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | @Test |
| 571 | public void testLabelAttrDefaultValueAsStringBadValue() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 572 | ev.checkEvalErrorContains( |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 573 | "invalid label '/foo:bar' in parameter 'default' of attribute 'label': " |
janakr | d3fe5e7 | 2018-03-30 12:49:12 -0700 | [diff] [blame] | 574 | + "invalid target name '/foo:bar'", |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 575 | "attr.label(default = '/foo:bar')"); |
| 576 | |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 577 | ev.checkEvalErrorContains( |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 578 | "invalid label '/bar:foo' in element 1 of parameter 'default' of attribute " |
janakr | d3fe5e7 | 2018-03-30 12:49:12 -0700 | [diff] [blame] | 579 | + "'label_list': invalid target name '/bar:foo'", |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 580 | "attr.label_list(default = ['//foo:bar', '/bar:foo'])"); |
| 581 | |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 582 | ev.checkEvalErrorContains( |
janakr | d3fe5e7 | 2018-03-30 12:49:12 -0700 | [diff] [blame] | 583 | "invalid label '/bar:foo' in dict key element: invalid target name '/bar:foo'", |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 584 | "attr.label_keyed_string_dict(default = {'//foo:bar': 'a', '/bar:foo': 'b'})"); |
| 585 | } |
| 586 | |
| 587 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 588 | public void testAttrDefaultValueBadType() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 589 | ev.checkEvalErrorContains("got value of type 'int', want 'string'", "attr.string(default = 1)"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 592 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 593 | public void testAttrMandatory() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 594 | Attribute attr = buildAttribute("a1", "attr.string(mandatory=True)"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 595 | assertThat(attr.isMandatory()).isTrue(); |
| 596 | assertThat(attr.isNonEmpty()).isFalse(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 599 | @Test |
Laurent Le Brun | bc16f72 | 2016-07-06 13:40:24 +0000 | [diff] [blame] | 600 | public void testAttrAllowEmpty() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 601 | Attribute attr = buildAttribute("a1", "attr.string_list(allow_empty=False)"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 602 | assertThat(attr.isNonEmpty()).isTrue(); |
| 603 | assertThat(attr.isMandatory()).isFalse(); |
Laurent Le Brun | bc16f72 | 2016-07-06 13:40:24 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 607 | public void testAttrBadKeywordArguments() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 608 | ev.checkEvalErrorContains( |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 609 | "string() got unexpected keyword argument 'bad_keyword'", "attr.string(bad_keyword = '')"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 612 | @Test |
Laurent Le Brun | c4ddf6f | 2016-07-04 13:38:38 +0000 | [diff] [blame] | 613 | public void testAttrCfg() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 614 | Attribute attr = buildAttribute("a1", "attr.label(cfg = 'host', allow_files = True)"); |
jcater | b44167f | 2019-04-02 12:06:26 -0700 | [diff] [blame] | 615 | assertThat(attr.getTransitionFactory().isHost()).isTrue(); |
Laurent Le Brun | c4ddf6f | 2016-07-04 13:38:38 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | @Test |
Vladimir Moskva | 5a51077 | 2016-11-23 19:03:38 +0000 | [diff] [blame] | 619 | public void testAttrCfgTarget() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 620 | Attribute attr = buildAttribute("a1", "attr.label(cfg = 'target', allow_files = True)"); |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 621 | assertThat(NoTransition.isInstance(attr.getTransitionFactory())).isTrue(); |
Vladimir Moskva | 5a51077 | 2016-11-23 19:03:38 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | @Test |
gregce | bceecab | 2018-06-27 17:44:45 -0700 | [diff] [blame] | 625 | public void incompatibleDataTransition() throws Exception { |
gregce | bceecab | 2018-06-27 17:44:45 -0700 | [diff] [blame] | 626 | EvalException expected = |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 627 | assertThrows(EvalException.class, () -> ev.eval("attr.label(cfg = 'data')")); |
laurentlb | d576ed0 | 2019-03-26 15:35:06 -0700 | [diff] [blame] | 628 | assertThat(expected).hasMessageThat().contains("cfg must be either 'host' or 'target'"); |
gregce | bceecab | 2018-06-27 17:44:45 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 632 | public void testAttrValues() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 633 | Attribute attr = buildAttribute("a1", "attr.string(values = ['ab', 'cd'])"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 634 | PredicateWithMessage<Object> predicate = attr.getAllowedValues(); |
| 635 | assertThat(predicate.apply("ab")).isTrue(); |
| 636 | assertThat(predicate.apply("xy")).isFalse(); |
| 637 | } |
| 638 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 639 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 640 | public void testAttrIntValues() throws Exception { |
Dmitry Lomov | 460db0f | 2016-11-24 10:54:19 +0000 | [diff] [blame] | 641 | Attribute attr = buildAttribute("a1", "attr.int(values = [1, 2])"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 642 | PredicateWithMessage<Object> predicate = attr.getAllowedValues(); |
| 643 | assertThat(predicate.apply(2)).isTrue(); |
| 644 | assertThat(predicate.apply(3)).isFalse(); |
| 645 | } |
| 646 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 647 | @Test |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 648 | public void testAttrDoc() throws Exception { |
| 649 | // We don't actually store the doc in the attr definition; right now it's just meant to be |
| 650 | // extracted by documentation generating tools. So we don't have anything to assert and we just |
| 651 | // verify that no exceptions were thrown from building them. |
| 652 | buildAttribute("a1", "attr.bool(doc='foo')"); |
| 653 | buildAttribute("a2", "attr.int(doc='foo')"); |
| 654 | buildAttribute("a3", "attr.int_list(doc='foo')"); |
| 655 | buildAttribute("a4", "attr.label(doc='foo')"); |
| 656 | buildAttribute("a5", "attr.label_keyed_string_dict(doc='foo')"); |
| 657 | buildAttribute("a6", "attr.label_list(doc='foo')"); |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 658 | buildAttribute("a8", "attr.output(doc='foo')"); |
| 659 | buildAttribute("a9", "attr.output_list(doc='foo')"); |
| 660 | buildAttribute("a10", "attr.string(doc='foo')"); |
| 661 | buildAttribute("a11", "attr.string_dict(doc='foo')"); |
| 662 | buildAttribute("a12", "attr.string_list(doc='foo')"); |
| 663 | buildAttribute("a13", "attr.string_list_dict(doc='foo')"); |
| 664 | } |
| 665 | |
| 666 | @Test |
laurentlb | d8d3776 | 2018-10-26 14:08:33 -0700 | [diff] [blame] | 667 | public void testNoAttrLicense() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 668 | EvalException expected = assertThrows(EvalException.class, () -> ev.eval("attr.license()")); |
adonovan | 54cdd58 | 2020-06-09 18:58:42 -0700 | [diff] [blame] | 669 | assertThat(expected).hasMessageThat().contains("'attr' value has no field or method 'license'"); |
laurentlb | d8d3776 | 2018-10-26 14:08:33 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | @Test |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 673 | public void testAttrDocValueBadType() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 674 | ev.checkEvalErrorContains("got value of type 'int', want 'string'", "attr.string(doc = 1)"); |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 678 | public void testRuleImplementation() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 679 | evalAndExport(ev, "def impl(ctx): return None", "rule1 = rule(impl)"); |
| 680 | RuleClass c = ((StarlarkRuleFunction) ev.lookup("rule1")).getRuleClass(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 681 | assertThat(c.getConfiguredTargetFunction().getName()).isEqualTo("impl"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 684 | @Test |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 685 | public void testRuleDoc() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 686 | evalAndExport(ev, "def impl(ctx): return None", "rule1 = rule(impl, doc='foo')"); |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | @Test |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 690 | public void testFunctionAsAttrDefault() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 691 | ev.exec("def f(): pass"); |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 692 | |
adonovan | af226dd | 2019-12-17 12:43:00 -0800 | [diff] [blame] | 693 | // Late-bound attributes, which are computed during analysis as a function |
| 694 | // of the configuration, are only available for attributes involving labels: |
| 695 | // attr.label |
| 696 | // attr.label_list |
| 697 | // attr.label_keyed_string_dict |
| 698 | // attr.output, |
| 699 | // attr.output_list |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 700 | // (See testRuleClassImplicitOutputFunctionDependingOnComputedAttribute |
| 701 | // for a more detailed positive test.) |
| 702 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 703 | ev, |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 704 | "attr.label(default=f)", |
| 705 | "attr.label_list(default=f)", |
| 706 | "attr.label_keyed_string_dict(default=f)"); |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 707 | // For all other attribute types, the default value may not be a function. |
adonovan | af226dd | 2019-12-17 12:43:00 -0800 | [diff] [blame] | 708 | // |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 709 | // (This is a regression test for github.com/bazelbuild/bazel/issues/9463. |
| 710 | // The loading-phase feature of "computed attribute defaults" is not exposed |
gregce | 8345b79 | 2020-05-15 13:23:29 -0700 | [diff] [blame] | 711 | // to Starlark; the bug was that the @StarlarkMethod |
adonovan | af226dd | 2019-12-17 12:43:00 -0800 | [diff] [blame] | 712 | // annotation was more permissive than the method declaration.) |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 713 | ev.checkEvalErrorContains( |
| 714 | "got value of type 'function', want 'string'", "attr.string(default=f)"); |
| 715 | ev.checkEvalErrorContains( |
adonovan | 7f26640 | 2020-04-20 10:15:47 -0700 | [diff] [blame] | 716 | "got value of type 'function', want 'sequence'", "attr.string_list(default=f)"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 717 | ev.checkEvalErrorContains("got value of type 'function', want 'int'", "attr.int(default=f)"); |
| 718 | ev.checkEvalErrorContains( |
adonovan | 7f26640 | 2020-04-20 10:15:47 -0700 | [diff] [blame] | 719 | "got value of type 'function', want 'sequence'", "attr.int_list(default=f)"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 720 | ev.checkEvalErrorContains("got value of type 'function', want 'bool'", "attr.bool(default=f)"); |
| 721 | ev.checkEvalErrorContains( |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 722 | "got value of type 'function', want 'dict'", "attr.string_dict(default=f)"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 723 | ev.checkEvalErrorContains( |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 724 | "got value of type 'function', want 'dict'", "attr.string_list_dict(default=f)"); |
| 725 | // Note: attr.license appears to be disabled already. |
| 726 | // (see --incompatible_no_attr_license) |
adonovan | af226dd | 2019-12-17 12:43:00 -0800 | [diff] [blame] | 727 | } |
| 728 | |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 729 | private static final Label FAKE_LABEL = Label.parseAbsoluteUnchecked("//fake/label.bzl"); |
| 730 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 731 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 732 | public void testRuleAddAttribute() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 733 | evalAndExport(ev, "def impl(ctx): return None", "r1 = rule(impl, attrs={'a1': attr.string()})"); |
| 734 | RuleClass c = ((StarlarkRuleFunction) ev.lookup("r1")).getRuleClass(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 735 | assertThat(c.hasAttr("a1", Type.STRING)).isTrue(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 736 | } |
| 737 | |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 738 | private static void evalAndExport(EvaluationTestCase ev, String... lines) throws Exception { |
Googler | 2abde27 | 2019-09-17 12:06:08 -0700 | [diff] [blame] | 739 | ParserInput input = ParserInput.fromLines(lines); |
adonovan | 09d1370 | 2020-05-19 08:26:55 -0700 | [diff] [blame] | 740 | Module module = ev.getModule(); |
adonovan | 034220a | 2020-03-24 10:11:26 -0700 | [diff] [blame] | 741 | StarlarkFile file = EvalUtils.parseAndValidate(input, FileOptions.DEFAULT, module); |
Googler | ba86854 | 2019-10-09 07:26:27 -0700 | [diff] [blame] | 742 | if (!file.ok()) { |
adonovan | ac1c41e | 2020-04-01 14:28:49 -0700 | [diff] [blame] | 743 | throw new SyntaxError.Exception(file.errors()); |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 744 | } |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 745 | BzlLoadFunction.execAndExport( |
adonovan | 09d1370 | 2020-05-19 08:26:55 -0700 | [diff] [blame] | 746 | file, FAKE_LABEL, ev.getEventHandler(), module, ev.getStarlarkThread()); |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 749 | @Test |
Jon Brandvein | ded4fbb | 2017-01-18 22:21:04 +0000 | [diff] [blame] | 750 | public void testExportAliasedName() throws Exception { |
gregce | b100b1d | 2020-05-20 10:22:17 -0700 | [diff] [blame] | 751 | // When there are multiple names aliasing the same StarlarkExportable, the first one to be |
Jon Brandvein | ded4fbb | 2017-01-18 22:21:04 +0000 | [diff] [blame] | 752 | // declared should be used. Make sure we're not using lexicographical order, hash order, |
| 753 | // non-deterministic order, or anything else. |
| 754 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 755 | ev, |
Jon Brandvein | ded4fbb | 2017-01-18 22:21:04 +0000 | [diff] [blame] | 756 | "def _impl(ctx): pass", |
| 757 | "d = rule(implementation = _impl)", |
| 758 | "a = d", |
| 759 | // Having more names improves the chance that non-determinism will be caught. |
| 760 | "b = d", |
| 761 | "c = d", |
| 762 | "e = d", |
| 763 | "f = d", |
| 764 | "foo = d", |
| 765 | "bar = d", |
| 766 | "baz = d", |
| 767 | "x = d", |
| 768 | "y = d", |
| 769 | "z = d"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 770 | String dName = ((StarlarkRuleFunction) ev.lookup("d")).getRuleClass().getName(); |
| 771 | String fooName = ((StarlarkRuleFunction) ev.lookup("foo")).getRuleClass().getName(); |
Jon Brandvein | ded4fbb | 2017-01-18 22:21:04 +0000 | [diff] [blame] | 772 | assertThat(dName).isEqualTo("d"); |
| 773 | assertThat(fooName).isEqualTo("d"); |
| 774 | } |
| 775 | |
| 776 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 777 | public void testOutputToGenfiles() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 778 | evalAndExport(ev, "def impl(ctx): pass", "r1 = rule(impl, output_to_genfiles=True)"); |
| 779 | RuleClass c = ((StarlarkRuleFunction) ev.lookup("r1")).getRuleClass(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 780 | assertThat(c.hasBinaryOutput()).isFalse(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 783 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 784 | public void testRuleAddMultipleAttributes() throws Exception { |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 785 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 786 | ev, |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 787 | "def impl(ctx): return None", |
| 788 | "r1 = rule(impl,", |
| 789 | " attrs = {", |
| 790 | " 'a1': attr.label_list(allow_files=True),", |
| 791 | " 'a2': attr.int()", |
| 792 | "})"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 793 | RuleClass c = ((StarlarkRuleFunction) ev.lookup("r1")).getRuleClass(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 794 | assertThat(c.hasAttr("a1", BuildType.LABEL_LIST)).isTrue(); |
| 795 | assertThat(c.hasAttr("a2", Type.INTEGER)).isTrue(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 796 | } |
Taras Tsugrii | 7fe7047 | 2018-07-25 13:58:02 -0700 | [diff] [blame] | 797 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 798 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 799 | public void testRuleAttributeFlag() throws Exception { |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 800 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 801 | ev, |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 802 | "def impl(ctx): return None", |
| 803 | "r1 = rule(impl, attrs = {'a1': attr.string(mandatory=True)})"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 804 | RuleClass c = ((StarlarkRuleFunction) ev.lookup("r1")).getRuleClass(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 805 | assertThat(c.getAttributeByName("a1").isMandatory()).isTrue(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 808 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 809 | public void testRuleOutputs() throws Exception { |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 810 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 811 | ev, |
| 812 | "def impl(ctx): return None", // |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 813 | "r1 = rule(impl, outputs = {'a': 'a.txt'})"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 814 | RuleClass c = ((StarlarkRuleFunction) ev.lookup("r1")).getRuleClass(); |
Michajlo Matijkiw | 6d47141 | 2016-08-09 20:35:45 +0000 | [diff] [blame] | 815 | ImplicitOutputsFunction function = c.getDefaultImplicitOutputsFunction(); |
vladmos | 076977e | 2017-12-02 14:15:32 -0800 | [diff] [blame] | 816 | assertThat(function.getImplicitOutputs(ev.getEventHandler(), null)).containsExactly("a.txt"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 819 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 820 | public void testRuleUnknownKeyword() throws Exception { |
Googler | 3fcfbe1 | 2019-08-28 08:10:11 -0700 | [diff] [blame] | 821 | registerDummyStarlarkFunction(); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 822 | ev.checkEvalErrorContains( |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 823 | "unexpected keyword argument 'bad_keyword'", "rule(impl, bad_keyword = 'some text')"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 826 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 827 | public void testRuleImplementationMissing() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 828 | ev.checkEvalErrorContains( |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 829 | "rule() missing 1 required positional argument: implementation", "rule(attrs = {})"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 832 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 833 | public void testRuleBadTypeForAdd() throws Exception { |
Googler | 3fcfbe1 | 2019-08-28 08:10:11 -0700 | [diff] [blame] | 834 | registerDummyStarlarkFunction(); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 835 | ev.checkEvalErrorContains( |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 836 | "in call to rule(), parameter 'attrs' got value of type 'string', want 'dict or NoneType'", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 837 | "rule(impl, attrs = 'some text')"); |
| 838 | } |
| 839 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 840 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 841 | public void testRuleBadTypeInAdd() throws Exception { |
Googler | 3fcfbe1 | 2019-08-28 08:10:11 -0700 | [diff] [blame] | 842 | registerDummyStarlarkFunction(); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 843 | ev.checkEvalErrorContains( |
adonovan | 07fb6a5 | 2020-03-20 14:36:28 -0700 | [diff] [blame] | 844 | "got dict<string, string> for 'attrs', want dict<string, Attribute>", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 845 | "rule(impl, attrs = {'a1': 'some text'})"); |
| 846 | } |
| 847 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 848 | @Test |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 849 | public void testRuleBadTypeForDoc() throws Exception { |
Googler | 3fcfbe1 | 2019-08-28 08:10:11 -0700 | [diff] [blame] | 850 | registerDummyStarlarkFunction(); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 851 | ev.checkEvalErrorContains("got value of type 'int', want 'string'", "rule(impl, doc = 1)"); |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 855 | public void testLabel() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 856 | Object result = ev.eval("Label('//foo/foo:foo')"); |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 857 | assertThat(result).isInstanceOf(Label.class); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 858 | assertThat(result.toString()).isEqualTo("//foo/foo:foo"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 861 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 862 | public void testLabelSameInstance() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 863 | Object l1 = ev.eval("Label('//foo/foo:foo')"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 864 | // Implicitly creates a new pkgContext and environment, yet labels should be the same. |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 865 | Object l2 = ev.eval("Label('//foo/foo:foo')"); |
cpovirk | a4d3da6 | 2019-05-02 14:27:33 -0700 | [diff] [blame] | 866 | assertThat(l1).isSameInstanceAs(l2); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 869 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 870 | public void testLabelNameAndPackage() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 871 | Object result = ev.eval("Label('//foo/bar:baz').name"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 872 | assertThat(result).isEqualTo("baz"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 873 | // NB: implicitly creates a new pkgContext and environments, yet labels should be the same. |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 874 | result = ev.eval("Label('//foo/bar:baz').package"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 875 | assertThat(result).isEqualTo("foo/bar"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 878 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 879 | public void testRuleLabelDefaultValue() throws Exception { |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 880 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 881 | ev, |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 882 | "def impl(ctx): return None\n" |
| 883 | + "r1 = rule(impl, attrs = {'a1': " |
| 884 | + "attr.label(default = Label('//foo:foo'), allow_files=True)})"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 885 | RuleClass c = ((StarlarkRuleFunction) ev.lookup("r1")).getRuleClass(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 886 | Attribute a = c.getAttributeByName("a1"); |
juliexxia | 84d1a66 | 2018-12-26 14:07:04 -0800 | [diff] [blame] | 887 | assertThat(a.getDefaultValueUnchecked()).isInstanceOf(Label.class); |
| 888 | assertThat(a.getDefaultValueUnchecked().toString()).isEqualTo("//foo:foo"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 891 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 892 | public void testIntDefaultValue() throws Exception { |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 893 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 894 | ev, |
Dmitry Lomov | 7b59945 | 2015-11-26 10:07:32 +0000 | [diff] [blame] | 895 | "def impl(ctx): return None", |
| 896 | "r1 = rule(impl, attrs = {'a1': attr.int(default = 40+2)})"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 897 | RuleClass c = ((StarlarkRuleFunction) ev.lookup("r1")).getRuleClass(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 898 | Attribute a = c.getAttributeByName("a1"); |
juliexxia | 84d1a66 | 2018-12-26 14:07:04 -0800 | [diff] [blame] | 899 | assertThat(a.getDefaultValueUnchecked()).isEqualTo(42); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 902 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 903 | public void testRuleInheritsBaseRuleAttributes() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 904 | evalAndExport(ev, "def impl(ctx): return None", "r1 = rule(impl)"); |
| 905 | RuleClass c = ((StarlarkRuleFunction) ev.lookup("r1")).getRuleClass(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 906 | assertThat(c.hasAttr("tags", Type.STRING_LIST)).isTrue(); |
| 907 | assertThat(c.hasAttr("visibility", BuildType.NODEP_LABEL_LIST)).isTrue(); |
| 908 | assertThat(c.hasAttr("deprecation", Type.STRING)).isTrue(); |
| 909 | assertThat(c.hasAttr(":action_listener", BuildType.LABEL_LIST)) |
| 910 | .isTrue(); // required for extra actions |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | private void checkTextMessage(String from, String... lines) throws Exception { |
fangism | ed97d84 | 2019-04-12 14:19:46 -0700 | [diff] [blame] | 914 | String[] strings = lines.clone(); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 915 | Object result = ev.eval(from); |
fangism | ed97d84 | 2019-04-12 14:19:46 -0700 | [diff] [blame] | 916 | String expect = ""; |
| 917 | if (strings.length > 0) { |
| 918 | expect = Joiner.on("\n").join(lines) + "\n"; |
| 919 | } |
| 920 | assertThat(result).isEqualTo(expect); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 923 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 924 | public void testSimpleTextMessagesBooleanFields() throws Exception { |
| 925 | checkTextMessage("struct(name=True).to_proto()", "name: true"); |
| 926 | checkTextMessage("struct(name=False).to_proto()", "name: false"); |
| 927 | } |
| 928 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 929 | @Test |
cparsons | 07460fc | 2018-06-20 10:41:48 -0700 | [diff] [blame] | 930 | public void testStructRestrictedOverrides() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 931 | ev.checkEvalErrorContains( |
Googler | ee05663 | 2019-10-10 13:04:49 -0700 | [diff] [blame] | 932 | "cannot override built-in struct function 'to_json'", "struct(to_json='foo')"); |
cparsons | 07460fc | 2018-06-20 10:41:48 -0700 | [diff] [blame] | 933 | |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 934 | ev.checkEvalErrorContains( |
Googler | ee05663 | 2019-10-10 13:04:49 -0700 | [diff] [blame] | 935 | "cannot override built-in struct function 'to_proto'", "struct(to_proto='foo')"); |
cparsons | 07460fc | 2018-06-20 10:41:48 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 939 | public void testSimpleTextMessages() throws Exception { |
| 940 | checkTextMessage("struct(name='value').to_proto()", "name: \"value\""); |
fangism | ed97d84 | 2019-04-12 14:19:46 -0700 | [diff] [blame] | 941 | checkTextMessage("struct(name=[]).to_proto()"); // empty lines |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 942 | checkTextMessage("struct(name=['a', 'b']).to_proto()", "name: \"a\"", "name: \"b\""); |
| 943 | checkTextMessage("struct(name=123).to_proto()", "name: 123"); |
| 944 | checkTextMessage("struct(name=[1, 2, 3]).to_proto()", "name: 1", "name: 2", "name: 3"); |
| 945 | checkTextMessage("struct(a=struct(b='b')).to_proto()", "a {", " b: \"b\"", "}"); |
| 946 | checkTextMessage( |
| 947 | "struct(a=[struct(b='x'), struct(b='y')]).to_proto()", |
| 948 | "a {", |
| 949 | " b: \"x\"", |
| 950 | "}", |
| 951 | "a {", |
| 952 | " b: \"y\"", |
| 953 | "}"); |
| 954 | checkTextMessage( |
| 955 | "struct(a=struct(b=struct(c='c'))).to_proto()", "a {", " b {", " c: \"c\"", " }", "}"); |
fangism | ed97d84 | 2019-04-12 14:19:46 -0700 | [diff] [blame] | 956 | // dict to_proto tests |
| 957 | checkTextMessage("struct(name={}).to_proto()"); // empty lines |
| 958 | checkTextMessage( |
| 959 | "struct(name={'a': 'b'}).to_proto()", "name {", " key: \"a\"", " value: \"b\"", "}"); |
| 960 | checkTextMessage( |
| 961 | "struct(name={'c': 'd', 'a': 'b'}).to_proto()", |
| 962 | "name {", |
| 963 | " key: \"c\"", |
| 964 | " value: \"d\"", |
| 965 | "}", |
| 966 | "name {", |
| 967 | " key: \"a\"", |
| 968 | " value: \"b\"", |
| 969 | "}"); |
| 970 | checkTextMessage( |
| 971 | "struct(x=struct(y={'a': 1})).to_proto()", |
| 972 | "x {", |
| 973 | " y {", |
| 974 | " key: \"a\"", |
| 975 | " value: 1", |
| 976 | " }", |
| 977 | "}"); |
| 978 | checkTextMessage( |
| 979 | "struct(name={'a': struct(b=1, c=2)}).to_proto()", |
| 980 | "name {", |
| 981 | " key: \"a\"", |
| 982 | " value {", |
| 983 | " b: 1", |
| 984 | " c: 2", |
| 985 | " }", |
| 986 | "}"); |
| 987 | checkTextMessage( |
| 988 | "struct(name={'a': struct(b={4: 'z', 3: 'y'}, c=2)}).to_proto()", |
| 989 | "name {", |
| 990 | " key: \"a\"", |
| 991 | " value {", |
| 992 | " b {", |
| 993 | " key: 4", |
| 994 | " value: \"z\"", |
| 995 | " }", |
| 996 | " b {", |
| 997 | " key: 3", |
| 998 | " value: \"y\"", |
| 999 | " }", |
| 1000 | " c: 2", |
| 1001 | " }", |
| 1002 | "}"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 1005 | @Test |
Vladimir Moskva | 76e31d1 | 2016-12-05 16:28:37 +0000 | [diff] [blame] | 1006 | public void testProtoFieldsOrder() throws Exception { |
| 1007 | checkTextMessage("struct(d=4, b=2, c=3, a=1).to_proto()", "a: 1", "b: 2", "c: 3", "d: 4"); |
| 1008 | } |
| 1009 | |
| 1010 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1011 | public void testTextMessageEscapes() throws Exception { |
| 1012 | checkTextMessage("struct(name='a\"b').to_proto()", "name: \"a\\\"b\""); |
| 1013 | checkTextMessage("struct(name='a\\'b').to_proto()", "name: \"a'b\""); |
| 1014 | checkTextMessage("struct(name='a\\nb').to_proto()", "name: \"a\\nb\""); |
Googler | 4489aaf | 2016-06-17 15:17:37 +0000 | [diff] [blame] | 1015 | |
| 1016 | // struct(name="a\\\"b") -> name: "a\\\"b" |
| 1017 | checkTextMessage("struct(name='a\\\\\\\"b').to_proto()", "name: \"a\\\\\\\"b\""); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 1020 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1021 | public void testTextMessageInvalidElementInListStructure() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1022 | ev.checkEvalErrorContains( |
fangism | ed97d84 | 2019-04-12 14:19:46 -0700 | [diff] [blame] | 1023 | "Invalid text format, expected a struct, a dict, a string, a bool, or " |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1024 | + "an int but got a list for list element in struct field 'a'", |
| 1025 | "struct(a=[['b']]).to_proto()"); |
| 1026 | } |
| 1027 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 1028 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1029 | public void testTextMessageInvalidStructure() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1030 | ev.checkEvalErrorContains( |
fangism | ed97d84 | 2019-04-12 14:19:46 -0700 | [diff] [blame] | 1031 | "Invalid text format, expected a struct, a dict, a string, a bool, or an int " |
Vladimir Moskva | cd12f77 | 2017-01-10 12:47:06 +0000 | [diff] [blame] | 1032 | + "but got a function for struct field 'a'", |
| 1033 | "struct(a=rule).to_proto()"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Erik Abair | 927f459 | 2016-02-29 18:57:22 +0000 | [diff] [blame] | 1036 | private void checkJson(String from, String expected) throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1037 | Object result = ev.eval(from); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1038 | assertThat(result).isEqualTo(expected); |
Erik Abair | 927f459 | 2016-02-29 18:57:22 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | @Test |
| 1042 | public void testJsonBooleanFields() throws Exception { |
| 1043 | checkJson("struct(name=True).to_json()", "{\"name\":true}"); |
| 1044 | checkJson("struct(name=False).to_json()", "{\"name\":false}"); |
| 1045 | } |
| 1046 | |
| 1047 | @Test |
Taras Tsugrii | 7fe7047 | 2018-07-25 13:58:02 -0700 | [diff] [blame] | 1048 | public void testJsonDictFields() throws Exception { |
| 1049 | checkJson("struct(config={}).to_json()", "{\"config\":{}}"); |
| 1050 | checkJson("struct(config={'key': 'value'}).to_json()", "{\"config\":{\"key\":\"value\"}}"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1051 | ev.checkEvalErrorContains( |
Taras Tsugrii | 7fe7047 | 2018-07-25 13:58:02 -0700 | [diff] [blame] | 1052 | "Keys must be a string but got a int for struct field 'config'", |
| 1053 | "struct(config={1:2}).to_json()"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1054 | ev.checkEvalErrorContains( |
Taras Tsugrii | 7fe7047 | 2018-07-25 13:58:02 -0700 | [diff] [blame] | 1055 | "Keys must be a string but got a int for dict value 'foo'", |
| 1056 | "struct(config={'foo':{1:2}}).to_json()"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1057 | ev.checkEvalErrorContains( |
Taras Tsugrii | 7fe7047 | 2018-07-25 13:58:02 -0700 | [diff] [blame] | 1058 | "Keys must be a string but got a bool for struct field 'config'", |
| 1059 | "struct(config={True: False}).to_json()"); |
| 1060 | } |
| 1061 | |
| 1062 | @Test |
Erik Abair | 927f459 | 2016-02-29 18:57:22 +0000 | [diff] [blame] | 1063 | public void testJsonEncoding() throws Exception { |
| 1064 | checkJson("struct(name='value').to_json()", "{\"name\":\"value\"}"); |
| 1065 | checkJson("struct(name=['a', 'b']).to_json()", "{\"name\":[\"a\",\"b\"]}"); |
| 1066 | checkJson("struct(name=123).to_json()", "{\"name\":123}"); |
| 1067 | checkJson("struct(name=[1, 2, 3]).to_json()", "{\"name\":[1,2,3]}"); |
| 1068 | checkJson("struct(a=struct(b='b')).to_json()", "{\"a\":{\"b\":\"b\"}}"); |
| 1069 | checkJson("struct(a=[struct(b='x'), struct(b='y')]).to_json()", |
| 1070 | "{\"a\":[{\"b\":\"x\"},{\"b\":\"y\"}]}"); |
| 1071 | checkJson("struct(a=struct(b=struct(c='c'))).to_json()", |
| 1072 | "{\"a\":{\"b\":{\"c\":\"c\"}}}"); |
| 1073 | } |
| 1074 | |
| 1075 | @Test |
| 1076 | public void testJsonEscapes() throws Exception { |
| 1077 | checkJson("struct(name='a\"b').to_json()", "{\"name\":\"a\\\"b\"}"); |
| 1078 | checkJson("struct(name='a\\'b').to_json()", "{\"name\":\"a'b\"}"); |
Erik Abair | ec1f2b9 | 2016-03-01 00:45:33 +0000 | [diff] [blame] | 1079 | checkJson("struct(name='a\\\\b').to_json()", "{\"name\":\"a\\\\b\"}"); |
Erik Abair | 927f459 | 2016-02-29 18:57:22 +0000 | [diff] [blame] | 1080 | checkJson("struct(name='a\\nb').to_json()", "{\"name\":\"a\\nb\"}"); |
Erik Abair | ec1f2b9 | 2016-03-01 00:45:33 +0000 | [diff] [blame] | 1081 | checkJson("struct(name='a\\rb').to_json()", "{\"name\":\"a\\rb\"}"); |
| 1082 | checkJson("struct(name='a\\tb').to_json()", "{\"name\":\"a\\tb\"}"); |
Erik Abair | 927f459 | 2016-02-29 18:57:22 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | @Test |
| 1086 | public void testJsonNestedListStructure() throws Exception { |
| 1087 | checkJson("struct(a=[['b']]).to_json()", "{\"a\":[[\"b\"]]}"); |
| 1088 | } |
| 1089 | |
| 1090 | @Test |
| 1091 | public void testJsonInvalidStructure() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1092 | ev.checkEvalErrorContains( |
Erik Abair | 927f459 | 2016-02-29 18:57:22 +0000 | [diff] [blame] | 1093 | "Invalid text format, expected a struct, a string, a bool, or an int but got a " |
Vladimir Moskva | cd12f77 | 2017-01-10 12:47:06 +0000 | [diff] [blame] | 1094 | + "function for struct field 'a'", |
| 1095 | "struct(a=rule).to_json()"); |
Erik Abair | 927f459 | 2016-02-29 18:57:22 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 1098 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1099 | public void testLabelAttrWrongDefault() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1100 | ev.checkEvalErrorContains( |
adonovan | 3f602f2 | 2020-01-08 10:28:10 -0800 | [diff] [blame] | 1101 | "got value of type 'int', want 'Label or string or LateBoundDefault or function or" |
| 1102 | + " NoneType'", |
vladmos | 9c787fa | 2017-07-04 11:45:22 -0400 | [diff] [blame] | 1103 | "attr.label(default = 123)"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 1106 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1107 | public void testLabelGetRelative() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1108 | assertThat(ev.eval("Label('//foo:bar').relative('baz')").toString()).isEqualTo("//foo:baz"); |
| 1109 | assertThat(ev.eval("Label('//foo:bar').relative('//baz:qux')").toString()) |
| 1110 | .isEqualTo("//baz:qux"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 1113 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1114 | public void testLabelGetRelativeSyntaxError() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1115 | ev.checkEvalErrorContains( |
Damien Martin-Guillerez | 934c1d5 | 2017-03-03 14:44:56 +0000 | [diff] [blame] | 1116 | "invalid target name 'bad//syntax': target names may not contain '//' path separators", |
| 1117 | "Label('//foo:bar').relative('bad//syntax')"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1118 | } |
Greg Estren | 223976c | 2016-02-04 22:40:56 +0000 | [diff] [blame] | 1119 | |
| 1120 | @Test |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1121 | public void testStructCreation() throws Exception { |
| 1122 | // TODO(fwe): cannot be handled by current testing suite |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1123 | ev.exec("x = struct(a = 1, b = 2)"); |
| 1124 | assertThat(ev.lookup("x")).isInstanceOf(ClassObject.class); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | @Test |
| 1128 | public void testStructFields() throws Exception { |
| 1129 | // TODO(fwe): cannot be handled by current testing suite |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1130 | ev.exec("x = struct(a = 1, b = 2)"); |
| 1131 | ClassObject x = (ClassObject) ev.lookup("x"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1132 | assertThat(x.getValue("a")).isEqualTo(1); |
| 1133 | assertThat(x.getValue("b")).isEqualTo(2); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | @Test |
Vladimir Moskva | fdfa988 | 2016-11-18 16:24:20 +0000 | [diff] [blame] | 1137 | public void testStructEquality() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1138 | assertThat((Boolean) ev.eval("struct(a = 1, b = 2) == struct(b = 2, a = 1)")).isTrue(); |
| 1139 | assertThat((Boolean) ev.eval("struct(a = 1) == struct(a = 1, b = 2)")).isFalse(); |
| 1140 | assertThat((Boolean) ev.eval("struct(a = 1, b = 2) == struct(a = 1)")).isFalse(); |
Vladimir Moskva | fdfa988 | 2016-11-18 16:24:20 +0000 | [diff] [blame] | 1141 | // Compare a recursive object to itself to make sure reference equality is checked |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1142 | ev.exec("s = struct(a = 1, b = []); s.b.append(s)"); |
| 1143 | assertThat((Boolean) ev.eval("s == s")).isTrue(); |
| 1144 | assertThat((Boolean) ev.eval("struct(a = 1, b = 2) == struct(a = 1, b = 3)")).isFalse(); |
| 1145 | assertThat((Boolean) ev.eval("struct(a = 1) == [1]")).isFalse(); |
| 1146 | assertThat((Boolean) ev.eval("[1] == struct(a = 1)")).isFalse(); |
| 1147 | assertThat((Boolean) ev.eval("struct() == struct()")).isTrue(); |
| 1148 | assertThat((Boolean) ev.eval("struct() == struct(a = 1)")).isFalse(); |
Vladimir Moskva | fdfa988 | 2016-11-18 16:24:20 +0000 | [diff] [blame] | 1149 | |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1150 | ev.exec("foo = provider(); bar = provider()"); |
| 1151 | assertThat((Boolean) ev.eval("struct(a = 1) == foo(a = 1)")).isFalse(); |
| 1152 | assertThat((Boolean) ev.eval("foo(a = 1) == struct(a = 1)")).isFalse(); |
| 1153 | assertThat((Boolean) ev.eval("foo(a = 1) == bar(a = 1)")).isFalse(); |
| 1154 | assertThat((Boolean) ev.eval("foo(a = 1) == foo(a = 1)")).isTrue(); |
Vladimir Moskva | fdfa988 | 2016-11-18 16:24:20 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | @Test |
Vladimir Moskva | 76e31d1 | 2016-12-05 16:28:37 +0000 | [diff] [blame] | 1158 | public void testStructIncomparability() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1159 | ev.checkEvalErrorContains("Cannot compare structs", "struct(a = 1) < struct(a = 2)"); |
| 1160 | ev.checkEvalErrorContains("Cannot compare structs", "struct(a = 1) > struct(a = 2)"); |
| 1161 | ev.checkEvalErrorContains("Cannot compare structs", "struct(a = 1) <= struct(a = 2)"); |
| 1162 | ev.checkEvalErrorContains("Cannot compare structs", "struct(a = 1) >= struct(a = 2)"); |
Vladimir Moskva | 76e31d1 | 2016-12-05 16:28:37 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | @Test |
gregce | d281df7 | 2020-05-11 12:27:06 -0700 | [diff] [blame] | 1166 | public void testStructAccessingFieldsFromStarlark() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1167 | ev.exec("x = struct(a = 1, b = 2)", "x1 = x.a", "x2 = x.b"); |
| 1168 | assertThat(ev.lookup("x1")).isEqualTo(1); |
| 1169 | assertThat(ev.lookup("x2")).isEqualTo(2); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | @Test |
| 1173 | public void testStructAccessingUnknownField() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1174 | ev.checkEvalErrorContains( |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 1175 | "'struct' value has no field or method 'c'\n" + "Available attributes: a, b", |
Googler | ee05663 | 2019-10-10 13:04:49 -0700 | [diff] [blame] | 1176 | "x = struct(a = 1, b = 2)", |
| 1177 | "y = x.c"); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | @Test |
| 1181 | public void testStructAccessingUnknownFieldWithArgs() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1182 | ev.checkEvalErrorContains( |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 1183 | "'struct' value has no field or method 'c'", "x = struct(a = 1, b = 2)", "y = x.c()"); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | @Test |
| 1187 | public void testStructAccessingNonFunctionFieldWithArgs() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1188 | ev.checkEvalErrorContains( |
Googler | ee05663 | 2019-10-10 13:04:49 -0700 | [diff] [blame] | 1189 | "'int' object is not callable", "x = struct(a = 1, b = 2)", "x1 = x.a(1)"); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | @Test |
| 1193 | public void testStructAccessingFunctionFieldWithArgs() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1194 | ev.exec("def f(x): return x+5", "x = struct(a = f, b = 2)", "x1 = x.a(1)"); |
| 1195 | assertThat(ev.lookup("x1")).isEqualTo(6); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | @Test |
| 1199 | public void testStructPosArgs() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1200 | ev.checkEvalErrorContains( |
| 1201 | "struct() got unexpected positional argument", "x = struct(1, b = 2)"); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | @Test |
| 1205 | public void testStructConcatenationFieldNames() throws Exception { |
| 1206 | // TODO(fwe): cannot be handled by current testing suite |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1207 | ev.exec( |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 1208 | "x = struct(a = 1, b = 2)", // |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1209 | "y = struct(c = 1, d = 2)", |
| 1210 | "z = x + y\n"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1211 | StructImpl z = (StructImpl) ev.lookup("z"); |
jcater | 91ed0f3 | 2019-11-07 10:07:16 -0800 | [diff] [blame] | 1212 | assertThat(z.getFieldNames()).containsExactly("a", "b", "c", "d"); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | @Test |
| 1216 | public void testStructConcatenationFieldValues() throws Exception { |
| 1217 | // TODO(fwe): cannot be handled by current testing suite |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1218 | ev.exec( |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 1219 | "x = struct(a = 1, b = 2)", // |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1220 | "y = struct(c = 1, d = 2)", |
| 1221 | "z = x + y\n"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1222 | StructImpl z = (StructImpl) ev.lookup("z"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1223 | assertThat(z.getValue("a")).isEqualTo(1); |
| 1224 | assertThat(z.getValue("b")).isEqualTo(2); |
| 1225 | assertThat(z.getValue("c")).isEqualTo(1); |
| 1226 | assertThat(z.getValue("d")).isEqualTo(2); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | @Test |
| 1230 | public void testStructConcatenationCommonFields() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1231 | ev.checkEvalErrorContains( |
adonovan | 553dc6c | 2019-12-10 11:22:48 -0800 | [diff] [blame] | 1232 | "cannot add struct instances with common field 'a'", |
Googler | ee05663 | 2019-10-10 13:04:49 -0700 | [diff] [blame] | 1233 | "x = struct(a = 1, b = 2)", |
| 1234 | "y = struct(c = 1, a = 2)", |
| 1235 | "z = x + y\n"); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | @Test |
| 1239 | public void testConditionalStructConcatenation() throws Exception { |
| 1240 | // TODO(fwe): cannot be handled by current testing suite |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1241 | ev.exec( |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 1242 | "def func():", |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1243 | " x = struct(a = 1, b = 2)", |
| 1244 | " if True:", |
| 1245 | " x += struct(c = 1, d = 2)", |
| 1246 | " return x", |
| 1247 | "x = func()"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1248 | StructImpl x = (StructImpl) ev.lookup("x"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1249 | assertThat(x.getValue("a")).isEqualTo(1); |
| 1250 | assertThat(x.getValue("b")).isEqualTo(2); |
| 1251 | assertThat(x.getValue("c")).isEqualTo(1); |
| 1252 | assertThat(x.getValue("d")).isEqualTo(2); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | @Test |
| 1256 | public void testGetattrNoAttr() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1257 | ev.checkEvalErrorContains( |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 1258 | "'struct' value has no field or method 'b'\nAvailable attributes: a", |
Googler | 055d6c6 | 2018-05-22 13:21:04 -0700 | [diff] [blame] | 1259 | "s = struct(a='val')", |
| 1260 | "getattr(s, 'b')"); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | @Test |
| 1264 | public void testGetattr() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1265 | ev.exec("s = struct(a='val')", "x = getattr(s, 'a')", "y = getattr(s, 'b', 'def')"); |
| 1266 | assertThat(ev.lookup("x")).isEqualTo("val"); |
| 1267 | assertThat(ev.lookup("y")).isEqualTo("def"); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | @Test |
| 1271 | public void testHasattr() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1272 | ev.exec( |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 1273 | "s = struct(a=1)", // |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1274 | "x = hasattr(s, 'a')", |
| 1275 | "y = hasattr(s, 'b')\n"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1276 | assertThat(ev.lookup("x")).isEqualTo(true); |
| 1277 | assertThat(ev.lookup("y")).isEqualTo(false); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | @Test |
| 1281 | public void testStructStr() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1282 | assertThat(ev.eval("str(struct(x = 2, y = 3, z = 4))")) |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1283 | .isEqualTo("struct(x = 2, y = 3, z = 4)"); |
| 1284 | } |
| 1285 | |
| 1286 | @Test |
| 1287 | public void testStructsInSets() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1288 | ev.exec("depset([struct(a='a')])"); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | @Test |
Vladimir Moskva | 76e31d1 | 2016-12-05 16:28:37 +0000 | [diff] [blame] | 1292 | public void testStructsInDicts() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1293 | ev.exec("d = {struct(a = 1): 'aa', struct(b = 2): 'bb'}"); |
| 1294 | assertThat(ev.eval("d[struct(a = 1)]")).isEqualTo("aa"); |
| 1295 | assertThat(ev.eval("d[struct(b = 2)]")).isEqualTo("bb"); |
| 1296 | assertThat(ev.eval("str([d[k] for k in d])")).isEqualTo("[\"aa\", \"bb\"]"); |
Vladimir Moskva | 76e31d1 | 2016-12-05 16:28:37 +0000 | [diff] [blame] | 1297 | |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1298 | ev.checkEvalErrorContains("unhashable type: 'struct'", "{struct(a = []): 'foo'}"); |
Vladimir Moskva | 76e31d1 | 2016-12-05 16:28:37 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | @Test |
Vladimir Moskva | 1077038 | 2016-08-23 15:04:54 +0000 | [diff] [blame] | 1302 | public void testStructDictMembersAreMutable() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1303 | ev.exec( |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 1304 | "s = struct(x = {'a' : 1})", // |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1305 | "s.x['b'] = 2\n"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1306 | assertThat(((StructImpl) ev.lookup("s")).getValue("x")) |
| 1307 | .isEqualTo(ImmutableMap.of("a", 1, "b", 2)); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | @Test |
adonovan | 0de69d0 | 2020-05-07 20:39:16 -0700 | [diff] [blame] | 1311 | public void testDepsetGoodCompositeItem() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1312 | ev.exec("def func():", " return depset([struct(a='a')])", "s = func()"); |
| 1313 | ImmutableList<?> result = ((Depset) ev.lookup("s")).toList(); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1314 | assertThat(result).hasSize(1); |
adonovan | 0de69d0 | 2020-05-07 20:39:16 -0700 | [diff] [blame] | 1315 | assertThat(result.get(0)).isInstanceOf(StructImpl.class); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 1318 | private static StructImpl makeStruct(String field, Object value) { |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 1319 | return StructProvider.STRUCT.create(ImmutableMap.of(field, value), "no field '%'"); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 1322 | private static StructImpl makeBigStruct(@Nullable Mutability mu) { |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1323 | // struct(a=[struct(x={1:1}), ()], b=(), c={2:2}) |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 1324 | return StructProvider.STRUCT.create( |
Dmitry Lomov | ea9de07 | 2016-08-09 09:35:40 +0000 | [diff] [blame] | 1325 | ImmutableMap.<String, Object>of( |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 1326 | "a", |
Googler | 942e1c4 | 2019-11-12 13:11:44 -0800 | [diff] [blame] | 1327 | StarlarkList.<Object>of( |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 1328 | mu, |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 1329 | StructProvider.STRUCT.create( |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 1330 | ImmutableMap.<String, Object>of("x", Dict.<Object, Object>of(mu, 1, 1)), |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 1331 | "no field '%s'"), |
| 1332 | Tuple.of()), |
Dmitry Lomov | ea9de07 | 2016-08-09 09:35:40 +0000 | [diff] [blame] | 1333 | "b", Tuple.of(), |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 1334 | "c", Dict.<Object, Object>of(mu, 2, 2)), |
Dmitry Lomov | ea9de07 | 2016-08-09 09:35:40 +0000 | [diff] [blame] | 1335 | "no field '%s'"); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | @Test |
| 1339 | public void testStructMutabilityShallow() throws Exception { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1340 | assertThat(EvalUtils.isImmutable(makeStruct("a", 1))).isTrue(); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 1343 | private static StarlarkList<Object> makeList(@Nullable Mutability mu) { |
| 1344 | return StarlarkList.<Object>of(mu, 1, 2, 3); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1347 | @Test |
| 1348 | public void testStructMutabilityDeep() throws Exception { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1349 | assertThat(EvalUtils.isImmutable(Tuple.<Object>of(makeList(null)))).isTrue(); |
| 1350 | assertThat(EvalUtils.isImmutable(makeStruct("a", makeList(null)))).isTrue(); |
| 1351 | assertThat(EvalUtils.isImmutable(makeBigStruct(null))).isTrue(); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1352 | |
adonovan | 09d1370 | 2020-05-19 08:26:55 -0700 | [diff] [blame] | 1353 | Mutability mu = Mutability.create("test"); |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 1354 | assertThat(EvalUtils.isImmutable(Tuple.<Object>of(makeList(mu)))).isFalse(); |
| 1355 | assertThat(EvalUtils.isImmutable(makeStruct("a", makeList(mu)))).isFalse(); |
| 1356 | assertThat(EvalUtils.isImmutable(makeBigStruct(mu))).isFalse(); |
Dmitry Lomov | cdb6ef5 | 2016-08-05 08:38:26 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Dmitry Lomov | 8a07a95 | 2016-08-10 18:29:04 +0000 | [diff] [blame] | 1359 | @Test |
| 1360 | public void declaredProviders() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1361 | evalAndExport(ev, "data = provider()", "d = data(x = 1, y ='abc')", "d_x = d.x", "d_y = d.y"); |
| 1362 | assertThat(ev.lookup("d_x")).isEqualTo(1); |
| 1363 | assertThat(ev.lookup("d_y")).isEqualTo("abc"); |
| 1364 | StarlarkProvider dataConstructor = (StarlarkProvider) ev.lookup("data"); |
| 1365 | StructImpl data = (StructImpl) ev.lookup("d"); |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 1366 | assertThat(data.getProvider()).isEqualTo(dataConstructor); |
Dmitry Lomov | 8a07a95 | 2016-08-10 18:29:04 +0000 | [diff] [blame] | 1367 | assertThat(dataConstructor.isExported()).isTrue(); |
| 1368 | assertThat(dataConstructor.getPrintableName()).isEqualTo("data"); |
gregce | b6eafee | 2020-04-20 08:04:51 -0700 | [diff] [blame] | 1369 | assertThat(dataConstructor.getKey()).isEqualTo(new StarlarkProvider.Key(FAKE_LABEL, "data")); |
Dmitry Lomov | 8a07a95 | 2016-08-10 18:29:04 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | @Test |
| 1373 | public void declaredProvidersConcatSuccess() throws Exception { |
| 1374 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1375 | ev, |
Dmitry Lomov | 8a07a95 | 2016-08-10 18:29:04 +0000 | [diff] [blame] | 1376 | "data = provider()", |
| 1377 | "dx = data(x = 1)", |
| 1378 | "dy = data(y = 'abc')", |
| 1379 | "dxy = dx + dy", |
| 1380 | "x = dxy.x", |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1381 | "y = dxy.y"); |
| 1382 | assertThat(ev.lookup("x")).isEqualTo(1); |
| 1383 | assertThat(ev.lookup("y")).isEqualTo("abc"); |
| 1384 | StarlarkProvider dataConstructor = (StarlarkProvider) ev.lookup("data"); |
| 1385 | StructImpl dx = (StructImpl) ev.lookup("dx"); |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 1386 | assertThat(dx.getProvider()).isEqualTo(dataConstructor); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1387 | StructImpl dy = (StructImpl) ev.lookup("dy"); |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 1388 | assertThat(dy.getProvider()).isEqualTo(dataConstructor); |
Dmitry Lomov | 8a07a95 | 2016-08-10 18:29:04 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | @Test |
| 1392 | public void declaredProvidersConcatError() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1393 | evalAndExport(ev, "data1 = provider()", "data2 = provider()"); |
Dmitry Lomov | 8a07a95 | 2016-08-10 18:29:04 +0000 | [diff] [blame] | 1394 | |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1395 | ev.checkEvalErrorContains( |
Googler | ee05663 | 2019-10-10 13:04:49 -0700 | [diff] [blame] | 1396 | "Cannot use '+' operator on instances of different providers (data1 and data2)", |
Dmitry Lomov | 8a07a95 | 2016-08-10 18:29:04 +0000 | [diff] [blame] | 1397 | "d1 = data1(x = 1)", |
| 1398 | "d2 = data2(y = 2)", |
Googler | ee05663 | 2019-10-10 13:04:49 -0700 | [diff] [blame] | 1399 | "d = d1 + d2"); |
Dmitry Lomov | 8a07a95 | 2016-08-10 18:29:04 +0000 | [diff] [blame] | 1400 | } |
Dmitry Lomov | 94b2c88 | 2016-09-25 20:24:24 +0000 | [diff] [blame] | 1401 | |
| 1402 | @Test |
Benjamin Peterson | 8610d97 | 2017-11-27 03:14:43 -0800 | [diff] [blame] | 1403 | public void declaredProvidersWithFieldsConcatSuccess() throws Exception { |
| 1404 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1405 | ev, |
Benjamin Peterson | 8610d97 | 2017-11-27 03:14:43 -0800 | [diff] [blame] | 1406 | "data = provider(fields=['f1', 'f2'])", |
| 1407 | "d1 = data(f1 = 4)", |
| 1408 | "d2 = data(f2 = 5)", |
| 1409 | "d3 = d1 + d2", |
| 1410 | "f1 = d3.f1", |
| 1411 | "f2 = d3.f2"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1412 | assertThat(ev.lookup("f1")).isEqualTo(4); |
| 1413 | assertThat(ev.lookup("f2")).isEqualTo(5); |
Benjamin Peterson | 8610d97 | 2017-11-27 03:14:43 -0800 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | @Test |
| 1417 | public void declaredProvidersWithFieldsConcatError() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1418 | evalAndExport(ev, "data1 = provider(fields=['f1', 'f2'])", "data2 = provider(fields=['f3'])"); |
| 1419 | ev.checkEvalErrorContains( |
brandjon | a29648a | 2018-01-10 08:38:43 -0800 | [diff] [blame] | 1420 | "Cannot use '+' operator on instances of different providers (data1 and data2)", |
Benjamin Peterson | 8610d97 | 2017-11-27 03:14:43 -0800 | [diff] [blame] | 1421 | "d1 = data1(f1=1, f2=2)", |
| 1422 | "d2 = data2(f3=3)", |
| 1423 | "d = d1 + d2"); |
| 1424 | } |
| 1425 | |
| 1426 | @Test |
| 1427 | public void declaredProvidersWithOverlappingFieldsConcatError() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1428 | evalAndExport(ev, "data = provider(fields=['f1', 'f2'])"); |
| 1429 | ev.checkEvalErrorContains( |
adonovan | 553dc6c | 2019-12-10 11:22:48 -0800 | [diff] [blame] | 1430 | "cannot add struct instances with common field 'f1'", |
Benjamin Peterson | 8610d97 | 2017-11-27 03:14:43 -0800 | [diff] [blame] | 1431 | "d1 = data(f1 = 4)", |
| 1432 | "d2 = data(f1 = 5)", |
| 1433 | "d1 + d2"); |
| 1434 | } |
| 1435 | |
| 1436 | @Test |
Dmitry Lomov | 94b2c88 | 2016-09-25 20:24:24 +0000 | [diff] [blame] | 1437 | public void structsAsDeclaredProvidersTest() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1438 | evalAndExport(ev, "data = struct(x = 1)"); |
| 1439 | StructImpl data = (StructImpl) ev.lookup("data"); |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 1440 | assertThat(StructProvider.STRUCT.isExported()).isTrue(); |
| 1441 | assertThat(data.getProvider()).isEqualTo(StructProvider.STRUCT); |
| 1442 | assertThat(data.getProvider().getKey()).isEqualTo(StructProvider.STRUCT.getKey()); |
Dmitry Lomov | 94b2c88 | 2016-09-25 20:24:24 +0000 | [diff] [blame] | 1443 | } |
Dmitry Lomov | 0692c7f | 2016-09-30 16:43:30 +0000 | [diff] [blame] | 1444 | |
| 1445 | @Test |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 1446 | public void declaredProvidersDoc() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1447 | evalAndExport(ev, "data1 = provider(doc='foo')"); |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | @Test |
| 1451 | public void declaredProvidersBadTypeForDoc() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1452 | ev.checkEvalErrorContains("got value of type 'int', want 'string'", "provider(doc = 1)"); |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | @Test |
Dmitry Lomov | 0692c7f | 2016-09-30 16:43:30 +0000 | [diff] [blame] | 1456 | public void aspectAllAttrs() throws Exception { |
| 1457 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1458 | ev, |
| 1459 | "def _impl(target, ctx):", // |
Dmitry Lomov | 0692c7f | 2016-09-30 16:43:30 +0000 | [diff] [blame] | 1460 | " pass", |
| 1461 | "my_aspect = aspect(_impl, attr_aspects=['*'])"); |
| 1462 | |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1463 | StarlarkDefinedAspect myAspect = (StarlarkDefinedAspect) ev.lookup("my_aspect"); |
lberki | 3bd2839 | 2019-02-05 01:01:02 -0800 | [diff] [blame] | 1464 | assertThat(myAspect.getDefinition(AspectParameters.EMPTY).propagateAlong("foo")).isTrue(); |
Dmitry Lomov | 0692c7f | 2016-09-30 16:43:30 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1467 | @Test |
| 1468 | public void aspectRequiredAspectProvidersSingle() throws Exception { |
| 1469 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1470 | ev, |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1471 | "def _impl(target, ctx):", |
| 1472 | " pass", |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 1473 | "cc = provider()", |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1474 | "my_aspect = aspect(_impl, required_aspect_providers=['java', cc])"); |
| 1475 | StarlarkDefinedAspect myAspect = (StarlarkDefinedAspect) ev.lookup("my_aspect"); |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1476 | RequiredProviders requiredProviders = myAspect.getDefinition(AspectParameters.EMPTY) |
| 1477 | .getRequiredProvidersForAspects(); |
| 1478 | assertThat(requiredProviders.isSatisfiedBy(AdvertisedProviderSet.ANY)).isTrue(); |
| 1479 | assertThat(requiredProviders.isSatisfiedBy(AdvertisedProviderSet.EMPTY)).isFalse(); |
gregce | d7c1cef | 2020-05-12 07:51:48 -0700 | [diff] [blame] | 1480 | assertThat( |
| 1481 | requiredProviders.isSatisfiedBy( |
| 1482 | AdvertisedProviderSet.builder() |
| 1483 | .addStarlark(declared("cc")) |
| 1484 | .addStarlark("java") |
| 1485 | .build())) |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1486 | .isTrue(); |
gregce | d7c1cef | 2020-05-12 07:51:48 -0700 | [diff] [blame] | 1487 | assertThat( |
| 1488 | requiredProviders.isSatisfiedBy( |
| 1489 | AdvertisedProviderSet.builder().addStarlark("cc").build())) |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1490 | .isFalse(); |
| 1491 | } |
| 1492 | |
| 1493 | @Test |
| 1494 | public void aspectRequiredAspectProvidersAlternatives() throws Exception { |
| 1495 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1496 | ev, |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1497 | "def _impl(target, ctx):", |
| 1498 | " pass", |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 1499 | "cc = provider()", |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1500 | "my_aspect = aspect(_impl, required_aspect_providers=[['java'], [cc]])"); |
| 1501 | StarlarkDefinedAspect myAspect = (StarlarkDefinedAspect) ev.lookup("my_aspect"); |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1502 | RequiredProviders requiredProviders = myAspect.getDefinition(AspectParameters.EMPTY) |
| 1503 | .getRequiredProvidersForAspects(); |
| 1504 | assertThat(requiredProviders.isSatisfiedBy(AdvertisedProviderSet.ANY)).isTrue(); |
| 1505 | assertThat(requiredProviders.isSatisfiedBy(AdvertisedProviderSet.EMPTY)).isFalse(); |
gregce | d7c1cef | 2020-05-12 07:51:48 -0700 | [diff] [blame] | 1506 | assertThat( |
| 1507 | requiredProviders.isSatisfiedBy( |
| 1508 | AdvertisedProviderSet.builder().addStarlark("java").build())) |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1509 | .isTrue(); |
gregce | d7c1cef | 2020-05-12 07:51:48 -0700 | [diff] [blame] | 1510 | assertThat( |
| 1511 | requiredProviders.isSatisfiedBy( |
| 1512 | AdvertisedProviderSet.builder().addStarlark(declared("cc")).build())) |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1513 | .isTrue(); |
gregce | d7c1cef | 2020-05-12 07:51:48 -0700 | [diff] [blame] | 1514 | assertThat( |
| 1515 | requiredProviders.isSatisfiedBy( |
| 1516 | AdvertisedProviderSet.builder().addStarlark("prolog").build())) |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1517 | .isFalse(); |
| 1518 | } |
| 1519 | |
| 1520 | @Test |
| 1521 | public void aspectRequiredAspectProvidersEmpty() throws Exception { |
| 1522 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1523 | ev, |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1524 | "def _impl(target, ctx):", |
| 1525 | " pass", |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1526 | "my_aspect = aspect(_impl, required_aspect_providers=[])"); |
| 1527 | StarlarkDefinedAspect myAspect = (StarlarkDefinedAspect) ev.lookup("my_aspect"); |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1528 | RequiredProviders requiredProviders = myAspect.getDefinition(AspectParameters.EMPTY) |
| 1529 | .getRequiredProvidersForAspects(); |
| 1530 | assertThat(requiredProviders.isSatisfiedBy(AdvertisedProviderSet.ANY)).isFalse(); |
| 1531 | assertThat(requiredProviders.isSatisfiedBy(AdvertisedProviderSet.EMPTY)).isFalse(); |
| 1532 | } |
| 1533 | |
| 1534 | @Test |
| 1535 | public void aspectRequiredAspectProvidersDefault() throws Exception { |
| 1536 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1537 | ev, |
| 1538 | "def _impl(target, ctx):", // |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1539 | " pass", |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1540 | "my_aspect = aspect(_impl)"); |
| 1541 | StarlarkDefinedAspect myAspect = (StarlarkDefinedAspect) ev.lookup("my_aspect"); |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1542 | RequiredProviders requiredProviders = myAspect.getDefinition(AspectParameters.EMPTY) |
| 1543 | .getRequiredProvidersForAspects(); |
| 1544 | assertThat(requiredProviders.isSatisfiedBy(AdvertisedProviderSet.ANY)).isFalse(); |
| 1545 | assertThat(requiredProviders.isSatisfiedBy(AdvertisedProviderSet.EMPTY)).isFalse(); |
| 1546 | } |
| 1547 | |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 1548 | @Test |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 1549 | public void aspectProvides() throws Exception { |
| 1550 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1551 | ev, |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 1552 | "def _impl(target, ctx):", |
| 1553 | " pass", |
| 1554 | "y = provider()", |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1555 | "my_aspect = aspect(_impl, provides = ['x', y])"); |
| 1556 | StarlarkDefinedAspect myAspect = (StarlarkDefinedAspect) ev.lookup("my_aspect"); |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 1557 | AdvertisedProviderSet advertisedProviders = myAspect.getDefinition(AspectParameters.EMPTY) |
| 1558 | .getAdvertisedProviders(); |
| 1559 | assertThat(advertisedProviders.canHaveAnyProvider()).isFalse(); |
gregce | d7c1cef | 2020-05-12 07:51:48 -0700 | [diff] [blame] | 1560 | assertThat(advertisedProviders.getStarlarkProviders()) |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 1561 | .containsExactly(legacy("x"), declared("y")); |
| 1562 | } |
| 1563 | |
| 1564 | @Test |
| 1565 | public void aspectProvidesError() throws Exception { |
| 1566 | ev.setFailFast(false); |
| 1567 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1568 | ev, |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 1569 | "def _impl(target, ctx):", |
| 1570 | " pass", |
| 1571 | "y = provider()", |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1572 | "my_aspect = aspect(_impl, provides = ['x', 1])"); |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 1573 | MoreAsserts.assertContainsEvent(ev.getEventCollector(), |
| 1574 | " Illegal argument: element in 'provides' is of unexpected type." |
cparsons | aab9868 | 2018-04-27 13:04:59 -0700 | [diff] [blame] | 1575 | + " Should be list of providers, but got item of type int. "); |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 1578 | @Test |
| 1579 | public void aspectDoc() throws Exception { |
| 1580 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1581 | ev, |
| 1582 | "def _impl(target, ctx):", // |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 1583 | " pass", |
| 1584 | "my_aspect = aspect(_impl, doc='foo')"); |
| 1585 | } |
| 1586 | |
| 1587 | @Test |
| 1588 | public void aspectBadTypeForDoc() throws Exception { |
Googler | 3fcfbe1 | 2019-08-28 08:10:11 -0700 | [diff] [blame] | 1589 | registerDummyStarlarkFunction(); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1590 | ev.checkEvalErrorContains("got value of type 'int', want 'string'", "aspect(impl, doc = 1)"); |
allevato | 45b79e5 | 2017-07-07 21:40:50 +0200 | [diff] [blame] | 1591 | } |
| 1592 | |
Dmitry Lomov | 8ff5a87 | 2017-03-04 00:58:14 +0000 | [diff] [blame] | 1593 | @Test |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 1594 | public void fancyExports() throws Exception { |
| 1595 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1596 | ev, |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 1597 | "def _impla(target, ctx): pass", |
| 1598 | "p, (a, p1) = [", |
| 1599 | " provider(),", |
| 1600 | " [ aspect(_impla),", |
| 1601 | " provider() ]", |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1602 | "]"); |
| 1603 | StarlarkProvider p = (StarlarkProvider) ev.lookup("p"); |
| 1604 | StarlarkDefinedAspect a = (StarlarkDefinedAspect) ev.lookup("a"); |
| 1605 | StarlarkProvider p1 = (StarlarkProvider) ev.lookup("p1"); |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 1606 | assertThat(p.getPrintableName()).isEqualTo("p"); |
gregce | b6eafee | 2020-04-20 08:04:51 -0700 | [diff] [blame] | 1607 | assertThat(p.getKey()).isEqualTo(new StarlarkProvider.Key(FAKE_LABEL, "p")); |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 1608 | assertThat(p1.getPrintableName()).isEqualTo("p1"); |
gregce | b6eafee | 2020-04-20 08:04:51 -0700 | [diff] [blame] | 1609 | assertThat(p1.getKey()).isEqualTo(new StarlarkProvider.Key(FAKE_LABEL, "p1")); |
gregce | 18694cd | 2020-05-12 15:40:05 -0700 | [diff] [blame] | 1610 | assertThat(a.getAspectClass()).isEqualTo(new StarlarkAspectClass(FAKE_LABEL, "a")); |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | @Test |
| 1614 | public void multipleTopLevels() throws Exception { |
| 1615 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1616 | ev, |
| 1617 | "p = provider()", // |
| 1618 | "p1 = p"); |
| 1619 | StarlarkProvider p = (StarlarkProvider) ev.lookup("p"); |
| 1620 | StarlarkProvider p1 = (StarlarkProvider) ev.lookup("p1"); |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 1621 | assertThat(p).isEqualTo(p1); |
gregce | b6eafee | 2020-04-20 08:04:51 -0700 | [diff] [blame] | 1622 | assertThat(p.getKey()).isEqualTo(new StarlarkProvider.Key(FAKE_LABEL, "p")); |
| 1623 | assertThat(p1.getKey()).isEqualTo(new StarlarkProvider.Key(FAKE_LABEL, "p")); |
Dmitry Lomov | 950310f | 2017-03-01 17:45:12 +0000 | [diff] [blame] | 1624 | } |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 1625 | |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1626 | @Test |
| 1627 | public void providerWithFields() throws Exception { |
| 1628 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1629 | ev, |
| 1630 | "p = provider(fields = ['x', 'y'])", // |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1631 | "p1 = p(x = 1, y = 2)", |
| 1632 | "x = p1.x", |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1633 | "y = p1.y"); |
| 1634 | StarlarkProvider p = (StarlarkProvider) ev.lookup("p"); |
| 1635 | StarlarkInfo p1 = (StarlarkInfo) ev.lookup("p1"); |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1636 | |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1637 | assertThat(p1.getProvider()).isEqualTo(p); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1638 | assertThat(ev.lookup("x")).isEqualTo(1); |
| 1639 | assertThat(ev.lookup("y")).isEqualTo(2); |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | @Test |
| 1643 | public void providerWithFieldsDict() throws Exception { |
| 1644 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1645 | ev, |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1646 | "p = provider(fields = { 'x' : 'I am x', 'y' : 'I am y'})", |
| 1647 | "p1 = p(x = 1, y = 2)", |
| 1648 | "x = p1.x", |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1649 | "y = p1.y"); |
| 1650 | StarlarkProvider p = (StarlarkProvider) ev.lookup("p"); |
| 1651 | StarlarkInfo p1 = (StarlarkInfo) ev.lookup("p1"); |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1652 | |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1653 | assertThat(p1.getProvider()).isEqualTo(p); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1654 | assertThat(ev.lookup("x")).isEqualTo(1); |
| 1655 | assertThat(ev.lookup("y")).isEqualTo(2); |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | @Test |
| 1659 | public void providerWithFieldsOptional() throws Exception { |
| 1660 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1661 | ev, |
| 1662 | "p = provider(fields = ['x', 'y'])", // |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1663 | "p1 = p(y = 2)", |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1664 | "y = p1.y"); |
| 1665 | StarlarkProvider p = (StarlarkProvider) ev.lookup("p"); |
| 1666 | StarlarkInfo p1 = (StarlarkInfo) ev.lookup("p1"); |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1667 | |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1668 | assertThat(p1.getProvider()).isEqualTo(p); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1669 | assertThat(ev.lookup("y")).isEqualTo(2); |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | @Test |
| 1673 | public void providerWithFieldsOptionalError() throws Exception { |
| 1674 | ev.setFailFast(false); |
| 1675 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1676 | ev, |
| 1677 | "p = provider(fields = ['x', 'y'])", // |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1678 | "p1 = p(y = 2)", |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1679 | "x = p1.x"); |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 1680 | MoreAsserts.assertContainsEvent( |
| 1681 | ev.getEventCollector(), " 'p' value has no field or method 'x'"); |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | @Test |
| 1685 | public void providerWithExtraFieldsError() throws Exception { |
| 1686 | ev.setFailFast(false); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1687 | evalAndExport(ev, "p = provider(fields = ['x', 'y'])", "p1 = p(x = 1, y = 2, z = 3)"); |
adonovan | 7f26640 | 2020-04-20 10:15:47 -0700 | [diff] [blame] | 1688 | MoreAsserts.assertContainsEvent( |
| 1689 | ev.getEventCollector(), "unexpected keyword z in call to instantiate provider p"); |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | @Test |
| 1693 | public void providerWithEmptyFieldsError() throws Exception { |
| 1694 | ev.setFailFast(false); |
| 1695 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1696 | ev, |
| 1697 | "p = provider(fields = [])", // |
| 1698 | "p1 = p(x = 1, y = 2, z = 3)"); |
adonovan | 7f26640 | 2020-04-20 10:15:47 -0700 | [diff] [blame] | 1699 | MoreAsserts.assertContainsEvent( |
| 1700 | ev.getEventCollector(), "unexpected keywords x, y, z in call to instantiate provider p"); |
| 1701 | } |
| 1702 | |
| 1703 | @Test |
| 1704 | public void providerWithDuplicateFieldsError() throws Exception { |
| 1705 | ev.setFailFast(false); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1706 | evalAndExport( |
| 1707 | ev, |
| 1708 | "p = provider(fields = ['a', 'b'])", // |
| 1709 | "p(a = 1, b = 2, **dict(b = 3))"); |
adonovan | 7f26640 | 2020-04-20 10:15:47 -0700 | [diff] [blame] | 1710 | MoreAsserts.assertContainsEvent( |
| 1711 | ev.getEventCollector(), |
| 1712 | "got multiple values for parameter b in call to instantiate provider p"); |
dslomov | 0667b83 | 2017-08-25 09:29:50 +0200 | [diff] [blame] | 1713 | } |
| 1714 | |
Dmitry Lomov | 0692c7f | 2016-09-30 16:43:30 +0000 | [diff] [blame] | 1715 | @Test |
| 1716 | public void starTheOnlyAspectArg() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1717 | ev.checkEvalErrorContains( |
Googler | ee05663 | 2019-10-10 13:04:49 -0700 | [diff] [blame] | 1718 | "'*' must be the only string in 'attr_aspects' list", |
Dmitry Lomov | 0692c7f | 2016-09-30 16:43:30 +0000 | [diff] [blame] | 1719 | "def _impl(target, ctx):", |
| 1720 | " pass", |
| 1721 | "aspect(_impl, attr_aspects=['*', 'foo'])"); |
| 1722 | } |
Vladimir Moskva | f7c552c | 2017-01-12 17:17:15 +0000 | [diff] [blame] | 1723 | |
| 1724 | @Test |
| 1725 | public void testMandatoryConfigParameterForExecutableLabels() throws Exception { |
| 1726 | scratch.file("third_party/foo/extension.bzl", |
| 1727 | "def _main_rule_impl(ctx):", |
| 1728 | " pass", |
| 1729 | "my_rule = rule(_main_rule_impl,", |
| 1730 | " attrs = { ", |
| 1731 | " 'exe' : attr.label(executable = True, allow_files = True),", |
| 1732 | " },", |
| 1733 | ")" |
| 1734 | ); |
| 1735 | scratch.file("third_party/foo/BUILD", |
laurentlb | 5ddd804 | 2017-11-30 12:03:31 -0800 | [diff] [blame] | 1736 | "load(':extension.bzl', 'my_rule')", |
Vladimir Moskva | f7c552c | 2017-01-12 17:17:15 +0000 | [diff] [blame] | 1737 | "my_rule(name = 'main', exe = ':tool.sh')" |
| 1738 | ); |
| 1739 | |
cushon | 978cb00 | 2018-02-24 14:05:37 -0800 | [diff] [blame] | 1740 | AssertionError expected = |
| 1741 | assertThrows(AssertionError.class, () -> createRuleContext("//third_party/foo:main")); |
brandjon | 8bd2016 | 2017-12-28 08:49:54 -0800 | [diff] [blame] | 1742 | assertThat(expected).hasMessageThat() |
| 1743 | .contains("cfg parameter is mandatory when executable=True is provided."); |
Vladimir Moskva | f7c552c | 2017-01-12 17:17:15 +0000 | [diff] [blame] | 1744 | } |
Vladimir Moskva | f7c552c | 2017-01-12 17:17:15 +0000 | [diff] [blame] | 1745 | |
John Cater | eca2840 | 2017-05-17 21:44:12 +0200 | [diff] [blame] | 1746 | @Test |
| 1747 | public void testRuleAddToolchain() throws Exception { |
John Cater | 9a8d16e | 2017-07-05 16:12:07 -0400 | [diff] [blame] | 1748 | scratch.file("test/BUILD", "toolchain_type(name = 'my_toolchain_type')"); |
John Cater | eca2840 | 2017-05-17 21:44:12 +0200 | [diff] [blame] | 1749 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1750 | ev, |
| 1751 | "def impl(ctx): return None", |
| 1752 | "r1 = rule(impl, toolchains=['//test:my_toolchain_type'])"); |
| 1753 | RuleClass c = ((StarlarkRuleFunction) ev.lookup("r1")).getRuleClass(); |
John Cater | 9a8d16e | 2017-07-05 16:12:07 -0400 | [diff] [blame] | 1754 | assertThat(c.getRequiredToolchains()).containsExactly(makeLabel("//test:my_toolchain_type")); |
John Cater | eca2840 | 2017-05-17 21:44:12 +0200 | [diff] [blame] | 1755 | } |
vladmos | 2f32e38 | 2017-06-19 12:44:21 -0400 | [diff] [blame] | 1756 | |
| 1757 | @Test |
John Cater | d79e977 | 2018-06-14 02:24:57 -0700 | [diff] [blame] | 1758 | public void testRuleAddExecutionConstraints() throws Exception { |
Googler | 3fcfbe1 | 2019-08-28 08:10:11 -0700 | [diff] [blame] | 1759 | registerDummyStarlarkFunction(); |
John Cater | d79e977 | 2018-06-14 02:24:57 -0700 | [diff] [blame] | 1760 | scratch.file("test/BUILD", "toolchain_type(name = 'my_toolchain_type')"); |
| 1761 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1762 | ev, |
John Cater | d79e977 | 2018-06-14 02:24:57 -0700 | [diff] [blame] | 1763 | "r1 = rule(", |
| 1764 | " implementation = impl,", |
| 1765 | " toolchains=['//test:my_toolchain_type'],", |
| 1766 | " exec_compatible_with=['//constraint:cv1', '//constraint:cv2'],", |
| 1767 | ")"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1768 | RuleClass c = ((StarlarkRuleFunction) ev.lookup("r1")).getRuleClass(); |
John Cater | d79e977 | 2018-06-14 02:24:57 -0700 | [diff] [blame] | 1769 | assertThat(c.getExecutionPlatformConstraints()) |
| 1770 | .containsExactly(makeLabel("//constraint:cv1"), makeLabel("//constraint:cv2")); |
| 1771 | } |
| 1772 | |
| 1773 | @Test |
juliexxia | 693048d | 2020-04-01 06:41:42 -0700 | [diff] [blame] | 1774 | public void testRuleAddExecGroup() throws Exception { |
gregce | 5ac9086 | 2020-04-20 13:07:58 -0700 | [diff] [blame] | 1775 | setStarlarkSemanticsOptions("--experimental_exec_groups=true"); |
juliexxia | 693048d | 2020-04-01 06:41:42 -0700 | [diff] [blame] | 1776 | |
| 1777 | registerDummyStarlarkFunction(); |
| 1778 | scratch.file("test/BUILD", "toolchain_type(name = 'my_toolchain_type')"); |
| 1779 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1780 | ev, |
juliexxia | 693048d | 2020-04-01 06:41:42 -0700 | [diff] [blame] | 1781 | "plum = rule(", |
| 1782 | " implementation = impl,", |
| 1783 | " exec_groups = {", |
| 1784 | " 'group': exec_group(", |
| 1785 | " toolchains=['//test:my_toolchain_type'],", |
| 1786 | " exec_compatible_with=['//constraint:cv1', '//constraint:cv2'],", |
| 1787 | " ),", |
| 1788 | " },", |
| 1789 | ")"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1790 | RuleClass plum = ((StarlarkRuleFunction) ev.lookup("plum")).getRuleClass(); |
juliexxia | 693048d | 2020-04-01 06:41:42 -0700 | [diff] [blame] | 1791 | assertThat(plum.getRequiredToolchains()).isEmpty(); |
juliexxia | fae8904 | 2020-06-12 12:05:21 -0700 | [diff] [blame] | 1792 | assertThat(plum.getExecGroups().get("group").requiredToolchains()) |
juliexxia | 693048d | 2020-04-01 06:41:42 -0700 | [diff] [blame] | 1793 | .containsExactly(makeLabel("//test:my_toolchain_type")); |
| 1794 | assertThat(plum.getExecutionPlatformConstraints()).isEmpty(); |
juliexxia | fae8904 | 2020-06-12 12:05:21 -0700 | [diff] [blame] | 1795 | assertThat(plum.getExecGroups().get("group").execCompatibleWith()) |
juliexxia | 693048d | 2020-04-01 06:41:42 -0700 | [diff] [blame] | 1796 | .containsExactly(makeLabel("//constraint:cv1"), makeLabel("//constraint:cv2")); |
| 1797 | } |
| 1798 | |
| 1799 | @Test |
vladmos | 2f32e38 | 2017-06-19 12:44:21 -0400 | [diff] [blame] | 1800 | public void testRuleFunctionReturnsNone() throws Exception { |
| 1801 | scratch.file("test/rule.bzl", |
| 1802 | "def _impl(ctx):", |
| 1803 | " pass", |
| 1804 | "foo_rule = rule(", |
| 1805 | " implementation = _impl,", |
| 1806 | " attrs = {'params': attr.string_list()},", |
| 1807 | ")"); |
| 1808 | scratch.file("test/BUILD", |
| 1809 | "load(':rule.bzl', 'foo_rule')", |
| 1810 | "r = foo_rule(name='foo')", // Custom rule should return None |
| 1811 | "c = cc_library(name='cc')", // Native rule should return None |
| 1812 | "", |
| 1813 | "foo_rule(", |
| 1814 | " name='check',", |
| 1815 | " params = [type(r), type(c)]", |
| 1816 | ")"); |
| 1817 | invalidatePackages(); |
gregce | d281df7 | 2020-05-11 12:27:06 -0700 | [diff] [blame] | 1818 | StarlarkRuleContext context = createRuleContext("//test:check"); |
vladmos | 2f32e38 | 2017-06-19 12:44:21 -0400 | [diff] [blame] | 1819 | @SuppressWarnings("unchecked") |
Googler | 942e1c4 | 2019-11-12 13:11:44 -0800 | [diff] [blame] | 1820 | StarlarkList<Object> params = (StarlarkList<Object>) context.getAttr().getValue("params"); |
vladmos | 2f32e38 | 2017-06-19 12:44:21 -0400 | [diff] [blame] | 1821 | assertThat(params.get(0)).isEqualTo("NoneType"); |
| 1822 | assertThat(params.get(1)).isEqualTo("NoneType"); |
| 1823 | } |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 1824 | |
| 1825 | @Test |
| 1826 | public void testTypeOfStruct() throws Exception { |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1827 | ev.exec("p = type(struct)", "s = type(struct())"); |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 1828 | |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1829 | assertThat(ev.lookup("p")).isEqualTo("Provider"); |
| 1830 | assertThat(ev.lookup("s")).isEqualTo("struct"); |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 1831 | } |
juliexxia | 44e2143 | 2020-03-31 08:21:20 -0700 | [diff] [blame] | 1832 | |
| 1833 | @Test |
| 1834 | public void testCreateExecGroup() throws Exception { |
gregce | 5ac9086 | 2020-04-20 13:07:58 -0700 | [diff] [blame] | 1835 | setStarlarkSemanticsOptions("--experimental_exec_groups=true"); |
juliexxia | 44e2143 | 2020-03-31 08:21:20 -0700 | [diff] [blame] | 1836 | |
| 1837 | scratch.file("test/BUILD", "toolchain_type(name = 'my_toolchain_type')"); |
| 1838 | evalAndExport( |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1839 | ev, |
juliexxia | 44e2143 | 2020-03-31 08:21:20 -0700 | [diff] [blame] | 1840 | "group = exec_group(", |
| 1841 | " toolchains=['//test:my_toolchain_type'],", |
| 1842 | " exec_compatible_with=['//constraint:cv1', '//constraint:cv2'],", |
| 1843 | ")"); |
adonovan | 4444f1a | 2020-05-12 20:03:11 -0700 | [diff] [blame] | 1844 | ExecGroup group = ((ExecGroup) ev.lookup("group")); |
juliexxia | fae8904 | 2020-06-12 12:05:21 -0700 | [diff] [blame] | 1845 | assertThat(group.requiredToolchains()).containsExactly(makeLabel("//test:my_toolchain_type")); |
| 1846 | assertThat(group.execCompatibleWith()) |
juliexxia | 44e2143 | 2020-03-31 08:21:20 -0700 | [diff] [blame] | 1847 | .containsExactly(makeLabel("//constraint:cv1"), makeLabel("//constraint:cv2")); |
| 1848 | } |
John Cater | eca2840 | 2017-05-17 21:44:12 +0200 | [diff] [blame] | 1849 | } |