bazel skylark: remove cruft from tests
This change removes all the test "helpers" from the util/ subpackage
that had a RuleContext parameter. Now, the tests each explicitly
call setRuleContext, which is simpler and makes clear that this
is (and always was) a side effect.
Also:
- remove many other test helpers
- remove unsound casts
- move a test of nested set to a more appropriate place
- fold SkylarkFileHelperTest (two tests) into SRIFT.java
If you think this is tedious to review, I can assure you it was more tedious to write.
PiperOrigin-RevId: 274021984
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkDefinedAspectsTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkDefinedAspectsTest.java
index c5670d3..379fbce 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkDefinedAspectsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkDefinedAspectsTest.java
@@ -1945,7 +1945,7 @@
"r2(name = 'r2', dep = ':r1')");
AnalysisResult analysisResult = update("//test:r2");
ConfiguredTarget target = Iterables.getOnlyElement(analysisResult.getTargetsToBuild());
- SkylarkList result = (SkylarkList) target.get("result");
+ SkylarkList<?> result = (SkylarkList) target.get("result");
// "yes" means that aspect a2 sees a1's providers.
assertThat(result)
@@ -2009,7 +2009,7 @@
"rcollect(name = 'rcollect', deps = [':r1', ':r2'])");
AnalysisResult analysisResult = update("//test:rcollect");
ConfiguredTarget target = Iterables.getOnlyElement(analysisResult.getTargetsToBuild());
- SkylarkList result = (SkylarkList) target.get("result");
+ SkylarkList<?> result = (SkylarkList) target.get("result");
assertThat(result)
.containsExactly(
"//test:r0[\"//test:aspect.bzl%a1\", \"//test:aspect.bzl%a3\"]=a1p",
@@ -2059,7 +2059,7 @@
"r2(name = 'r2', dep = ':r1')");
AnalysisResult analysisResult = update("//test:r2");
ConfiguredTarget target = Iterables.getOnlyElement(analysisResult.getTargetsToBuild());
- SkylarkList result = (SkylarkList) target.get("result");
+ SkylarkList<?> result = (SkylarkList) target.get("result");
// "yes" means that aspect a2 sees a1's providers.
assertThat(result)
.containsExactly(
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkFileHelperTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkFileHelperTest.java
deleted file mode 100644
index 7f474f9..0000000
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkFileHelperTest.java
+++ /dev/null
@@ -1,57 +0,0 @@
-// Copyright 2014 The Bazel Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.google.devtools.build.lib.skylark;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import com.google.devtools.build.lib.analysis.skylark.SkylarkRuleContext;
-import com.google.devtools.build.lib.skylark.util.SkylarkTestCase;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
-
-/**
- * Tests for SkylarkFileset and SkylarkFileType.
- */
-@RunWith(JUnit4.class)
-public class SkylarkFileHelperTest extends SkylarkTestCase {
-
- @Before
- public final void createBuildFile() throws Exception {
- scratch.file(
- "foo/BUILD",
- "genrule(name = 'foo',",
- " cmd = 'dummy_cmd',",
- " srcs = ['a.txt', 'b.img'],",
- " tools = ['t.exe'],",
- " outs = ['c.txt'])");
- }
-
- @Test
- public void testArtifactPath() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- String result = (String) evalRuleContextCode(ruleContext, "ruleContext.files.tools[0].path");
- assertThat(result).isEqualTo("foo/t.exe");
- }
-
- @Test
- public void testArtifactShortPath() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- String result =
- (String) evalRuleContextCode(ruleContext, "ruleContext.files.tools[0].short_path");
- assertThat(result).isEqualTo("foo/t.exe");
- }
-}
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
index f490bf6..43bdbc7 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
@@ -57,7 +57,6 @@
import com.google.devtools.build.lib.syntax.SkylarkList.Tuple;
import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
import com.google.devtools.build.lib.syntax.StarlarkFile;
-import com.google.devtools.build.lib.syntax.StarlarkSemantics;
import com.google.devtools.build.lib.syntax.StarlarkThread;
import com.google.devtools.build.lib.syntax.SyntaxError;
import com.google.devtools.build.lib.testutil.MoreAsserts;
@@ -150,7 +149,7 @@
String[] strings = lines.clone();
strings[strings.length - 1] = String.format("%s = %s", name, strings[strings.length - 1]);
evalAndExport(strings);
- Descriptor lookup = (Descriptor) ev.lookup(name);
+ Descriptor lookup = (Descriptor) lookup(name);
return lookup != null ? lookup.build(name) : null;
}
@@ -192,9 +191,8 @@
@Test
public void testAttrAllowedFileTypesWrongType() throws Exception {
- checkErrorContains(
- "allow_files should be a boolean or a string list",
- "attr.label_list(allow_files = 18)");
+ checkEvalErrorContains(
+ "allow_files should be a boolean or a string list", "attr.label_list(allow_files = 18)");
}
@Test
@@ -213,13 +211,7 @@
@Test
public void testDisableDeprecatedParams() throws Exception {
- ev =
- createEvaluationTestCase(
- StarlarkSemantics.DEFAULT_SEMANTICS
- .toBuilder()
- .incompatibleDisableDeprecatedAttrParams(true)
- .build());
- ev.initialize();
+ setSkylarkSemanticsOptions("--incompatible_disable_deprecated_attr_params=true");
// Verify 'single_file' deprecation.
EvalException expected =
@@ -240,7 +232,7 @@
@Test
public void testAttrAllowedSingleFileTypesWrongType() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"allow_single_file should be a boolean or a string list",
"attr.label(allow_single_file = 18)");
}
@@ -345,8 +337,8 @@
" pass",
"my_aspect = aspect(implementation = _impl)",
"a = attr.label_list(aspects = [my_aspect])");
- SkylarkAttr.Descriptor attr = (SkylarkAttr.Descriptor) ev.lookup("a");
- SkylarkDefinedAspect aspect = (SkylarkDefinedAspect) ev.lookup("my_aspect");
+ SkylarkAttr.Descriptor attr = (SkylarkAttr.Descriptor) lookup("a");
+ SkylarkDefinedAspect aspect = (SkylarkDefinedAspect) lookup("my_aspect");
assertThat(aspect).isNotNull();
assertThat(attr.build("xxx").getAspectClasses()).containsExactly(aspect.getAspectClass());
}
@@ -358,15 +350,15 @@
" pass",
"my_aspect = aspect(implementation = _impl)",
"a = attr.label(aspects = [my_aspect])");
- SkylarkAttr.Descriptor attr = (SkylarkAttr.Descriptor) ev.lookup("a");
- SkylarkDefinedAspect aspect = (SkylarkDefinedAspect) ev.lookup("my_aspect");
+ SkylarkAttr.Descriptor attr = (SkylarkAttr.Descriptor) lookup("a");
+ SkylarkDefinedAspect aspect = (SkylarkDefinedAspect) lookup("my_aspect");
assertThat(aspect).isNotNull();
assertThat(attr.build("xxx").getAspectClasses()).containsExactly(aspect.getAspectClass());
}
@Test
public void testLabelListWithAspectsError() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"expected type 'Aspect' for 'aspects' element but got type 'int' instead",
"def _impl(target, ctx):",
" pass",
@@ -382,7 +374,7 @@
"my_aspect = aspect(_impl,",
" attrs = { '_extra_deps' : attr.label(default = Label('//foo/bar:baz')) }",
")");
- SkylarkDefinedAspect aspect = (SkylarkDefinedAspect) ev.lookup("my_aspect");
+ SkylarkDefinedAspect aspect = (SkylarkDefinedAspect) lookup("my_aspect");
Attribute attribute = Iterables.getOnlyElement(aspect.getAttributes());
assertThat(attribute.getName()).isEqualTo("$extra_deps");
assertThat(attribute.getDefaultValue(null))
@@ -401,16 +393,16 @@
"my_aspect = aspect(_impl,",
" attrs = { 'param' : attr.string(values=['a', 'b']) }",
")");
- SkylarkDefinedAspect aspect = (SkylarkDefinedAspect) ev.lookup("my_aspect");
+ SkylarkDefinedAspect aspect = (SkylarkDefinedAspect) lookup("my_aspect");
Attribute attribute = Iterables.getOnlyElement(aspect.getAttributes());
assertThat(attribute.getName()).isEqualTo("param");
}
@Test
public void testAspectParameterRequiresValues() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"Aspect parameter attribute 'param' must have type 'string' and use the 'values' "
- + "restriction.",
+ + "restriction.",
"def _impl(target, ctx):",
" pass",
"my_aspect = aspect(_impl,",
@@ -420,9 +412,9 @@
@Test
public void testAspectParameterBadType() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"Aspect parameter attribute 'param' must have type 'string' and use the 'values' "
- + "restriction.",
+ + "restriction.",
"def _impl(target, ctx):",
" pass",
"my_aspect = aspect(_impl,",
@@ -439,14 +431,14 @@
" attrs = { 'param' : attr.string(values=['a', 'b']),",
" '_extra' : attr.label(default = Label('//foo/bar:baz')) }",
")");
- SkylarkDefinedAspect aspect = (SkylarkDefinedAspect) ev.lookup("my_aspect");
+ SkylarkDefinedAspect aspect = (SkylarkDefinedAspect) lookup("my_aspect");
assertThat(aspect.getAttributes()).hasSize(2);
assertThat(aspect.getParamAttributes()).containsExactly("param");
}
@Test
public void testAspectNoDefaultValueAttribute() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"Aspect attribute '_extra_deps' has no default value",
"def _impl(target, ctx):",
" pass",
@@ -466,7 +458,7 @@
@Test
public void testNonLabelAttrWithProviders() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"unexpected keyword 'providers', for call to method "
+ "string(default = '', doc = '', mandatory = False, values = []) "
+ "of 'attr (a language module)'",
@@ -538,24 +530,24 @@
@Test
public void testLabelAttrDefaultValueAsStringBadValue() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"invalid label '/foo:bar' in parameter 'default' of attribute 'label': "
+ "invalid target name '/foo:bar'",
"attr.label(default = '/foo:bar')");
- checkErrorContains(
+ checkEvalErrorContains(
"invalid label '/bar:foo' in element 1 of parameter 'default' of attribute "
+ "'label_list': invalid target name '/bar:foo'",
"attr.label_list(default = ['//foo:bar', '/bar:foo'])");
- checkErrorContains(
+ checkEvalErrorContains(
"invalid label '/bar:foo' in dict key element: invalid target name '/bar:foo'",
"attr.label_keyed_string_dict(default = {'//foo:bar': 'a', '/bar:foo': 'b'})");
}
@Test
public void testAttrDefaultValueBadType() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"expected value of type 'string' for parameter 'default', for call to method "
+ "string(default = '', doc = '', mandatory = False, values = []) "
+ "of 'attr (a language module)'",
@@ -571,12 +563,8 @@
@Test
public void testAttrNonEmpty() throws Exception {
- ev =
- createEvaluationTestCase(
- StarlarkSemantics.DEFAULT_SEMANTICS.toBuilder()
- .incompatibleDisableDeprecatedAttrParams(false)
- .build());
- ev.initialize();
+ setSkylarkSemanticsOptions("--incompatible_disable_deprecated_attr_params=false");
+ reset();
Attribute attr = buildAttribute("a1", "attr.string_list(non_empty=True)");
assertThat(attr.isNonEmpty()).isTrue();
@@ -592,7 +580,7 @@
@Test
public void testAttrBadKeywordArguments() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"unexpected keyword 'bad_keyword', for call to method "
+ "string(default = '', doc = '', mandatory = False, values = []) of "
+ "'attr (a language module)'",
@@ -663,7 +651,7 @@
@Test
public void testAttrDocValueBadType() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"expected value of type 'string' for parameter 'doc', for call to method "
+ "string(default = '', doc = '', mandatory = False, values = []) "
+ "of 'attr (a language module)'",
@@ -684,7 +672,7 @@
@Test
public void testLateBoundAttrWorksWithOnlyLabel() throws Exception {
- checkEvalError(
+ checkEvalErrorContains(
"expected value of type 'string' for parameter 'default', for call to method "
+ "string(default = '', doc = '', mandatory = False, values = []) "
+ "of 'attr (a language module)'",
@@ -780,14 +768,14 @@
@Test
public void testRuleUnknownKeyword() throws Exception {
registerDummyStarlarkFunction();
- checkErrorContains(
+ checkEvalErrorContains(
"unexpected keyword 'bad_keyword', for call to function rule(",
"rule(impl, bad_keyword = 'some text')");
}
@Test
public void testRuleImplementationMissing() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"parameter 'implementation' has no default value, for call to function rule(",
"rule(attrs = {})");
}
@@ -795,7 +783,7 @@
@Test
public void testRuleBadTypeForAdd() throws Exception {
registerDummyStarlarkFunction();
- checkErrorContains(
+ checkEvalErrorContains(
"expected value of type 'dict or NoneType' for parameter 'attrs', "
+ "for call to function rule(",
"rule(impl, attrs = 'some text')");
@@ -804,7 +792,7 @@
@Test
public void testRuleBadTypeInAdd() throws Exception {
registerDummyStarlarkFunction();
- checkErrorContains(
+ checkEvalErrorContains(
"expected <String, Descriptor> type for 'attrs' but got <string, string> instead",
"rule(impl, attrs = {'a1': 'some text'})");
}
@@ -812,32 +800,32 @@
@Test
public void testRuleBadTypeForDoc() throws Exception {
registerDummyStarlarkFunction();
- checkErrorContains(
+ checkEvalErrorContains(
"expected value of type 'string' for parameter 'doc', for call to function rule(",
"rule(impl, doc = 1)");
}
@Test
public void testLabel() throws Exception {
- Object result = evalRuleClassCode("Label('//foo/foo:foo')");
+ Object result = eval("Label('//foo/foo:foo')");
assertThat(result).isInstanceOf(Label.class);
assertThat(result.toString()).isEqualTo("//foo/foo:foo");
}
@Test
public void testLabelSameInstance() throws Exception {
- Object l1 = evalRuleClassCode("Label('//foo/foo:foo')");
+ Object l1 = eval("Label('//foo/foo:foo')");
// Implicitly creates a new pkgContext and environment, yet labels should be the same.
- Object l2 = evalRuleClassCode("Label('//foo/foo:foo')");
+ Object l2 = eval("Label('//foo/foo:foo')");
assertThat(l1).isSameInstanceAs(l2);
}
@Test
public void testLabelNameAndPackage() throws Exception {
- Object result = evalRuleClassCode("Label('//foo/bar:baz').name");
+ Object result = eval("Label('//foo/bar:baz').name");
assertThat(result).isEqualTo("baz");
// NB: implicitly creates a new pkgContext and environments, yet labels should be the same.
- result = evalRuleClassCode("Label('//foo/bar:baz').package");
+ result = eval("Label('//foo/bar:baz').package");
assertThat(result).isEqualTo("foo/bar");
}
@@ -876,7 +864,7 @@
private void checkTextMessage(String from, String... lines) throws Exception {
String[] strings = lines.clone();
- Object result = evalRuleClassCode(from);
+ Object result = eval(from);
String expect = "";
if (strings.length > 0) {
expect = Joiner.on("\n").join(lines) + "\n";
@@ -892,13 +880,11 @@
@Test
public void testStructRestrictedOverrides() throws Exception {
- checkErrorContains(
- "cannot override built-in struct function 'to_json'",
- "struct(to_json='foo')");
+ checkEvalErrorContains(
+ "cannot override built-in struct function 'to_json'", "struct(to_json='foo')");
- checkErrorContains(
- "cannot override built-in struct function 'to_proto'",
- "struct(to_proto='foo')");
+ checkEvalErrorContains(
+ "cannot override built-in struct function 'to_proto'", "struct(to_proto='foo')");
}
@Test
@@ -985,7 +971,7 @@
@Test
public void testTextMessageInvalidElementInListStructure() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"Invalid text format, expected a struct, a dict, a string, a bool, or "
+ "an int but got a list for list element in struct field 'a'",
"struct(a=[['b']]).to_proto()");
@@ -993,14 +979,14 @@
@Test
public void testTextMessageInvalidStructure() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"Invalid text format, expected a struct, a dict, a string, a bool, or an int "
+ "but got a function for struct field 'a'",
"struct(a=rule).to_proto()");
}
private void checkJson(String from, String expected) throws Exception {
- Object result = evalRuleClassCode(from);
+ Object result = eval(from);
assertThat(result).isEqualTo(expected);
}
@@ -1014,13 +1000,13 @@
public void testJsonDictFields() throws Exception {
checkJson("struct(config={}).to_json()", "{\"config\":{}}");
checkJson("struct(config={'key': 'value'}).to_json()", "{\"config\":{\"key\":\"value\"}}");
- checkErrorContains(
+ checkEvalErrorContains(
"Keys must be a string but got a int for struct field 'config'",
"struct(config={1:2}).to_json()");
- checkErrorContains(
+ checkEvalErrorContains(
"Keys must be a string but got a int for dict value 'foo'",
"struct(config={'foo':{1:2}}).to_json()");
- checkErrorContains(
+ checkEvalErrorContains(
"Keys must be a string but got a bool for struct field 'config'",
"struct(config={True: False}).to_json()");
}
@@ -1055,7 +1041,7 @@
@Test
public void testJsonInvalidStructure() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"Invalid text format, expected a struct, a string, a bool, or an int but got a "
+ "function for struct field 'a'",
"struct(a=rule).to_json()");
@@ -1063,7 +1049,7 @@
@Test
public void testLabelAttrWrongDefault() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"expected value of type 'Label or string or LateBoundDefault or function or NoneType' "
+ "for parameter 'default', for call to method label(",
"attr.label(default = 123)");
@@ -1077,7 +1063,7 @@
@Test
public void testLabelGetRelativeSyntaxError() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"invalid target name 'bad//syntax': target names may not contain '//' path separators",
"Label('//foo:bar').relative('bad//syntax')");
}
@@ -1120,10 +1106,10 @@
@Test
public void testStructIncomparability() throws Exception {
- checkErrorContains("Cannot compare structs", "struct(a = 1) < struct(a = 2)");
- checkErrorContains("Cannot compare structs", "struct(a = 1) > struct(a = 2)");
- checkErrorContains("Cannot compare structs", "struct(a = 1) <= struct(a = 2)");
- checkErrorContains("Cannot compare structs", "struct(a = 1) >= struct(a = 2)");
+ checkEvalErrorContains("Cannot compare structs", "struct(a = 1) < struct(a = 2)");
+ checkEvalErrorContains("Cannot compare structs", "struct(a = 1) > struct(a = 2)");
+ checkEvalErrorContains("Cannot compare structs", "struct(a = 1) <= struct(a = 2)");
+ checkEvalErrorContains("Cannot compare structs", "struct(a = 1) >= struct(a = 2)");
}
@Test
@@ -1135,21 +1121,22 @@
@Test
public void testStructAccessingUnknownField() throws Exception {
- checkErrorContains(
- "'struct' object has no attribute 'c'\n" + "Available attributes: a, b",
- "x = struct(a = 1, b = 2)",
- "y = x.c");
+ checkEvalErrorContains(
+ "'struct' object has no attribute 'c'\n" + "Available attributes: a, b",
+ "x = struct(a = 1, b = 2)",
+ "y = x.c");
}
@Test
public void testStructAccessingUnknownFieldWithArgs() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"type 'struct' has no method c()", "x = struct(a = 1, b = 2)", "y = x.c()");
}
@Test
public void testStructAccessingNonFunctionFieldWithArgs() throws Exception {
- checkErrorContains("'int' object is not callable", "x = struct(a = 1, b = 2)", "x1 = x.a(1)");
+ checkEvalErrorContains(
+ "'int' object is not callable", "x = struct(a = 1, b = 2)", "x1 = x.a(1)");
}
@Test
@@ -1160,7 +1147,7 @@
@Test
public void testStructPosArgs() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"expected no more than 0 positional arguments, but got 1", "x = struct(1, b = 2)");
}
@@ -1189,8 +1176,11 @@
@Test
public void testStructConcatenationCommonFields() throws Exception {
- checkErrorContains("Cannot use '+' operator on provider instances with overlapping field(s): a",
- "x = struct(a = 1, b = 2)", "y = struct(c = 1, a = 2)", "z = x + y\n");
+ checkEvalErrorContains(
+ "Cannot use '+' operator on provider instances with overlapping field(s): a",
+ "x = struct(a = 1, b = 2)",
+ "y = struct(c = 1, a = 2)",
+ "z = x + y\n");
}
@Test
@@ -1211,7 +1201,7 @@
@Test
public void testGetattrNoAttr() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"'struct' object has no attribute 'b'\nAvailable attributes: a",
"s = struct(a='val')",
"getattr(s, 'b')");
@@ -1251,9 +1241,7 @@
assertThat(eval("d[struct(b = 2)]")).isEqualTo("bb");
assertThat(eval("str([d[k] for k in d])")).isEqualTo("[\"aa\", \"bb\"]");
- checkErrorContains(
- "unhashable type: 'struct'",
- "{struct(a = []): 'foo'}");
+ checkEvalErrorContains("unhashable type: 'struct'", "{struct(a = []): 'foo'}");
}
@Test
@@ -1274,8 +1262,8 @@
@Test
public void testNsetBadMutableItem() throws Exception {
- checkEvalError("depsets cannot contain mutable items", "depset([([],)])");
- checkEvalError("depsets cannot contain mutable items", "depset([struct(a=[])])");
+ checkEvalErrorContains("depsets cannot contain mutable items", "depset([([],)])");
+ checkEvalErrorContains("depsets cannot contain mutable items", "depset([struct(a=[])])");
}
private static StructImpl makeStruct(String field, Object value) {
@@ -1364,11 +1352,11 @@
"data2 = provider()"
);
- checkEvalError("Cannot use '+' operator on instances of different providers (data1 and data2)",
+ checkEvalErrorContains(
+ "Cannot use '+' operator on instances of different providers (data1 and data2)",
"d1 = data1(x = 1)",
"d2 = data2(y = 2)",
- "d = d1 + d2"
- );
+ "d = d1 + d2");
}
@Test
@@ -1387,7 +1375,7 @@
@Test
public void declaredProvidersWithFieldsConcatError() throws Exception {
evalAndExport("data1 = provider(fields=['f1', 'f2'])", "data2 = provider(fields=['f3'])");
- checkEvalError(
+ checkEvalErrorContains(
"Cannot use '+' operator on instances of different providers (data1 and data2)",
"d1 = data1(f1=1, f2=2)",
"d2 = data2(f3=3)",
@@ -1397,7 +1385,7 @@
@Test
public void declaredProvidersWithOverlappingFieldsConcatError() throws Exception {
evalAndExport("data = provider(fields=['f1', 'f2'])");
- checkEvalError(
+ checkEvalErrorContains(
"Cannot use '+' operator on provider instances with overlapping field(s): f1",
"d1 = data(f1 = 4)",
"d2 = data(f1 = 5)",
@@ -1422,7 +1410,7 @@
@Test
public void declaredProvidersBadTypeForDoc() throws Exception {
- checkErrorContains(
+ checkEvalErrorContains(
"expected value of type 'string' for parameter 'doc', for call to function "
+ "provider(doc = '', fields = None)",
"provider(doc = 1)");
@@ -1564,7 +1552,7 @@
@Test
public void aspectBadTypeForDoc() throws Exception {
registerDummyStarlarkFunction();
- checkErrorContains(
+ checkEvalErrorContains(
"expected value of type 'string' for parameter 'doc', for call to function aspect(",
"aspect(impl, doc = 1)");
}
@@ -1686,7 +1674,8 @@
@Test
public void starTheOnlyAspectArg() throws Exception {
- checkEvalError("'*' must be the only string in 'attr_aspects' list",
+ checkEvalErrorContains(
+ "'*' must be the only string in 'attr_aspects' list",
"def _impl(target, ctx):",
" pass",
"aspect(_impl, attr_aspects=['*', 'foo'])");
@@ -1740,12 +1729,9 @@
@Test
public void testTargetsCanAddExecutionPlatformConstraints_enabled() throws Exception {
- StarlarkSemantics semantics =
- StarlarkSemantics.DEFAULT_SEMANTICS.toBuilder()
- .incompatibleDisallowRuleExecutionPlatformConstraintsAllowed(false)
- .build();
- ev = createEvaluationTestCase(semantics);
- ev.initialize();
+ setSkylarkSemanticsOptions(
+ "--incompatible_disallow_rule_execution_platform_constraints_allowed=false");
+ reset();
registerDummyStarlarkFunction();
scratch.file("test/BUILD", "toolchain_type(name = 'my_toolchain_type')");
@@ -1761,12 +1747,9 @@
@Test
public void testTargetsCanAddExecutionPlatformConstraints_notEnabled() throws Exception {
- StarlarkSemantics semantics =
- StarlarkSemantics.DEFAULT_SEMANTICS.toBuilder()
- .incompatibleDisallowRuleExecutionPlatformConstraintsAllowed(false)
- .build();
- ev = createEvaluationTestCase(semantics);
- ev.initialize();
+ setSkylarkSemanticsOptions(
+ "--incompatible_disallow_rule_execution_platform_constraints_allowed=false");
+ reset();
registerDummyStarlarkFunction();
scratch.file("test/BUILD", "toolchain_type(name = 'my_toolchain_type')");
@@ -1782,14 +1765,11 @@
@Test
public void testTargetsCanAddExecutionPlatformConstraints_disallowed() throws Exception {
- StarlarkSemantics semantics =
- StarlarkSemantics.DEFAULT_SEMANTICS.toBuilder()
- .incompatibleDisallowRuleExecutionPlatformConstraintsAllowed(true)
- .build();
- ev = createEvaluationTestCase(semantics);
- ev.setFailFast(false);
- ev.initialize();
+ setSkylarkSemanticsOptions(
+ "--incompatible_disallow_rule_execution_platform_constraints_allowed=true");
+ reset();
+ ev.setFailFast(false);
registerDummyStarlarkFunction();
scratch.file("test/BUILD", "toolchain_type(name = 'my_toolchain_type')");
evalAndExport(
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java
index ff632c9..f336712 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java
@@ -136,6 +136,10 @@
);
}
+ private void setRuleContext(SkylarkRuleContext ctx) throws Exception {
+ update("ruleContext", ctx);
+ }
+
private void setUpAttributeErrorTest() throws Exception {
scratch.file(
"test/BUILD",
@@ -446,15 +450,15 @@
@Test
public void shouldGetPrerequisiteArtifacts() throws Exception {
-
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.files.srcs");
+ setRuleContext(ruleContext);
+ Object result = eval("ruleContext.files.srcs");
assertArtifactList(result, ImmutableList.of("a.txt", "b.img"));
}
private void assertArtifactList(Object result, List<String> artifacts) {
assertThat(result).isInstanceOf(SkylarkList.class);
- SkylarkList resultList = (SkylarkList) result;
+ SkylarkList<?> resultList = (SkylarkList) result;
assertThat(resultList).hasSize(artifacts.size());
int i = 0;
for (String artifact : artifacts) {
@@ -465,7 +469,8 @@
@Test
public void shouldGetPrerequisites() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:bar");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.attr.srcs");
+ setRuleContext(ruleContext);
+ Object result = eval("ruleContext.attr.srcs");
// Check for a known provider
TransitiveInfoCollection tic1 = (TransitiveInfoCollection) ((SkylarkList) result).get(0);
assertThat(JavaInfo.getProvider(JavaSourceJarsProvider.class, tic1)).isNotNull();
@@ -476,7 +481,8 @@
@Test
public void shouldGetPrerequisite() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:asr");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.attr.srcjar");
+ setRuleContext(ruleContext);
+ Object result = eval("ruleContext.attr.srcjar");
TransitiveInfoCollection tic = (TransitiveInfoCollection) result;
assertThat(tic).isInstanceOf(FileConfiguredTarget.class);
assertThat(tic.getLabel().getName()).isEqualTo("asr-src.jar");
@@ -485,7 +491,8 @@
@Test
public void testGetRuleAttributeListType() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.attr.outs");
+ setRuleContext(ruleContext);
+ Object result = eval("ruleContext.attr.outs");
assertThat(result).isInstanceOf(SkylarkList.class);
}
@@ -576,19 +583,20 @@
"consume_rule(name = 'c_str', s = [cdict['kind'], cdict['name'], cdict['x']])");
SkylarkRuleContext allContext = createRuleContext("//test/getrule:all_str");
- List<String> result = (List<String>) evalRuleContextCode(allContext, "ruleContext.attr.s");
+ setRuleContext(allContext);
+ List<?> result = (List) eval("ruleContext.attr.s");
assertThat(result).containsExactly("genrule", "a", "nop_rule", "c");
- result = (List<String>) evalRuleContextCode(
- createRuleContext("//test/getrule:a_str"), "ruleContext.attr.s");
+ setRuleContext(createRuleContext("//test/getrule:a_str"));
+ result = (List) eval("ruleContext.attr.s");
assertThat(result).containsExactly("genrule", "a", ":a.txt", "//test:bla");
- result = (List<String>) evalRuleContextCode(
- createRuleContext("//test/getrule:c_str"), "ruleContext.attr.s");
+ setRuleContext(createRuleContext("//test/getrule:c_str"));
+ result = (List) eval("ruleContext.attr.s");
assertThat(result).containsExactly("nop_rule", "c", ":a");
- result = (List<String>) evalRuleContextCode(
- createRuleContext("//test/getrule:genrule_attr"), "ruleContext.attr.s");
+ setRuleContext(createRuleContext("//test/getrule:genrule_attr"));
+ result = (List) eval("ruleContext.attr.s");
assertThat(result)
.containsAtLeast(
"name",
@@ -619,69 +627,72 @@
@Test
public void testGetRuleAttributeListValue() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.attr.outs");
+ setRuleContext(ruleContext);
+ Object result = eval("ruleContext.attr.outs");
assertThat(((SkylarkList) result)).hasSize(1);
}
@Test
public void testGetRuleAttributeListValueNoGet() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.attr.outs");
+ setRuleContext(ruleContext);
+ Object result = eval("ruleContext.attr.outs");
assertThat(((SkylarkList) result)).hasSize(1);
}
@Test
public void testGetRuleAttributeStringTypeValue() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.attr.cmd");
+ setRuleContext(ruleContext);
+ Object result = eval("ruleContext.attr.cmd");
assertThat((String) result).isEqualTo("dummy_cmd");
}
@Test
public void testGetRuleAttributeStringTypeValueNoGet() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.attr.cmd");
+ setRuleContext(ruleContext);
+ Object result = eval("ruleContext.attr.cmd");
assertThat((String) result).isEqualTo("dummy_cmd");
}
@Test
public void testGetRuleAttributeBadAttributeName() throws Exception {
- checkErrorContains(
- createRuleContext("//foo:foo"), "No attribute 'bad'", "ruleContext.attr.bad");
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains("No attribute 'bad'", "ruleContext.attr.bad");
}
@Test
public void testGetLabel() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.label");
+ setRuleContext(createRuleContext("//foo:foo"));
+ Object result = eval("ruleContext.label");
assertThat(((Label) result).toString()).isEqualTo("//foo:foo");
}
@Test
public void testRuleError() throws Exception {
- checkErrorContains(createRuleContext("//foo:foo"), "message", "fail('message')");
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains("message", "fail('message')");
}
@Test
public void testAttributeError() throws Exception {
- checkErrorContains(
- createRuleContext("//foo:foo"),
- "attribute srcs: message",
- "fail(attr='srcs', msg='message')");
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains("attribute srcs: message", "fail(attr='srcs', msg='message')");
}
@Test
public void testGetExecutablePrerequisite() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:androidlib");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.executable._idlclass");
+ setRuleContext(createRuleContext("//foo:androidlib"));
+ Object result = eval("ruleContext.executable._idlclass");
assertThat(((Artifact) result).getFilename()).matches("^IdlClass(\\.exe){0,1}$");
}
@Test
public void testCreateSpawnActionArgumentsWithExecutableFilesToRunProvider() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:androidlib");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"ruleContext.actions.run(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
@@ -697,8 +708,8 @@
@Test
public void testCreateStarlarkActionArgumentsWithUnusedInputsList() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"ruleContext.actions.run(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
@@ -717,8 +728,8 @@
@Test
public void testCreateStarlarkActionArgumentsWithoutUnusedInputsList() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"ruleContext.actions.run(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
@@ -734,48 +745,43 @@
@Test
public void testOutputs() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:bar");
- Iterable<?> result = (Iterable<?>) evalRuleContextCode(ruleContext, "ruleContext.outputs.outs");
+ setRuleContext(createRuleContext("//foo:bar"));
+ Iterable<?> result = (Iterable) eval("ruleContext.outputs.outs");
assertThat(((Artifact) Iterables.getOnlyElement(result)).getFilename()).isEqualTo("d.txt");
}
@Test
public void testSkylarkRuleContextGetDefaultShellEnv() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.configuration.default_shell_env");
+ setRuleContext(createRuleContext("//foo:foo"));
+ Object result = eval("ruleContext.configuration.default_shell_env");
assertThat(result).isInstanceOf(SkylarkDict.class);
}
@Test
public void testCheckPlaceholders() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result =
- evalRuleContextCode(ruleContext, "ruleContext.check_placeholders('%{name}', ['name'])");
+ setRuleContext(createRuleContext("//foo:foo"));
+ Object result = eval("ruleContext.check_placeholders('%{name}', ['name'])");
assertThat(result).isEqualTo(true);
}
@Test
public void testCheckPlaceholdersBadPlaceholder() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result =
- evalRuleContextCode(ruleContext, "ruleContext.check_placeholders('%{name}', ['abc'])");
+ setRuleContext(createRuleContext("//foo:foo"));
+ Object result = eval("ruleContext.check_placeholders('%{name}', ['abc'])");
assertThat(result).isEqualTo(false);
}
@Test
public void testExpandMakeVariables() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result =
- evalRuleContextCode(
- ruleContext, "ruleContext.expand_make_variables('cmd', '$(ABC)', {'ABC': 'DEF'})");
+ setRuleContext(createRuleContext("//foo:foo"));
+ Object result = eval("ruleContext.expand_make_variables('cmd', '$(ABC)', {'ABC': 'DEF'})");
assertThat(result).isEqualTo("DEF");
}
@Test
public void testExpandMakeVariablesShell() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result =
- evalRuleContextCode(ruleContext, "ruleContext.expand_make_variables('cmd', '$$ABC', {})");
+ setRuleContext(createRuleContext("//foo:foo"));
+ Object result = eval("ruleContext.expand_make_variables('cmd', '$$ABC', {})");
assertThat(result).isEqualTo("$ABC");
}
@@ -809,57 +815,54 @@
@Test
public void testExpandMakeVariables_cc() throws Exception {
setUpMakeVarToolchain();
- SkylarkRuleContext ruleContext = createRuleContext("//vars:vars");
- String result =
- (String)
- evalRuleContextCode(
- ruleContext, "ruleContext.expand_make_variables('cmd', '$(CC)', {})");
+ setRuleContext(createRuleContext("//vars:vars"));
+ String result = (String) eval("ruleContext.expand_make_variables('cmd', '$(CC)', {})");
assertThat(result).isNotEmpty();
}
@Test
public void testExpandMakeVariables_toolchain() throws Exception {
setUpMakeVarToolchain();
- SkylarkRuleContext ruleContext = createRuleContext("//vars:vars");
- Object result =
- evalRuleContextCode(
- ruleContext, "ruleContext.expand_make_variables('cmd', '$(MAKE_VAR_VALUE)', {})");
+ setRuleContext(createRuleContext("//vars:vars"));
+ Object result = eval("ruleContext.expand_make_variables('cmd', '$(MAKE_VAR_VALUE)', {})");
assertThat(result).isEqualTo("foo");
}
@Test
public void testVar_toolchain() throws Exception {
setUpMakeVarToolchain();
- SkylarkRuleContext ruleContext = createRuleContext("//vars:vars");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.var['MAKE_VAR_VALUE']");
+ setRuleContext(createRuleContext("//vars:vars"));
+ Object result = eval("ruleContext.var['MAKE_VAR_VALUE']");
assertThat(result).isEqualTo("foo");
}
@Test
public void testConfiguration() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.configuration");
+ setRuleContext(ruleContext);
+ Object result = eval("ruleContext.configuration");
assertThat(ruleContext.getRuleContext().getConfiguration()).isSameInstanceAs(result);
}
@Test
public void testFeatures() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:cc_with_features");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.features");
- assertThat((SkylarkList<?>) result).containsExactly("cc_include_scanning", "f1", "f2");
+ setRuleContext(createRuleContext("//foo:cc_with_features"));
+ Object result = eval("ruleContext.features");
+ assertThat((SkylarkList) result).containsExactly("cc_include_scanning", "f1", "f2");
}
@Test
public void testDisabledFeatures() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:cc_with_features");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.disabled_features");
- assertThat((SkylarkList<?>) result).containsExactly("f3");
+ setRuleContext(createRuleContext("//foo:cc_with_features"));
+ Object result = eval("ruleContext.disabled_features");
+ assertThat((SkylarkList) result).containsExactly("f3");
}
@Test
public void testHostConfiguration() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.host_configuration");
+ setRuleContext(ruleContext);
+ Object result = eval("ruleContext.host_configuration");
assertThat(ruleContext.getRuleContext().getHostConfiguration()).isSameInstanceAs(result);
}
@@ -867,36 +870,32 @@
public void testWorkspaceName() throws Exception {
assertThat(ruleClassProvider.getRunfilesPrefix()).isNotNull();
assertThat(ruleClassProvider.getRunfilesPrefix()).isNotEmpty();
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.workspace_name");
+ setRuleContext(createRuleContext("//foo:foo"));
+ Object result = eval("ruleContext.workspace_name");
assertThat(ruleClassProvider.getRunfilesPrefix()).isEqualTo(result);
}
@Test
public void testDeriveArtifactLegacy() throws Exception {
setSkylarkSemanticsOptions("--incompatible_new_actions_api=false");
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result =
- evalRuleContextCode(
- ruleContext,
- "ruleContext.new_file(ruleContext.genfiles_dir," + " 'a/b.txt')");
+ setRuleContext(createRuleContext("//foo:foo"));
+ Object result = eval("ruleContext.new_file(ruleContext.genfiles_dir," + " 'a/b.txt')");
PathFragment fragment = ((Artifact) result).getRootRelativePath();
assertThat(fragment.getPathString()).isEqualTo("foo/a/b.txt");
}
@Test
public void testDeriveArtifact() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.actions.declare_file('a/b.txt')");
+ setRuleContext(createRuleContext("//foo:foo"));
+ Object result = eval("ruleContext.actions.declare_file('a/b.txt')");
PathFragment fragment = ((Artifact) result).getRootRelativePath();
assertThat(fragment.getPathString()).isEqualTo("foo/a/b.txt");
}
@Test
public void testDeriveTreeArtifact() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result =
- evalRuleContextCode(ruleContext, "ruleContext.actions.declare_directory('a/b')");
+ setRuleContext(createRuleContext("//foo:foo"));
+ Object result = eval("ruleContext.actions.declare_directory('a/b')");
Artifact artifact = (Artifact) result;
PathFragment fragment = artifact.getRootRelativePath();
assertThat(fragment.getPathString()).isEqualTo("foo/a/b");
@@ -905,11 +904,8 @@
@Test
public void testDeriveTreeArtifactType() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result =
- evalRuleContextCode(ruleContext,
- "b = ruleContext.actions.declare_directory('a/b')\n"
- + "type(b)");
+ setRuleContext(createRuleContext("//foo:foo"));
+ Object result = eval("b = ruleContext.actions.declare_directory('a/b')\n" + "type(b)");
assertThat(result).isInstanceOf(String.class);
assertThat(result).isEqualTo("File");
}
@@ -917,10 +913,9 @@
@Test
public void testDeriveTreeArtifactNextToSibling() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
+ setRuleContext(createRuleContext("//foo:foo"));
Object result =
- evalRuleContextCode(
- ruleContext,
+ eval(
"b = ruleContext.actions.declare_directory('a/b')\n"
+ "ruleContext.actions.declare_directory('c', sibling=b)");
Artifact artifact = (Artifact) result;
@@ -932,12 +927,10 @@
@Test
public void testParamFileLegacy() throws Exception {
setSkylarkSemanticsOptions("--incompatible_new_actions_api=false");
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
+ setRuleContext(createRuleContext("//foo:foo"));
Object result =
- evalRuleContextCode(
- ruleContext,
- "ruleContext.new_file(ruleContext.bin_dir,"
- + "ruleContext.files.tools[0], '.params')");
+ eval(
+ "ruleContext.new_file(ruleContext.bin_dir," + "ruleContext.files.tools[0], '.params')");
PathFragment fragment = ((Artifact) result).getRootRelativePath();
assertThat(fragment.getPathString()).isEqualTo("foo/t.exe.params");
}
@@ -945,10 +938,9 @@
@Test
public void testParamFileSuffixLegacy() throws Exception {
setSkylarkSemanticsOptions("--incompatible_new_actions_api=false");
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
+ setRuleContext(createRuleContext("//foo:foo"));
Object result =
- evalRuleContextCode(
- ruleContext,
+ eval(
"ruleContext.new_file(ruleContext.files.tools[0], "
+ "ruleContext.files.tools[0].basename + '.params')");
PathFragment fragment = ((Artifact) result).getRootRelativePath();
@@ -957,10 +949,9 @@
@Test
public void testParamFileSuffix() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
+ setRuleContext(createRuleContext("//foo:foo"));
Object result =
- evalRuleContextCode(
- ruleContext,
+ eval(
"ruleContext.actions.declare_file(ruleContext.files.tools[0].basename + '.params', "
+ "sibling = ruleContext.files.tools[0])");
PathFragment fragment = ((Artifact) result).getRootRelativePath();
@@ -989,12 +980,10 @@
" label_dict={':dep': 'value'})");
invalidatePackages();
- SkylarkRuleContext context = createRuleContext("//:r");
- Label keyLabel =
- (Label) evalRuleContextCode(context, "ruleContext.attr.label_dict.keys()[0].label");
+ setRuleContext(createRuleContext("//:r"));
+ Label keyLabel = (Label) eval("ruleContext.attr.label_dict.keys()[0].label");
assertThat(keyLabel).isEqualTo(Label.parseAbsolute("//:dep", ImmutableMap.of()));
- String valueString =
- (String) evalRuleContextCode(context, "ruleContext.attr.label_dict.values()[0]");
+ String valueString = (String) eval("ruleContext.attr.label_dict.values()[0]");
assertThat(valueString).isEqualTo("value");
}
@@ -1020,12 +1009,10 @@
" label_dict={':alias': 'value'})");
invalidatePackages();
- SkylarkRuleContext context = createRuleContext("//:r");
- Label keyLabel =
- (Label) evalRuleContextCode(context, "ruleContext.attr.label_dict.keys()[0].label");
+ setRuleContext(createRuleContext("//:r"));
+ Label keyLabel = (Label) eval("ruleContext.attr.label_dict.keys()[0].label");
assertThat(keyLabel).isEqualTo(Label.parseAbsolute("//:dep", ImmutableMap.of()));
- String valueString =
- (String) evalRuleContextCode(context, "ruleContext.attr.label_dict.values()[0]");
+ String valueString = (String) eval("ruleContext.attr.label_dict.values()[0]");
assertThat(valueString).isEqualTo("value");
}
@@ -1049,12 +1036,10 @@
"my_rule(name='r')");
invalidatePackages();
- SkylarkRuleContext context = createRuleContext("//:r");
- Label keyLabel =
- (Label) evalRuleContextCode(context, "ruleContext.attr.label_dict.keys()[0].label");
+ setRuleContext(createRuleContext("//:r"));
+ Label keyLabel = (Label) eval("ruleContext.attr.label_dict.keys()[0].label");
assertThat(keyLabel).isEqualTo(Label.parseAbsolute("//:default", ImmutableMap.of()));
- String valueString =
- (String) evalRuleContextCode(context, "ruleContext.attr.label_dict.values()[0]");
+ String valueString = (String) eval("ruleContext.attr.label_dict.values()[0]");
assertThat(valueString).isEqualTo("defs");
}
@@ -1381,18 +1366,14 @@
"BUILD", "filegroup(name='dep')", "load('//:my_rule.bzl', 'my_rule')", "my_rule(name='r')");
invalidatePackages();
- SkylarkRuleContext context = createRuleContext("//:r");
- Label explicitDepLabel =
- (Label) evalRuleContextCode(context, "ruleContext.attr.explicit_dep.label");
+ setRuleContext(createRuleContext("//:r"));
+ Label explicitDepLabel = (Label) eval("ruleContext.attr.explicit_dep.label");
assertThat(explicitDepLabel).isEqualTo(Label.parseAbsolute("//:dep", ImmutableMap.of()));
- Label implicitDepLabel =
- (Label) evalRuleContextCode(context, "ruleContext.attr._implicit_dep.label");
+ Label implicitDepLabel = (Label) eval("ruleContext.attr._implicit_dep.label");
assertThat(implicitDepLabel).isEqualTo(Label.parseAbsolute("//:dep", ImmutableMap.of()));
- Label explicitDepListLabel =
- (Label) evalRuleContextCode(context, "ruleContext.attr.explicit_dep_list[0].label");
+ Label explicitDepListLabel = (Label) eval("ruleContext.attr.explicit_dep_list[0].label");
assertThat(explicitDepListLabel).isEqualTo(Label.parseAbsolute("//:dep", ImmutableMap.of()));
- Label implicitDepListLabel =
- (Label) evalRuleContextCode(context, "ruleContext.attr._implicit_dep_list[0].label");
+ Label implicitDepListLabel = (Label) eval("ruleContext.attr._implicit_dep_list[0].label");
assertThat(implicitDepListLabel).isEqualTo(Label.parseAbsolute("//:dep", ImmutableMap.of()));
}
@@ -1423,8 +1404,8 @@
.build());
invalidatePackages(/*alsoConfigs=*/false); // Repository shuffling messes with toolchain labels.
- SkylarkRuleContext context = createRuleContext("@r//a:r");
- Label depLabel = (Label) evalRuleContextCode(context, "ruleContext.attr.internal_dep.label");
+ setRuleContext(createRuleContext("@r//a:r"));
+ Label depLabel = (Label) eval("ruleContext.attr.internal_dep.label");
assertThat(depLabel).isEqualTo(Label.parseAbsolute("//:dep", ImmutableMap.of()));
}
@@ -1458,8 +1439,8 @@
.build());
invalidatePackages(/*alsoConfigs=*/false); // Repository shuffling messes with toolchain labels.
- SkylarkRuleContext context = createRuleContext("@r//a:r");
- Label depLabel = (Label) evalRuleContextCode(context, "ruleContext.attr.internal_dep.label");
+ setRuleContext(createRuleContext("@r//a:r"));
+ Label depLabel = (Label) eval("ruleContext.attr.internal_dep.label");
assertThat(depLabel).isEqualTo(Label.parseAbsolute("@r//:dep", ImmutableMap.of()));
}
@@ -1521,7 +1502,7 @@
invalidatePackages(/*alsoConfigs=*/false); // Repository shuffling messes with toolchain labels.
assertThat(
- (List<Label>)
+ (List)
getConfiguredTargetAndData("@foo//:baz")
.getTarget()
.getAssociatedRule()
@@ -1571,27 +1552,22 @@
"py_binary(name = 'lib_with_init', srcs = ['lib_with_init.py', 'lib2.py', '__init__.py'])",
"skylark_rule(name = 'foo_with_init', dep = ':lib_with_init')");
- SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
+ setRuleContext(createRuleContext("//test:foo"));
Object filenames =
- evalRuleContextCode(
- ruleContext,
- "[f.short_path for f in ruleContext.attr.dep.default_runfiles.files.to_list()]");
+ eval("[f.short_path for f in ruleContext.attr.dep.default_runfiles.files.to_list()]");
assertThat(filenames).isInstanceOf(SkylarkList.class);
- SkylarkList filenamesList = (SkylarkList) filenames;
+ SkylarkList<?> filenamesList = (SkylarkList) filenames;
assertThat(filenamesList).containsAtLeast("test/lib.py", "test/lib2.py");
- Object emptyFilenames =
- evalRuleContextCode(
- ruleContext, "ruleContext.attr.dep.default_runfiles.empty_filenames.to_list()");
+ Object emptyFilenames = eval("ruleContext.attr.dep.default_runfiles.empty_filenames.to_list()");
assertThat(emptyFilenames).isInstanceOf(SkylarkList.class);
- SkylarkList emptyFilenamesList = (SkylarkList) emptyFilenames;
+ SkylarkList<?> emptyFilenamesList = (SkylarkList) emptyFilenames;
assertThat(emptyFilenamesList).containsExactly("test/__init__.py");
- SkylarkRuleContext ruleWithInitContext = createRuleContext("//test:foo_with_init");
+ setRuleContext(createRuleContext("//test:foo_with_init"));
Object noEmptyFilenames =
- evalRuleContextCode(
- ruleWithInitContext, "ruleContext.attr.dep.default_runfiles.empty_filenames.to_list()");
+ eval("ruleContext.attr.dep.default_runfiles.empty_filenames.to_list()");
assertThat(noEmptyFilenames).isInstanceOf(SkylarkList.class);
- SkylarkList noEmptyFilenamesList = (SkylarkList) noEmptyFilenames;
+ SkylarkList<?> noEmptyFilenamesList = (SkylarkList) noEmptyFilenames;
assertThat(noEmptyFilenamesList).isEmpty();
}
@@ -1627,22 +1603,18 @@
" srcs = ['test/b.py'],",
" data = [':lib_with_symlink'],",
")");
- SkylarkRuleContext ruleWithSymlinkContext = createRuleContext("//test:test_with_symlink");
+ setRuleContext(createRuleContext("//test:test_with_symlink"));
Object symlinkPaths =
- evalRuleContextCode(
- ruleWithSymlinkContext,
- "[s.path for s in",
- "ruleContext.attr.data[0].data_runfiles.symlinks.to_list()]");
+ eval("[s.path for s in ruleContext.attr.data[0].data_runfiles.symlinks.to_list()]");
assertThat(symlinkPaths).isInstanceOf(SkylarkList.class);
- SkylarkList<String> symlinkPathsList = (SkylarkList<String>) symlinkPaths;
+ SkylarkList<?> symlinkPathsList = (SkylarkList) symlinkPaths;
assertThat(symlinkPathsList).containsExactly("symlink_test/a.py").inOrder();
Object symlinkFilenames =
- evalRuleContextCode(
- ruleWithSymlinkContext,
- "[s.target_file.short_path for s in",
- "ruleContext.attr.data[0].data_runfiles.symlinks.to_list()]");
+ eval(
+ "[s.target_file.short_path for s in"
+ + " ruleContext.attr.data[0].data_runfiles.symlinks.to_list()]");
assertThat(symlinkFilenames).isInstanceOf(SkylarkList.class);
- SkylarkList<String> symlinkFilenamesList = (SkylarkList<String>) symlinkFilenames;
+ SkylarkList<?> symlinkFilenamesList = (SkylarkList) symlinkFilenames;
assertThat(symlinkFilenamesList).containsExactly("test/a.py").inOrder();
}
@@ -1678,22 +1650,18 @@
" srcs = ['test/b.py'],",
" data = [':lib_with_symlink'],",
")");
- SkylarkRuleContext ruleWithSymlinkContext = createRuleContext("//test:test_with_symlink");
+ setRuleContext(createRuleContext("//test:test_with_symlink"));
Object symlinkPaths =
- evalRuleContextCode(
- ruleWithSymlinkContext,
- "[s.path for s in",
- "ruleContext.attr.data[0].data_runfiles.symlinks.to_list()]");
+ eval("[s.path for s in ruleContext.attr.data[0].data_runfiles.symlinks.to_list()]");
assertThat(symlinkPaths).isInstanceOf(SkylarkList.class);
- SkylarkList<String> symlinkPathsList = (SkylarkList<String>) symlinkPaths;
+ SkylarkList<?> symlinkPathsList = (SkylarkList) symlinkPaths;
assertThat(symlinkPathsList).containsExactly("symlink_test/a.py").inOrder();
Object symlinkFilenames =
- evalRuleContextCode(
- ruleWithSymlinkContext,
- "[s.target_file.short_path for s in",
- "ruleContext.attr.data[0].data_runfiles.symlinks.to_list()]");
+ eval(
+ "[s.target_file.short_path for s in"
+ + " ruleContext.attr.data[0].data_runfiles.symlinks.to_list()]");
assertThat(symlinkFilenames).isInstanceOf(SkylarkList.class);
- SkylarkList<String> symlinkFilenamesList = (SkylarkList<String>) symlinkFilenames;
+ SkylarkList<?> symlinkFilenamesList = (SkylarkList) symlinkFilenames;
assertThat(symlinkFilenamesList).containsExactly("test/a.py").inOrder();
}
@@ -1729,23 +1697,18 @@
" srcs = ['test/b.py'],",
" data = [':lib_with_root_symlink'],",
")");
- SkylarkRuleContext ruleWithRootSymlinkContext =
- createRuleContext("//test:test_with_root_symlink");
+ setRuleContext(createRuleContext("//test:test_with_root_symlink"));
Object rootSymlinkPaths =
- evalRuleContextCode(
- ruleWithRootSymlinkContext,
- "[s.path for s in",
- "ruleContext.attr.data[0].data_runfiles.root_symlinks.to_list()]");
+ eval("[s.path for s in ruleContext.attr.data[0].data_runfiles.root_symlinks.to_list()]");
assertThat(rootSymlinkPaths).isInstanceOf(SkylarkList.class);
- SkylarkList<String> rootSymlinkPathsList = (SkylarkList<String>) rootSymlinkPaths;
+ SkylarkList<?> rootSymlinkPathsList = (SkylarkList) rootSymlinkPaths;
assertThat(rootSymlinkPathsList).containsExactly("root_symlink_test/a.py").inOrder();
Object rootSymlinkFilenames =
- evalRuleContextCode(
- ruleWithRootSymlinkContext,
- "[s.target_file.short_path for s in",
- "ruleContext.attr.data[0].data_runfiles.root_symlinks.to_list()]");
+ eval(
+ "[s.target_file.short_path for s in"
+ + " ruleContext.attr.data[0].data_runfiles.root_symlinks.to_list()]");
assertThat(rootSymlinkFilenames).isInstanceOf(SkylarkList.class);
- SkylarkList<String> rootSymlinkFilenamesList = (SkylarkList<String>) rootSymlinkFilenames;
+ SkylarkList<?> rootSymlinkFilenamesList = (SkylarkList) rootSymlinkFilenames;
assertThat(rootSymlinkFilenamesList).containsExactly("test/a.py").inOrder();
}
@@ -1781,23 +1744,18 @@
" srcs = ['test/b.py'],",
" data = [':lib_with_root_symlink'],",
")");
- SkylarkRuleContext ruleWithRootSymlinkContext =
- createRuleContext("//test:test_with_root_symlink");
+ setRuleContext(createRuleContext("//test:test_with_root_symlink"));
Object rootSymlinkPaths =
- evalRuleContextCode(
- ruleWithRootSymlinkContext,
- "[s.path for s in",
- "ruleContext.attr.data[0].data_runfiles.root_symlinks.to_list()]");
+ eval("[s.path for s in ruleContext.attr.data[0].data_runfiles.root_symlinks.to_list()]");
assertThat(rootSymlinkPaths).isInstanceOf(SkylarkList.class);
- SkylarkList<String> rootSymlinkPathsList = (SkylarkList<String>) rootSymlinkPaths;
+ SkylarkList<?> rootSymlinkPathsList = (SkylarkList) rootSymlinkPaths;
assertThat(rootSymlinkPathsList).containsExactly("root_symlink_test/a.py").inOrder();
Object rootSymlinkFilenames =
- evalRuleContextCode(
- ruleWithRootSymlinkContext,
- "[s.target_file.short_path for s in",
- "ruleContext.attr.data[0].data_runfiles.root_symlinks.to_list()]");
+ eval(
+ "[s.target_file.short_path for s in"
+ + " ruleContext.attr.data[0].data_runfiles.root_symlinks.to_list()]");
assertThat(rootSymlinkFilenames).isInstanceOf(SkylarkList.class);
- SkylarkList<String> rootSymlinkFilenamesList = (SkylarkList<String>) rootSymlinkFilenames;
+ SkylarkList<?> rootSymlinkFilenamesList = (SkylarkList) rootSymlinkFilenames;
assertThat(rootSymlinkFilenamesList).containsExactly("test/a.py").inOrder();
}
@@ -1820,8 +1778,8 @@
")");
invalidatePackages();
SkylarkRuleContext ruleContext = createRuleContext("//test:lib");
- String filename = evalRuleContextCode(ruleContext, "ruleContext.files.srcs[0].short_path")
- .toString();
+ setRuleContext(ruleContext);
+ String filename = eval("ruleContext.files.srcs[0].short_path").toString();
assertThat(filename).isEqualTo("../foo/bar.txt");
}
@@ -1887,15 +1845,16 @@
scratch.file("test/BUILD",
simpleBuildDefinition);
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
+ setRuleContext(ruleContext);
- Object provider = evalRuleContextCode(ruleContext, "ruleContext.attr.dep[Actions]");
+ Object provider = eval("ruleContext.attr.dep[Actions]");
assertThat(provider).isInstanceOf(StructImpl.class);
assertThat(((StructImpl) provider).getProvider()).isEqualTo(ActionsProvider.INSTANCE);
update("actions", provider);
Object mapping = eval("actions.by_file");
assertThat(mapping).isInstanceOf(SkylarkDict.class);
- assertThat((SkylarkDict<?, ?>) mapping).hasSize(1);
+ assertThat((SkylarkDict) mapping).hasSize(1);
update("file", eval("ruleContext.attr.dep.files.to_list()[0]"));
Object actionUnchecked = eval("actions.by_file[file]");
assertThat(actionUnchecked).isInstanceOf(ActionAnalysisMetadata.class);
@@ -1911,11 +1870,9 @@
scratch.file("test/BUILD",
simpleBuildDefinition);
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
+ setRuleContext(ruleContext);
- Exception e =
- assertThrows(
- Exception.class,
- () -> evalRuleContextCode(ruleContext, "ruleContext.attr.dep[Actions]"));
+ Exception e = assertThrows(Exception.class, () -> eval("ruleContext.attr.dep[Actions]"));
assertThat(e)
.hasMessageThat()
.contains(
@@ -1946,7 +1903,7 @@
scratch.file("test/BUILD",
simpleBuildDefinition);
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
- update("ruleContext", ruleContext);
+ setRuleContext(ruleContext);
update("file1", eval("ruleContext.attr.dep.out1"));
update("file2", eval("ruleContext.attr.dep.out2"));
update("action1", eval("ruleContext.attr.dep[Actions].by_file[file1]"));
@@ -1997,10 +1954,11 @@
scratch.file("test/BUILD",
simpleBuildDefinition);
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
+ setRuleContext(ruleContext);
- Object mapUnchecked = evalRuleContextCode(ruleContext, "ruleContext.attr.dep.v");
+ Object mapUnchecked = eval("ruleContext.attr.dep.v");
assertThat(mapUnchecked).isInstanceOf(SkylarkDict.class);
- SkylarkDict<?, ?> map = (SkylarkDict<?, ?>) mapUnchecked;
+ SkylarkDict<?, ?> map = (SkylarkDict) mapUnchecked;
// Should only have the first action because created_actions() was called
// before the second action was created.
Object file = eval("ruleContext.attr.dep.out1");
@@ -2023,8 +1981,9 @@
" name = 'undertest',",
")");
SkylarkRuleContext ruleContext = createRuleContext("//test:undertest");
+ setRuleContext(ruleContext);
- Object result = evalRuleContextCode(ruleContext, "ruleContext.created_actions()");
+ Object result = eval("ruleContext.created_actions()");
assertThat(result).isEqualTo(Runtime.NONE);
}
@@ -2037,7 +1996,7 @@
scratch.file("test/BUILD",
simpleBuildDefinition);
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
- update("ruleContext", ruleContext);
+ setRuleContext(ruleContext);
update("file", eval("ruleContext.attr.dep.files.to_list()[0]"));
update("action", eval("ruleContext.attr.dep[Actions].by_file[file]"));
@@ -2045,7 +2004,7 @@
Object argvUnchecked = eval("action.argv");
assertThat(argvUnchecked).isInstanceOf(SkylarkList.MutableList.class);
- SkylarkList.MutableList<?> argv = (SkylarkList.MutableList<?>) argvUnchecked;
+ SkylarkList.MutableList<?> argv = (SkylarkList.MutableList) argvUnchecked;
assertThat(argv).hasSize(3);
assertThat(argv.isImmutable()).isTrue();
Object result = eval("action.argv[2].startswith('echo foo123')");
@@ -2093,10 +2052,11 @@
testingRuleDefinition);
scratch.file("test/BUILD", simpleBuildDefinition);
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
+ setRuleContext(ruleContext);
- Object mapUnchecked = evalRuleContextCode(ruleContext, "ruleContext.attr.dep.v");
+ Object mapUnchecked = eval("ruleContext.attr.dep.v");
assertThat(mapUnchecked).isInstanceOf(SkylarkDict.class);
- SkylarkDict<?, ?> map = (SkylarkDict<?, ?>) mapUnchecked;
+ SkylarkDict<?, ?> map = (SkylarkDict) mapUnchecked;
Object out1 = eval("ruleContext.attr.dep.out1");
Object out2 = eval("ruleContext.attr.dep.out2");
Object out3 = eval("ruleContext.attr.dep.out3");
@@ -2149,7 +2109,7 @@
scratch.file("test/BUILD",
simpleBuildDefinition);
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
- update("ruleContext", ruleContext);
+ setRuleContext(ruleContext);
update("file", eval("ruleContext.attr.dep.files.to_list()[0]"));
update("action", eval("ruleContext.attr.dep[Actions].by_file[file]"));
@@ -2188,7 +2148,7 @@
" dep = ':undertest',",
")");
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
- update("ruleContext", ruleContext);
+ setRuleContext(ruleContext);
update("file", eval("ruleContext.attr.dep.files.to_list()[0]"));
update("action", eval("ruleContext.attr.dep[Actions].by_file[file]"));
@@ -2221,7 +2181,8 @@
setUpCoverageInstrumentedTest();
useConfiguration("--nocollect_code_coverage", "--instrumentation_filter=.");
SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.coverage_instrumented()");
+ setRuleContext(ruleContext);
+ Object result = eval("ruleContext.coverage_instrumented()");
assertThat((Boolean) result).isFalse();
}
@@ -2229,9 +2190,8 @@
public void testCoverageInstrumentedFalseForSourceFileLabel() throws Exception {
setUpCoverageInstrumentedTest();
useConfiguration("--collect_code_coverage", "--instrumentation_filter=.");
- SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
- Object result = evalRuleContextCode(ruleContext,
- "ruleContext.coverage_instrumented(ruleContext.attr.srcs[0])");
+ setRuleContext(createRuleContext("//test:foo"));
+ Object result = eval("ruleContext.coverage_instrumented(ruleContext.attr.srcs[0])");
assertThat((Boolean) result).isFalse();
}
@@ -2239,8 +2199,8 @@
public void testCoverageInstrumentedDoesNotMatchFilter() throws Exception {
setUpCoverageInstrumentedTest();
useConfiguration("--collect_code_coverage", "--instrumentation_filter=:foo");
- SkylarkRuleContext ruleContext = createRuleContext("//test:bar");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.coverage_instrumented()");
+ setRuleContext(createRuleContext("//test:bar"));
+ Object result = eval("ruleContext.coverage_instrumented()");
assertThat((Boolean) result).isFalse();
}
@@ -2248,8 +2208,8 @@
public void testCoverageInstrumentedMatchesFilter() throws Exception {
setUpCoverageInstrumentedTest();
useConfiguration("--collect_code_coverage", "--instrumentation_filter=:foo");
- SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
- Object result = evalRuleContextCode(ruleContext, "ruleContext.coverage_instrumented()");
+ setRuleContext(createRuleContext("//test:foo"));
+ Object result = eval("ruleContext.coverage_instrumented()");
assertThat((Boolean) result).isTrue();
}
@@ -2257,10 +2217,9 @@
public void testCoverageInstrumentedDoesNotMatchFilterNonDefaultLabel() throws Exception {
setUpCoverageInstrumentedTest();
useConfiguration("--collect_code_coverage", "--instrumentation_filter=:foo");
- SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
+ setRuleContext(createRuleContext("//test:foo"));
// //test:bar does not match :foo, though //test:foo would.
- Object result = evalRuleContextCode(ruleContext,
- "ruleContext.coverage_instrumented(ruleContext.attr.deps[0])");
+ Object result = eval("ruleContext.coverage_instrumented(ruleContext.attr.deps[0])");
assertThat((Boolean) result).isFalse();
}
@@ -2268,10 +2227,9 @@
public void testCoverageInstrumentedMatchesFilterNonDefaultLabel() throws Exception {
setUpCoverageInstrumentedTest();
useConfiguration("--collect_code_coverage", "--instrumentation_filter=:bar");
- SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
+ setRuleContext(createRuleContext("//test:foo"));
// //test:bar does match :bar, though //test:foo would not.
- Object result = evalRuleContextCode(ruleContext,
- "ruleContext.coverage_instrumented(ruleContext.attr.deps[0])");
+ Object result = eval("ruleContext.coverage_instrumented(ruleContext.attr.deps[0])");
assertThat((Boolean) result).isTrue();
}
@@ -2412,7 +2370,6 @@
}
}
-
private static final List<String> deprecatedActionsApi =
ImmutableList.of(
"new_file('foo.txt')",
@@ -2475,7 +2432,7 @@
new SkylarkKey(Label.parseAbsolute("//a:a.bzl", ImmutableMap.of()), "key_provider");
SkylarkInfo keyInfo = (SkylarkInfo) a.get(key);
- SkylarkList<String> keys = (SkylarkList<String>) keyInfo.getValue("keys");
+ SkylarkList<?> keys = (SkylarkList) keyInfo.getValue("keys");
assertThat(keys).containsExactly("c", "b", "a", "f", "e", "d").inOrder();
}
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java
index e3565e1..c7c5909 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java
@@ -23,6 +23,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
import com.google.devtools.build.lib.actions.Action;
import com.google.devtools.build.lib.actions.ActionAnalysisMetadata;
import com.google.devtools.build.lib.actions.ActionKeyContext;
@@ -66,7 +67,6 @@
import com.google.devtools.build.lib.syntax.SkylarkList;
import com.google.devtools.build.lib.syntax.SkylarkList.MutableList;
import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
-import com.google.devtools.build.lib.syntax.StarlarkSemantics;
import com.google.devtools.build.lib.syntax.StarlarkThread;
import com.google.devtools.build.lib.testutil.MoreAsserts;
import com.google.devtools.build.lib.util.Fingerprint;
@@ -156,6 +156,18 @@
")");
}
+ private void setRuleContext(SkylarkRuleContext ctx) throws Exception {
+ update("ruleContext", ctx);
+ }
+
+ private static void assertArtifactFilenames(Iterable<Artifact> artifacts, String... expected) {
+ ImmutableList.Builder<String> filenames = ImmutableList.builder();
+ for (Artifact a : artifacts) {
+ filenames.add(a.getFilename());
+ }
+ assertThat(filenames.build()).containsAtLeastElementsIn(Lists.newArrayList(expected));
+ }
+
private StructImpl getMyInfoFromTarget(ConfiguredTarget configuredTarget) throws Exception {
Provider.Key key =
new SkylarkProvider.SkylarkKey(
@@ -244,23 +256,10 @@
"mock('by position', mandatory='by_key', mandatory_key='c')");
}
- @SuppressWarnings("unchecked")
- @Test
- public void testListComprehensionsWithNestedSet() throws Exception {
- ev =
- createEvaluationTestCase(
- StarlarkSemantics.DEFAULT_SEMANTICS.toBuilder()
- .incompatibleDepsetIsNotIterable(false)
- .build());
- ev.initialize();
-
- Object result = eval("[x + x for x in depset([1, 2, 3])]");
- assertThat((Iterable<Object>) result).containsExactly(2, 4, 6).inOrder();
- }
-
@Test
public void testCreateSpawnActionCreatesSpawnAction() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
+ setRuleContext(ruleContext);
createTestSpawnAction(ruleContext);
ActionAnalysisMetadata action =
Iterables.getOnlyElement(
@@ -269,8 +268,23 @@
}
@Test
+ public void testArtifactPath() throws Exception {
+ setRuleContext(createRuleContext("//foo:foo"));
+ String result = (String) eval("ruleContext.files.tools[0].path");
+ assertThat(result).isEqualTo("foo/t.exe");
+ }
+
+ @Test
+ public void testArtifactShortPath() throws Exception {
+ setRuleContext(createRuleContext("//foo:foo"));
+ String result = (String) eval("ruleContext.files.tools[0].short_path");
+ assertThat(result).isEqualTo("foo/t.exe");
+ }
+
+ @Test
public void testCreateSpawnActionArgumentsWithCommand() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
+ setRuleContext(ruleContext);
createTestSpawnAction(ruleContext);
SpawnAction action =
(SpawnAction)
@@ -288,8 +302,8 @@
@Test
public void testCreateSpawnActionArgumentsWithExecutable() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"ruleContext.actions.run(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
@@ -308,8 +322,8 @@
public void testCreateActionWithDepsetInput() throws Exception {
// Same test as above, with depset as inputs.
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"ruleContext.actions.run(",
" inputs = depset(ruleContext.files.srcs),",
" outputs = ruleContext.files.srcs,",
@@ -326,8 +340,8 @@
@Test
public void testCreateSpawnActionArgumentsBadExecutable() throws Exception {
- checkErrorContains(
- createRuleContext("//foo:foo"),
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"expected value of type 'File or string or FilesToRunProvider' for parameter 'executable', "
+ "for call to method run(",
"ruleContext.actions.run(",
@@ -340,8 +354,8 @@
@Test
public void testCreateSpawnActionShellCommandList() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"ruleContext.actions.run_shell(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
@@ -360,8 +374,8 @@
@Test
public void testCreateSpawnActionEnvAndExecInfo() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"ruleContext.actions.run_shell(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
@@ -381,17 +395,16 @@
@Test
public void testCreateSpawnActionUnknownParam() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- checkErrorContains(
- ruleContext,
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"unexpected keyword 'bad_param', for call to method run(",
"f = ruleContext.actions.declare_file('foo.sh')",
"ruleContext.actions.run(outputs=[], bad_param = 'some text', executable = f)");
}
private Object createTestSpawnAction(SkylarkRuleContext ruleContext) throws Exception {
- return evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ return eval(
"ruleContext.actions.run_shell(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
@@ -404,8 +417,8 @@
@Test
public void testCreateSpawnActionBadGenericArg() throws Exception {
- checkErrorContains(
- createRuleContext("//foo:foo"),
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"expected type 'File' for 'outputs' element but got type 'string' instead",
"l = ['a', 'b']",
"ruleContext.actions.run_shell(",
@@ -415,8 +428,8 @@
@Test
public void testRunShellArgumentsWithCommandSequence() throws Exception {
- checkErrorContains(
- createRuleContext("//foo:foo"),
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"'arguments' must be empty if 'command' is a sequence of strings",
"ruleContext.actions.run_shell(outputs = ruleContext.files.srcs,",
" command = [\"echo\", \"'hello world'\", \"&&\", \"touch\"],",
@@ -521,8 +534,8 @@
@Test
public void testCreateFileAction() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"ruleContext.actions.write(",
" output = ruleContext.files.srcs[0],",
" content = 'hello world',",
@@ -539,23 +552,19 @@
@Test
public void testEmptyAction() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEmptyAction("mnemonic = 'test'");
+ checkEmptyAction("mnemonic = 'test', inputs = ruleContext.files.srcs");
+ checkEmptyAction("mnemonic = 'test', inputs = depset(ruleContext.files.srcs)");
- checkEmptyAction(ruleContext, "mnemonic = 'test'");
- checkEmptyAction(ruleContext, "mnemonic = 'test', inputs = ruleContext.files.srcs");
- checkEmptyAction(ruleContext, "mnemonic = 'test', inputs = depset(ruleContext.files.srcs)");
-
- checkErrorContains(
- ruleContext,
+ checkEvalErrorContains(
"parameter 'mnemonic' has no default value, for call to method "
+ "do_nothing(mnemonic, inputs = []) of 'actions'",
"ruleContext.actions.do_nothing(inputs = ruleContext.files.srcs)");
}
- private void checkEmptyAction(SkylarkRuleContext ruleContext, String namedArgs) throws Exception {
- assertThat(
- evalRuleContextCode(
- ruleContext, String.format("ruleContext.actions.do_nothing(%s)", namedArgs)))
+ private void checkEmptyAction(String namedArgs) throws Exception {
+ assertThat(eval(String.format("ruleContext.actions.do_nothing(%s)", namedArgs)))
.isEqualTo(Runtime.NONE);
}
@@ -592,16 +601,16 @@
@Test
public void testExpandLocation() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:bar");
+ setRuleContext(ruleContext);
// If there is only a single target, both "location" and "locations" should work
- runExpansion(ruleContext, "location :jl", "[blaze]*-out/.*/bin/foo/libjl.jar");
- runExpansion(ruleContext, "locations :jl", "[blaze]*-out/.*/bin/foo/libjl.jar");
+ runExpansion("location :jl", "[blaze]*-out/.*/bin/foo/libjl.jar");
+ runExpansion("locations :jl", "[blaze]*-out/.*/bin/foo/libjl.jar");
- runExpansion(ruleContext, "location //foo:jl", "[blaze]*-out/.*/bin/foo/libjl.jar");
+ runExpansion("location //foo:jl", "[blaze]*-out/.*/bin/foo/libjl.jar");
// Multiple targets and "location" should result in an error
checkReportedErrorStartsWith(
- ruleContext,
"in genrule rule //foo:bar: label '//foo:gl' "
+ "in $(location) expression expands to more than one file, please use $(locations "
+ "//foo:gl) instead.",
@@ -609,15 +618,13 @@
// We have to use "locations" for multiple targets
runExpansion(
- ruleContext,
"locations :gl",
"[blaze]*-out/.*/bin/foo/gl.a [blaze]*-out/.*/bin/foo/gl.gcgox");
// LocationExpander just returns the input string if there is no label
- runExpansion(ruleContext, "location", "\\$\\(location\\)");
+ runExpansion("location", "\\$\\(location\\)");
checkReportedErrorStartsWith(
- ruleContext,
"in genrule rule //foo:bar: label '//foo:abc' in $(locations) expression "
+ "is not a declared prerequisite of this rule",
"ruleContext.expand_location('$(locations :abc)')");
@@ -627,28 +634,24 @@
@Test
public void testExpandLocationWithDollarSignsAndCurlys() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:bar");
- assertThat((String)
- evalRuleContextCode(
- ruleContext, "ruleContext.expand_location('${abc} $(echo) $$ $')"))
+ setRuleContext(ruleContext);
+ assertThat((String) eval("ruleContext.expand_location('${abc} $(echo) $$ $')"))
.isEqualTo("${abc} $(echo) $$ $");
}
/**
* Invokes ctx.expand_location() with the given parameters and checks whether this led to the
* expected result
- * @param ruleContext The rule context
+ *
* @param command Either "location" or "locations". This only matters when the label has multiple
- * targets
+ * targets
* @param expectedPattern Regex pattern that matches the expected result
*/
- private void runExpansion(SkylarkRuleContext ruleContext, String command, String expectedPattern)
- throws Exception {
+ private void runExpansion(String command, String expectedPattern) throws Exception {
assertMatches(
"Expanded string",
expectedPattern,
- (String)
- evalRuleContextCode(
- ruleContext, String.format("ruleContext.expand_location('$(%s)')", command)));
+ (String) eval(String.format("ruleContext.expand_location('$(%s)')", command)));
}
private void assertMatches(String description, String expectedPattern, String computedValue)
@@ -662,8 +665,8 @@
@Test
public void testResolveCommandMakeVariables() throws Exception {
- evalRuleContextCode(
- createRuleContext("//foo:resolve_me"),
+ setRuleContext(createRuleContext("//foo:resolve_me"));
+ eval(
"inputs, argv, manifests = ruleContext.resolve_command(",
" command='I got the $(HELLO) on a $(DAVE)', ",
" make_variables={'HELLO': 'World', 'DAVE': type('')})");
@@ -677,8 +680,8 @@
@Test
public void testResolveCommandInputs() throws Exception {
- evalRuleContextCode(
- createRuleContext("//foo:resolve_me"),
+ setRuleContext(createRuleContext("//foo:resolve_me"));
+ eval(
"inputs, argv, input_manifests = ruleContext.resolve_command(",
" tools=ruleContext.attr.tools)");
@SuppressWarnings("unchecked")
@@ -697,8 +700,8 @@
@Test
public void testResolveCommandExpandLocations() throws Exception {
- evalRuleContextCode(
- createRuleContext("//foo:resolve_me"),
+ setRuleContext(createRuleContext("//foo:resolve_me"));
+ eval(
"def foo():", // no for loops at top-level
" label_dict = {}",
" all = []",
@@ -720,8 +723,8 @@
@Test
public void testResolveCommandExecutionRequirements() throws Exception {
// Tests that requires-darwin execution requirements result in the usage of /bin/bash.
- evalRuleContextCode(
- createRuleContext("//foo:resolve_me"),
+ setRuleContext(createRuleContext("//foo:resolve_me"));
+ eval(
"inputs, argv, manifests = ruleContext.resolve_command(",
" execution_requirements={'requires-darwin': ''})");
@SuppressWarnings("unchecked")
@@ -731,8 +734,8 @@
@Test
public void testResolveCommandScript() throws Exception {
- evalRuleContextCode(
- createRuleContext("//foo:resolve_me"),
+ setRuleContext(createRuleContext("//foo:resolve_me"));
+ eval(
"def foo():", // no for loops at top-level
" s = 'a'",
" for i in range(1,17): s = s + s", // 2**17 > CommandHelper.maxCommandLength (=64000)
@@ -749,8 +752,8 @@
@Test
public void testResolveTools() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:resolve_me");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"inputs, input_manifests = ruleContext.resolve_tools(tools=ruleContext.attr.tools)",
"ruleContext.actions.run(",
" outputs = [ruleContext.actions.declare_file('x.out')],",
@@ -783,9 +786,8 @@
@Test
public void testBadParamTypeErrorMessage() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- checkErrorContains(
- ruleContext,
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"expected value of type 'string or Args' for parameter 'content'",
"ruleContext.actions.write(",
" output = ruleContext.files.srcs[0],",
@@ -796,8 +798,8 @@
@Test
public void testCreateTemplateAction() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"ruleContext.actions.expand_template(",
" template = ruleContext.files.srcs[0],",
" output = ruleContext.files.srcs[1],",
@@ -833,8 +835,8 @@
Charset latin1 = StandardCharsets.ISO_8859_1;
Charset utf8 = StandardCharsets.UTF_8;
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"ruleContext.actions.expand_template(",
" template = ruleContext.files.srcs[0],",
" output = ruleContext.files.srcs[1],",
@@ -849,23 +851,24 @@
@Test
public void testRunfilesAddFromDependencies() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:bar");
- Object result =
- evalRuleContextCode(ruleContext, "ruleContext.runfiles(collect_default = True)");
+ setRuleContext(createRuleContext("//foo:bar"));
+ Object result = eval("ruleContext.runfiles(collect_default = True)");
assertThat(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result)))
.contains("libjl.jar");
}
@Test
public void testRunfilesBadListGenericType() throws Exception {
- checkErrorContains(
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"expected type 'File' for 'files' element but got type 'string' instead",
"ruleContext.runfiles(files = ['some string'])");
}
@Test
public void testRunfilesBadSetGenericType() throws Exception {
- checkErrorContains(
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"expected value of type 'depset of Files or NoneType' for parameter 'transitive_files', "
+ "for call to method runfiles(",
"ruleContext.runfiles(transitive_files=depset([1, 2, 3]))");
@@ -873,61 +876,53 @@
@Test
public void testRunfilesBadMapGenericType() throws Exception {
- checkErrorContains(
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"expected type 'string' for 'symlinks' key but got type 'int' instead",
"ruleContext.runfiles(symlinks = {123: ruleContext.files.srcs[0]})");
- checkErrorContains(
+ checkEvalErrorContains(
"expected type 'File' for 'symlinks' value but got type 'int' instead",
"ruleContext.runfiles(symlinks = {'some string': 123})");
- checkErrorContains(
+ checkEvalErrorContains(
"expected type 'string' for 'root_symlinks' key but got type 'int' instead",
"ruleContext.runfiles(root_symlinks = {123: ruleContext.files.srcs[0]})");
- checkErrorContains(
+ checkEvalErrorContains(
"expected type 'File' for 'root_symlinks' value but got type 'int' instead",
"ruleContext.runfiles(root_symlinks = {'some string': 123})");
}
@Test
public void testRunfilesArtifactsFromArtifact() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
+ setRuleContext(createRuleContext("//foo:foo"));
Object result =
- evalRuleContextCode(
- ruleContext,
- "artifacts = ruleContext.files.tools",
- "ruleContext.runfiles(files = artifacts)");
+ eval("artifacts = ruleContext.files.tools", "ruleContext.runfiles(files = artifacts)");
assertThat(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result))).contains("t.exe");
}
@Test
public void testRunfilesArtifactsFromIterableArtifacts() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
+ setRuleContext(createRuleContext("//foo:foo"));
Object result =
- evalRuleContextCode(
- ruleContext,
- "artifacts = ruleContext.files.srcs",
- "ruleContext.runfiles(files = artifacts)");
+ eval("artifacts = ruleContext.files.srcs", "ruleContext.runfiles(files = artifacts)");
assertThat(ImmutableList.of("a.txt", "b.img"))
.isEqualTo(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result)));
}
@Test
public void testRunfilesArtifactsFromNestedSetArtifacts() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
+ setRuleContext(createRuleContext("//foo:foo"));
Object result =
- evalRuleContextCode(
- ruleContext,
- "ftb = depset(ruleContext.files.srcs)",
- "ruleContext.runfiles(transitive_files = ftb)");
+ eval(
+ "ftb = depset(ruleContext.files.srcs)", "ruleContext.runfiles(transitive_files = ftb)");
assertThat(ImmutableList.of("a.txt", "b.img"))
.isEqualTo(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result)));
}
@Test
public void testRunfilesArtifactsFromDefaultAndFiles() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:bar");
+ setRuleContext(createRuleContext("//foo:bar"));
Object result =
- evalRuleContextCode(
- ruleContext,
+ eval(
"artifacts = ruleContext.files.srcs",
// It would be nice to write [DEFAULT] + artifacts, but artifacts
// is an ImmutableList and Skylark interprets it as a tuple.
@@ -939,8 +934,9 @@
@Test
public void testRunfilesArtifactsFromSymlink() throws Exception {
+ setRuleContext(createRuleContext("//foo:foo"));
Object result =
- evalRuleContextCode(
+ eval(
"artifacts = ruleContext.files.srcs",
"ruleContext.runfiles(symlinks = {'sym1': artifacts[0]})");
assertThat(ImmutableList.of("a.txt"))
@@ -949,8 +945,9 @@
@Test
public void testRunfilesArtifactsFromRootSymlink() throws Exception {
+ setRuleContext(createRuleContext("//foo:foo"));
Object result =
- evalRuleContextCode(
+ eval(
"artifacts = ruleContext.files.srcs",
"ruleContext.runfiles(root_symlinks = {'sym1': artifacts[0]})");
assertThat(ImmutableList.of("a.txt"))
@@ -960,8 +957,9 @@
@Test
public void testRunfilesSymlinkConflict() throws Exception {
// Two different artifacts mapped to same path in runfiles
+ setRuleContext(createRuleContext("//foo:foo"));
Object result =
- evalRuleContextCode(
+ eval(
"artifacts = ruleContext.files.srcs",
"prefix = ruleContext.workspace_name + '/' if ruleContext.workspace_name else ''",
"ruleContext.runfiles(",
@@ -973,55 +971,52 @@
assertContainsEvent("ERROR <no location>: overwrote runfile");
}
- private Iterable<Artifact> getRunfileArtifacts(Object runfiles) {
+ private static Iterable<Artifact> getRunfileArtifacts(Object runfiles) {
return ((Runfiles) runfiles).getAllArtifacts();
}
@Test
public void testRunfilesBadKeywordArguments() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- checkErrorContains(
- ruleContext,
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"unexpected keyword 'bad_keyword', for call to method runfiles(",
"ruleContext.runfiles(bad_keyword = '')");
}
@Test
public void testNsetContainsList() throws Exception {
- checkErrorContains(
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"depsets cannot contain items of type 'list'", "depset([[ruleContext.files.srcs]])");
}
@Test
public void testCmdJoinPaths() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- Object result =
- evalRuleContextCode(
- ruleContext, "f = depset(ruleContext.files.srcs)", "cmd_helper.join_paths(':', f)");
+ setRuleContext(createRuleContext("//foo:foo"));
+ Object result = eval("f = depset(ruleContext.files.srcs)", "cmd_helper.join_paths(':', f)");
assertThat(result).isEqualTo("foo/a.txt:foo/b.img");
}
@Test
public void testStructPlusArtifactErrorMessage() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- checkErrorContains(
- ruleContext,
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"unsupported operand type(s) for +: 'File' and 'struct'",
"ruleContext.files.tools[0] + struct(a = 1)");
}
@Test
public void testNoSuchProviderErrorMessage() throws Exception {
- checkErrorContains(
- createRuleContext("//foo:bar"),
+ setRuleContext(createRuleContext("//foo:bar"));
+ checkEvalErrorContains(
"<target //foo:jl> (rule 'java_library') doesn't have provider 'my_provider'",
"ruleContext.attr.srcs[0].my_provider");
}
@Test
public void testFilesForRuleConfiguredTarget() throws Exception {
- Object result =
- evalRuleContextCode(createRuleContext("//foo:foo"), "ruleContext.attr.srcs[0].files");
+ setRuleContext(createRuleContext("//foo:foo"));
+ Object result = eval("ruleContext.attr.srcs[0].files");
assertThat(ActionsTestUtil.baseNamesOf(((SkylarkNestedSet) result).getSet(Artifact.class)))
.isEqualTo("a.txt");
}
@@ -1824,61 +1819,56 @@
@Test
public void testFilesForFileConfiguredTarget() throws Exception {
- Object result =
- evalRuleContextCode(createRuleContext("//foo:bar"), "ruleContext.attr.srcs[0].files");
+ setRuleContext(createRuleContext("//foo:bar"));
+ Object result = eval("ruleContext.attr.srcs[0].files");
assertThat(ActionsTestUtil.baseNamesOf(((SkylarkNestedSet) result).getSet(Artifact.class)))
.isEqualTo("libjl.jar");
}
@Test
public void testCtxStructFieldsCustomErrorMessages() throws Exception {
- checkErrorContains("No attribute 'foo' in attr.", "ruleContext.attr.foo");
- checkErrorContains("No attribute 'foo' in outputs.", "ruleContext.outputs.foo");
- checkErrorContains("No attribute 'foo' in files.", "ruleContext.files.foo");
- checkErrorContains("No attribute 'foo' in file.", "ruleContext.file.foo");
- checkErrorContains("No attribute 'foo' in executable.", "ruleContext.executable.foo");
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains("No attribute 'foo' in attr.", "ruleContext.attr.foo");
+ checkEvalErrorContains("No attribute 'foo' in outputs.", "ruleContext.outputs.foo");
+ checkEvalErrorContains("No attribute 'foo' in files.", "ruleContext.files.foo");
+ checkEvalErrorContains("No attribute 'foo' in file.", "ruleContext.file.foo");
+ checkEvalErrorContains("No attribute 'foo' in executable.", "ruleContext.executable.foo");
}
@Test
public void testBinDirPath() throws Exception {
SkylarkRuleContext ctx = createRuleContext("//foo:bar");
- Object result = evalRuleContextCode(ctx, "ruleContext.bin_dir.path");
+ setRuleContext(ctx);
+ Object result = eval("ruleContext.bin_dir.path");
assertThat(result).isEqualTo(ctx.getConfiguration().getBinFragment().getPathString());
}
@Test
public void testEmptyLabelListTypeAttrInCtx() throws Exception {
- SkylarkRuleContext ctx = createRuleContext("//foo:baz");
- Object result = evalRuleContextCode(ctx, "ruleContext.attr.srcs");
+ setRuleContext(createRuleContext("//foo:baz"));
+ Object result = eval("ruleContext.attr.srcs");
assertThat(result).isEqualTo(MutableList.empty());
}
@Test
public void testDefinedMakeVariable() throws Exception {
useConfiguration("--define=FOO=bar");
- SkylarkRuleContext ctx = createRuleContext("//foo:baz");
- String foo = (String) evalRuleContextCode(ctx, "ruleContext.var['FOO']");
+ setRuleContext(createRuleContext("//foo:baz"));
+ String foo = (String) eval("ruleContext.var['FOO']");
assertThat(foo).isEqualTo("bar");
}
@Test
public void testCodeCoverageConfigurationAccess() throws Exception {
SkylarkRuleContext ctx = createRuleContext("//foo:baz");
- boolean coverage =
- (Boolean) evalRuleContextCode(ctx, "ruleContext.configuration.coverage_enabled");
+ setRuleContext(ctx);
+ boolean coverage = (Boolean) eval("ruleContext.configuration.coverage_enabled");
assertThat(ctx.getRuleContext().getConfiguration().isCodeCoverageEnabled()).isEqualTo(coverage);
}
- @Override
- protected void checkErrorContains(String errorMsg, String... lines) throws Exception {
- super.checkErrorContains(createRuleContext("//foo:foo"), errorMsg, lines);
- }
-
- /**
- * Checks whether the given (invalid) statement leads to the expected error
- */
- private void checkReportedErrorStartsWith(
- SkylarkRuleContext ruleContext, String errorMsg, String... statements) throws Exception {
+ /** Checks whether the given (invalid) statement leads to the expected error */
+ private void checkReportedErrorStartsWith(String errorMsg, String... statements)
+ throws Exception {
// If the component under test relies on Reporter and EventCollector for error handling, any
// error would lead to an asynchronous AssertionFailedError thanks to failFastHandler in
// FoundationTestCase.
@@ -1886,7 +1876,7 @@
// Consequently, we disable failFastHandler and check all events for the expected error message
reporter.removeHandler(failFastHandler);
- Object result = evalRuleContextCode(ruleContext, statements);
+ Object result = eval(statements);
String first = null;
int count = 0;
@@ -2041,8 +2031,8 @@
@Test
public void testArgsScalarAdd() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"args = ruleContext.actions.args()",
"args.add('--foo')",
"args.add('-')",
@@ -2067,9 +2057,8 @@
@Test
public void testArgsScalarAddThrowsWithVectorArg() throws Exception {
setSkylarkSemanticsOptions("--incompatible_disallow_old_style_args_add=true");
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- checkErrorContains(
- ruleContext,
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"Args#add no longer accepts vectorized",
"args = ruleContext.actions.args()",
"args.add([1, 2])",
@@ -2084,8 +2073,8 @@
@Test
public void testArgsAddAll() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"args = ruleContext.actions.args()",
"args.add_all([1, 2])",
"args.add('-')",
@@ -2143,8 +2132,8 @@
@Test
public void testArgsAddAllWithMapEach() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"def add_one(val): return str(val + 1)",
"def expand_to_many(val): return ['hey', 'hey']",
"args = ruleContext.actions.args()",
@@ -2169,8 +2158,8 @@
@Test
public void testOmitIfEmpty() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"def add_one(val): return str(val + 1)",
"def filter(val): return None",
"args = ruleContext.actions.args()",
@@ -2212,8 +2201,8 @@
@Test
public void testUniquify() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"def add_one(val): return str(val + 1)",
"args = ruleContext.actions.args()",
"args.add_all(['a', 'b', 'a'])",
@@ -2237,8 +2226,8 @@
@Test
public void testArgsAddJoined() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"def add_one(val): return str(val + 1)",
"args = ruleContext.actions.args()",
"args.add_joined([1, 2], join_with=':')",
@@ -2283,8 +2272,8 @@
public void testLazyArgsLegacy() throws Exception {
setSkylarkSemanticsOptions("--incompatible_disallow_old_style_args_add=false");
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"def map_scalar(val): return 'mapped' + val",
"def map_vector(vals): return [x + 1 for x in vals]",
"args = ruleContext.actions.args()",
@@ -2338,8 +2327,8 @@
public void testLegacyLazyArgMapFnReturnsWrongArgumentCount() throws Exception {
setSkylarkSemanticsOptions("--incompatible_disallow_old_style_args_add=false");
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"args = ruleContext.actions.args()",
"def bad_fn(args): return [0]",
"args.add([1, 2], map_fn=bad_fn)",
@@ -2363,8 +2352,8 @@
@Test
public void testMultipleLazyArgsMixedWithStrings() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"foo_args = ruleContext.actions.args()",
"foo_args.add('--foo')",
"bar_args = ruleContext.actions.args()",
@@ -2411,8 +2400,8 @@
@Test
public void testWriteArgsToParamFile() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"args = ruleContext.actions.args()",
"args.add('--foo')",
"output=ruleContext.actions.declare_file('out')",
@@ -2431,13 +2420,11 @@
@Test
public void testLazyArgsWithParamFileInvalidFormatString() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- checkError(
- ruleContext,
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"Invalid value for parameter \"param_file_arg\": Expected string with a single \"--file=\"",
"args = ruleContext.actions.args()\n" + "args.use_param_file('--file=')");
- checkError(
- ruleContext,
+ checkEvalErrorContains(
"Invalid value for parameter \"param_file_arg\": "
+ "Expected string with a single \"--file=%s%s\"",
"args = ruleContext.actions.args()\n" + "args.use_param_file('--file=%s%s')");
@@ -2445,27 +2432,24 @@
@Test
public void testLazyArgsWithParamFileInvalidFormat() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- checkError(
- ruleContext,
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"Invalid value for parameter \"format\": Expected one of \"shell\", \"multiline\"",
"args = ruleContext.actions.args()\n" + "args.set_param_file_format('illegal')");
}
@Test
public void testScalarJoinWithErrorMessage() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- checkError(
- ruleContext,
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"'join_with' is not supported for scalar arguments",
"args = ruleContext.actions.args()\n" + "args.add(1, join_with=':')");
}
@Test
public void testScalarBeforeEachErrorMessage() throws Exception {
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- checkError(
- ruleContext,
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"'before_each' is not supported for scalar arguments",
"args = ruleContext.actions.args()\n" + "args.add(1, before_each='illegal')");
}
@@ -2473,24 +2457,20 @@
@Test
public void testArgsAddInvalidTypesForArgAndValues() throws Exception {
setSkylarkSemanticsOptions("--incompatible_disallow_old_style_args_add=true");
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- checkError(
- ruleContext,
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"expected value of type 'string' for arg name, got 'Integer'",
"args = ruleContext.actions.args()",
"args.add(1, 'value')");
- checkError(
- ruleContext,
+ checkEvalErrorContains(
"expected value of type 'string' for arg name, got 'Integer'",
"args = ruleContext.actions.args()",
"args.add_all(1, [1, 2])");
- checkError(
- ruleContext,
+ checkEvalErrorContains(
"expected value of type 'sequence or depset' for values, got 'Integer'",
"args = ruleContext.actions.args()",
"args.add_all(1)");
- checkErrorContains(
- ruleContext,
+ checkEvalErrorContains(
"expected value of type 'sequence or depset' for parameter 'values'",
"args = ruleContext.actions.args()",
"args.add_all('--foo', 1)");
@@ -2500,8 +2480,8 @@
public void testLazyArgIllegalLegacyFormatString() throws Exception {
setSkylarkSemanticsOptions("--incompatible_disallow_old_style_args_add=false");
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"args = ruleContext.actions.args()",
"args.add('foo', format='format/%s%s')", // Expects two args, will only be given one
"ruleContext.actions.run(",
@@ -2522,9 +2502,8 @@
@Test
public void testLazyArgIllegalFormatString() throws Exception {
setSkylarkSemanticsOptions("--incompatible_disallow_old_style_args_add=true");
- SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- checkError(
- ruleContext,
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"Invalid value for parameter \"format\": Expected string with a single \"%s\"",
"args = ruleContext.actions.args()",
"args.add('foo', format='illegal_format')", // Expects two args, will only be given one
@@ -2539,8 +2518,8 @@
@Test
public void testLazyArgMapEachWrongArgCount() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- checkErrorContains(
- ruleContext,
+ setRuleContext(ruleContext);
+ checkEvalErrorContains(
"map_each must be a function that accepts a single",
"args = ruleContext.actions.args()",
"def bad_fn(val, val2): return str(val)",
@@ -2556,8 +2535,8 @@
@Test
public void testLazyArgMapEachThrowsError() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"args = ruleContext.actions.args()",
"def bad_fn(val): 'hello'.nosuchmethod()",
"args.add_all([1, 2], map_each=bad_fn)",
@@ -2579,8 +2558,8 @@
@Test
public void testLazyArgMapEachReturnsNone() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"args = ruleContext.actions.args()",
"def none_fn(val): return None if val == 'nokeep' else val",
"args.add_all(['keep', 'nokeep'], map_each=none_fn)",
@@ -2600,8 +2579,8 @@
@Test
public void testLazyArgMapEachReturnsWrongType() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"args = ruleContext.actions.args()",
"def bad_fn(val): return 1",
"args.add_all([1, 2], map_each=bad_fn)",
@@ -2624,8 +2603,8 @@
@Test
public void createShellWithLazyArgs() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
- evalRuleContextCode(
- ruleContext,
+ setRuleContext(ruleContext);
+ eval(
"args = ruleContext.actions.args()",
"args.add('--foo')",
"ruleContext.actions.run_shell(",
@@ -2944,7 +2923,10 @@
@Test
public void testSkylarkCustomCommandLineKeyComputation() throws Exception {
+ setRuleContext(createRuleContext("//foo:foo"));
+
ImmutableList.Builder<SkylarkCustomCommandLine> commandLines = ImmutableList.builder();
+
commandLines.add(getCommandLine("ruleContext.actions.args()"));
commandLines.add(
getCommandLine("args = ruleContext.actions.args()", "args.add('foo')", "args"));
@@ -3053,24 +3035,25 @@
}
private SkylarkCustomCommandLine getCommandLine(String... lines) throws Exception {
- return ((SkylarkActionFactory.Args) evalRuleContextCode(lines)).build();
+ return ((SkylarkActionFactory.Args) eval(lines)).build();
}
@Test
public void testPrintArgs() throws Exception {
+ setRuleContext(createRuleContext("//foo:foo"));
Args args =
(Args)
- evalRuleContextCode(
- "args = ruleContext.actions.args()", "args.add_all(['--foo', '--bar'])", "args");
+ eval("args = ruleContext.actions.args()", "args.add_all(['--foo', '--bar'])", "args");
assertThat(Printer.debugPrint(args)).isEqualTo("--foo --bar");
}
@Test
public void testDirectoryInArgs() throws Exception {
setSkylarkSemanticsOptions("--incompatible_expand_directories");
+ setRuleContext(createRuleContext("//foo:foo"));
SkylarkList<?> result =
(SkylarkList<?>)
- evalRuleContextCode(
+ eval(
"args = ruleContext.actions.args()",
"directory = ruleContext.actions.declare_directory('dir')",
"def _short_path(f): return f.short_path", // For easier assertions
@@ -3096,9 +3079,10 @@
@Test
public void testDirectoryInArgsIncompatibleFlagOff() throws Exception {
setSkylarkSemanticsOptions("--noincompatible_expand_directories");
+ setRuleContext(createRuleContext("//foo:foo"));
SkylarkList<?> result =
(SkylarkList<?>)
- evalRuleContextCode(
+ eval(
"args = ruleContext.actions.args()",
"directory = ruleContext.actions.declare_directory('dir')",
"def _short_path(f): return f.short_path", // For easier assertions
@@ -3120,9 +3104,10 @@
@Test
public void testDirectoryInArgsExpandDirectories() throws Exception {
setSkylarkSemanticsOptions("--incompatible_expand_directories");
+ setRuleContext(createRuleContext("//foo:foo"));
SkylarkList<?> result =
(SkylarkList<?>)
- evalRuleContextCode(
+ eval(
"args = ruleContext.actions.args()",
"directory = ruleContext.actions.declare_directory('dir')",
"def _short_path(f): return f.short_path", // For easier assertions
@@ -3146,7 +3131,8 @@
@Test
public void testDirectoryInScalarArgsFails() throws Exception {
setSkylarkSemanticsOptions("--incompatible_expand_directories");
- checkErrorContains(
+ setRuleContext(createRuleContext("//foo:foo"));
+ checkEvalErrorContains(
"Cannot add directories to Args#add",
"args = ruleContext.actions.args()",
"directory = ruleContext.actions.declare_directory('dir')",
@@ -3156,9 +3142,10 @@
@Test
public void testDirectoryInScalarArgsIsOkWithoutIncompatibleFlag() throws Exception {
setSkylarkSemanticsOptions("--noincompatible_expand_directories");
+ setRuleContext(createRuleContext("//foo:foo"));
Args args =
(Args)
- evalRuleContextCode(
+ eval(
"args = ruleContext.actions.args()",
"directory = ruleContext.actions.declare_directory('dir')",
"args.add(directory)",
@@ -3169,11 +3156,11 @@
@Test
public void testParamFileHasDirectoryAsInput() throws Exception {
setSkylarkSemanticsOptions("--incompatible_expand_directories");
- SkylarkRuleContext ctx = dummyRuleContext();
+ SkylarkRuleContext ctx = createRuleContext("//foo:foo");
+ setRuleContext(ctx);
SkylarkList<?> result =
(SkylarkList<?>)
- evalRuleContextCode(
- ctx,
+ eval(
"args = ruleContext.actions.args()",
"directory = ruleContext.actions.declare_directory('dir')",
"args.add_all([directory])",
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/util/SkylarkTestCase.java b/src/test/java/com/google/devtools/build/lib/skylark/util/SkylarkTestCase.java
index 71c3622..9490885 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/util/SkylarkTestCase.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/util/SkylarkTestCase.java
@@ -14,26 +14,19 @@
package com.google.devtools.build.lib.skylark.util;
-import static com.google.common.truth.Truth.assertThat;
-import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
-import com.google.common.base.Joiner;
-import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Lists;
-import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.skylark.SkylarkModules;
import com.google.devtools.build.lib.analysis.skylark.SkylarkRuleContext;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.packages.BazelStarlarkContext;
import com.google.devtools.build.lib.packages.SymbolGenerator;
import com.google.devtools.build.lib.rules.platform.PlatformCommon;
-import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.Runtime;
import com.google.devtools.build.lib.syntax.SkylarkUtils;
import com.google.devtools.build.lib.syntax.SkylarkUtils.Phase;
-import com.google.devtools.build.lib.syntax.StarlarkSemantics;
import com.google.devtools.build.lib.syntax.StarlarkThread;
import com.google.devtools.build.lib.syntax.StarlarkThread.GlobalFrame;
import com.google.devtools.build.lib.syntax.util.EvaluationTestCase;
@@ -41,151 +34,84 @@
import org.junit.Before;
/**
- * A class to contain the common functionality for Skylark tests.
+ * A class to contain the common functionality for analysis-phase Skylark tests. The less stuff
+ * here, the better.
*/
public abstract class SkylarkTestCase extends BuildViewTestCase {
- // We don't have multiple inheritance, so we fake it.
protected EvaluationTestCase ev;
+ // Subclasses must call this method after change SkylarkSemantics (among other things).
@Before
- public final void setUpEvaluator() throws Exception {
- ev = createEvaluationTestCase(StarlarkSemantics.DEFAULT_SEMANTICS);
- ev.initialize();
+ public final void reset() throws Exception {
+ ev = createEvaluationTestCase();
}
- private static final StarlarkThread.GlobalFrame getSkylarkGlobals() {
- ImmutableMap.Builder<String, Object> envBuilder = ImmutableMap.builder();
+ private EvaluationTestCase createEvaluationTestCase() throws Exception {
+ // Set up globals.
+ ImmutableMap.Builder<String, Object> env = ImmutableMap.builder();
+ SkylarkModules.addSkylarkGlobalsToBuilder(env);
+ Runtime.setupSkylarkLibrary(env, new PlatformCommon());
+ GlobalFrame globals = GlobalFrame.createForBuiltins(env.build());
- SkylarkModules.addSkylarkGlobalsToBuilder(envBuilder);
- Runtime.setupSkylarkLibrary(envBuilder, new PlatformCommon());
-
- return GlobalFrame.createForBuiltins(envBuilder.build());
- }
-
- protected EvaluationTestCase createEvaluationTestCase(StarlarkSemantics semantics) {
- return new EvaluationTestCase() {
- @Override
- public StarlarkThread newStarlarkThread() throws Exception {
- StarlarkThread thread =
- StarlarkThread.builder(mutability)
- .setSemantics(semantics)
- .setEventHandler(getEventHandler())
- .setGlobals(
- getSkylarkGlobals()
- .withLabel(
+ EvaluationTestCase ev =
+ new EvaluationTestCase() {
+ @Override
+ public StarlarkThread newStarlarkThread() throws Exception {
+ StarlarkThread thread =
+ StarlarkThread.builder(mutability)
+ .setSemantics(getSkylarkSemantics())
+ .setEventHandler(getEventHandler())
+ .setGlobals(
+ globals.withLabel(
Label.parseAbsoluteUnchecked("//test:label", /*defaultToMain=*/ false)))
- .build();
+ .build();
- SkylarkUtils.setPhase(thread, Phase.LOADING);
+ SkylarkUtils.setPhase(thread, Phase.LOADING);
- // This StarlarkThread has no PackageContext, so attempts to create a rule will fail.
- // Rule creation is tested by SkylarkIntegrationTest.
+ // This StarlarkThread has no PackageContext, so attempts to create a rule will fail.
+ // Rule creation is tested by SkylarkIntegrationTest.
- new BazelStarlarkContext(
- TestConstants.TOOLS_REPOSITORY,
- /*fragmentNameToClass=*/ null,
- /*repoMapping=*/ ImmutableMap.of(),
- new SymbolGenerator<>(new Object()),
- /*analysisRuleLabel=*/ null)
- .storeInThread(thread);
+ new BazelStarlarkContext(
+ TestConstants.TOOLS_REPOSITORY,
+ /*fragmentNameToClass=*/ null,
+ /*repoMapping=*/ ImmutableMap.of(),
+ new SymbolGenerator<>(new Object()),
+ /*analysisRuleLabel=*/ null)
+ .storeInThread(thread);
- return thread;
- }
- };
+ return thread;
+ }
+ };
+ ev.initialize();
+ return ev;
}
- protected Object eval(String... input) throws Exception {
+ protected final Object eval(String... input) throws Exception {
return ev.eval(input);
}
- protected void update(String name, Object value) throws Exception {
+ protected final void update(String name, Object value) throws Exception {
ev.update(name, value);
}
- protected Object lookup(String name) throws Exception {
+ protected final Object lookup(String name) throws Exception {
return ev.lookup(name);
}
- protected void checkEvalError(String msg, String... input) throws Exception {
- ev.checkEvalError(msg, input);
- }
-
- protected void checkEvalErrorContains(String msg, String... input) throws Exception {
+ protected final void checkEvalErrorContains(String msg, String... input) throws Exception {
ev.checkEvalErrorContains(msg, input);
}
- protected SkylarkRuleContext dummyRuleContext() throws Exception {
- return createRuleContext("//foo:foo");
- }
-
- protected SkylarkRuleContext createRuleContext(String label) throws Exception {
+ protected final SkylarkRuleContext createRuleContext(String label) throws Exception {
return new SkylarkRuleContext(getRuleContextForSkylark(getConfiguredTarget(label)), null,
getSkylarkSemantics());
}
- protected Object evalRuleContextCode(String... lines) throws Exception {
- return evalRuleContextCode(dummyRuleContext(), lines);
+ // Disable BuildViewTestCase's overload to avoid unintended calls.
+ @Override
+ protected final Event checkError(String x, String y) throws Exception {
+ throw new IllegalStateException("wrong method");
}
- /**
- * RuleContext can't be null, SkylarkBuiltInFunctions checks it.
- * However not all built in functions use it, if usesRuleContext == false
- * the wrapping function won't need a ruleContext parameter.
- */
- protected Object evalRuleContextCode(SkylarkRuleContext ruleContext, String... code)
- throws Exception {
- setUpEvaluator();
- if (ruleContext != null) {
- update("ruleContext", ruleContext);
- }
- return eval(code);
- }
-
- protected void assertArtifactFilenames(Iterable<Artifact> artifacts, String... expected) {
- ImmutableList.Builder<String> artifactFilenames = ImmutableList.builder();
- for (Artifact artifact : artifacts) {
- artifactFilenames.add(artifact.getFilename());
- }
- assertThat(artifactFilenames.build()).containsAtLeastElementsIn(Lists.newArrayList(expected));
- }
-
- protected Object evalRuleClassCode(String... lines) throws Exception {
- setUpEvaluator();
- return eval("def impl(ctx): return None\n" + Joiner.on("\n").join(lines));
- }
-
- protected Object evalRuleClassCode(StarlarkSemantics semantics, String... lines)
- throws Exception {
- ev = createEvaluationTestCase(semantics);
- ev.initialize();
- return eval("def impl(ctx): return None\n" + Joiner.on("\n").join(lines));
- }
-
- protected void checkError(SkylarkRuleContext ruleContext, String errorMsg, String... lines)
- throws Exception {
- EvalException e =
- assertThrows(EvalException.class, () -> evalRuleContextCode(ruleContext, lines));
- assertThat(e).hasMessageThat().isEqualTo(errorMsg);
- }
-
- protected void checkErrorStartsWith(
- SkylarkRuleContext ruleContext, String errorMsg, String... lines) throws Exception {
- EvalException e =
- assertThrows(EvalException.class, () -> evalRuleContextCode(ruleContext, lines));
- assertThat(e).hasMessageThat().startsWith(errorMsg);
- }
-
- protected void checkErrorContains(String errorMsg, String... lines) throws Exception {
- ev.setFailFast(false);
- EvalException e = assertThrows(EvalException.class, () -> eval(lines));
- assertThat(e).hasMessageThat().contains(errorMsg);
- }
-
- protected void checkErrorContains(
- SkylarkRuleContext ruleContext, String errorMsg, String... lines) throws Exception {
- EvalException e =
- assertThrows(EvalException.class, () -> evalRuleContextCode(ruleContext, lines));
- assertThat(e).hasMessageThat().contains(errorMsg);
- }
}
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkNestedSetTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkNestedSetTest.java
index fa72e3e..6318768 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkNestedSetTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkNestedSetTest.java
@@ -606,6 +606,12 @@
.testIfErrorContains("depset exceeded maximum depth 2000", "create_depset(3000)");
}
+ @Test
+ public void testListComprehensionsWithNestedSet() throws Exception {
+ new SkylarkTest("--incompatible_depset_is_not_iterable=false")
+ .testEval("[x + x for x in depset([1, 2, 3])]", "[2, 4, 6]");
+ }
+
private interface MergeStrategy {
SkylarkNestedSet merge(SkylarkNestedSet[] sets) throws Exception;
}