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