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