diff options
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/syntax/BuildFileASTTest.java')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/syntax/BuildFileASTTest.java | 118 |
1 files changed, 48 insertions, 70 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/BuildFileASTTest.java b/src/test/java/com/google/devtools/build/lib/syntax/BuildFileASTTest.java index eff5badfb6..00e9062d63 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/BuildFileASTTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/BuildFileASTTest.java @@ -22,11 +22,7 @@ import static org.junit.Assert.assertTrue; import com.google.devtools.build.lib.cmdline.PackageIdentifier; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.EventCollector; -import com.google.devtools.build.lib.events.EventKind; -import com.google.devtools.build.lib.events.Reporter; -import com.google.devtools.build.lib.events.util.EventCollectionApparatus; import com.google.devtools.build.lib.packages.CachingPackageLocator; -import com.google.devtools.build.lib.testutil.MoreAsserts; import com.google.devtools.build.lib.testutil.Scratch; import com.google.devtools.build.lib.vfs.Path; @@ -37,13 +33,14 @@ import org.junit.runners.JUnit4; import java.io.IOException; import java.util.Arrays; +/** + * Unit tests for BuildFileAST. + */ @RunWith(JUnit4.class) -public class BuildFileASTTest { +public class BuildFileASTTest extends EvaluationTestCase { private Scratch scratch = new Scratch(); - private EventCollectionApparatus events = new EventCollectionApparatus(EventKind.ALL_EVENTS); - private class ScratchPathPackageLocator implements CachingPackageLocator { @Override public Path getBuildFileForPackage(PackageIdentifier packageName) { @@ -53,13 +50,18 @@ public class BuildFileASTTest { private CachingPackageLocator locator = new ScratchPathPackageLocator(); + @Override + public Environment newEnvironment() throws Exception { + return newBuildEnvironment(); + } + /** * Parses the contents of the specified string (using DUMMY_PATH as the fake * filename) and returns the AST. Resets the error handler beforehand. */ private BuildFileAST parseBuildFile(String... lines) throws IOException { Path file = scratch.file("/a/build/file/BUILD", lines); - return BuildFileAST.parseBuildFile(file, events.reporter(), locator, false); + return BuildFileAST.parseBuildFile(file, getEventHandler(), locator, false); } @Test @@ -69,11 +71,9 @@ public class BuildFileASTTest { "", "x = [1,2,'foo',4] + [1,2, \"%s%d\" % ('foo', 1)]"); - Environment env = new Environment(); - Reporter reporter = new Reporter(); - BuildFileAST buildfile = BuildFileAST.parseBuildFile(buildFile, reporter, null, false); + BuildFileAST buildfile = BuildFileAST.parseBuildFile(buildFile, getEventHandler(), null, false); - assertTrue(buildfile.exec(env, reporter)); + assertTrue(buildfile.exec(env, getEventHandler())); // Test final environment is correctly modified: // @@ -91,15 +91,11 @@ public class BuildFileASTTest { "", "z = x + y"); - Environment env = new Environment(); - Reporter reporter = new Reporter(); - EventCollector collector = new EventCollector(EventKind.ALL_EVENTS); - reporter.addHandler(collector); - BuildFileAST buildfile = BuildFileAST.parseBuildFile(buildFile, reporter, null, false); + setFailFast(false); + BuildFileAST buildfile = BuildFileAST.parseBuildFile(buildFile, getEventHandler(), null, false); - assertFalse(buildfile.exec(env, reporter)); - Event e = MoreAsserts.assertContainsEvent(collector, - "unsupported operand type(s) for +: 'int' and 'List'"); + assertFalse(buildfile.exec(env, getEventHandler())); + Event e = assertContainsEvent("unsupported operand type(s) for +: 'int' and 'List'"); assertEquals(4, e.getLocation().getStartLineAndColumn().getLine()); } @@ -114,13 +110,12 @@ public class BuildFileASTTest { @Test public void testFailsIfNewlinesAreMissing() throws Exception { - events.setFailFast(false); + setFailFast(false); BuildFileAST buildFileAST = parseBuildFile("foo() bar() something = baz() bar()"); - Event event = events.collector().iterator().next(); - assertEquals("syntax error at \'bar\': expected newline", event.getMessage()); + Event event = assertContainsEvent("syntax error at \'bar\': expected newline"); assertEquals("/a/build/file/BUILD", event.getLocation().getPath().toString()); assertEquals(1, event.getLocation().getStartLineAndColumn().getLine()); @@ -129,11 +124,10 @@ public class BuildFileASTTest { @Test public void testImplicitStringConcatenationFails() throws Exception { - events.setFailFast(false); + setFailFast(false); BuildFileAST buildFileAST = parseBuildFile("a = 'foo' 'bar'"); - Event event = events.collector().iterator().next(); - assertEquals("Implicit string concatenation is forbidden, use the + operator", - event.getMessage()); + Event event = assertContainsEvent( + "Implicit string concatenation is forbidden, use the + operator"); assertEquals("/a/build/file/BUILD", event.getLocation().getPath().toString()); assertEquals(1, event.getLocation().getStartLineAndColumn().getLine()); @@ -143,11 +137,10 @@ public class BuildFileASTTest { @Test public void testImplicitStringConcatenationAcrossLinesIsIllegal() throws Exception { - events.setFailFast(false); + setFailFast(false); BuildFileAST buildFileAST = parseBuildFile("a = 'foo'\n 'bar'"); - Event event = events.collector().iterator().next(); - assertEquals("indentation error", event.getMessage()); + Event event = assertContainsEvent("indentation error"); assertEquals("/a/build/file/BUILD", event.getLocation().getPath().toString()); assertEquals(2, event.getLocation().getStartLineAndColumn().getLine()); @@ -172,7 +165,7 @@ public class BuildFileASTTest { @Test public void testWithSyntaxErrorsDoesNotPrintDollarError() throws Exception { - events.setFailFast(false); + setFailFast(false); BuildFileAST buildFile = parseBuildFile( "abi = cxx_abi + '-glibc-' + glibc_version + '-' + generic_cpu + '-' + sysname", "libs = [abi + opt_level + '/lib/libcc.a']", @@ -182,14 +175,11 @@ public class BuildFileASTTest { " srcs = libs,", " includes = [ abi + opt_level + '/include' ])"); assertTrue(buildFile.containsErrors()); - Event event = events.collector().iterator().next(); - assertEquals("syntax error at '+': expected expression", event.getMessage()); - Environment env = new Environment(); - assertFalse(buildFile.exec(env, events.reporter())); - assertNull(findEvent(events.collector(), "$error$")); + assertContainsEvent("syntax error at '+': expected expression"); + assertFalse(buildFile.exec(env, getEventHandler())); + assertNull(findEvent(getEventCollector(), "$error$")); // This message should not be printed anymore. - Event event2 = findEvent(events.collector(), "contains syntax error(s)"); - assertNull(event2); + assertNull(findEvent(getEventCollector(), "contains syntax error(s)")); } @Test @@ -202,7 +192,7 @@ public class BuildFileASTTest { + "include(\"//foo/bar:BUILD\")\n" + "b = 4\n"); - BuildFileAST buildFileAST = BuildFileAST.parseBuildFile(buildFile, events.reporter(), + BuildFileAST buildFileAST = BuildFileAST.parseBuildFile(buildFile, getEventHandler(), locator, false); assertFalse(buildFileAST.containsErrors()); @@ -217,15 +207,14 @@ public class BuildFileASTTest { "include(\"//foo/bar:defs\")\n" + "b = a + 1\n"); - BuildFileAST buildFileAST = BuildFileAST.parseBuildFile(buildFile, events.reporter(), + BuildFileAST buildFileAST = BuildFileAST.parseBuildFile(buildFile, getEventHandler(), locator, false); assertFalse(buildFileAST.containsErrors()); assertThat(buildFileAST.getStatements()).hasSize(3); - Environment env = new Environment(); - Reporter reporter = new Reporter(); - assertFalse(buildFileAST.exec(env, reporter)); + setFailFast(false); + assertFalse(buildFileAST.exec(env, getEventHandler())); assertEquals(2, env.lookup("b")); } @@ -247,63 +236,53 @@ public class BuildFileASTTest { assertFalse(buildFileAST.containsErrors()); assertThat(buildFileAST.getStatements()).hasSize(8); - Environment env = new Environment(); - Reporter reporter = new Reporter(); - assertFalse(buildFileAST.exec(env, reporter)); + setFailFast(false); + assertFalse(buildFileAST.exec(env, getEventHandler())); assertEquals(5, env.lookup("b")); assertEquals(7, env.lookup("c")); } @Test public void testFailInclude() throws Exception { - events.setFailFast(false); + setFailFast(false); BuildFileAST buildFileAST = parseBuildFile("include(\"//nonexistent\")"); assertThat(buildFileAST.getStatements()).hasSize(1); - events.assertContainsEvent("Include of '//nonexistent' failed"); - } - - - private class EmptyPackageLocator implements CachingPackageLocator { - @Override - public Path getBuildFileForPackage(PackageIdentifier packageName) { - return null; - } + assertContainsEvent("Include of '//nonexistent' failed"); } - private CachingPackageLocator emptyLocator = new EmptyPackageLocator(); @Test public void testFailInclude2() throws Exception { - events.setFailFast(false); + setFailFast(false); Path buildFile = scratch.file("/foo/bar/BUILD", "include(\"//nonexistent:foo\")\n"); - BuildFileAST buildFileAST = BuildFileAST.parseBuildFile(buildFile, events.reporter(), - emptyLocator, false); + BuildFileAST buildFileAST = BuildFileAST.parseBuildFile( + buildFile, getEventHandler(), Environment.EMPTY_PACKAGE_LOCATOR, false); assertThat(buildFileAST.getStatements()).hasSize(1); - events.assertContainsEvent("Package 'nonexistent' not found"); + assertContainsEvent("Package 'nonexistent' not found"); } @Test public void testInvalidInclude() throws Exception { - events.setFailFast(false); + setFailFast(false); BuildFileAST buildFileAST = parseBuildFile("include(2)"); assertThat(buildFileAST.getStatements()).isEmpty(); - events.assertContainsEvent("syntax error at '2'"); + assertContainsEvent("syntax error at '2'"); } @Test public void testRecursiveInclude() throws Exception { - events.setFailFast(false); + setFailFast(false); Path buildFile = scratch.file("/foo/bar/BUILD", "include(\"//foo/bar:BUILD\")\n"); - BuildFileAST.parseBuildFile(buildFile, events.reporter(), locator, false); - events.assertContainsEvent("Recursive inclusion"); + BuildFileAST.parseBuildFile(buildFile, getEventHandler(), locator, false); + assertContainsEvent("Recursive inclusion"); } @Test public void testParseErrorInclude() throws Exception { - events.setFailFast(false); + setFailFast(false); scratch.file("/foo/bar/file", "a = 2 + % 3\n"); // parse error @@ -311,14 +290,13 @@ public class BuildFileASTTest { parseBuildFile("include(\"//foo/bar:file\")"); // Check the location is properly reported - Event event = events.collector().iterator().next(); + Event event = assertContainsEvent("syntax error at '%': expected expression"); assertEquals("/foo/bar/file:1:9", event.getLocation().print()); - assertEquals("syntax error at '%': expected expression", event.getMessage()); } @Test public void testNonExistentIncludeReported() throws Exception { - events.setFailFast(false); + setFailFast(false); BuildFileAST buildFileAST = parseBuildFile("include('//foo:bar')"); assertThat(buildFileAST.getStatements()).hasSize(1); } |