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; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 18 | import static com.google.common.truth.Truth.assertWithMessage; |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 19 | import static org.junit.Assert.fail; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 20 | |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 22 | import com.google.common.collect.Iterables; |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.ActionAnalysisMetadata; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.Artifact; |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.CompositeRunfilesSupplier; |
| 26 | import com.google.devtools.build.lib.actions.RunfilesSupplier; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
Francois-Rene Rideau | 1bae01f | 2016-01-30 04:25:58 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.analysis.DefaultProvider; |
| 30 | import com.google.devtools.build.lib.analysis.FilesToRunProvider; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.analysis.Runfiles; |
| 32 | import com.google.devtools.build.lib.analysis.actions.FileWriteAction; |
| 33 | import com.google.devtools.build.lib.analysis.actions.SpawnAction; |
| 34 | import com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction; |
| 35 | import com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution; |
vladmos | 97d6708 | 2017-07-13 14:54:03 +0200 | [diff] [blame] | 36 | import com.google.devtools.build.lib.cmdline.Label; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.events.Event; |
Vladimir Moskva | 748ba86 | 2016-09-20 13:46:11 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.packages.SkylarkClassObject; |
vladmos | 97d6708 | 2017-07-13 14:54:03 +0200 | [diff] [blame] | 39 | import com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.rules.SkylarkRuleContext; |
| 41 | import com.google.devtools.build.lib.skylark.util.SkylarkTestCase; |
Damien Martin-Guillerez | 2ca9b72 | 2016-06-09 17:43:55 +0000 | [diff] [blame] | 42 | import com.google.devtools.build.lib.skylarkinterface.Param; |
John Field | 585d1a0 | 2015-12-16 16:03:52 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.lib.skylarkinterface.SkylarkSignature; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.syntax.BuiltinFunction; |
Francois-Rene Rideau | ab049e0 | 2016-02-17 16:13:46 +0000 | [diff] [blame] | 45 | import com.google.devtools.build.lib.syntax.Environment; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 46 | import com.google.devtools.build.lib.syntax.EvalException; |
| 47 | import com.google.devtools.build.lib.syntax.EvalUtils; |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 48 | import com.google.devtools.build.lib.syntax.Printer; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 49 | import com.google.devtools.build.lib.syntax.Runtime; |
| 50 | import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; |
| 51 | import com.google.devtools.build.lib.syntax.SkylarkNestedSet; |
Carmi Grushko | 7136f6e | 2015-11-05 19:26:16 +0000 | [diff] [blame] | 52 | import com.google.devtools.build.lib.testutil.MoreAsserts; |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 53 | import com.google.devtools.build.lib.util.OsUtils; |
Vladimir Moskva | 748ba86 | 2016-09-20 13:46:11 +0000 | [diff] [blame] | 54 | import java.nio.charset.Charset; |
| 55 | import java.nio.charset.StandardCharsets; |
| 56 | import java.util.List; |
| 57 | import java.util.Map; |
| 58 | import java.util.regex.Pattern; |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 59 | import org.junit.Before; |
Vladimir Moskva | 658a8ea | 2016-09-02 15:39:17 +0000 | [diff] [blame] | 60 | import org.junit.Rule; |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 61 | import org.junit.Test; |
Vladimir Moskva | 658a8ea | 2016-09-02 15:39:17 +0000 | [diff] [blame] | 62 | import org.junit.rules.ExpectedException; |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 63 | import org.junit.runner.RunWith; |
| 64 | import org.junit.runners.JUnit4; |
| 65 | |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 66 | /** |
| 67 | * Tests for SkylarkRuleImplementationFunctions. |
| 68 | */ |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 69 | @RunWith(JUnit4.class) |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 70 | public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase { |
Vladimir Moskva | 658a8ea | 2016-09-02 15:39:17 +0000 | [diff] [blame] | 71 | @Rule public ExpectedException thrown = ExpectedException.none(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 72 | |
| 73 | @SkylarkSignature( |
| 74 | name = "mock", |
| 75 | documented = false, |
Damien Martin-Guillerez | 014388c | 2016-06-14 10:28:31 +0000 | [diff] [blame] | 76 | parameters = { |
| 77 | @Param(name = "mandatory", doc = ""), |
| 78 | @Param(name = "optional", doc = "", defaultValue = "None"), |
| 79 | @Param(name = "mandatory_key", doc = "", positional = false, named = true), |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 80 | @Param( |
| 81 | name = "optional_key", |
| 82 | doc = "", |
| 83 | defaultValue = "'x'", |
| 84 | positional = false, |
| 85 | named = true |
| 86 | ) |
| 87 | }, |
Francois-Rene Rideau | ab049e0 | 2016-02-17 16:13:46 +0000 | [diff] [blame] | 88 | useEnvironment = true |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 89 | ) |
| 90 | private BuiltinFunction mockFunc; |
| 91 | |
| 92 | /** |
| 93 | * Used for {@link #testStackTraceWithoutOriginalMessage()} and {@link |
| 94 | * #testNoStackTraceOnInterrupt}. |
| 95 | */ |
| 96 | @SkylarkSignature(name = "throw", documented = false) |
| 97 | BuiltinFunction throwFunction; |
| 98 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 99 | @Before |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 100 | public final void createBuildFile() throws Exception { |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 101 | scratch.file( |
| 102 | "foo/BUILD", |
| 103 | "genrule(name = 'foo',", |
| 104 | " cmd = 'dummy_cmd',", |
| 105 | " srcs = ['a.txt', 'b.img'],", |
| 106 | " tools = ['t.exe'],", |
| 107 | " outs = ['c.txt'])", |
| 108 | "genrule(name = 'bar',", |
| 109 | " cmd = 'dummy_cmd',", |
| 110 | " srcs = [':jl', ':gl'],", |
| 111 | " outs = ['d.txt'])", |
| 112 | "genrule(name = 'baz',", |
| 113 | " cmd = 'dummy_cmd',", |
| 114 | " outs = ['e.txt'])", |
| 115 | "java_library(name = 'jl',", |
| 116 | " srcs = ['a.java'])", |
| 117 | "genrule(name = 'gl',", |
| 118 | " cmd = 'touch $(OUTS)',", |
| 119 | " srcs = ['a.go'],", |
| 120 | " outs = [ 'gl.a', 'gl.gcgox', ],", |
| 121 | " output_to_bindir = 1,", |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 122 | ")", |
| 123 | // The two below are used by testResolveCommand |
| 124 | "sh_binary(name = 'mytool',", |
| 125 | " srcs = ['mytool.sh'],", |
| 126 | " data = ['file1.dat', 'file2.dat'],", |
| 127 | ")", |
| 128 | "genrule(name = 'resolve_me',", |
| 129 | " cmd = 'aa',", |
| 130 | " tools = [':mytool', 't.exe'],", |
| 131 | " srcs = ['file3.dat', 'file4.dat'],", |
| 132 | " outs = ['r1.txt', 'r2.txt'],", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 133 | ")"); |
| 134 | } |
| 135 | |
| 136 | private void setupSkylarkFunction(String line) throws Exception { |
| 137 | mockFunc = |
| 138 | new BuiltinFunction("mock") { |
| 139 | @SuppressWarnings("unused") |
| 140 | public Object invoke( |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 141 | Object mandatory, |
| 142 | Object optional, |
| 143 | Object mandatoryKey, |
| 144 | Object optionalKey, |
Francois-Rene Rideau | ab049e0 | 2016-02-17 16:13:46 +0000 | [diff] [blame] | 145 | Environment env) { |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 146 | return EvalUtils.optionMap( |
Francois-Rene Rideau | ab049e0 | 2016-02-17 16:13:46 +0000 | [diff] [blame] | 147 | env, |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 148 | "mandatory", |
| 149 | mandatory, |
| 150 | "optional", |
| 151 | optional, |
| 152 | "mandatory_key", |
| 153 | mandatoryKey, |
| 154 | "optional_key", |
| 155 | optionalKey); |
| 156 | } |
| 157 | }; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 158 | assertThat(mockFunc.isConfigured()).isFalse(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 159 | mockFunc.configure( |
| 160 | SkylarkRuleImplementationFunctionsTest.class |
| 161 | .getDeclaredField("mockFunc") |
| 162 | .getAnnotation(SkylarkSignature.class)); |
| 163 | update("mock", mockFunc); |
| 164 | eval(line); |
| 165 | } |
| 166 | |
| 167 | private void checkSkylarkFunctionError(String errorMsg, String line) throws Exception { |
| 168 | try { |
| 169 | setupSkylarkFunction(line); |
| 170 | fail(); |
| 171 | } catch (EvalException e) { |
| 172 | assertThat(e).hasMessage(errorMsg); |
| 173 | } |
| 174 | } |
| 175 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 176 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 177 | public void testSkylarkFunctionPosArgs() throws Exception { |
| 178 | setupSkylarkFunction("a = mock('a', 'b', mandatory_key='c')"); |
| 179 | Map<?, ?> params = (Map<?, ?>) lookup("a"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 180 | assertThat(params.get("mandatory")).isEqualTo("a"); |
| 181 | assertThat(params.get("optional")).isEqualTo("b"); |
| 182 | assertThat(params.get("mandatory_key")).isEqualTo("c"); |
| 183 | assertThat(params.get("optional_key")).isEqualTo("x"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 186 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 187 | public void testSkylarkFunctionKwArgs() throws Exception { |
| 188 | setupSkylarkFunction("a = mock(optional='b', mandatory='a', mandatory_key='c')"); |
| 189 | Map<?, ?> params = (Map<?, ?>) lookup("a"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 190 | assertThat(params.get("mandatory")).isEqualTo("a"); |
| 191 | assertThat(params.get("optional")).isEqualTo("b"); |
| 192 | assertThat(params.get("mandatory_key")).isEqualTo("c"); |
| 193 | assertThat(params.get("optional_key")).isEqualTo("x"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 196 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 197 | public void testSkylarkFunctionTooFewArguments() throws Exception { |
| 198 | checkSkylarkFunctionError( |
| 199 | "insufficient arguments received by mock(" |
| 200 | + "mandatory, optional = None, *, mandatory_key, optional_key = \"x\") " |
| 201 | + "(got 0, expected at least 1)", |
| 202 | "mock()"); |
| 203 | } |
| 204 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 205 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 206 | public void testSkylarkFunctionTooManyArguments() throws Exception { |
| 207 | checkSkylarkFunctionError( |
| 208 | "too many (3) positional arguments in call to " |
| 209 | + "mock(mandatory, optional = None, *, mandatory_key, optional_key = \"x\")", |
| 210 | "mock('a', 'b', 'c')"); |
| 211 | } |
| 212 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 213 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 214 | public void testSkylarkFunctionAmbiguousArguments() throws Exception { |
| 215 | checkSkylarkFunctionError( |
| 216 | "argument 'mandatory' passed both by position and by name " |
| 217 | + "in call to mock(mandatory, optional = None, *, mandatory_key, optional_key = \"x\")", |
| 218 | "mock('by position', mandatory='by_key', mandatory_key='c')"); |
| 219 | } |
| 220 | |
| 221 | @SuppressWarnings("unchecked") |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 222 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 223 | public void testListComprehensionsWithNestedSet() throws Exception { |
Vladimir Moskva | d200daf | 2016-12-23 16:35:37 +0000 | [diff] [blame] | 224 | Object result = eval("[x + x for x in depset([1, 2, 3])]"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 225 | assertThat((Iterable<Object>) result).containsExactly(2, 4, 6).inOrder(); |
| 226 | } |
| 227 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 228 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 229 | public void testCreateSpawnActionCreatesSpawnAction() throws Exception { |
| 230 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 231 | createTestSpawnAction(ruleContext); |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 232 | ActionAnalysisMetadata action = |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 233 | Iterables.getOnlyElement( |
| 234 | ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions()); |
| 235 | assertThat(action).isInstanceOf(SpawnAction.class); |
| 236 | } |
| 237 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 238 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 239 | public void testCreateSpawnActionArgumentsWithCommand() throws Exception { |
| 240 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 241 | createTestSpawnAction(ruleContext); |
| 242 | SpawnAction action = |
| 243 | (SpawnAction) |
| 244 | Iterables.getOnlyElement( |
| 245 | ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions()); |
| 246 | assertArtifactFilenames(action.getInputs(), "a.txt", "b.img"); |
| 247 | assertArtifactFilenames(action.getOutputs(), "a.txt", "b.img"); |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 248 | MoreAsserts.assertContainsSublist( |
| 249 | action.getArguments(), "-c", "dummy_command", "", "--a", "--b"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 250 | assertThat(action.getMnemonic()).isEqualTo("DummyMnemonic"); |
| 251 | assertThat(action.getProgressMessage()).isEqualTo("dummy_message"); |
| 252 | assertThat(action.getEnvironment()).isEqualTo(targetConfig.getLocalShellEnvironment()); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 255 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 256 | public void testCreateSpawnActionArgumentsWithExecutable() throws Exception { |
| 257 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 258 | evalRuleContextCode( |
| 259 | ruleContext, |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 260 | "ruleContext.actions.run(", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 261 | " inputs = ruleContext.files.srcs,", |
| 262 | " outputs = ruleContext.files.srcs,", |
| 263 | " arguments = ['--a','--b'],", |
| 264 | " executable = ruleContext.files.tools[0])"); |
| 265 | SpawnAction action = |
| 266 | (SpawnAction) |
| 267 | Iterables.getOnlyElement( |
| 268 | ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions()); |
| 269 | assertArtifactFilenames(action.getInputs(), "a.txt", "b.img", "t.exe"); |
| 270 | assertArtifactFilenames(action.getOutputs(), "a.txt", "b.img"); |
Carmi Grushko | 7136f6e | 2015-11-05 19:26:16 +0000 | [diff] [blame] | 271 | MoreAsserts.assertContainsSublist(action.getArguments(), "foo/t.exe", "--a", "--b"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 274 | @Test |
laurentlb | 4d30273 | 2017-06-14 15:57:30 +0200 | [diff] [blame] | 275 | public void testCreateActionWithDepsetInput() throws Exception { |
| 276 | // Same test as above, with depset as inputs. |
| 277 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 278 | evalRuleContextCode( |
| 279 | ruleContext, |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 280 | "ruleContext.actions.run(", |
laurentlb | 4d30273 | 2017-06-14 15:57:30 +0200 | [diff] [blame] | 281 | " inputs = depset(ruleContext.files.srcs),", |
| 282 | " outputs = ruleContext.files.srcs,", |
| 283 | " arguments = ['--a','--b'],", |
| 284 | " executable = ruleContext.files.tools[0])"); |
| 285 | SpawnAction action = |
| 286 | (SpawnAction) |
| 287 | Iterables.getOnlyElement( |
| 288 | ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions()); |
| 289 | assertArtifactFilenames(action.getInputs(), "a.txt", "b.img", "t.exe"); |
| 290 | assertArtifactFilenames(action.getOutputs(), "a.txt", "b.img"); |
| 291 | MoreAsserts.assertContainsSublist(action.getArguments(), "foo/t.exe", "--a", "--b"); |
| 292 | } |
| 293 | |
| 294 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 295 | public void testCreateSpawnActionArgumentsBadExecutable() throws Exception { |
| 296 | checkErrorContains( |
| 297 | createRuleContext("//foo:foo"), |
dslomov | c756700 | 2017-07-28 22:34:10 +0200 | [diff] [blame] | 298 | "expected file or string for executable but got int instead", |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 299 | "ruleContext.actions.run(", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 300 | " inputs = ruleContext.files.srcs,", |
| 301 | " outputs = ruleContext.files.srcs,", |
| 302 | " arguments = ['--a','--b'],", |
dslomov | c756700 | 2017-07-28 22:34:10 +0200 | [diff] [blame] | 303 | " executable = 123)"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 306 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 307 | public void testCreateSpawnActionShellCommandList() throws Exception { |
| 308 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 309 | evalRuleContextCode( |
| 310 | ruleContext, |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 311 | "ruleContext.actions.run_shell(", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 312 | " inputs = ruleContext.files.srcs,", |
| 313 | " outputs = ruleContext.files.srcs,", |
| 314 | " mnemonic = 'DummyMnemonic',", |
| 315 | " command = ['dummy_command', '--arg1', '--arg2'],", |
| 316 | " progress_message = 'dummy_message')"); |
| 317 | SpawnAction action = |
| 318 | (SpawnAction) |
| 319 | Iterables.getOnlyElement( |
| 320 | ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions()); |
| 321 | assertThat(action.getArguments()) |
| 322 | .containsExactly("dummy_command", "--arg1", "--arg2") |
| 323 | .inOrder(); |
| 324 | } |
| 325 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 326 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 327 | public void testCreateSpawnActionEnvAndExecInfo() throws Exception { |
| 328 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 329 | evalRuleContextCode( |
| 330 | ruleContext, |
| 331 | "env = {'a' : 'b'}", |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 332 | "ruleContext.actions.run_shell(", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 333 | " inputs = ruleContext.files.srcs,", |
| 334 | " outputs = ruleContext.files.srcs,", |
| 335 | " env = env,", |
| 336 | " execution_requirements = env,", |
| 337 | " mnemonic = 'DummyMnemonic',", |
| 338 | " command = 'dummy_command',", |
| 339 | " progress_message = 'dummy_message')"); |
| 340 | SpawnAction action = |
| 341 | (SpawnAction) |
| 342 | Iterables.getOnlyElement( |
| 343 | ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions()); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 344 | assertThat(action.getEnvironment()).containsExactly("a", "b"); |
| 345 | assertThat(action.getExecutionInfo()).containsExactly("a", "b"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 348 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 349 | public void testCreateSpawnActionUnknownParam() throws Exception { |
| 350 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 351 | checkErrorContains( |
| 352 | ruleContext, |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 353 | "unexpected keyword 'bad_param', in method run(" |
| 354 | + "list outputs, string bad_param, File executable) of 'actions'", |
| 355 | "f = ruleContext.actions.declare_file('foo.sh')", |
| 356 | "ruleContext.actions.run(outputs=[], bad_param = 'some text', executable = f)"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 359 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 360 | public void testCreateSpawnActionNoExecutable() throws Exception { |
| 361 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 362 | checkErrorContains( |
| 363 | ruleContext, |
| 364 | "You must specify either 'command' or 'executable' argument", |
| 365 | "ruleContext.action(outputs=[])"); |
| 366 | } |
| 367 | |
| 368 | private Object createTestSpawnAction(SkylarkRuleContext ruleContext) throws Exception { |
| 369 | return evalRuleContextCode( |
| 370 | ruleContext, |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 371 | "ruleContext.actions.run_shell(", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 372 | " inputs = ruleContext.files.srcs,", |
| 373 | " outputs = ruleContext.files.srcs,", |
| 374 | " arguments = ['--a','--b'],", |
| 375 | " mnemonic = 'DummyMnemonic',", |
| 376 | " command = 'dummy_command',", |
| 377 | " progress_message = 'dummy_message',", |
| 378 | " use_default_shell_env = True)"); |
| 379 | } |
| 380 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 381 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 382 | public void testCreateSpawnActionBadGenericArg() throws Exception { |
| 383 | checkErrorContains( |
| 384 | createRuleContext("//foo:foo"), |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 385 | "expected type 'File' for 'outputs' element but got type 'string' instead", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 386 | "l = ['a', 'b']", |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 387 | "ruleContext.actions.run_shell(", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 388 | " outputs = l,", |
| 389 | " command = 'dummy_command')"); |
| 390 | } |
| 391 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 392 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 393 | public void testCreateSpawnActionCommandsListTooShort() throws Exception { |
| 394 | checkErrorContains( |
| 395 | createRuleContext("//foo:foo"), |
| 396 | "'command' list has to be of size at least 3", |
dslomov | 40ddec3 | 2017-07-03 07:15:31 -0400 | [diff] [blame] | 397 | "ruleContext.actions.run_shell(", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 398 | " outputs = ruleContext.files.srcs,", |
| 399 | " command = ['dummy_command', '--arg'])"); |
| 400 | } |
| 401 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 402 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 403 | public void testCreateFileAction() throws Exception { |
| 404 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
Laurent Le Brun | a708b62 | 2016-05-11 14:24:39 +0000 | [diff] [blame] | 405 | evalRuleContextCode( |
| 406 | ruleContext, |
dslomov | 90ccaa5 | 2017-06-30 12:21:28 +0200 | [diff] [blame] | 407 | "ruleContext.actions.write(", |
Laurent Le Brun | a708b62 | 2016-05-11 14:24:39 +0000 | [diff] [blame] | 408 | " output = ruleContext.files.srcs[0],", |
| 409 | " content = 'hello world',", |
dslomov | 90ccaa5 | 2017-06-30 12:21:28 +0200 | [diff] [blame] | 410 | " is_executable = False)"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 411 | FileWriteAction action = |
| 412 | (FileWriteAction) |
Laurent Le Brun | a708b62 | 2016-05-11 14:24:39 +0000 | [diff] [blame] | 413 | Iterables.getOnlyElement( |
| 414 | ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions()); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 415 | assertThat(Iterables.getOnlyElement(action.getOutputs()).getExecPathString()) |
| 416 | .isEqualTo("foo/a.txt"); |
| 417 | assertThat(action.getFileContents()).isEqualTo("hello world"); |
| 418 | assertThat(action.makeExecutable()).isFalse(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 421 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 422 | public void testEmptyAction() throws Exception { |
| 423 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 424 | |
| 425 | checkEmptyAction(ruleContext, "mnemonic = 'test'"); |
| 426 | checkEmptyAction(ruleContext, "mnemonic = 'test', inputs = ruleContext.files.srcs"); |
laurentlb | 4cbe5f3 | 2017-06-14 16:52:03 +0200 | [diff] [blame] | 427 | checkEmptyAction(ruleContext, "mnemonic = 'test', inputs = depset(ruleContext.files.srcs)"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 428 | |
| 429 | checkErrorContains( |
| 430 | ruleContext, |
dslomov | ae0b774 | 2017-06-30 10:07:48 +0200 | [diff] [blame] | 431 | "parameter 'mnemonic' has no default value, in method do_nothing(list inputs) of 'actions'", |
| 432 | "ruleContext.actions.do_nothing(inputs = ruleContext.files.srcs)"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | private void checkEmptyAction(SkylarkRuleContext ruleContext, String namedArgs) throws Exception { |
| 436 | assertThat( |
| 437 | evalRuleContextCode( |
dslomov | ae0b774 | 2017-06-30 10:07:48 +0200 | [diff] [blame] | 438 | ruleContext, String.format("ruleContext.actions.do_nothing(%s)", namedArgs))) |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 439 | .isEqualTo(Runtime.NONE); |
| 440 | } |
| 441 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 442 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 443 | public void testEmptyActionWithExtraAction() throws Exception { |
| 444 | scratch.file( |
| 445 | "test/empty.bzl", |
| 446 | "def _impl(ctx):", |
dslomov | ae0b774 | 2017-06-30 10:07:48 +0200 | [diff] [blame] | 447 | " ctx.actions.do_nothing(", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 448 | " inputs = ctx.files.srcs,", |
| 449 | " mnemonic = 'EA',", |
| 450 | " )", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 451 | "empty_action_rule = rule(", |
| 452 | " implementation = _impl,", |
| 453 | " attrs = {", |
| 454 | " \"srcs\": attr.label_list(allow_files=True),", |
| 455 | " }", |
| 456 | ")"); |
| 457 | |
| 458 | scratch.file( |
| 459 | "test/BUILD", |
| 460 | "load('/test/empty', 'empty_action_rule')", |
| 461 | "empty_action_rule(name = 'my_empty_action',", |
| 462 | " srcs = ['foo.in', 'other_foo.in'])", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 463 | "action_listener(name = 'listener',", |
| 464 | " mnemonics = ['EA'],", |
| 465 | " extra_actions = [':extra'])", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 466 | "extra_action(name = 'extra',", |
| 467 | " cmd='')"); |
| 468 | |
| 469 | getPseudoActionViaExtraAction("//test:my_empty_action", "//test:listener"); |
| 470 | } |
| 471 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 472 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 473 | public void testExpandLocation() throws Exception { |
| 474 | SkylarkRuleContext ruleContext = createRuleContext("//foo:bar"); |
| 475 | |
| 476 | // If there is only a single target, both "location" and "locations" should work |
| 477 | runExpansion(ruleContext, "location :jl", "[blaze]*-out/.*/bin/foo/libjl.jar"); |
| 478 | runExpansion(ruleContext, "locations :jl", "[blaze]*-out/.*/bin/foo/libjl.jar"); |
| 479 | |
| 480 | runExpansion(ruleContext, "location //foo:jl", "[blaze]*-out/.*/bin/foo/libjl.jar"); |
| 481 | |
| 482 | // Multiple targets and "location" should result in an error |
| 483 | checkReportedErrorStartsWith( |
| 484 | ruleContext, |
| 485 | "in genrule rule //foo:bar: label '//foo:gl' " |
| 486 | + "in $(location) expression expands to more than one file, please use $(locations " |
| 487 | + "//foo:gl) instead.", |
| 488 | "ruleContext.expand_location('$(location :gl)')"); |
| 489 | |
| 490 | // We have to use "locations" for multiple targets |
| 491 | runExpansion( |
| 492 | ruleContext, |
| 493 | "locations :gl", |
| 494 | "[blaze]*-out/.*/bin/foo/gl.a [blaze]*-out/.*/bin/foo/gl.gcgox"); |
| 495 | |
| 496 | // LocationExpander just returns the input string if there is no label |
| 497 | runExpansion(ruleContext, "location", "\\$\\(location\\)"); |
| 498 | |
| 499 | checkReportedErrorStartsWith( |
| 500 | ruleContext, |
| 501 | "in genrule rule //foo:bar: label '//foo:abc' in $(locations) expression " |
| 502 | + "is not a declared prerequisite of this rule", |
| 503 | "ruleContext.expand_location('$(locations :abc)')"); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Invokes ctx.expand_location() with the given parameters and checks whether this led to the |
| 508 | * expected result |
| 509 | * @param ruleContext The rule context |
| 510 | * @param command Either "location" or "locations". This only matters when the label has multiple |
| 511 | * targets |
| 512 | * @param expectedPattern Regex pattern that matches the expected result |
| 513 | */ |
| 514 | private void runExpansion(SkylarkRuleContext ruleContext, String command, String expectedPattern) |
| 515 | throws Exception { |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 516 | assertMatches( |
| 517 | "Expanded string", |
| 518 | expectedPattern, |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 519 | (String) |
| 520 | evalRuleContextCode( |
| 521 | ruleContext, String.format("ruleContext.expand_location('$(%s)')", command))); |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 522 | } |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 523 | |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 524 | private void assertMatches(String description, String expectedPattern, String computedValue) |
| 525 | throws Exception { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 526 | assertWithMessage( |
| 527 | Printer.format( |
| 528 | "%s %r did not match pattern '%s'", description, computedValue, expectedPattern)) |
| 529 | .that(Pattern.matches(expectedPattern, computedValue)) |
| 530 | .isTrue(); |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 533 | @Test |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 534 | public void testResolveCommandMakeVariables() throws Exception { |
| 535 | evalRuleContextCode( |
| 536 | createRuleContext("//foo:resolve_me"), |
| 537 | "inputs, argv, manifests = ruleContext.resolve_command(", |
| 538 | " command='I got the $(HELLO) on a $(DAVE)', ", |
| 539 | " make_variables={'HELLO': 'World', 'DAVE': type('')})"); |
| 540 | @SuppressWarnings("unchecked") |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 541 | List<String> argv = (List<String>) (List<?>) (MutableList) lookup("argv"); |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 542 | assertThat(argv).hasSize(3); |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 543 | assertMatches("argv[0]", "^.*/bash" + OsUtils.executableExtension() + "$", argv.get(0)); |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 544 | assertThat(argv.get(1)).isEqualTo("-c"); |
| 545 | assertThat(argv.get(2)).isEqualTo("I got the World on a string"); |
| 546 | } |
| 547 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 548 | @Test |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 549 | public void testResolveCommandInputs() throws Exception { |
| 550 | evalRuleContextCode( |
| 551 | createRuleContext("//foo:resolve_me"), |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 552 | "inputs, argv, input_manifests = ruleContext.resolve_command(", |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 553 | " tools=ruleContext.attr.tools)"); |
| 554 | @SuppressWarnings("unchecked") |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 555 | List<Artifact> inputs = (List<Artifact>) (List<?>) (MutableList) lookup("inputs"); |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 556 | assertArtifactFilenames(inputs, "mytool.sh", "mytool", "foo_Smytool-runfiles", "t.exe"); |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 557 | @SuppressWarnings("unchecked") |
| 558 | CompositeRunfilesSupplier runfilesSupplier = |
| 559 | new CompositeRunfilesSupplier((List<RunfilesSupplier>) lookup("input_manifests")); |
| 560 | assertThat(runfilesSupplier.getMappings()).hasSize(1); |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 563 | @Test |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 564 | public void testResolveCommandExpandLocations() throws Exception { |
| 565 | evalRuleContextCode( |
| 566 | createRuleContext("//foo:resolve_me"), |
| 567 | "def foo():", // no for loops at top-level |
| 568 | " label_dict = {}", |
| 569 | " all = []", |
| 570 | " for dep in ruleContext.attr.srcs + ruleContext.attr.tools:", |
| 571 | " all.extend(list(dep.files))", |
| 572 | " label_dict[dep.label] = list(dep.files)", |
| 573 | " return ruleContext.resolve_command(", |
| 574 | " command='A$(locations //foo:mytool) B$(location //foo:file3.dat)',", |
| 575 | " attribute='cmd', expand_locations=True, label_dict=label_dict)", |
| 576 | "inputs, argv, manifests = foo()"); |
| 577 | @SuppressWarnings("unchecked") |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 578 | List<String> argv = (List<String>) (List<?>) (MutableList) lookup("argv"); |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 579 | assertThat(argv).hasSize(3); |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 580 | assertMatches("argv[0]", "^.*/bash" + OsUtils.executableExtension() + "$", argv.get(0)); |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 581 | assertThat(argv.get(1)).isEqualTo("-c"); |
| 582 | assertMatches("argv[2]", "A.*/mytool .*/mytool.sh B.*file3.dat", argv.get(2)); |
| 583 | } |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 584 | |
Chris Parsons | 9a9573b | 2016-02-10 21:15:11 +0000 | [diff] [blame] | 585 | @Test |
| 586 | public void testResolveCommandExecutionRequirements() throws Exception { |
| 587 | // Tests that requires-darwin execution requirements result in the usage of /bin/bash. |
| 588 | evalRuleContextCode( |
| 589 | createRuleContext("//foo:resolve_me"), |
| 590 | "inputs, argv, manifests = ruleContext.resolve_command(", |
| 591 | " execution_requirements={'requires-darwin': ''})"); |
| 592 | @SuppressWarnings("unchecked") |
| 593 | List<String> argv = (List<String>) (List<?>) (MutableList) lookup("argv"); |
| 594 | assertMatches("argv[0]", "^/bin/bash$", argv.get(0)); |
| 595 | } |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 596 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 597 | @Test |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 598 | public void testResolveCommandScript() throws Exception { |
| 599 | evalRuleContextCode( |
| 600 | createRuleContext("//foo:resolve_me"), |
| 601 | "def foo():", // no for loops at top-level |
| 602 | " s = 'a'", |
| 603 | " for i in range(1,17): s = s + s", // 2**17 > CommandHelper.maxCommandLength (=64000) |
| 604 | " return ruleContext.resolve_command(", |
| 605 | " command=s)", |
| 606 | "argv = foo()[1]"); |
| 607 | @SuppressWarnings("unchecked") |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 608 | List<String> argv = (List<String>) (List<?>) (MutableList) lookup("argv"); |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 609 | assertThat(argv).hasSize(2); |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 610 | assertMatches("argv[0]", "^.*/bash" + OsUtils.executableExtension() + "$", argv.get(0)); |
Francois-Rene Rideau | 0f1b51e | 2015-10-20 17:32:16 +0000 | [diff] [blame] | 611 | assertMatches("argv[1]", "^.*/resolve_me[.]script[.]sh$", argv.get(1)); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 614 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 615 | public void testBadParamTypeErrorMessage() throws Exception { |
| 616 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 617 | checkErrorContains( |
| 618 | ruleContext, |
dslomov | 90ccaa5 | 2017-06-30 12:21:28 +0200 | [diff] [blame] | 619 | "Cannot convert parameter 'content' to type string, in method " |
| 620 | + "write(File output, int content, bool is_executable) of 'actions'", |
| 621 | "ruleContext.actions.write(", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 622 | " output = ruleContext.files.srcs[0],", |
| 623 | " content = 1,", |
dslomov | 90ccaa5 | 2017-06-30 12:21:28 +0200 | [diff] [blame] | 624 | " is_executable = False)"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 627 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 628 | public void testCreateTemplateAction() throws Exception { |
| 629 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
dslomov | 68aafa2 | 2017-07-03 09:43:58 -0400 | [diff] [blame] | 630 | evalRuleContextCode( |
| 631 | ruleContext, |
| 632 | "ruleContext.actions.expand_template(", |
| 633 | " template = ruleContext.files.srcs[0],", |
| 634 | " output = ruleContext.files.srcs[1],", |
| 635 | " substitutions = {'a': 'b'},", |
laurentlb | a4bfe16 | 2017-07-31 15:53:07 +0200 | [diff] [blame] | 636 | " is_executable = False)"); |
dslomov | 68aafa2 | 2017-07-03 09:43:58 -0400 | [diff] [blame] | 637 | |
| 638 | TemplateExpansionAction action = (TemplateExpansionAction) Iterables.getOnlyElement( |
| 639 | ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions()); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 640 | assertThat(Iterables.getOnlyElement(action.getInputs()).getExecPathString()) |
| 641 | .isEqualTo("foo/a.txt"); |
| 642 | assertThat(Iterables.getOnlyElement(action.getOutputs()).getExecPathString()) |
| 643 | .isEqualTo("foo/b.img"); |
| 644 | assertThat(Iterables.getOnlyElement(action.getSubstitutions()).getKey()).isEqualTo("a"); |
| 645 | assertThat(Iterables.getOnlyElement(action.getSubstitutions()).getValue()).isEqualTo("b"); |
| 646 | assertThat(action.makeExecutable()).isFalse(); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Simulates the fact that the Parser currently uses Latin1 to read BUILD files, while users |
John Field | 585d1a0 | 2015-12-16 16:03:52 +0000 | [diff] [blame] | 651 | * usually write those files using UTF-8 encoding. Currently, the string-valued 'substitutions' |
| 652 | * parameter of the template_action function contains a hack that assumes its input is a UTF-8 |
| 653 | * encoded string which has been ingested as Latin 1. The hack converts the string to its |
| 654 | * "correct" UTF-8 value. Once {@link com.google.devtools.build.lib.syntax.ParserInputSource#create(com.google.devtools.build.lib.vfs.Path)} |
| 655 | * parses files using UTF-8 and the hack for the substituations parameter is removed, this test |
| 656 | * will fail. |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 657 | */ |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 658 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 659 | public void testCreateTemplateActionWithWrongEncoding() throws Exception { |
John Field | 585d1a0 | 2015-12-16 16:03:52 +0000 | [diff] [blame] | 660 | // The following array contains bytes that represent a string of length two when treated as |
| 661 | // UTF-8 and a string of length four when treated as ISO-8859-1 (a.k.a. Latin 1). |
| 662 | byte[] bytesToDecode = {(byte) 0xC2, (byte) 0xA2, (byte) 0xC2, (byte) 0xA2}; |
| 663 | Charset latin1 = StandardCharsets.ISO_8859_1; |
| 664 | Charset utf8 = StandardCharsets.UTF_8; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 665 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
dslomov | 68aafa2 | 2017-07-03 09:43:58 -0400 | [diff] [blame] | 666 | evalRuleContextCode( |
| 667 | ruleContext, |
| 668 | "ruleContext.actions.expand_template(", |
| 669 | " template = ruleContext.files.srcs[0],", |
| 670 | " output = ruleContext.files.srcs[1],", |
| 671 | " substitutions = {'a': '" + new String(bytesToDecode, latin1) + "'},", |
laurentlb | a4bfe16 | 2017-07-31 15:53:07 +0200 | [diff] [blame] | 672 | " is_executable = False)"); |
dslomov | 68aafa2 | 2017-07-03 09:43:58 -0400 | [diff] [blame] | 673 | TemplateExpansionAction action = (TemplateExpansionAction) Iterables.getOnlyElement( |
| 674 | ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions()); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 675 | List<Substitution> substitutions = action.getSubstitutions(); |
| 676 | assertThat(substitutions).hasSize(1); |
John Field | 585d1a0 | 2015-12-16 16:03:52 +0000 | [diff] [blame] | 677 | assertThat(substitutions.get(0).getValue()).isEqualTo(new String(bytesToDecode, utf8)); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 678 | } |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 679 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 680 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 681 | public void testRunfilesAddFromDependencies() throws Exception { |
| 682 | SkylarkRuleContext ruleContext = createRuleContext("//foo:bar"); |
| 683 | Object result = |
| 684 | evalRuleContextCode(ruleContext, "ruleContext.runfiles(collect_default = True)"); |
| 685 | assertThat(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result))) |
| 686 | .contains("libjl.jar"); |
| 687 | } |
| 688 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 689 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 690 | public void testRunfilesBadListGenericType() throws Exception { |
| 691 | checkErrorContains( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 692 | "expected type 'File' for 'files' element but got type 'string' instead", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 693 | "ruleContext.runfiles(files = ['some string'])"); |
| 694 | } |
| 695 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 696 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 697 | public void testRunfilesBadSetGenericType() throws Exception { |
| 698 | checkErrorContains( |
Vladimir Moskva | d200daf | 2016-12-23 16:35:37 +0000 | [diff] [blame] | 699 | "expected depset of Files or NoneType for 'transitive_files' while calling runfiles " |
Vladimir Moskva | ba4f0bb | 2017-01-30 15:45:49 +0000 | [diff] [blame] | 700 | + "but got depset of ints instead: depset([1, 2, 3])", |
Vladimir Moskva | d200daf | 2016-12-23 16:35:37 +0000 | [diff] [blame] | 701 | "ruleContext.runfiles(transitive_files=depset([1, 2, 3]))"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 704 | @Test |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 705 | public void testRunfilesBadMapGenericType() throws Exception { |
| 706 | checkErrorContains( |
vladmos | 7f6d3a1 | 2017-07-17 23:15:21 +0200 | [diff] [blame] | 707 | "expected type 'string' for 'symlinks' key but got type 'int' instead", |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 708 | "ruleContext.runfiles(symlinks = {123: ruleContext.files.srcs[0]})"); |
| 709 | checkErrorContains( |
vladmos | 7f6d3a1 | 2017-07-17 23:15:21 +0200 | [diff] [blame] | 710 | "expected type 'File' for 'symlinks' value but got type 'int' instead", |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 711 | "ruleContext.runfiles(symlinks = {'some string': 123})"); |
| 712 | checkErrorContains( |
vladmos | 7f6d3a1 | 2017-07-17 23:15:21 +0200 | [diff] [blame] | 713 | "expected type 'string' for 'root_symlinks' key but got type 'int' instead", |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 714 | "ruleContext.runfiles(root_symlinks = {123: ruleContext.files.srcs[0]})"); |
| 715 | checkErrorContains( |
vladmos | 7f6d3a1 | 2017-07-17 23:15:21 +0200 | [diff] [blame] | 716 | "expected type 'File' for 'root_symlinks' value but got type 'int' instead", |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 717 | "ruleContext.runfiles(root_symlinks = {'some string': 123})"); |
| 718 | } |
| 719 | |
| 720 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 721 | public void testRunfilesArtifactsFromArtifact() throws Exception { |
| 722 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 723 | Object result = |
| 724 | evalRuleContextCode( |
| 725 | ruleContext, |
| 726 | "artifacts = ruleContext.files.tools", |
| 727 | "ruleContext.runfiles(files = artifacts)"); |
| 728 | assertThat(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result))).contains("t.exe"); |
| 729 | } |
| 730 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 731 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 732 | public void testRunfilesArtifactsFromIterableArtifacts() throws Exception { |
| 733 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 734 | Object result = |
| 735 | evalRuleContextCode( |
| 736 | ruleContext, |
| 737 | "artifacts = ruleContext.files.srcs", |
| 738 | "ruleContext.runfiles(files = artifacts)"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 739 | assertThat(ImmutableList.of("a.txt", "b.img")) |
| 740 | .isEqualTo(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result))); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 743 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 744 | public void testRunfilesArtifactsFromNestedSetArtifacts() throws Exception { |
| 745 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 746 | Object result = |
| 747 | evalRuleContextCode( |
| 748 | ruleContext, |
Vladimir Moskva | d200daf | 2016-12-23 16:35:37 +0000 | [diff] [blame] | 749 | "ftb = depset() + ruleContext.files.srcs", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 750 | "ruleContext.runfiles(transitive_files = ftb)"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 751 | assertThat(ImmutableList.of("a.txt", "b.img")) |
| 752 | .isEqualTo(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result))); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 755 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 756 | public void testRunfilesArtifactsFromDefaultAndFiles() throws Exception { |
| 757 | SkylarkRuleContext ruleContext = createRuleContext("//foo:bar"); |
| 758 | Object result = |
| 759 | evalRuleContextCode( |
| 760 | ruleContext, |
| 761 | "artifacts = ruleContext.files.srcs", |
| 762 | // It would be nice to write [DEFAULT] + artifacts, but artifacts |
| 763 | // is an ImmutableList and Skylark interprets it as a tuple. |
| 764 | "ruleContext.runfiles(collect_default = True, files = artifacts)"); |
| 765 | // From DEFAULT only libjl.jar comes, see testRunfilesAddFromDependencies(). |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 766 | assertThat(ImmutableList.of("libjl.jar", "gl.a", "gl.gcgox")) |
| 767 | .isEqualTo(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result))); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 770 | @Test |
| 771 | public void testRunfilesArtifactsFromSymlink() throws Exception { |
| 772 | Object result = |
| 773 | evalRuleContextCode( |
| 774 | "artifacts = ruleContext.files.srcs", |
| 775 | "ruleContext.runfiles(symlinks = {'sym1': artifacts[0]})"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 776 | assertThat(ImmutableList.of("a.txt")) |
| 777 | .isEqualTo(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result))); |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | @Test |
| 781 | public void testRunfilesArtifactsFromRootSymlink() throws Exception { |
| 782 | Object result = |
| 783 | evalRuleContextCode( |
| 784 | "artifacts = ruleContext.files.srcs", |
| 785 | "ruleContext.runfiles(root_symlinks = {'sym1': artifacts[0]})"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 786 | assertThat(ImmutableList.of("a.txt")) |
| 787 | .isEqualTo(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result))); |
Googler | c85af31 | 2016-03-10 17:55:17 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | @Test |
| 791 | public void testRunfilesSymlinkConflict() throws Exception { |
| 792 | // Two different artifacts mapped to same path in runfiles |
| 793 | Object result = |
| 794 | evalRuleContextCode( |
| 795 | "artifacts = ruleContext.files.srcs", |
| 796 | "prefix = ruleContext.workspace_name + '/' if ruleContext.workspace_name else ''", |
| 797 | "ruleContext.runfiles(", |
| 798 | "root_symlinks = {prefix + 'sym1': artifacts[0]},", |
| 799 | "symlinks = {'sym1': artifacts[1]})"); |
| 800 | Runfiles runfiles = (Runfiles) result; |
| 801 | reporter.removeHandler(failFastHandler); // So it doesn't throw exception |
| 802 | runfiles.getRunfilesInputs(reporter, null); |
| 803 | assertContainsEvent("ERROR <no location>: overwrote runfile"); |
| 804 | } |
| 805 | |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 806 | private Iterable<Artifact> getRunfileArtifacts(Object runfiles) { |
| 807 | return ((Runfiles) runfiles).getAllArtifacts(); |
| 808 | } |
| 809 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 810 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 811 | public void testRunfilesBadKeywordArguments() throws Exception { |
| 812 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 813 | checkErrorContains( |
| 814 | ruleContext, |
| 815 | "unexpected keyword 'bad_keyword' in call to runfiles(self: ctx, ", |
| 816 | "ruleContext.runfiles(bad_keyword = '')"); |
| 817 | } |
| 818 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 819 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 820 | public void testNsetContainsList() throws Exception { |
| 821 | checkErrorContains( |
Vladimir Moskva | d200daf | 2016-12-23 16:35:37 +0000 | [diff] [blame] | 822 | "depsets cannot contain items of type 'list'", "depset() + [ruleContext.files.srcs]"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 825 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 826 | public void testCmdJoinPaths() throws Exception { |
| 827 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 828 | Object result = |
| 829 | evalRuleContextCode( |
Vladimir Moskva | d200daf | 2016-12-23 16:35:37 +0000 | [diff] [blame] | 830 | ruleContext, "f = depset(ruleContext.files.srcs)", "cmd_helper.join_paths(':', f)"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 831 | assertThat(result).isEqualTo("foo/a.txt:foo/b.img"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 834 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 835 | public void testStructPlusArtifactErrorMessage() throws Exception { |
| 836 | SkylarkRuleContext ruleContext = createRuleContext("//foo:foo"); |
| 837 | checkErrorContains( |
| 838 | ruleContext, |
| 839 | "unsupported operand type(s) for +: 'File' and 'struct'", |
| 840 | "ruleContext.files.tools[0] + struct(a = 1)"); |
| 841 | } |
| 842 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 843 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 844 | public void testNoSuchProviderErrorMessage() throws Exception { |
| 845 | checkErrorContains( |
| 846 | createRuleContext("//foo:bar"), |
vladmos | 7f6d3a1 | 2017-07-17 23:15:21 +0200 | [diff] [blame] | 847 | "<target //foo:jl> (rule 'java_library') doesn't have provider 'my_provider'", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 848 | "ruleContext.attr.srcs[0].my_provider"); |
| 849 | } |
| 850 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 851 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 852 | public void testFilesForRuleConfiguredTarget() throws Exception { |
| 853 | Object result = |
| 854 | evalRuleContextCode(createRuleContext("//foo:foo"), "ruleContext.attr.srcs[0].files"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 855 | assertThat(ActionsTestUtil.baseNamesOf(((SkylarkNestedSet) result).getSet(Artifact.class))) |
| 856 | .isEqualTo("a.txt"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 859 | @Test |
Vladimir Moskva | eb0cfc5 | 2016-11-09 12:54:51 +0000 | [diff] [blame] | 860 | public void testDefaultProvider() throws Exception { |
| 861 | scratch.file( |
| 862 | "test/foo.bzl", |
| 863 | "foo_provider = provider()", |
| 864 | "def _impl(ctx):", |
dslomov | 3aa7d2f | 2017-04-11 17:55:55 +0000 | [diff] [blame] | 865 | " default = DefaultInfo(", |
Vladimir Moskva | eb0cfc5 | 2016-11-09 12:54:51 +0000 | [diff] [blame] | 866 | " runfiles=ctx.runfiles(ctx.files.runs),", |
| 867 | " )", |
| 868 | " foo = foo_provider()", |
| 869 | " return [foo, default]", |
| 870 | "foo_rule = rule(", |
| 871 | " implementation = _impl,", |
| 872 | " attrs = {", |
| 873 | " 'runs': attr.label_list(allow_files=True),", |
| 874 | " }", |
| 875 | ")" |
| 876 | ); |
| 877 | scratch.file( |
| 878 | "test/bar.bzl", |
| 879 | "load(':foo.bzl', 'foo_provider')", |
| 880 | "def _impl(ctx):", |
dslomov | 3aa7d2f | 2017-04-11 17:55:55 +0000 | [diff] [blame] | 881 | " provider = ctx.attr.deps[0][DefaultInfo]", |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 882 | " return struct(", |
vladmos | 1341e3e | 2017-04-14 16:34:15 +0200 | [diff] [blame] | 883 | " is_provided = DefaultInfo in ctx.attr.deps[0],", |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 884 | " provider = provider,", |
| 885 | " dir = str(sorted(dir(provider))),", |
| 886 | " rule_data_runfiles = provider.data_runfiles,", |
| 887 | " rule_default_runfiles = provider.default_runfiles,", |
| 888 | " rule_files = provider.files,", |
| 889 | " rule_files_to_run = provider.files_to_run,", |
Vladimir Moskva | eb0cfc5 | 2016-11-09 12:54:51 +0000 | [diff] [blame] | 890 | " )", |
| 891 | "bar_rule = rule(", |
| 892 | " implementation = _impl,", |
| 893 | " attrs = {", |
| 894 | " 'deps': attr.label_list(allow_files=True),", |
| 895 | " }", |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 896 | ")"); |
Vladimir Moskva | eb0cfc5 | 2016-11-09 12:54:51 +0000 | [diff] [blame] | 897 | scratch.file( |
| 898 | "test/BUILD", |
| 899 | "load(':foo.bzl', 'foo_rule')", |
| 900 | "load(':bar.bzl', 'bar_rule')", |
| 901 | "foo_rule(name = 'dep_rule', runs = ['run.file', 'run2.file'])", |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 902 | "bar_rule(name = 'my_rule', deps = [':dep_rule', 'file.txt'])"); |
Vladimir Moskva | eb0cfc5 | 2016-11-09 12:54:51 +0000 | [diff] [blame] | 903 | ConfiguredTarget configuredTarget = getConfiguredTarget("//test:my_rule"); |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 904 | |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 905 | assertThat((Boolean) configuredTarget.get("is_provided")).isTrue(); |
vladmos | 1341e3e | 2017-04-14 16:34:15 +0200 | [diff] [blame] | 906 | |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 907 | Object provider = configuredTarget.get("provider"); |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 908 | assertThat(provider).isInstanceOf(DefaultProvider.class); |
vladmos | 97d6708 | 2017-07-13 14:54:03 +0200 | [diff] [blame] | 909 | assertThat(((SkylarkClassObject) provider).getConstructor().getKey()).isEqualTo( |
| 910 | DefaultProvider.SKYLARK_CONSTRUCTOR.getKey()); |
Vladimir Moskva | eb0cfc5 | 2016-11-09 12:54:51 +0000 | [diff] [blame] | 911 | |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 912 | assertThat(configuredTarget.get("dir")) |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 913 | .isEqualTo( |
| 914 | "[\"data_runfiles\", \"default_runfiles\", \"files\", \"files_to_run\", \"to_json\", " |
| 915 | + "\"to_proto\"]"); |
| 916 | |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 917 | assertThat(configuredTarget.get("rule_data_runfiles")).isInstanceOf(Runfiles.class); |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 918 | assertThat( |
| 919 | Iterables.transform( |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 920 | ((Runfiles) configuredTarget.get("rule_data_runfiles")).getAllArtifacts(), |
laurentlb | 5980012 | 2017-07-03 11:42:25 -0400 | [diff] [blame] | 921 | String::valueOf)) |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 922 | .containsExactly( |
| 923 | "File:[/workspace[source]]test/run.file", "File:[/workspace[source]]test/run2.file"); |
| 924 | |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 925 | assertThat(configuredTarget.get("rule_default_runfiles")).isInstanceOf(Runfiles.class); |
| 926 | assertThat( |
| 927 | Iterables.transform( |
| 928 | ((Runfiles) configuredTarget.get("rule_default_runfiles")).getAllArtifacts(), |
laurentlb | 5980012 | 2017-07-03 11:42:25 -0400 | [diff] [blame] | 929 | String::valueOf)) |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 930 | .containsExactly( |
| 931 | "File:[/workspace[source]]test/run.file", "File:[/workspace[source]]test/run2.file"); |
| 932 | |
| 933 | assertThat(configuredTarget.get("rule_files")).isInstanceOf(SkylarkNestedSet.class); |
| 934 | assertThat(configuredTarget.get("rule_files_to_run")).isInstanceOf(FilesToRunProvider.class); |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | @Test |
vladmos | 97d6708 | 2017-07-13 14:54:03 +0200 | [diff] [blame] | 938 | public void testDefaultProviderInStruct() throws Exception { |
| 939 | scratch.file( |
| 940 | "test/foo.bzl", |
| 941 | "foo_provider = provider()", |
| 942 | "def _impl(ctx):", |
| 943 | " default = DefaultInfo(", |
| 944 | " runfiles=ctx.runfiles(ctx.files.runs),", |
| 945 | " )", |
| 946 | " foo = foo_provider()", |
| 947 | " return struct(providers=[foo, default])", |
| 948 | "foo_rule = rule(", |
| 949 | " implementation = _impl,", |
| 950 | " attrs = {", |
| 951 | " 'runs': attr.label_list(allow_files=True),", |
| 952 | " }", |
| 953 | ")"); |
| 954 | scratch.file( |
| 955 | "test/bar.bzl", |
| 956 | "load(':foo.bzl', 'foo_provider')", |
| 957 | "def _impl(ctx):", |
| 958 | " provider = ctx.attr.deps[0][DefaultInfo]", |
| 959 | " return struct(", |
| 960 | " is_provided = DefaultInfo in ctx.attr.deps[0],", |
| 961 | " provider = provider,", |
| 962 | " dir = str(sorted(dir(provider))),", |
| 963 | " rule_data_runfiles = provider.data_runfiles,", |
| 964 | " rule_default_runfiles = provider.default_runfiles,", |
| 965 | " rule_files = provider.files,", |
| 966 | " rule_files_to_run = provider.files_to_run,", |
| 967 | " )", |
| 968 | "bar_rule = rule(", |
| 969 | " implementation = _impl,", |
| 970 | " attrs = {", |
| 971 | " 'deps': attr.label_list(allow_files=True),", |
| 972 | " }", |
| 973 | ")"); |
| 974 | scratch.file( |
| 975 | "test/BUILD", |
| 976 | "load(':foo.bzl', 'foo_rule')", |
| 977 | "load(':bar.bzl', 'bar_rule')", |
| 978 | "foo_rule(name = 'dep_rule', runs = ['run.file', 'run2.file'])", |
| 979 | "bar_rule(name = 'my_rule', deps = [':dep_rule', 'file.txt'])"); |
| 980 | ConfiguredTarget configuredTarget = getConfiguredTarget("//test:my_rule"); |
| 981 | |
| 982 | assertThat((Boolean) configuredTarget.get("is_provided")).isTrue(); |
| 983 | |
| 984 | Object provider = configuredTarget.get("provider"); |
| 985 | assertThat(provider).isInstanceOf(DefaultProvider.class); |
| 986 | assertThat(((SkylarkClassObject) provider).getConstructor().getKey()).isEqualTo( |
| 987 | DefaultProvider.SKYLARK_CONSTRUCTOR.getKey()); |
| 988 | |
| 989 | assertThat(configuredTarget.get("dir")) |
| 990 | .isEqualTo( |
| 991 | "[\"data_runfiles\", \"default_runfiles\", \"files\", \"files_to_run\", \"to_json\", " |
| 992 | + "\"to_proto\"]"); |
| 993 | |
| 994 | assertThat(configuredTarget.get("rule_data_runfiles")).isInstanceOf(Runfiles.class); |
| 995 | assertThat( |
| 996 | Iterables.transform( |
| 997 | ((Runfiles) configuredTarget.get("rule_data_runfiles")).getAllArtifacts(), |
| 998 | String::valueOf)) |
| 999 | .containsExactly( |
| 1000 | "File:[/workspace[source]]test/run.file", "File:[/workspace[source]]test/run2.file"); |
| 1001 | |
| 1002 | assertThat(configuredTarget.get("rule_default_runfiles")).isInstanceOf(Runfiles.class); |
| 1003 | assertThat( |
| 1004 | Iterables.transform( |
| 1005 | ((Runfiles) configuredTarget.get("rule_default_runfiles")).getAllArtifacts(), |
| 1006 | String::valueOf)) |
| 1007 | .containsExactly( |
| 1008 | "File:[/workspace[source]]test/run.file", "File:[/workspace[source]]test/run2.file"); |
| 1009 | |
| 1010 | assertThat(configuredTarget.get("rule_files")).isInstanceOf(SkylarkNestedSet.class); |
| 1011 | assertThat(configuredTarget.get("rule_files_to_run")).isInstanceOf(FilesToRunProvider.class); |
| 1012 | } |
| 1013 | |
| 1014 | @Test |
| 1015 | public void testDefaultProviderInvalidConfiguration() throws Exception { |
| 1016 | scratch.file( |
| 1017 | "test/foo.bzl", |
| 1018 | "foo_provider = provider()", |
| 1019 | "def _impl(ctx):", |
| 1020 | " default = DefaultInfo(", |
| 1021 | " runfiles=ctx.runfiles(ctx.files.runs),", |
| 1022 | " )", |
| 1023 | " foo = foo_provider()", |
| 1024 | " return struct(providers=[foo, default], files=depset([]))", |
| 1025 | "foo_rule = rule(", |
| 1026 | " implementation = _impl,", |
| 1027 | " attrs = {", |
| 1028 | " 'runs': attr.label_list(allow_files=True),", |
| 1029 | " }", |
| 1030 | ")"); |
| 1031 | scratch.file( |
| 1032 | "test/BUILD", |
| 1033 | "load(':foo.bzl', 'foo_rule')", |
| 1034 | "foo_rule(name = 'my_rule', runs = ['run.file', 'run2.file'])"); |
| 1035 | |
| 1036 | try { |
| 1037 | getConfiguredTarget("//test:my_rule"); |
| 1038 | fail(); |
| 1039 | } catch (AssertionError expected) { |
| 1040 | assertThat(expected) |
| 1041 | .hasMessageThat() |
| 1042 | .contains( |
| 1043 | "Provider 'files' should be specified in DefaultInfo " |
| 1044 | + "if it's provided explicitly."); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | @Test |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 1049 | public void testDefaultProviderOnFileTarget() throws Exception { |
| 1050 | scratch.file( |
| 1051 | "test/bar.bzl", |
| 1052 | "def _impl(ctx):", |
dslomov | 3aa7d2f | 2017-04-11 17:55:55 +0000 | [diff] [blame] | 1053 | " provider = ctx.attr.deps[0][DefaultInfo]", |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 1054 | " return struct(", |
vladmos | 1341e3e | 2017-04-14 16:34:15 +0200 | [diff] [blame] | 1055 | " is_provided = DefaultInfo in ctx.attr.deps[0],", |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 1056 | " provider = provider,", |
| 1057 | " dir = str(sorted(dir(provider))),", |
| 1058 | " file_data_runfiles = provider.data_runfiles,", |
| 1059 | " file_default_runfiles = provider.default_runfiles,", |
| 1060 | " file_files = provider.files,", |
| 1061 | " file_files_to_run = provider.files_to_run,", |
| 1062 | " )", |
| 1063 | "bar_rule = rule(", |
| 1064 | " implementation = _impl,", |
| 1065 | " attrs = {", |
| 1066 | " 'deps': attr.label_list(allow_files=True),", |
| 1067 | " }", |
| 1068 | ")"); |
| 1069 | scratch.file( |
| 1070 | "test/BUILD", |
| 1071 | "load(':bar.bzl', 'bar_rule')", |
| 1072 | "bar_rule(name = 'my_rule', deps = ['file.txt'])"); |
| 1073 | ConfiguredTarget configuredTarget = getConfiguredTarget("//test:my_rule"); |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 1074 | |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 1075 | assertThat((Boolean) configuredTarget.get("is_provided")).isTrue(); |
vladmos | 1341e3e | 2017-04-14 16:34:15 +0200 | [diff] [blame] | 1076 | |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 1077 | Object provider = configuredTarget.get("provider"); |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 1078 | assertThat(provider).isInstanceOf(DefaultProvider.class); |
vladmos | 97d6708 | 2017-07-13 14:54:03 +0200 | [diff] [blame] | 1079 | assertThat(((SkylarkClassObject) provider).getConstructor().getKey()).isEqualTo( |
| 1080 | DefaultProvider.SKYLARK_CONSTRUCTOR.getKey()); |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 1081 | |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 1082 | assertThat(configuredTarget.get("dir")) |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 1083 | .isEqualTo( |
| 1084 | "[\"data_runfiles\", \"default_runfiles\", \"files\", \"files_to_run\", \"to_json\", " |
| 1085 | + "\"to_proto\"]"); |
| 1086 | |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 1087 | assertThat(configuredTarget.get("file_data_runfiles")).isInstanceOf(Runfiles.class); |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 1088 | assertThat( |
| 1089 | Iterables.transform( |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 1090 | ((Runfiles) configuredTarget.get("file_data_runfiles")).getAllArtifacts(), |
laurentlb | 5980012 | 2017-07-03 11:42:25 -0400 | [diff] [blame] | 1091 | String::valueOf)) |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 1092 | .isEmpty(); |
| 1093 | |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 1094 | assertThat(configuredTarget.get("file_default_runfiles")).isInstanceOf(Runfiles.class); |
| 1095 | assertThat( |
| 1096 | Iterables.transform( |
| 1097 | ((Runfiles) configuredTarget.get("file_default_runfiles")).getAllArtifacts(), |
laurentlb | 5980012 | 2017-07-03 11:42:25 -0400 | [diff] [blame] | 1098 | String::valueOf)) |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 1099 | .isEmpty(); |
| 1100 | |
| 1101 | assertThat(configuredTarget.get("file_files")).isInstanceOf(SkylarkNestedSet.class); |
| 1102 | assertThat(configuredTarget.get("file_files_to_run")).isInstanceOf(FilesToRunProvider.class); |
Vladimir Moskva | eb0cfc5 | 2016-11-09 12:54:51 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | @Test |
| 1106 | public void testDefaultProviderProvidedImplicitly() throws Exception { |
| 1107 | scratch.file( |
| 1108 | "test/foo.bzl", |
| 1109 | "foo_provider = provider()", |
| 1110 | "def _impl(ctx):", |
| 1111 | " foo = foo_provider()", |
| 1112 | " return [foo]", |
| 1113 | "foo_rule = rule(", |
| 1114 | " implementation = _impl,", |
| 1115 | ")" |
| 1116 | ); |
| 1117 | scratch.file( |
| 1118 | "test/bar.bzl", |
| 1119 | "load(':foo.bzl', 'foo_provider')", |
| 1120 | "def _impl(ctx):", |
| 1121 | " dep = ctx.attr.deps[0]", |
dslomov | 3aa7d2f | 2017-04-11 17:55:55 +0000 | [diff] [blame] | 1122 | " provider = dep[DefaultInfo]", // The goal is to test this object |
| 1123 | " return struct(", // so we return it here |
Vladimir Moskva | eb0cfc5 | 2016-11-09 12:54:51 +0000 | [diff] [blame] | 1124 | " default = provider,", |
| 1125 | " )", |
| 1126 | "bar_rule = rule(", |
| 1127 | " implementation = _impl,", |
| 1128 | " attrs = {", |
| 1129 | " 'deps': attr.label_list(allow_files=True),", |
| 1130 | " }", |
| 1131 | ")" |
| 1132 | ); |
| 1133 | scratch.file( |
| 1134 | "test/BUILD", |
| 1135 | "load(':foo.bzl', 'foo_rule')", |
| 1136 | "load(':bar.bzl', 'bar_rule')", |
| 1137 | "foo_rule(name = 'dep_rule')", |
| 1138 | "bar_rule(name = 'my_rule', deps = [':dep_rule'])"); |
| 1139 | ConfiguredTarget configuredTarget = getConfiguredTarget("//test:my_rule"); |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 1140 | Object provider = configuredTarget.get("default"); |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 1141 | assertThat(provider).isInstanceOf(DefaultProvider.class); |
vladmos | 97d6708 | 2017-07-13 14:54:03 +0200 | [diff] [blame] | 1142 | assertThat(((SkylarkClassObject) provider).getConstructor().getKey()).isEqualTo( |
| 1143 | DefaultProvider.SKYLARK_CONSTRUCTOR.getKey()); |
Vladimir Moskva | eb0cfc5 | 2016-11-09 12:54:51 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | @Test |
| 1147 | public void testDefaultProviderUnknownFields() throws Exception { |
| 1148 | scratch.file( |
| 1149 | "test/foo.bzl", |
| 1150 | "foo_provider = provider()", |
| 1151 | "def _impl(ctx):", |
dslomov | 3aa7d2f | 2017-04-11 17:55:55 +0000 | [diff] [blame] | 1152 | " default = DefaultInfo(", |
Vladimir Moskva | eb0cfc5 | 2016-11-09 12:54:51 +0000 | [diff] [blame] | 1153 | " foo=ctx.runfiles(),", |
| 1154 | " )", |
| 1155 | " return [default]", |
| 1156 | "foo_rule = rule(", |
| 1157 | " implementation = _impl,", |
| 1158 | ")" |
| 1159 | ); |
| 1160 | scratch.file( |
| 1161 | "test/BUILD", |
| 1162 | "load(':foo.bzl', 'foo_rule')", |
| 1163 | "foo_rule(name = 'my_rule')" |
| 1164 | ); |
| 1165 | try { |
| 1166 | getConfiguredTarget("//test:my_rule"); |
| 1167 | fail(); |
| 1168 | } catch (AssertionError expected) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1169 | assertThat(expected).hasMessageThat().contains("Invalid key for default provider: foo"); |
Vladimir Moskva | eb0cfc5 | 2016-11-09 12:54:51 +0000 | [diff] [blame] | 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | @Test |
Vladimir Moskva | 748ba86 | 2016-09-20 13:46:11 +0000 | [diff] [blame] | 1174 | public void testDeclaredProviders() throws Exception { |
| 1175 | scratch.file( |
| 1176 | "test/foo.bzl", |
| 1177 | "foo_provider = provider()", |
| 1178 | "foobar_provider = provider()", |
| 1179 | "def _impl(ctx):", |
| 1180 | " foo = foo_provider()", |
| 1181 | " foobar = foobar_provider()", |
| 1182 | " return [foo, foobar]", |
| 1183 | "foo_rule = rule(", |
| 1184 | " implementation = _impl,", |
| 1185 | " attrs = {", |
| 1186 | " \"srcs\": attr.label_list(allow_files=True),", |
| 1187 | " }", |
| 1188 | ")" |
| 1189 | ); |
| 1190 | scratch.file( |
| 1191 | "test/bar.bzl", |
| 1192 | "load(':foo.bzl', 'foo_provider')", |
| 1193 | "def _impl(ctx):", |
| 1194 | " dep = ctx.attr.deps[0]", |
| 1195 | " provider = dep[foo_provider]", // The goal is to test this object |
| 1196 | " return struct(proxy = provider)", // so we return it here |
| 1197 | "bar_rule = rule(", |
| 1198 | " implementation = _impl,", |
| 1199 | " attrs = {", |
| 1200 | " 'srcs': attr.label_list(allow_files=True),", |
| 1201 | " 'deps': attr.label_list(allow_files=True),", |
| 1202 | " }", |
| 1203 | ")" |
| 1204 | ); |
| 1205 | scratch.file( |
| 1206 | "test/BUILD", |
| 1207 | "load(':foo.bzl', 'foo_rule')", |
| 1208 | "load(':bar.bzl', 'bar_rule')", |
| 1209 | "foo_rule(name = 'dep_rule')", |
| 1210 | "bar_rule(name = 'my_rule', deps = [':dep_rule'])"); |
| 1211 | ConfiguredTarget configuredTarget = getConfiguredTarget("//test:my_rule"); |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 1212 | Object provider = configuredTarget.get("proxy"); |
Vladimir Moskva | 748ba86 | 2016-09-20 13:46:11 +0000 | [diff] [blame] | 1213 | assertThat(provider).isInstanceOf(SkylarkClassObject.class); |
vladmos | 97d6708 | 2017-07-13 14:54:03 +0200 | [diff] [blame] | 1214 | assertThat(((SkylarkClassObject) provider).getConstructor().getKey()).isEqualTo( |
| 1215 | new SkylarkKey(Label.parseAbsolute("//test:foo.bzl"), "foo_provider")); |
| 1216 | } |
| 1217 | |
| 1218 | @Test |
| 1219 | public void testSingleDeclaredProvider() throws Exception { |
| 1220 | scratch.file( |
| 1221 | "test/foo.bzl", |
| 1222 | "foo_provider = provider()", |
| 1223 | "def _impl(ctx):", |
| 1224 | " return foo_provider(a=123)", |
| 1225 | "foo_rule = rule(", |
| 1226 | " implementation = _impl,", |
| 1227 | " attrs = {", |
| 1228 | " \"srcs\": attr.label_list(allow_files=True),", |
| 1229 | " }", |
| 1230 | ")"); |
| 1231 | scratch.file( |
| 1232 | "test/bar.bzl", |
| 1233 | "load(':foo.bzl', 'foo_provider')", |
| 1234 | "def _impl(ctx):", |
| 1235 | " dep = ctx.attr.deps[0]", |
| 1236 | " provider = dep[foo_provider]", // The goal is to test this object |
| 1237 | " return struct(proxy = provider)", // so we return it here |
| 1238 | "bar_rule = rule(", |
| 1239 | " implementation = _impl,", |
| 1240 | " attrs = {", |
| 1241 | " 'srcs': attr.label_list(allow_files=True),", |
| 1242 | " 'deps': attr.label_list(allow_files=True),", |
| 1243 | " }", |
| 1244 | ")"); |
| 1245 | scratch.file( |
| 1246 | "test/BUILD", |
| 1247 | "load(':foo.bzl', 'foo_rule')", |
| 1248 | "load(':bar.bzl', 'bar_rule')", |
| 1249 | "foo_rule(name = 'dep_rule')", |
| 1250 | "bar_rule(name = 'my_rule', deps = [':dep_rule'])"); |
| 1251 | ConfiguredTarget configuredTarget = getConfiguredTarget("//test:my_rule"); |
| 1252 | Object provider = configuredTarget.get("proxy"); |
| 1253 | assertThat(provider).isInstanceOf(SkylarkClassObject.class); |
| 1254 | assertThat(((SkylarkClassObject) provider).getConstructor().getKey()).isEqualTo( |
| 1255 | new SkylarkKey(Label.parseAbsolute("//test:foo.bzl"), "foo_provider")); |
| 1256 | assertThat(((SkylarkClassObject) provider).getValue("a")).isEqualTo(123); |
Vladimir Moskva | 748ba86 | 2016-09-20 13:46:11 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | @Test |
| 1260 | public void testDeclaredProvidersAliasTarget() throws Exception { |
| 1261 | scratch.file( |
| 1262 | "test/foo.bzl", |
| 1263 | "foo_provider = provider()", |
| 1264 | "foobar_provider = provider()", |
| 1265 | "def _impl(ctx):", |
| 1266 | " foo = foo_provider()", |
| 1267 | " foobar = foobar_provider()", |
| 1268 | " return [foo, foobar]", |
| 1269 | "foo_rule = rule(", |
| 1270 | " implementation = _impl,", |
| 1271 | " attrs = {", |
| 1272 | " \"srcs\": attr.label_list(allow_files=True),", |
| 1273 | " }", |
| 1274 | ")" |
| 1275 | ); |
| 1276 | scratch.file( |
| 1277 | "test/bar.bzl", |
| 1278 | "load(':foo.bzl', 'foo_provider')", |
| 1279 | "def _impl(ctx):", |
| 1280 | " dep = ctx.attr.deps[0]", |
| 1281 | " provider = dep[foo_provider]", // The goal is to test this object |
| 1282 | " return struct(proxy = provider)", // so we return it here |
| 1283 | "bar_rule = rule(", |
| 1284 | " implementation = _impl,", |
| 1285 | " attrs = {", |
| 1286 | " 'srcs': attr.label_list(allow_files=True),", |
| 1287 | " 'deps': attr.label_list(allow_files=True),", |
| 1288 | " }", |
| 1289 | ")" |
| 1290 | ); |
| 1291 | scratch.file( |
| 1292 | "test/BUILD", |
| 1293 | "load(':foo.bzl', 'foo_rule')", |
| 1294 | "load(':bar.bzl', 'bar_rule')", |
| 1295 | "foo_rule(name = 'foo_rule')", |
| 1296 | "alias(name = 'dep_rule', actual=':foo_rule')", |
| 1297 | "bar_rule(name = 'my_rule', deps = [':dep_rule'])"); |
| 1298 | ConfiguredTarget configuredTarget = getConfiguredTarget("//test:my_rule"); |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 1299 | Object provider = configuredTarget.get("proxy"); |
Vladimir Moskva | 748ba86 | 2016-09-20 13:46:11 +0000 | [diff] [blame] | 1300 | assertThat(provider).isInstanceOf(SkylarkClassObject.class); |
vladmos | 97d6708 | 2017-07-13 14:54:03 +0200 | [diff] [blame] | 1301 | assertThat(((SkylarkClassObject) provider).getConstructor().getKey()).isEqualTo( |
| 1302 | new SkylarkKey(Label.parseAbsolute("//test:foo.bzl"), "foo_provider")); |
Vladimir Moskva | 748ba86 | 2016-09-20 13:46:11 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | @Test |
| 1306 | public void testDeclaredProvidersWrongKey() throws Exception { |
| 1307 | scratch.file( |
| 1308 | "test/foo.bzl", |
| 1309 | "foo_provider = provider()", |
| 1310 | "unused_provider = provider()", |
| 1311 | "def _impl(ctx):", |
| 1312 | " foo = foo_provider()", |
| 1313 | " return [foo]", |
| 1314 | "foo_rule = rule(", |
| 1315 | " implementation = _impl,", |
| 1316 | " attrs = {", |
| 1317 | " \"srcs\": attr.label_list(allow_files=True),", |
| 1318 | " }", |
| 1319 | ")" |
| 1320 | ); |
| 1321 | scratch.file( |
| 1322 | "test/bar.bzl", |
| 1323 | "load(':foo.bzl', 'unused_provider')", |
| 1324 | "def _impl(ctx):", |
| 1325 | " dep = ctx.attr.deps[0]", |
| 1326 | " provider = dep[unused_provider]", // Should throw an error here |
| 1327 | "bar_rule = rule(", |
| 1328 | " implementation = _impl,", |
| 1329 | " attrs = {", |
| 1330 | " 'srcs': attr.label_list(allow_files=True),", |
| 1331 | " 'deps': attr.label_list(allow_files=True),", |
| 1332 | " }", |
| 1333 | ")" |
| 1334 | ); |
| 1335 | scratch.file( |
| 1336 | "test/BUILD", |
| 1337 | "load(':foo.bzl', 'foo_rule')", |
| 1338 | "load(':bar.bzl', 'bar_rule')", |
| 1339 | "foo_rule(name = 'dep_rule')", |
| 1340 | "bar_rule(name = 'my_rule', deps = [':dep_rule'])"); |
| 1341 | |
| 1342 | try { |
| 1343 | getConfiguredTarget("//test:my_rule"); |
| 1344 | fail(); |
| 1345 | } catch (AssertionError expected) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1346 | assertThat(expected) |
| 1347 | .hasMessageThat() |
vladmos | 7f6d3a1 | 2017-07-17 23:15:21 +0200 | [diff] [blame] | 1348 | .contains("<target //test:dep_rule> (rule 'foo_rule') doesn't contain " |
| 1349 | + "declared provider 'unused_provider'"); |
Vladimir Moskva | 748ba86 | 2016-09-20 13:46:11 +0000 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | @Test |
| 1354 | public void testDeclaredProvidersInvalidKey() throws Exception { |
| 1355 | scratch.file( |
| 1356 | "test/foo.bzl", |
| 1357 | "foo_provider = provider()", |
| 1358 | "def _impl(ctx):", |
| 1359 | " foo = foo_provider()", |
| 1360 | " return [foo]", |
| 1361 | "foo_rule = rule(", |
| 1362 | " implementation = _impl,", |
| 1363 | " attrs = {", |
| 1364 | " \"srcs\": attr.label_list(allow_files=True),", |
| 1365 | " }", |
| 1366 | ")" |
| 1367 | ); |
| 1368 | scratch.file( |
| 1369 | "test/bar.bzl", |
| 1370 | "def _impl(ctx):", |
| 1371 | " dep = ctx.attr.deps[0]", |
| 1372 | " provider = dep['foo_provider']", // Should throw an error here |
| 1373 | "bar_rule = rule(", |
| 1374 | " implementation = _impl,", |
| 1375 | " attrs = {", |
| 1376 | " 'srcs': attr.label_list(allow_files=True),", |
| 1377 | " 'deps': attr.label_list(allow_files=True),", |
| 1378 | " }", |
| 1379 | ")" |
| 1380 | ); |
| 1381 | scratch.file( |
| 1382 | "test/BUILD", |
| 1383 | "load(':foo.bzl', 'foo_rule')", |
| 1384 | "load(':bar.bzl', 'bar_rule')", |
| 1385 | "foo_rule(name = 'dep_rule')", |
| 1386 | "bar_rule(name = 'my_rule', deps = [':dep_rule'])"); |
| 1387 | |
| 1388 | try { |
| 1389 | getConfiguredTarget("//test:my_rule"); |
| 1390 | fail(); |
| 1391 | } catch (AssertionError expected) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1392 | assertThat(expected) |
| 1393 | .hasMessageThat() |
| 1394 | .contains( |
vladmos | 7f6d3a1 | 2017-07-17 23:15:21 +0200 | [diff] [blame] | 1395 | "Type Target only supports indexing by object constructors, got string instead"); |
Vladimir Moskva | 748ba86 | 2016-09-20 13:46:11 +0000 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | @Test |
| 1400 | public void testDeclaredProvidersFileTarget() throws Exception { |
| 1401 | scratch.file( |
| 1402 | "test/bar.bzl", |
| 1403 | "unused_provider = provider()", |
| 1404 | "def _impl(ctx):", |
| 1405 | " src = ctx.attr.srcs[0]", |
| 1406 | " provider = src[unused_provider]", // Should throw an error here |
| 1407 | "bar_rule = rule(", |
| 1408 | " implementation = _impl,", |
| 1409 | " attrs = {", |
| 1410 | " 'srcs': attr.label_list(allow_files=True),", |
| 1411 | " }", |
| 1412 | ")" |
| 1413 | ); |
| 1414 | scratch.file( |
| 1415 | "test/BUILD", |
| 1416 | "load(':bar.bzl', 'bar_rule')", |
| 1417 | "bar_rule(name = 'my_rule', srcs = ['input.txt'])"); |
| 1418 | |
| 1419 | try { |
| 1420 | getConfiguredTarget("//test:my_rule"); |
| 1421 | fail(); |
| 1422 | } catch (AssertionError expected) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1423 | assertThat(expected) |
| 1424 | .hasMessageThat() |
vladmos | 7f6d3a1 | 2017-07-17 23:15:21 +0200 | [diff] [blame] | 1425 | .contains("<input file target //test:input.txt> doesn't contain " |
| 1426 | + "declared provider 'unused_provider'"); |
Vladimir Moskva | 748ba86 | 2016-09-20 13:46:11 +0000 | [diff] [blame] | 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | @Test |
Vladimir Moskva | 4f5e12d | 2016-10-07 16:28:23 +0000 | [diff] [blame] | 1431 | public void testDeclaredProvidersInOperator() throws Exception { |
| 1432 | scratch.file( |
| 1433 | "test/foo.bzl", |
| 1434 | "foo_provider = provider()", |
| 1435 | "bar_provider = provider()", |
| 1436 | "", |
| 1437 | "def _inner_impl(ctx):", |
| 1438 | " foo = foo_provider()", |
| 1439 | " return [foo]", |
| 1440 | "inner_rule = rule(", |
| 1441 | " implementation = _inner_impl,", |
| 1442 | ")", |
| 1443 | "", |
| 1444 | "def _outer_impl(ctx):", |
| 1445 | " dep = ctx.attr.deps[0]", |
| 1446 | " return struct(", |
| 1447 | " foo = (foo_provider in dep),", // Should be true |
| 1448 | " bar = (bar_provider in dep),", // Should be false |
| 1449 | " )", |
| 1450 | "outer_rule = rule(", |
| 1451 | " implementation = _outer_impl,", |
| 1452 | " attrs = {", |
| 1453 | " 'deps': attr.label_list(),", |
| 1454 | " }", |
| 1455 | ")" |
| 1456 | ); |
| 1457 | scratch.file( |
| 1458 | "test/BUILD", |
| 1459 | "load(':foo.bzl', 'inner_rule', 'outer_rule')", |
| 1460 | "inner_rule(name = 'dep_rule')", |
| 1461 | "outer_rule(name = 'my_rule', deps = [':dep_rule'])"); |
| 1462 | |
| 1463 | ConfiguredTarget configuredTarget = getConfiguredTarget("//test:my_rule"); |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 1464 | Object foo = configuredTarget.get("foo"); |
Vladimir Moskva | 4f5e12d | 2016-10-07 16:28:23 +0000 | [diff] [blame] | 1465 | assertThat(foo).isInstanceOf(Boolean.class); |
| 1466 | assertThat((Boolean) foo).isTrue(); |
dslomov | 211a3ba | 2017-05-16 00:21:22 +0200 | [diff] [blame] | 1467 | Object bar = configuredTarget.get("bar"); |
Vladimir Moskva | 4f5e12d | 2016-10-07 16:28:23 +0000 | [diff] [blame] | 1468 | assertThat(bar).isInstanceOf(Boolean.class); |
| 1469 | assertThat((Boolean) bar).isFalse(); |
| 1470 | } |
| 1471 | |
| 1472 | @Test |
| 1473 | public void testDeclaredProvidersInOperatorInvalidKey() throws Exception { |
| 1474 | scratch.file( |
| 1475 | "test/foo.bzl", |
| 1476 | "foo_provider = provider()", |
| 1477 | "bar_provider = provider()", |
| 1478 | "", |
| 1479 | "def _inner_impl(ctx):", |
| 1480 | " foo = foo_provider()", |
| 1481 | " return [foo]", |
| 1482 | "inner_rule = rule(", |
| 1483 | " implementation = _inner_impl,", |
| 1484 | ")", |
| 1485 | "", |
| 1486 | "def _outer_impl(ctx):", |
| 1487 | " dep = ctx.attr.deps[0]", |
| 1488 | " 'foo_provider' in dep", // Should throw an error here |
| 1489 | "outer_rule = rule(", |
| 1490 | " implementation = _outer_impl,", |
| 1491 | " attrs = {", |
| 1492 | " 'deps': attr.label_list(),", |
| 1493 | " }", |
| 1494 | ")" |
| 1495 | ); |
| 1496 | scratch.file( |
| 1497 | "test/BUILD", |
| 1498 | "load(':foo.bzl', 'inner_rule', 'outer_rule')", |
| 1499 | "inner_rule(name = 'dep_rule')", |
| 1500 | "outer_rule(name = 'my_rule', deps = [':dep_rule'])"); |
| 1501 | |
| 1502 | try { |
| 1503 | getConfiguredTarget("//test:my_rule"); |
| 1504 | fail(); |
| 1505 | } catch (AssertionError expected) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1506 | assertThat(expected) |
| 1507 | .hasMessageThat() |
| 1508 | .contains( |
vladmos | 7f6d3a1 | 2017-07-17 23:15:21 +0200 | [diff] [blame] | 1509 | "Type Target only supports querying by object constructors, got string instead"); |
Vladimir Moskva | 4f5e12d | 2016-10-07 16:28:23 +0000 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1514 | public void testFilesForFileConfiguredTarget() throws Exception { |
| 1515 | Object result = |
| 1516 | evalRuleContextCode(createRuleContext("//foo:bar"), "ruleContext.attr.srcs[0].files"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1517 | assertThat(ActionsTestUtil.baseNamesOf(((SkylarkNestedSet) result).getSet(Artifact.class))) |
| 1518 | .isEqualTo("libjl.jar"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 1521 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1522 | public void testCtxStructFieldsCustomErrorMessages() throws Exception { |
| 1523 | checkErrorContains("No attribute 'foo' in attr.", "ruleContext.attr.foo"); |
| 1524 | checkErrorContains("No attribute 'foo' in outputs.", "ruleContext.outputs.foo"); |
| 1525 | checkErrorContains("No attribute 'foo' in files.", "ruleContext.files.foo"); |
| 1526 | checkErrorContains("No attribute 'foo' in file.", "ruleContext.file.foo"); |
| 1527 | checkErrorContains("No attribute 'foo' in executable.", "ruleContext.executable.foo"); |
| 1528 | } |
| 1529 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 1530 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1531 | public void testBinDirPath() throws Exception { |
| 1532 | SkylarkRuleContext ctx = createRuleContext("//foo:bar"); |
Kristina Chodorow | ba41c2d | 2016-10-10 17:21:24 +0000 | [diff] [blame] | 1533 | Object result = evalRuleContextCode(ctx, "ruleContext.bin_dir.path"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1534 | assertThat(result).isEqualTo(ctx.getConfiguration().getBinFragment().getPathString()); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 1537 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1538 | public void testEmptyLabelListTypeAttrInCtx() throws Exception { |
| 1539 | SkylarkRuleContext ctx = createRuleContext("//foo:baz"); |
| 1540 | Object result = evalRuleContextCode(ctx, "ruleContext.attr.srcs"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1541 | assertThat(result).isEqualTo(MutableList.EMPTY); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 1544 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1545 | public void testDefinedMakeVariable() throws Exception { |
| 1546 | SkylarkRuleContext ctx = createRuleContext("//foo:baz"); |
Liam Miller-Cushon | 191646d | 2016-01-29 20:14:48 +0000 | [diff] [blame] | 1547 | String java = (String) evalRuleContextCode(ctx, "ruleContext.var['JAVA']"); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1548 | // Get the last path segment |
Liam Miller-Cushon | 191646d | 2016-01-29 20:14:48 +0000 | [diff] [blame] | 1549 | java = java.substring(java.lastIndexOf('/')); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1550 | assertThat(java).isEqualTo("/java" + OsUtils.executableExtension()); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 1553 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1554 | public void testCodeCoverageConfigurationAccess() throws Exception { |
| 1555 | SkylarkRuleContext ctx = createRuleContext("//foo:baz"); |
| 1556 | boolean coverage = |
| 1557 | (Boolean) evalRuleContextCode(ctx, "ruleContext.configuration.coverage_enabled"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 1558 | assertThat(ctx.getRuleContext().getConfiguration().isCodeCoverageEnabled()).isEqualTo(coverage); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | @Override |
| 1562 | protected void checkErrorContains(String errorMsg, String... lines) throws Exception { |
| 1563 | super.checkErrorContains(createRuleContext("//foo:foo"), errorMsg, lines); |
| 1564 | } |
| 1565 | |
| 1566 | /** |
| 1567 | * Checks whether the given (invalid) statement leads to the expected error |
| 1568 | */ |
| 1569 | private void checkReportedErrorStartsWith( |
| 1570 | SkylarkRuleContext ruleContext, String errorMsg, String... statements) throws Exception { |
| 1571 | // If the component under test relies on Reporter and EventCollector for error handling, any |
| 1572 | // error would lead to an asynchronous AssertionFailedError thanks to failFastHandler in |
| 1573 | // FoundationTestCase. |
| 1574 | // |
| 1575 | // Consequently, we disable failFastHandler and check all events for the expected error message |
| 1576 | reporter.removeHandler(failFastHandler); |
| 1577 | |
| 1578 | Object result = evalRuleContextCode(ruleContext, statements); |
| 1579 | |
| 1580 | String first = null; |
| 1581 | int count = 0; |
| 1582 | |
| 1583 | try { |
| 1584 | for (Event evt : eventCollector) { |
| 1585 | if (evt.getMessage().startsWith(errorMsg)) { |
| 1586 | return; |
| 1587 | } |
| 1588 | |
| 1589 | ++count; |
| 1590 | first = evt.getMessage(); |
| 1591 | } |
| 1592 | |
| 1593 | if (count == 0) { |
| 1594 | fail( |
| 1595 | String.format( |
| 1596 | "checkReportedErrorStartsWith(): There was no error; the result is '%s'", result)); |
| 1597 | } else { |
| 1598 | fail( |
| 1599 | String.format( |
| 1600 | "Found %d error(s), but none with the expected message '%s'. First error: '%s'", |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 1601 | count, errorMsg, first)); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1602 | } |
| 1603 | } finally { |
| 1604 | eventCollector.clear(); |
| 1605 | } |
| 1606 | } |
| 1607 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 1608 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1609 | public void testStackTraceWithoutOriginalMessage() throws Exception { |
| 1610 | setupThrowFunction( |
| 1611 | new BuiltinFunction("throw") { |
| 1612 | @SuppressWarnings("unused") |
| 1613 | public Object invoke() throws Exception { |
| 1614 | throw new ThereIsNoMessageException(); |
| 1615 | } |
| 1616 | }); |
| 1617 | |
| 1618 | checkEvalErrorContains( |
laurentlb | 5980012 | 2017-07-03 11:42:25 -0400 | [diff] [blame] | 1619 | "There Is No Message: SkylarkRuleImplementationFunctionsTest", |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1620 | "throw()"); |
| 1621 | } |
| 1622 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 1623 | @Test |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1624 | public void testNoStackTraceOnInterrupt() throws Exception { |
| 1625 | setupThrowFunction( |
| 1626 | new BuiltinFunction("throw") { |
| 1627 | @SuppressWarnings("unused") |
| 1628 | public Object invoke() throws Exception { |
| 1629 | throw new InterruptedException(); |
| 1630 | } |
| 1631 | }); |
| 1632 | try { |
| 1633 | eval("throw()"); |
| 1634 | fail("Expected an InterruptedException"); |
| 1635 | } catch (InterruptedException ex) { |
| 1636 | // Expected. |
| 1637 | } |
| 1638 | } |
| 1639 | |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 1640 | @Test |
Florian Weikert | 3c5f534 | 2015-09-24 17:29:04 +0000 | [diff] [blame] | 1641 | public void testGlobInImplicitOutputs() throws Exception { |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 1642 | scratch.file( |
| 1643 | "test/glob.bzl", |
Florian Weikert | 3c5f534 | 2015-09-24 17:29:04 +0000 | [diff] [blame] | 1644 | "def _impl(ctx):", |
| 1645 | " ctx.empty_action(", |
| 1646 | " inputs = [],", |
| 1647 | " )", |
Florian Weikert | 3ae5912 | 2016-06-06 09:11:43 +0000 | [diff] [blame] | 1648 | "def _foo():", |
Florian Weikert | 3c5f534 | 2015-09-24 17:29:04 +0000 | [diff] [blame] | 1649 | " return native.glob(['*'])", |
| 1650 | "glob_rule = rule(", |
| 1651 | " implementation = _impl,", |
| 1652 | " outputs = _foo,", |
| 1653 | ")"); |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 1654 | scratch.file( |
| 1655 | "test/BUILD", |
Florian Weikert | 3c5f534 | 2015-09-24 17:29:04 +0000 | [diff] [blame] | 1656 | "load('/test/glob', 'glob_rule')", |
| 1657 | "glob_rule(name = 'my_glob',", |
| 1658 | " srcs = ['foo.bar', 'other_foo.bar'])"); |
| 1659 | reporter.removeHandler(failFastHandler); |
| 1660 | getConfiguredTarget("//test:my_glob"); |
| 1661 | assertContainsEvent("native.glob() can only be called during the loading phase"); |
| 1662 | } |
| 1663 | |
Francois-Rene Rideau | 1bae01f | 2016-01-30 04:25:58 +0000 | [diff] [blame] | 1664 | @Test |
Laurent Le Brun | 1d19f3d | 2016-05-13 12:20:36 +0000 | [diff] [blame] | 1665 | public void testRuleFromBzlFile() throws Exception { |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 1666 | scratch.file("test/rule.bzl", "def _impl(ctx): return", "foo = rule(implementation = _impl)"); |
| 1667 | scratch.file("test/ext.bzl", "load('//test:rule.bzl', 'foo')", "a = 1", "foo(name = 'x')"); |
| 1668 | scratch.file("test/BUILD", "load('//test:ext.bzl', 'a')"); |
Laurent Le Brun | 1d19f3d | 2016-05-13 12:20:36 +0000 | [diff] [blame] | 1669 | reporter.removeHandler(failFastHandler); |
| 1670 | getConfiguredTarget("//test:x"); |
| 1671 | assertContainsEvent("Cannot instantiate a rule when loading a .bzl file"); |
| 1672 | } |
| 1673 | |
| 1674 | @Test |
Francois-Rene Rideau | 1bae01f | 2016-01-30 04:25:58 +0000 | [diff] [blame] | 1675 | public void testImplicitOutputsFromGlob() throws Exception { |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 1676 | scratch.file( |
| 1677 | "test/glob.bzl", |
Francois-Rene Rideau | 1bae01f | 2016-01-30 04:25:58 +0000 | [diff] [blame] | 1678 | "def _impl(ctx):", |
| 1679 | " outs = ctx.outputs", |
| 1680 | " for i in ctx.attr.srcs:", |
| 1681 | " o = getattr(outs, 'foo_' + i.label.name)", |
dslomov | 90ccaa5 | 2017-06-30 12:21:28 +0200 | [diff] [blame] | 1682 | " ctx.actions.write(", |
Francois-Rene Rideau | 1bae01f | 2016-01-30 04:25:58 +0000 | [diff] [blame] | 1683 | " output = o,", |
| 1684 | " content = 'hoho')", |
| 1685 | "", |
Florian Weikert | 3ae5912 | 2016-06-06 09:11:43 +0000 | [diff] [blame] | 1686 | "def _foo(srcs):", |
Francois-Rene Rideau | 1bae01f | 2016-01-30 04:25:58 +0000 | [diff] [blame] | 1687 | " outs = {}", |
Florian Weikert | 3ae5912 | 2016-06-06 09:11:43 +0000 | [diff] [blame] | 1688 | " for i in srcs:", |
Francois-Rene Rideau | 1bae01f | 2016-01-30 04:25:58 +0000 | [diff] [blame] | 1689 | " outs['foo_' + i.name] = i.name + '.out'", |
| 1690 | " return outs", |
| 1691 | "", |
| 1692 | "glob_rule = rule(", |
| 1693 | " attrs = {", |
| 1694 | " 'srcs': attr.label_list(allow_files = True),", |
| 1695 | " },", |
| 1696 | " outputs = _foo,", |
| 1697 | " implementation = _impl,", |
| 1698 | ")"); |
| 1699 | scratch.file("test/a.bar", "a"); |
| 1700 | scratch.file("test/b.bar", "b"); |
Yun Peng | fc61005 | 2016-06-20 11:44:06 +0000 | [diff] [blame] | 1701 | scratch.file( |
| 1702 | "test/BUILD", |
Francois-Rene Rideau | 1bae01f | 2016-01-30 04:25:58 +0000 | [diff] [blame] | 1703 | "load('/test/glob', 'glob_rule')", |
| 1704 | "glob_rule(name = 'my_glob', srcs = glob(['*.bar']))"); |
| 1705 | ConfiguredTarget ct = getConfiguredTarget("//test:my_glob"); |
| 1706 | assertThat(ct).isNotNull(); |
| 1707 | assertThat(getGeneratingAction(getBinArtifact("a.bar.out", ct))).isNotNull(); |
| 1708 | assertThat(getGeneratingAction(getBinArtifact("b.bar.out", ct))).isNotNull(); |
| 1709 | } |
| 1710 | |
Vladimir Moskva | 658a8ea | 2016-09-02 15:39:17 +0000 | [diff] [blame] | 1711 | @Test |
| 1712 | public void testBuiltInFunctionAsRuleImplementation() throws Exception { |
| 1713 | // Using built-in functions as rule implementations shouldn't cause runtime errors |
| 1714 | scratch.file( |
| 1715 | "test/rule.bzl", |
| 1716 | "silly_rule = rule(", |
| 1717 | " implementation = int,", |
| 1718 | " attrs = {", |
| 1719 | " \"srcs\": attr.label_list(allow_files=True),", |
| 1720 | " }", |
| 1721 | ")" |
| 1722 | ); |
| 1723 | scratch.file( |
| 1724 | "test/BUILD", |
| 1725 | "load('/test/rule', 'silly_rule')", |
| 1726 | "silly_rule(name = 'silly')"); |
| 1727 | thrown.handleAssertionErrors(); // Compatibility with JUnit 4.11 |
| 1728 | thrown.expect(AssertionError.class); |
vladmos | 25ef6a8 | 2017-07-07 11:45:01 -0400 | [diff] [blame] | 1729 | thrown.expectMessage("<rule context for //test:silly> is not of type string or int or bool"); |
Vladimir Moskva | 658a8ea | 2016-09-02 15:39:17 +0000 | [diff] [blame] | 1730 | getConfiguredTarget("//test:silly"); |
| 1731 | } |
| 1732 | |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1733 | private void setupThrowFunction(BuiltinFunction func) throws Exception { |
| 1734 | throwFunction = func; |
| 1735 | throwFunction.configure( |
| 1736 | getClass().getDeclaredField("throwFunction").getAnnotation(SkylarkSignature.class)); |
| 1737 | update("throw", throwFunction); |
| 1738 | } |
| 1739 | |
| 1740 | private static class ThereIsNoMessageException extends EvalException { |
| 1741 | public ThereIsNoMessageException() { |
| 1742 | super(null, "This is not the message you are looking for."); // Unused dummy message |
| 1743 | } |
| 1744 | |
| 1745 | @Override |
| 1746 | public String getMessage() { |
| 1747 | return ""; |
| 1748 | } |
| 1749 | } |
| 1750 | } |