Francois-Rene Rideau | 93ed7f1 | 2015-10-20 15:39:33 +0000 | [diff] [blame] | 1 | // Copyright 2006 The Bazel Authors. All rights reserved. |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.syntax; |
| 15 | |
Francois-Rene Rideau | c673a82 | 2015-03-02 19:52:39 +0000 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 17 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.events.Event; |
Francois-Rene Rideau | 93ed7f1 | 2015-10-20 15:39:33 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.syntax.SkylarkList.Tuple; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.syntax.util.EvaluationTestCase; |
Ulf Adams | b089c8d | 2015-10-26 12:20:33 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.testutil.MoreAsserts; |
Ulf Adams | bdd9c1f | 2015-04-24 14:30:01 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.testutil.Scratch; |
tomlu | 67c84b1 | 2017-11-06 19:49:16 +0100 | [diff] [blame] | 23 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.vfs.Path; |
Laurent Le Brun | f1112b3 | 2016-08-03 13:16:02 +0000 | [diff] [blame] | 25 | import java.io.IOException; |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
| 28 | import org.junit.runners.JUnit4; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 29 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 30 | /** |
| 31 | * Unit tests for BuildFileAST. |
| 32 | */ |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 33 | @RunWith(JUnit4.class) |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 34 | public class BuildFileASTTest extends EvaluationTestCase { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 35 | |
Ulf Adams | bdd9c1f | 2015-04-24 14:30:01 +0000 | [diff] [blame] | 36 | private Scratch scratch = new Scratch(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 37 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 38 | @Override |
| 39 | public Environment newEnvironment() throws Exception { |
| 40 | return newBuildEnvironment(); |
| 41 | } |
| 42 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 43 | /** |
| 44 | * Parses the contents of the specified string (using DUMMY_PATH as the fake |
| 45 | * filename) and returns the AST. Resets the error handler beforehand. |
| 46 | */ |
| 47 | private BuildFileAST parseBuildFile(String... lines) throws IOException { |
| 48 | Path file = scratch.file("/a/build/file/BUILD", lines); |
tomlu | 67c84b1 | 2017-11-06 19:49:16 +0100 | [diff] [blame] | 49 | byte[] bytes = FileSystemUtils.readWithKnownFileSize(file, file.getFileSize()); |
| 50 | ParserInputSource inputSource = ParserInputSource.create(bytes, file.asFragment()); |
Laurent Le Brun | 4956d32 | 2016-11-23 17:23:21 +0000 | [diff] [blame] | 51 | return BuildFileAST.parseBuildFile(inputSource, getEventHandler()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 54 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 55 | public void testParseBuildFileOK() throws Exception { |
Laurent Le Brun | 884ca5c | 2016-10-11 12:23:01 +0000 | [diff] [blame] | 56 | BuildFileAST buildfile = parseBuildFile( |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 57 | "# a file in the build language", |
| 58 | "", |
| 59 | "x = [1,2,'foo',4] + [1,2, \"%s%d\" % ('foo', 1)]"); |
| 60 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 61 | assertThat(buildfile.exec(env, getEventHandler())).isTrue(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 62 | |
| 63 | // Test final environment is correctly modified: |
| 64 | // |
| 65 | // input1.BUILD contains: |
| 66 | // x = [1,2,'foo',4] + [1,2, "%s%d" % ('foo', 1)] |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 67 | assertThat(env.lookup("x")) |
| 68 | .isEqualTo(SkylarkList.createImmutable(Tuple.of(1, 2, "foo", 4, 1, 2, "foo1"))); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 71 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 72 | public void testEvalException() throws Exception { |
Laurent Le Brun | 884ca5c | 2016-10-11 12:23:01 +0000 | [diff] [blame] | 73 | setFailFast(false); |
| 74 | BuildFileAST buildfile = parseBuildFile( |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 75 | "x = 1", |
| 76 | "y = [2,3]", |
| 77 | "", |
| 78 | "z = x + y"); |
| 79 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 80 | assertThat(buildfile.exec(env, getEventHandler())).isFalse(); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 81 | Event e = assertContainsError("unsupported operand type(s) for +: 'int' and 'list'"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 82 | assertThat(e.getLocation().getStartLineAndColumn().getLine()).isEqualTo(4); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 85 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 86 | public void testParsesFineWithNewlines() throws Exception { |
Laurent Le Brun | b639ca8 | 2017-01-17 11:18:23 +0000 | [diff] [blame] | 87 | BuildFileAST buildFileAST = parseBuildFile("foo()", "bar()", "something = baz()", "bar()"); |
Francois-Rene Rideau | c673a82 | 2015-03-02 19:52:39 +0000 | [diff] [blame] | 88 | assertThat(buildFileAST.getStatements()).hasSize(4); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 91 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 92 | public void testFailsIfNewlinesAreMissing() throws Exception { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 93 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 94 | |
| 95 | BuildFileAST buildFileAST = |
| 96 | parseBuildFile("foo() bar() something = baz() bar()"); |
| 97 | |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 98 | Event event = assertContainsError("syntax error at \'bar\': expected newline"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 99 | assertThat(event.getLocation().getPath().toString()).isEqualTo("/a/build/file/BUILD"); |
| 100 | assertThat(event.getLocation().getStartLineAndColumn().getLine()).isEqualTo(1); |
| 101 | assertThat(buildFileAST.containsErrors()).isTrue(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 104 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 105 | public void testImplicitStringConcatenationFails() throws Exception { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 106 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 107 | BuildFileAST buildFileAST = parseBuildFile("a = 'foo' 'bar'"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 108 | Event event = assertContainsError( |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 109 | "Implicit string concatenation is forbidden, use the + operator"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 110 | assertThat(event.getLocation().getPath().toString()).isEqualTo("/a/build/file/BUILD"); |
| 111 | assertThat(event.getLocation().getStartLineAndColumn().getLine()).isEqualTo(1); |
| 112 | assertThat(event.getLocation().getStartLineAndColumn().getColumn()).isEqualTo(10); |
| 113 | assertThat(buildFileAST.containsErrors()).isTrue(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 116 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 117 | public void testImplicitStringConcatenationAcrossLinesIsIllegal() throws Exception { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 118 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 119 | BuildFileAST buildFileAST = parseBuildFile("a = 'foo'\n 'bar'"); |
| 120 | |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 121 | Event event = assertContainsError("indentation error"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 122 | assertThat(event.getLocation().getPath().toString()).isEqualTo("/a/build/file/BUILD"); |
| 123 | assertThat(event.getLocation().getStartLineAndColumn().getLine()).isEqualTo(2); |
| 124 | assertThat(event.getLocation().getStartLineAndColumn().getColumn()).isEqualTo(2); |
| 125 | assertThat(buildFileAST.containsErrors()).isTrue(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 128 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 129 | public void testWithSyntaxErrorsDoesNotPrintDollarError() throws Exception { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 130 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 131 | BuildFileAST buildFile = parseBuildFile( |
gregce | e39ccad | 2017-06-08 21:45:08 +0200 | [diff] [blame] | 132 | "abi = '$(ABI)-glibc-' + glibc_version + '-' + $(TARGET_CPU) + '-linux'", |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 133 | "libs = [abi + opt_level + '/lib/libcc.a']", |
| 134 | "shlibs = [abi + opt_level + '/lib/libcc.so']", |
| 135 | "+* shlibs", // syntax error at '+' |
| 136 | "cc_library(name = 'cc',", |
| 137 | " srcs = libs,", |
| 138 | " includes = [ abi + opt_level + '/include' ])"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 139 | assertThat(buildFile.containsErrors()).isTrue(); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 140 | assertContainsError("syntax error at '+': expected expression"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 141 | assertThat(buildFile.exec(env, getEventHandler())).isFalse(); |
Ulf Adams | b089c8d | 2015-10-26 12:20:33 +0000 | [diff] [blame] | 142 | MoreAsserts.assertDoesNotContainEvent(getEventCollector(), "$error$"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 143 | // This message should not be printed anymore. |
Ulf Adams | b089c8d | 2015-10-26 12:20:33 +0000 | [diff] [blame] | 144 | MoreAsserts.assertDoesNotContainEvent(getEventCollector(), "contains syntax error(s)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 145 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 146 | } |