bazel syntax: use EvalUtils.{exec,eval} instead of execOrEval
execOrEval, now renamed execWithOptionalFinalExpression, accepts a
sequence of statements optionally followed by an expression. It is
appropriate for use in an interactive UI such as the REPL or debugger,
but should not be used elsewhere.
This change makes all non-REPL callers choose either to execute statements
for their effects, or to evaluate an expression for its value.
PiperOrigin-RevId: 274594764
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 43bdbc7..f9dc053 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
@@ -136,7 +136,7 @@
}
private void registerDummyStarlarkFunction() throws Exception {
- eval("def impl():", " pass");
+ exec("def impl():", " pass");
}
@Test
@@ -689,6 +689,7 @@
assertThat(c.hasAttr("a1", Type.STRING)).isTrue();
}
+ // TODO(adonovan): rename execAndExport
private void evalAndExport(String... lines) throws Exception {
ParserInput input = ParserInput.fromLines(lines);
StarlarkFile file = EvalUtils.parseAndValidateSkylark(input, ev.getStarlarkThread());
@@ -1071,14 +1072,14 @@
@Test
public void testStructCreation() throws Exception {
// TODO(fwe): cannot be handled by current testing suite
- eval("x = struct(a = 1, b = 2)");
+ exec("x = struct(a = 1, b = 2)");
assertThat(lookup("x")).isInstanceOf(ClassObject.class);
}
@Test
public void testStructFields() throws Exception {
// TODO(fwe): cannot be handled by current testing suite
- eval("x = struct(a = 1, b = 2)");
+ exec("x = struct(a = 1, b = 2)");
ClassObject x = (ClassObject) lookup("x");
assertThat(x.getValue("a")).isEqualTo(1);
assertThat(x.getValue("b")).isEqualTo(2);
@@ -1090,14 +1091,15 @@
assertThat((Boolean) eval("struct(a = 1) == struct(a = 1, b = 2)")).isFalse();
assertThat((Boolean) eval("struct(a = 1, b = 2) == struct(a = 1)")).isFalse();
// Compare a recursive object to itself to make sure reference equality is checked
- assertThat((Boolean) eval("s = (struct(a = 1, b = [])); s.b.append(s); s == s")).isTrue();
+ exec("s = struct(a = 1, b = []); s.b.append(s)");
+ assertThat((Boolean) eval("s == s")).isTrue();
assertThat((Boolean) eval("struct(a = 1, b = 2) == struct(a = 1, b = 3)")).isFalse();
assertThat((Boolean) eval("struct(a = 1) == [1]")).isFalse();
assertThat((Boolean) eval("[1] == struct(a = 1)")).isFalse();
assertThat((Boolean) eval("struct() == struct()")).isTrue();
assertThat((Boolean) eval("struct() == struct(a = 1)")).isFalse();
- eval("foo = provider(); bar = provider()");
+ exec("foo = provider(); bar = provider()");
assertThat((Boolean) eval("struct(a = 1) == foo(a = 1)")).isFalse();
assertThat((Boolean) eval("foo(a = 1) == struct(a = 1)")).isFalse();
assertThat((Boolean) eval("foo(a = 1) == bar(a = 1)")).isFalse();
@@ -1114,7 +1116,7 @@
@Test
public void testStructAccessingFieldsFromSkylark() throws Exception {
- eval("x = struct(a = 1, b = 2)", "x1 = x.a", "x2 = x.b");
+ exec("x = struct(a = 1, b = 2)", "x1 = x.a", "x2 = x.b");
assertThat(lookup("x1")).isEqualTo(1);
assertThat(lookup("x2")).isEqualTo(2);
}
@@ -1141,7 +1143,7 @@
@Test
public void testStructAccessingFunctionFieldWithArgs() throws Exception {
- eval("def f(x): return x+5", "x = struct(a = f, b = 2)", "x1 = x.a(1)");
+ exec("def f(x): return x+5", "x = struct(a = f, b = 2)", "x1 = x.a(1)");
assertThat(lookup("x1")).isEqualTo(6);
}
@@ -1154,7 +1156,8 @@
@Test
public void testStructConcatenationFieldNames() throws Exception {
// TODO(fwe): cannot be handled by current testing suite
- eval("x = struct(a = 1, b = 2)",
+ exec(
+ "x = struct(a = 1, b = 2)", //
"y = struct(c = 1, d = 2)",
"z = x + y\n");
StructImpl z = (StructImpl) lookup("z");
@@ -1164,7 +1167,8 @@
@Test
public void testStructConcatenationFieldValues() throws Exception {
// TODO(fwe): cannot be handled by current testing suite
- eval("x = struct(a = 1, b = 2)",
+ exec(
+ "x = struct(a = 1, b = 2)", //
"y = struct(c = 1, d = 2)",
"z = x + y\n");
StructImpl z = (StructImpl) lookup("z");
@@ -1186,7 +1190,8 @@
@Test
public void testConditionalStructConcatenation() throws Exception {
// TODO(fwe): cannot be handled by current testing suite
- eval("def func():",
+ exec(
+ "def func():",
" x = struct(a = 1, b = 2)",
" if True:",
" x += struct(c = 1, d = 2)",
@@ -1209,14 +1214,15 @@
@Test
public void testGetattr() throws Exception {
- eval("s = struct(a='val')", "x = getattr(s, 'a')", "y = getattr(s, 'b', 'def')");
+ exec("s = struct(a='val')", "x = getattr(s, 'a')", "y = getattr(s, 'b', 'def')");
assertThat(lookup("x")).isEqualTo("val");
assertThat(lookup("y")).isEqualTo("def");
}
@Test
public void testHasattr() throws Exception {
- eval("s = struct(a=1)",
+ exec(
+ "s = struct(a=1)", //
"x = hasattr(s, 'a')",
"y = hasattr(s, 'b')\n");
assertThat(lookup("x")).isEqualTo(true);
@@ -1231,12 +1237,12 @@
@Test
public void testStructsInSets() throws Exception {
- eval("depset([struct(a='a')])");
+ exec("depset([struct(a='a')])");
}
@Test
public void testStructsInDicts() throws Exception {
- eval("d = {struct(a = 1): 'aa', struct(b = 2): 'bb'}");
+ exec("d = {struct(a = 1): 'aa', struct(b = 2): 'bb'}");
assertThat(eval("d[struct(a = 1)]")).isEqualTo("aa");
assertThat(eval("d[struct(b = 2)]")).isEqualTo("bb");
assertThat(eval("str([d[k] for k in d])")).isEqualTo("[\"aa\", \"bb\"]");
@@ -1246,15 +1252,15 @@
@Test
public void testStructDictMembersAreMutable() throws Exception {
- eval(
- "s = struct(x = {'a' : 1})",
+ exec(
+ "s = struct(x = {'a' : 1})", //
"s.x['b'] = 2\n");
assertThat(((StructImpl) lookup("s")).getValue("x")).isEqualTo(ImmutableMap.of("a", 1, "b", 2));
}
@Test
public void testNsetGoodCompositeItem() throws Exception {
- eval("def func():", " return depset([struct(a='a')])", "s = func()");
+ exec("def func():", " return depset([struct(a='a')])", "s = func()");
Collection<?> result = ((SkylarkNestedSet) lookup("s")).toCollection();
assertThat(result).hasSize(1);
assertThat(result.iterator().next()).isInstanceOf(StructImpl.class);
@@ -1808,7 +1814,7 @@
@Test
public void testTypeOfStruct() throws Exception {
- eval("p = type(struct)", "s = type(struct())");
+ exec("p = type(struct)", "s = type(struct())");
assertThat(lookup("p")).isEqualTo("Provider");
assertThat(lookup("s")).isEqualTo("struct");
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 f336712..59b4b9a 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
@@ -692,7 +692,7 @@
public void testCreateSpawnActionArgumentsWithExecutableFilesToRunProvider() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:androidlib");
setRuleContext(ruleContext);
- eval(
+ exec(
"ruleContext.actions.run(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
@@ -709,7 +709,7 @@
public void testCreateStarlarkActionArgumentsWithUnusedInputsList() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"ruleContext.actions.run(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
@@ -729,7 +729,7 @@
public void testCreateStarlarkActionArgumentsWithoutUnusedInputsList() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"ruleContext.actions.run(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
@@ -905,8 +905,7 @@
@Test
public void testDeriveTreeArtifactType() throws Exception {
setRuleContext(createRuleContext("//foo:foo"));
- Object result = eval("b = ruleContext.actions.declare_directory('a/b')\n" + "type(b)");
- assertThat(result).isInstanceOf(String.class);
+ String result = (String) eval("type(ruleContext.actions.declare_directory('a/b'))");
assertThat(result).isEqualTo("File");
}
@@ -914,11 +913,11 @@
@Test
public void testDeriveTreeArtifactNextToSibling() throws Exception {
setRuleContext(createRuleContext("//foo:foo"));
- Object result =
- eval(
- "b = ruleContext.actions.declare_directory('a/b')\n"
- + "ruleContext.actions.declare_directory('c', sibling=b)");
- Artifact artifact = (Artifact) result;
+ Artifact artifact =
+ (Artifact)
+ eval(
+ "ruleContext.actions.declare_directory('c',"
+ + " sibling=ruleContext.actions.declare_directory('a/b'))");
PathFragment fragment = artifact.getRootRelativePath();
assertThat(fragment.getPathString()).isEqualTo("foo/a/c");
assertThat(artifact.isTreeArtifact()).isTrue();
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 c7c5909..99425da 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
@@ -203,7 +203,7 @@
.getDeclaredField("mockFunc")
.getAnnotation(SkylarkSignature.class));
update("mock", mockFunc);
- eval(line);
+ exec(line);
}
private void checkSkylarkFunctionError(String errorMsg, String line) throws Exception {
@@ -303,7 +303,7 @@
public void testCreateSpawnActionArgumentsWithExecutable() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"ruleContext.actions.run(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
@@ -323,7 +323,7 @@
// Same test as above, with depset as inputs.
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"ruleContext.actions.run(",
" inputs = depset(ruleContext.files.srcs),",
" outputs = ruleContext.files.srcs,",
@@ -355,7 +355,7 @@
public void testCreateSpawnActionShellCommandList() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"ruleContext.actions.run_shell(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
@@ -375,7 +375,7 @@
public void testCreateSpawnActionEnvAndExecInfo() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"ruleContext.actions.run_shell(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
@@ -535,7 +535,7 @@
public void testCreateFileAction() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"ruleContext.actions.write(",
" output = ruleContext.files.srcs[0],",
" content = 'hello world',",
@@ -666,7 +666,7 @@
@Test
public void testResolveCommandMakeVariables() throws Exception {
setRuleContext(createRuleContext("//foo:resolve_me"));
- eval(
+ exec(
"inputs, argv, manifests = ruleContext.resolve_command(",
" command='I got the $(HELLO) on a $(DAVE)', ",
" make_variables={'HELLO': 'World', 'DAVE': type('')})");
@@ -681,7 +681,7 @@
@Test
public void testResolveCommandInputs() throws Exception {
setRuleContext(createRuleContext("//foo:resolve_me"));
- eval(
+ exec(
"inputs, argv, input_manifests = ruleContext.resolve_command(",
" tools=ruleContext.attr.tools)");
@SuppressWarnings("unchecked")
@@ -701,7 +701,7 @@
@Test
public void testResolveCommandExpandLocations() throws Exception {
setRuleContext(createRuleContext("//foo:resolve_me"));
- eval(
+ exec(
"def foo():", // no for loops at top-level
" label_dict = {}",
" all = []",
@@ -724,7 +724,7 @@
public void testResolveCommandExecutionRequirements() throws Exception {
// Tests that requires-darwin execution requirements result in the usage of /bin/bash.
setRuleContext(createRuleContext("//foo:resolve_me"));
- eval(
+ exec(
"inputs, argv, manifests = ruleContext.resolve_command(",
" execution_requirements={'requires-darwin': ''})");
@SuppressWarnings("unchecked")
@@ -735,7 +735,7 @@
@Test
public void testResolveCommandScript() throws Exception {
setRuleContext(createRuleContext("//foo:resolve_me"));
- eval(
+ exec(
"def foo():", // no for loops at top-level
" s = 'a'",
" for i in range(1,17): s = s + s", // 2**17 > CommandHelper.maxCommandLength (=64000)
@@ -753,7 +753,7 @@
public void testResolveTools() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:resolve_me");
setRuleContext(ruleContext);
- eval(
+ exec(
"inputs, input_manifests = ruleContext.resolve_tools(tools=ruleContext.attr.tools)",
"ruleContext.actions.run(",
" outputs = [ruleContext.actions.declare_file('x.out')],",
@@ -799,7 +799,7 @@
public void testCreateTemplateAction() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"ruleContext.actions.expand_template(",
" template = ruleContext.files.srcs[0],",
" output = ruleContext.files.srcs[1],",
@@ -836,7 +836,7 @@
Charset utf8 = StandardCharsets.UTF_8;
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"ruleContext.actions.expand_template(",
" template = ruleContext.files.srcs[0],",
" output = ruleContext.files.srcs[1],",
@@ -894,16 +894,14 @@
@Test
public void testRunfilesArtifactsFromArtifact() throws Exception {
setRuleContext(createRuleContext("//foo:foo"));
- Object result =
- eval("artifacts = ruleContext.files.tools", "ruleContext.runfiles(files = artifacts)");
+ Object result = eval("ruleContext.runfiles(files = ruleContext.files.tools)");
assertThat(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result))).contains("t.exe");
}
@Test
public void testRunfilesArtifactsFromIterableArtifacts() throws Exception {
setRuleContext(createRuleContext("//foo:foo"));
- Object result =
- eval("artifacts = ruleContext.files.srcs", "ruleContext.runfiles(files = artifacts)");
+ Object result = eval("ruleContext.runfiles(files = ruleContext.files.srcs)");
assertThat(ImmutableList.of("a.txt", "b.img"))
.isEqualTo(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result)));
}
@@ -911,9 +909,7 @@
@Test
public void testRunfilesArtifactsFromNestedSetArtifacts() throws Exception {
setRuleContext(createRuleContext("//foo:foo"));
- Object result =
- eval(
- "ftb = depset(ruleContext.files.srcs)", "ruleContext.runfiles(transitive_files = ftb)");
+ Object result = eval("ruleContext.runfiles(transitive_files = depset(ruleContext.files.srcs))");
assertThat(ImmutableList.of("a.txt", "b.img"))
.isEqualTo(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result)));
}
@@ -921,12 +917,10 @@
@Test
public void testRunfilesArtifactsFromDefaultAndFiles() throws Exception {
setRuleContext(createRuleContext("//foo:bar"));
+ // It would be nice to write [DEFAULT] + ruleContext.files.srcs, but artifacts
+ // is an ImmutableList and Skylark interprets it as a tuple.
Object result =
- 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.
- "ruleContext.runfiles(collect_default = True, files = artifacts)");
+ eval("ruleContext.runfiles(collect_default = True, files = ruleContext.files.srcs)");
// From DEFAULT only libjl.jar comes, see testRunfilesAddFromDependencies().
assertThat(ImmutableList.of("libjl.jar", "gl.a", "gl.gcgox"))
.isEqualTo(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result)));
@@ -935,10 +929,7 @@
@Test
public void testRunfilesArtifactsFromSymlink() throws Exception {
setRuleContext(createRuleContext("//foo:foo"));
- Object result =
- eval(
- "artifacts = ruleContext.files.srcs",
- "ruleContext.runfiles(symlinks = {'sym1': artifacts[0]})");
+ Object result = eval("ruleContext.runfiles(symlinks = {'sym1': ruleContext.files.srcs[0]})");
assertThat(ImmutableList.of("a.txt"))
.isEqualTo(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result)));
}
@@ -947,9 +938,7 @@
public void testRunfilesArtifactsFromRootSymlink() throws Exception {
setRuleContext(createRuleContext("//foo:foo"));
Object result =
- eval(
- "artifacts = ruleContext.files.srcs",
- "ruleContext.runfiles(root_symlinks = {'sym1': artifacts[0]})");
+ eval("ruleContext.runfiles(root_symlinks = {'sym1': ruleContext.files.srcs[0]})");
assertThat(ImmutableList.of("a.txt"))
.isEqualTo(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result)));
}
@@ -958,13 +947,12 @@
public void testRunfilesSymlinkConflict() throws Exception {
// Two different artifacts mapped to same path in runfiles
setRuleContext(createRuleContext("//foo:foo"));
+ exec("prefix = ruleContext.workspace_name + '/' if ruleContext.workspace_name else ''");
Object result =
eval(
- "artifacts = ruleContext.files.srcs",
- "prefix = ruleContext.workspace_name + '/' if ruleContext.workspace_name else ''",
"ruleContext.runfiles(",
- "root_symlinks = {prefix + 'sym1': artifacts[0]},",
- "symlinks = {'sym1': artifacts[1]})");
+ " root_symlinks = {prefix + 'sym1': ruleContext.files.srcs[0]},",
+ " symlinks = {'sym1': ruleContext.files.srcs[1]})");
Runfiles runfiles = (Runfiles) result;
reporter.removeHandler(failFastHandler); // So it doesn't throw an exception.
runfiles.getRunfilesInputs(reporter, null, ArtifactPathResolver.IDENTITY);
@@ -993,7 +981,7 @@
@Test
public void testCmdJoinPaths() throws Exception {
setRuleContext(createRuleContext("//foo:foo"));
- Object result = eval("f = depset(ruleContext.files.srcs)", "cmd_helper.join_paths(':', f)");
+ Object result = eval("cmd_helper.join_paths(':', depset(ruleContext.files.srcs))");
assertThat(result).isEqualTo("foo/a.txt:foo/b.img");
}
@@ -2032,7 +2020,7 @@
public void testArgsScalarAdd() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"args = ruleContext.actions.args()",
"args.add('--foo')",
"args.add('-')",
@@ -2074,7 +2062,7 @@
public void testArgsAddAll() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"args = ruleContext.actions.args()",
"args.add_all([1, 2])",
"args.add('-')",
@@ -2133,7 +2121,7 @@
public void testArgsAddAllWithMapEach() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"def add_one(val): return str(val + 1)",
"def expand_to_many(val): return ['hey', 'hey']",
"args = ruleContext.actions.args()",
@@ -2159,7 +2147,7 @@
public void testOmitIfEmpty() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"def add_one(val): return str(val + 1)",
"def filter(val): return None",
"args = ruleContext.actions.args()",
@@ -2202,7 +2190,7 @@
public void testUniquify() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"def add_one(val): return str(val + 1)",
"args = ruleContext.actions.args()",
"args.add_all(['a', 'b', 'a'])",
@@ -2227,7 +2215,7 @@
public void testArgsAddJoined() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"def add_one(val): return str(val + 1)",
"args = ruleContext.actions.args()",
"args.add_joined([1, 2], join_with=':')",
@@ -2273,7 +2261,7 @@
setSkylarkSemanticsOptions("--incompatible_disallow_old_style_args_add=false");
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"def map_scalar(val): return 'mapped' + val",
"def map_vector(vals): return [x + 1 for x in vals]",
"args = ruleContext.actions.args()",
@@ -2328,7 +2316,7 @@
setSkylarkSemanticsOptions("--incompatible_disallow_old_style_args_add=false");
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"args = ruleContext.actions.args()",
"def bad_fn(args): return [0]",
"args.add([1, 2], map_fn=bad_fn)",
@@ -2353,7 +2341,7 @@
public void testMultipleLazyArgsMixedWithStrings() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"foo_args = ruleContext.actions.args()",
"foo_args.add('--foo')",
"bar_args = ruleContext.actions.args()",
@@ -2401,7 +2389,7 @@
public void testWriteArgsToParamFile() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"args = ruleContext.actions.args()",
"args.add('--foo')",
"output=ruleContext.actions.declare_file('out')",
@@ -2481,7 +2469,7 @@
setSkylarkSemanticsOptions("--incompatible_disallow_old_style_args_add=false");
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"args = ruleContext.actions.args()",
"args.add('foo', format='format/%s%s')", // Expects two args, will only be given one
"ruleContext.actions.run(",
@@ -2536,7 +2524,7 @@
public void testLazyArgMapEachThrowsError() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"args = ruleContext.actions.args()",
"def bad_fn(val): 'hello'.nosuchmethod()",
"args.add_all([1, 2], map_each=bad_fn)",
@@ -2559,7 +2547,7 @@
public void testLazyArgMapEachReturnsNone() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"args = ruleContext.actions.args()",
"def none_fn(val): return None if val == 'nokeep' else val",
"args.add_all(['keep', 'nokeep'], map_each=none_fn)",
@@ -2580,7 +2568,7 @@
public void testLazyArgMapEachReturnsWrongType() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"args = ruleContext.actions.args()",
"def bad_fn(val): return 1",
"args.add_all([1, 2], map_each=bad_fn)",
@@ -2604,7 +2592,7 @@
public void createShellWithLazyArgs() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
setRuleContext(ruleContext);
- eval(
+ exec(
"args = ruleContext.actions.args()",
"args.add('--foo')",
"ruleContext.actions.run_shell(",
@@ -2927,81 +2915,64 @@
ImmutableList.Builder<SkylarkCustomCommandLine> commandLines = ImmutableList.builder();
- commandLines.add(getCommandLine("ruleContext.actions.args()"));
+ commandLines.add(getCommandLine("args = ruleContext.actions.args()"));
+ commandLines.add(getCommandLine("args = ruleContext.actions.args()", "args.add('foo')"));
commandLines.add(
- getCommandLine("args = ruleContext.actions.args()", "args.add('foo')", "args"));
+ getCommandLine("args = ruleContext.actions.args()", "args.add('--foo', 'foo')"));
commandLines.add(
- getCommandLine("args = ruleContext.actions.args()", "args.add('--foo', 'foo')", "args"));
+ getCommandLine("args = ruleContext.actions.args()", "args.add('foo', format='--foo=%s')"));
+ commandLines.add(
+ getCommandLine("args = ruleContext.actions.args()", "args.add_all(['foo', 'bar'])"));
commandLines.add(
getCommandLine(
- "args = ruleContext.actions.args()", "args.add('foo', format='--foo=%s')", "args"));
- commandLines.add(
- getCommandLine(
- "args = ruleContext.actions.args()", "args.add_all(['foo', 'bar'])", "args"));
- commandLines.add(
- getCommandLine(
- "args = ruleContext.actions.args()", "args.add_all('-foo', ['foo', 'bar'])", "args"));
+ "args = ruleContext.actions.args()", "args.add_all('-foo', ['foo', 'bar'])"));
commandLines.add(
getCommandLine(
"args = ruleContext.actions.args()",
- "args.add_all(['foo', 'bar'], format_each='format%s')",
- "args"));
+ "args.add_all(['foo', 'bar'], format_each='format%s')"));
+ commandLines.add(
+ getCommandLine(
+ "args = ruleContext.actions.args()", "args.add_all(['foo', 'bar'], before_each='-I')"));
+ commandLines.add(
+ getCommandLine(
+ "args = ruleContext.actions.args()", "args.add_all(['boing', 'boing', 'boing'])"));
commandLines.add(
getCommandLine(
"args = ruleContext.actions.args()",
- "args.add_all(['foo', 'bar'], before_each='-I')",
- "args"));
+ "args.add_all(['boing', 'boing', 'boing'], uniquify=True)"));
commandLines.add(
getCommandLine(
"args = ruleContext.actions.args()",
- "args.add_all(['boing', 'boing', 'boing'])",
- "args"));
+ "args.add_all(['foo', 'bar'], terminate_with='baz')"));
+ commandLines.add(
+ getCommandLine(
+ "args = ruleContext.actions.args()", "args.add_joined(['foo', 'bar'], join_with=',')"));
commandLines.add(
getCommandLine(
"args = ruleContext.actions.args()",
- "args.add_all(['boing', 'boing', 'boing'], uniquify=True)",
- "args"));
- commandLines.add(
- getCommandLine(
- "args = ruleContext.actions.args()",
- "args.add_all(['foo', 'bar'], terminate_with='baz')",
- "args"));
- commandLines.add(
- getCommandLine(
- "args = ruleContext.actions.args()",
- "args.add_joined(['foo', 'bar'], join_with=',')",
- "args"));
- commandLines.add(
- getCommandLine(
- "args = ruleContext.actions.args()",
- "args.add_joined(['foo', 'bar'], join_with=',', format_joined='--foo=%s')",
- "args"));
+ "args.add_joined(['foo', 'bar'], join_with=',', format_joined='--foo=%s')"));
commandLines.add(
getCommandLine(
"args = ruleContext.actions.args()",
"def _map_each(s): return s + '_mapped'",
- "args.add_all(['foo', 'bar'], map_each=_map_each)",
- "args"));
+ "args.add_all(['foo', 'bar'], map_each=_map_each)"));
commandLines.add(
getCommandLine(
"args = ruleContext.actions.args()",
"values = depset(['a', 'b'])",
- "args.add_all(values)",
- "args"));
+ "args.add_all(values)"));
commandLines.add(
getCommandLine(
"args = ruleContext.actions.args()",
"def _map_each(s): return s + '_mapped'",
"values = depset(['a', 'b'])",
- "args.add_all(values, map_each=_map_each)",
- "args"));
+ "args.add_all(values, map_each=_map_each)"));
commandLines.add(
getCommandLine(
"args = ruleContext.actions.args()",
"def _map_each(s): return s + '_mapped_again'",
"values = depset(['a', 'b'])",
- "args.add_all(values, map_each=_map_each)",
- "args"));
+ "args.add_all(values, map_each=_map_each)"));
// Ensure all these command lines have distinct keys
ActionKeyContext actionKeyContext = new ActionKeyContext();
@@ -3027,23 +2998,22 @@
"args = ruleContext.actions.args()",
"def _bad_fn(s): return s.doesnotexist()",
"values = depset(['a', 'b'])",
- "args.add_all(values, map_each=_bad_fn)",
- "args");
+ "args.add_all(values, map_each=_bad_fn)");
assertThrows(
CommandLineExpansionException.class,
() -> commandLine.addToFingerprint(actionKeyContext, new Fingerprint()));
}
private SkylarkCustomCommandLine getCommandLine(String... lines) throws Exception {
- return ((SkylarkActionFactory.Args) eval(lines)).build();
+ exec(lines);
+ return ((SkylarkActionFactory.Args) eval("args")).build();
}
@Test
public void testPrintArgs() throws Exception {
setRuleContext(createRuleContext("//foo:foo"));
- Args args =
- (Args)
- eval("args = ruleContext.actions.args()", "args.add_all(['--foo', '--bar'])", "args");
+ exec("args = ruleContext.actions.args()", "args.add_all(['--foo', '--bar'])");
+ Args args = (Args) eval("args");
assertThat(Printer.debugPrint(args)).isEqualTo("--foo --bar");
}
@@ -3051,14 +3021,12 @@
public void testDirectoryInArgs() throws Exception {
setSkylarkSemanticsOptions("--incompatible_expand_directories");
setRuleContext(createRuleContext("//foo:foo"));
- SkylarkList<?> result =
- (SkylarkList<?>)
- eval(
- "args = ruleContext.actions.args()",
- "directory = ruleContext.actions.declare_directory('dir')",
- "def _short_path(f): return f.short_path", // For easier assertions
- "args.add_all([directory], map_each=_short_path)",
- "args, directory");
+ exec(
+ "args = ruleContext.actions.args()",
+ "directory = ruleContext.actions.declare_directory('dir')",
+ "def _short_path(f): return f.short_path", // For easier assertions
+ "args.add_all([directory], map_each=_short_path)");
+ SkylarkList<?> result = (SkylarkList<?>) eval("args, directory");
Args args = (Args) result.get(0);
Artifact directory = (Artifact) result.get(1);
CommandLine commandLine = args.build();
@@ -3080,14 +3048,12 @@
public void testDirectoryInArgsIncompatibleFlagOff() throws Exception {
setSkylarkSemanticsOptions("--noincompatible_expand_directories");
setRuleContext(createRuleContext("//foo:foo"));
- SkylarkList<?> result =
- (SkylarkList<?>)
- eval(
- "args = ruleContext.actions.args()",
- "directory = ruleContext.actions.declare_directory('dir')",
- "def _short_path(f): return f.short_path", // For easier assertions
- "args.add_all([directory], map_each=_short_path)",
- "args, directory");
+ exec(
+ "args = ruleContext.actions.args()",
+ "directory = ruleContext.actions.declare_directory('dir')",
+ "def _short_path(f): return f.short_path", // For easier assertions
+ "args.add_all([directory], map_each=_short_path)");
+ SkylarkList<?> result = (SkylarkList<?>) eval("args, directory");
Args args = (Args) result.get(0);
Artifact directory = (Artifact) result.get(1);
CommandLine commandLine = args.build();
@@ -3105,15 +3071,13 @@
public void testDirectoryInArgsExpandDirectories() throws Exception {
setSkylarkSemanticsOptions("--incompatible_expand_directories");
setRuleContext(createRuleContext("//foo:foo"));
- SkylarkList<?> result =
- (SkylarkList<?>)
- eval(
- "args = ruleContext.actions.args()",
- "directory = ruleContext.actions.declare_directory('dir')",
- "def _short_path(f): return f.short_path", // For easier assertions
- "args.add_all([directory], map_each=_short_path, expand_directories=True)",
- "args.add_all([directory], map_each=_short_path, expand_directories=False)",
- "args, directory");
+ exec(
+ "args = ruleContext.actions.args()",
+ "directory = ruleContext.actions.declare_directory('dir')",
+ "def _short_path(f): return f.short_path", // For easier assertions
+ "args.add_all([directory], map_each=_short_path, expand_directories=True)",
+ "args.add_all([directory], map_each=_short_path, expand_directories=False)");
+ SkylarkList<?> result = (SkylarkList<?>) eval("args, directory");
Args args = (Args) result.get(0);
Artifact directory = (Artifact) result.get(1);
CommandLine commandLine = args.build();
@@ -3143,13 +3107,11 @@
public void testDirectoryInScalarArgsIsOkWithoutIncompatibleFlag() throws Exception {
setSkylarkSemanticsOptions("--noincompatible_expand_directories");
setRuleContext(createRuleContext("//foo:foo"));
- Args args =
- (Args)
- eval(
- "args = ruleContext.actions.args()",
- "directory = ruleContext.actions.declare_directory('dir')",
- "args.add(directory)",
- "args");
+ exec(
+ "args = ruleContext.actions.args()",
+ "directory = ruleContext.actions.declare_directory('dir')",
+ "args.add(directory)");
+ Args args = (Args) eval("args");
assertThat(Iterables.getOnlyElement(args.build().arguments())).endsWith("foo/dir");
}
@@ -3158,15 +3120,13 @@
setSkylarkSemanticsOptions("--incompatible_expand_directories");
SkylarkRuleContext ctx = createRuleContext("//foo:foo");
setRuleContext(ctx);
- SkylarkList<?> result =
- (SkylarkList<?>)
- eval(
- "args = ruleContext.actions.args()",
- "directory = ruleContext.actions.declare_directory('dir')",
- "args.add_all([directory])",
- "params = ruleContext.actions.declare_file('params')",
- "ruleContext.actions.write(params, args)",
- "params, directory");
+ exec(
+ "args = ruleContext.actions.args()",
+ "directory = ruleContext.actions.declare_directory('dir')",
+ "args.add_all([directory])",
+ "params = ruleContext.actions.declare_file('params')",
+ "ruleContext.actions.write(params, args)");
+ SkylarkList<?> result = (SkylarkList<?>) eval("params, directory");
Artifact params = (Artifact) result.get(0);
Artifact directory = (Artifact) result.get(1);
ActionAnalysisMetadata action =
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 9490885..27fda47 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
@@ -91,6 +91,10 @@
return ev.eval(input);
}
+ protected final void exec(String... lines) throws Exception {
+ ev.exec(lines);
+ }
+
protected final void update(String name, Object value) throws Exception {
ev.update(name, value);
}