private void assertCompDir(Path compDir, Optional<String> failure) throws Exception { ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot().toPath()); CxxPlatform platform = DefaultCxxPlatforms.build(new CxxBuckConfig(new FakeBuckConfig())); // Build up the paths to various files the archive step will use. ImmutableList<String> compiler = platform.getCc().getCommandPrefix(new SourcePathResolver(new BuildRuleResolver())); Path output = filesystem.resolve(Paths.get("output.o")); Path relativeInput = Paths.get("input.c"); Path input = filesystem.resolve(relativeInput); filesystem.writeContentsToPath("int main() {}", relativeInput); ImmutableList.Builder<String> preprocessorCommand = ImmutableList.builder(); preprocessorCommand.addAll(compiler); ImmutableList.Builder<String> compilerCommand = ImmutableList.builder(); compilerCommand.addAll(compiler); compilerCommand.add("-g"); DebugPathSanitizer sanitizer = new DebugPathSanitizer(200, File.separatorChar, compDir, ImmutableBiMap.<Path, Path>of()); // Build an archive step. CxxPreprocessAndCompileStep step = new CxxPreprocessAndCompileStep( CxxPreprocessAndCompileStep.Operation.COMPILE_MUNGE_DEBUGINFO, output, relativeInput, CxxSource.Type.C, Optional.of(preprocessorCommand.build()), Optional.of(compilerCommand.build()), ImmutableMap.<Path, Path>of(), sanitizer); // Execute the archive step and verify it ran successfully. ExecutionContext executionContext = TestExecutionContext.newBuilder() .setProjectFilesystem(new ProjectFilesystem(tmp.getRoot().toPath())) .build(); TestConsole console = (TestConsole) executionContext.getConsole(); int exitCode = step.execute(executionContext); if (failure.isPresent()) { assertNotEquals("compile step succeeded", 0, exitCode); assertThat( console.getTextWrittenToStdErr(), console.getTextWrittenToStdErr(), Matchers.containsString(failure.get())); } else { assertEquals("compile step failed: " + console.getTextWrittenToStdErr(), 0, exitCode); // Verify that we find the expected compilation dir embedded in the file. String contents = new String(Files.readAllBytes(output)); assertThat(contents, Matchers.containsString(sanitizer.getCompilationDirectory())); } // Cleanup. Files.delete(input); Files.deleteIfExists(output); }
@Test public void whenAndroidDirectoryResolverChangesParserInvalidated() throws IOException, InterruptedException { ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot().toPath()); Object daemon = Main.getDaemon( new TestCellBuilder() .setAndroidDirectoryResolver( new FakeAndroidDirectoryResolver( Optional.<Path>absent(), Optional.<Path>absent(), Optional.<Path>absent(), Optional.of("something"))) .setFilesystem(filesystem) .build(), new ObjectMapper()); assertNotEquals( "Daemon should be replaced when not equal.", daemon, Main.getDaemon( new TestCellBuilder() .setAndroidDirectoryResolver( new FakeAndroidDirectoryResolver( Optional.<Path>absent(), Optional.<Path>absent(), Optional.<Path>absent(), Optional.of("different"))) .setFilesystem(filesystem) .build(), new ObjectMapper())); }
@Test public void testGetDescription() throws IOException { Jsr199Javac javac = createJavac(/* withSyntaxError */ false); String pathToOutputDir = new File(tmp.getRoot(), "out").getAbsolutePath(); assertEquals( String.format( "javac -source %s -target %s -g " + "-d %s " + "-classpath '' " + "@" + pathToSrcsList.toString(), JavaBuckConfig.TARGETED_JAVA_VERSION, JavaBuckConfig.TARGETED_JAVA_VERSION, pathToOutputDir), javac.getDescription( ImmutableList.of( "-source", JavaBuckConfig.TARGETED_JAVA_VERSION, "-target", JavaBuckConfig.TARGETED_JAVA_VERSION, "-g", "-d", pathToOutputDir, "-classpath", "''"), SOURCE_PATHS, Optional.of(pathToSrcsList))); }
@Test public void skipsFirstCache() throws IOException { Path fullPath = Paths.get("some/path"); ProjectFilesystem filesystem = new FakeProjectFilesystem(tmp.getRoot()); DefaultFileHashCache innerCache = new DefaultFileHashCache(filesystem); StackedFileHashCache cache = new StackedFileHashCache(ImmutableList.of(innerCache)); expectedException.expect(NoSuchFileException.class); cache.get(fullPath); }
@Test public void whenBuckConfigChangesParserInvalidated() throws IOException, InterruptedException { ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot().toPath()); Object daemon = Main.getDaemon( new TestCellBuilder() .setBuckConfig( FakeBuckConfig.builder() .setSections( ImmutableMap.of( "somesection", ImmutableMap.of("somename", "somevalue"))) .build()) .setFilesystem(filesystem) .build(), new ObjectMapper()); assertEquals( "Daemon should not be replaced when config equal.", daemon, Main.getDaemon( new TestCellBuilder() .setBuckConfig( FakeBuckConfig.builder() .setSections( ImmutableMap.of( "somesection", ImmutableMap.of("somename", "somevalue"))) .build()) .setFilesystem(filesystem) .build(), new ObjectMapper())); assertNotEquals( "Daemon should be replaced when config not equal.", daemon, Main.getDaemon( new TestCellBuilder() .setBuckConfig( FakeBuckConfig.builder() .setSections( ImmutableMap.of( "somesection", ImmutableMap.of("somename", "someothervalue"))) .build()) .setFilesystem(filesystem) .build(), new ObjectMapper())); }
@Test public void testKeepGoingWithBuildReport() throws IOException { ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "keep_going", tmp).setUp(); File buildReport = new File(tmpFolderForBuildReport.getRoot(), "build-report.txt"); workspace .runBuckBuild( "--build-report", buildReport.getAbsolutePath(), "--keep-going", "//:rule_with_output", "//:failing_rule") .assertFailure(); assertTrue(buildReport.exists()); String buildReportContents = com.google.common.io.Files.toString(buildReport, Charsets.UTF_8); String expectedReport = Joiner.on('\n') .join( "{", " \"success\" : false,", " \"results\" : {", " \"//:rule_with_output\" : {", " \"success\" : true,", " \"type\" : \"BUILT_LOCALLY\",", " \"output\" : \"" + GENRULE_OUTPUT + "\"", " },", " \"//:failing_rule\" : {", " \"success\" : false", " }", " },", " \"failures\" : {", " \"//:failing_rule\" : \"//:failing_rule failed with exit code 2:\\ngenrule\"", " }", "}"); assertEquals(expectedReport, buildReportContents); }
@Before public void setUp() { pathToSrcsList = Paths.get(tmp.getRoot().getPath(), "srcs_list"); }