Skip to content

Commit

Permalink
BuildFileAST.eval() doesn't execute the code when there's a static er…
Browse files Browse the repository at this point in the history
…ror.

This has no visible effect on Bazel (eval is used just for internal
expressions that don't fail), and is therefore not tested.

It will be tested when we add tests to the standalone Skylark interpreter.

--
PiperOrigin-RevId: 147495990
MOS_MIGRATED_REVID=147495990
  • Loading branch information
laurentlb authored and dslomov committed Feb 15, 2017
1 parent a2f9f68 commit d4d7fca
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,17 @@ public static boolean checkSyntax(ParserInputSource input, EventHandler eventHan
return last;
}

/**
* Evaluates the lines from input and return the value of the last statement if it's an
* Expression or else null. In case of error (either during validation or evaluation), it
* throws an EvalException.
*/
@Nullable
public static Object eval(Environment env, String... input)
throws EvalException, InterruptedException {
BuildFileAST ast = parseSkylarkString(env.getEventHandler(), input);
ValidationEnvironment valid = new ValidationEnvironment(env);
valid.validateAst(ast.getStatements(), env.getEventHandler());
valid.validateAst(ast.getStatements());
return ast.eval(env);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ public void testReadOnly() throws Exception {
try {
BuildFileAST.eval(env, "special_var = 41");
throw new AssertionError("failed to fail");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("ERROR 1:1: Variable special_var is read only");
} catch (EvalException e) {
assertThat(e.getMessage()).contains("Variable special_var is read only");
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,9 @@ public void testRecursiveTupleDestructuring() throws Exception {

@Test
public void testListComprehensionModifiesGlobalEnv() throws Exception {
new SkylarkTest().update("x", 42).testIfErrorContains("ERROR 1:1: Variable x is read only",
"[x + 1 for x in [1,2,3]]");
new SkylarkTest()
.update("x", 42)
.testIfErrorContains("Variable x is read only", "[x + 1 for x in [1,2,3]]");
new BuildTest().update("x", 42).setUp("y =[x + 1 for x in [1,2,3]]")
.testExactOrder("y", 2, 3, 4).testLookup("x", 3); // (x is global)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,20 +504,14 @@ public void testBooleanUnsupportedOperationFails() throws Exception {

@Test
public void testPyStringJoin() throws Exception {
new BothModesTest().testStatement("'-'.join([ 'a', 'b', 'c' ])", "a-b-c");
new BothModesTest().testStatement("'-'.join(['a', 'b', 'c'])", "a-b-c");
}

@Test
public void testPyStringGlobalJoin() throws Exception {
// TODO(bazel-team): BUILD and Skylark should use the same code path (and same error message).
new BuildTest()
.testIfErrorContains("name 'join' is not defined", "join(' ', [ 'a', 'b', 'c' ])");

new SkylarkTest()
.testIfErrorContains(
"ERROR 1:1: name 'join' is not defined", "join(' ', [ 'a', 'b', 'c' ])");

new BothModesTest().testStatement("' '.join([ 'a', 'b', 'c' ])", "a b c");
new BothModesTest()
.testIfErrorContains("name 'join' is not defined", "join(' ', ['a', 'b', 'c'])")
.testStatement("' '.join(['a', 'b', 'c'])", "a b c");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1230,9 +1230,7 @@ public void testListComprehensionsMultipleVariablesFail() throws Exception {
// can't reuse the same local variable twice(!)
new SkylarkTest()
.testIfErrorContains(
"ERROR 2:1: Variable x is read only",
"[x + y for x, y in (1, 2)]",
"[x + y for x, y in (1, 2)]");
"Variable x is read only", "[x + y for x, y in (1, 2)]", "[x + y for x, y in (1, 2)]");

new SkylarkTest()
.testIfErrorContains("type 'int' is not a collection", "[x2 + y2 for x2, y2 in (1, 2)]");
Expand Down

0 comments on commit d4d7fca

Please sign in to comment.