use of com.facebook.buck.step.ExecutionContext in project buck by facebook.
the class JavacStepTest method successfulCompileDoesNotSendStdoutAndStderrToConsole.
@Test
public void successfulCompileDoesNotSendStdoutAndStderrToConsole() throws Exception {
FakeJavac fakeJavac = new FakeJavac();
BuildRuleResolver buildRuleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(buildRuleResolver);
SourcePathResolver sourcePathResolver = new SourcePathResolver(ruleFinder);
ProjectFilesystem fakeFilesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
JavacOptions javacOptions = JavacOptions.builder().setSourceLevel("8.0").setTargetLevel("8.0").build();
ClasspathChecker classpathChecker = new ClasspathChecker("/", ":", Paths::get, dir -> false, file -> false, (path, glob) -> ImmutableSet.of());
JavacStep step = new JavacStep(Paths.get("output"), NoOpClassUsageFileWriter.instance(), Optional.empty(), ImmutableSortedSet.of(), Paths.get("pathToSrcsList"), ImmutableSortedSet.of(), fakeJavac, javacOptions, BuildTargetFactory.newInstance("//foo:bar"), Optional.empty(), sourcePathResolver, ruleFinder, fakeFilesystem, classpathChecker, Optional.empty());
FakeProcess fakeJavacProcess = new FakeProcess(0, "javac stdout\n", "javac stderr\n");
ExecutionContext executionContext = TestExecutionContext.newBuilder().setProcessExecutor(new FakeProcessExecutor(Functions.constant(fakeJavacProcess), new TestConsole())).build();
BuckEventBusFactory.CapturingConsoleEventListener listener = new BuckEventBusFactory.CapturingConsoleEventListener();
executionContext.getBuckEventBus().register(listener);
StepExecutionResult result = step.execute(executionContext);
// Note that we don't include stderr in the step result on success.
assertThat(result, equalTo(StepExecutionResult.SUCCESS));
assertThat(listener.getLogMessages(), empty());
}
use of com.facebook.buck.step.ExecutionContext in project buck by facebook.
the class JavacStepTest method missingBootclasspathDirFailsWithError.
@Test
public void missingBootclasspathDirFailsWithError() throws Exception {
FakeJavac fakeJavac = new FakeJavac();
BuildRuleResolver buildRuleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(buildRuleResolver);
SourcePathResolver sourcePathResolver = new SourcePathResolver(ruleFinder);
ProjectFilesystem fakeFilesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
JavacOptions javacOptions = JavacOptions.builder().setSourceLevel("8.0").setTargetLevel("8.0").setBootclasspath("/no-such-dir").build();
ClasspathChecker classpathChecker = new ClasspathChecker("/", ":", Paths::get, dir -> false, file -> false, (path, glob) -> ImmutableSet.of());
JavacStep step = new JavacStep(Paths.get("output"), NoOpClassUsageFileWriter.instance(), Optional.empty(), ImmutableSortedSet.of(), Paths.get("pathToSrcsList"), ImmutableSortedSet.of(), fakeJavac, javacOptions, BuildTargetFactory.newInstance("//foo:bar"), Optional.empty(), sourcePathResolver, ruleFinder, fakeFilesystem, classpathChecker, Optional.empty());
FakeProcess fakeJavacProcess = new FakeProcess(1, "javac stdout\n", "javac stderr\n");
ExecutionContext executionContext = TestExecutionContext.newBuilder().setProcessExecutor(new FakeProcessExecutor(Functions.constant(fakeJavacProcess), new TestConsole())).build();
BuckEventBusFactory.CapturingConsoleEventListener listener = new BuckEventBusFactory.CapturingConsoleEventListener();
executionContext.getBuckEventBus().register(listener);
StepExecutionResult result = step.execute(executionContext);
assertThat(result, equalTo(StepExecutionResult.ERROR));
assertThat(listener.getLogMessages(), equalTo(ImmutableList.of("Invalid Java compiler options: Bootstrap classpath /no-such-dir " + "contains no valid entries")));
}
use of com.facebook.buck.step.ExecutionContext in project buck by facebook.
the class Jsr199JavacIntegrationTest method shouldWriteResolvedBuildTargetSourcePathsToClassesFile.
/**
* There was a bug where `BuildTargetSourcePath` sources were written to the classes file using
* their string representation, rather than their resolved path.
*/
@Test
public void shouldWriteResolvedBuildTargetSourcePathsToClassesFile() throws IOException, InterruptedException {
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
BuildRule rule = new FakeBuildRule("//:fake", pathResolver);
resolver.addToIndex(rule);
Jsr199Javac javac = createJavac(/* withSyntaxError */
false);
ExecutionContext executionContext = TestExecutionContext.newInstance();
JavacExecutionContext javacExecutionContext = JavacExecutionContext.of(new JavacEventSinkToBuckEventBusBridge(executionContext.getBuckEventBus()), executionContext.getStdErr(), executionContext.getClassLoaderCache(), executionContext.getObjectMapper(), executionContext.getVerbosity(), executionContext.getCellPathResolver(), executionContext.getJavaPackageFinder(), createProjectFilesystem(), NoOpClassUsageFileWriter.instance(), executionContext.getEnvironment(), executionContext.getProcessExecutor(), ImmutableList.of(), Optional.empty());
int exitCode = javac.buildWithClasspath(javacExecutionContext, BuildTargetFactory.newInstance("//some:example"), ImmutableList.of(), ImmutableList.of(), SOURCE_PATHS, pathToSrcsList, Optional.empty(), JavacOptions.AbiGenerationMode.CLASS);
assertEquals("javac should exit with code 0.", exitCode, 0);
assertTrue(Files.exists(pathToSrcsList));
assertTrue(Files.isRegularFile(pathToSrcsList));
assertEquals("Example.java", new String(Files.readAllBytes(pathToSrcsList), StandardCharsets.UTF_8).trim());
}
use of com.facebook.buck.step.ExecutionContext in project buck by facebook.
the class Jsr199JavacIntegrationTest method shouldUseSpecifiedJavacJar.
@Test
public void shouldUseSpecifiedJavacJar() throws Exception {
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
BuildRule rule = new FakeBuildRule("//:fake", pathResolver);
resolver.addToIndex(rule);
Path fakeJavacJar = Paths.get("ae036e57-77a7-4356-a79c-0f85b1a3290d", "fakeJavac.jar");
ExecutionContext executionContext = TestExecutionContext.newInstance();
MockClassLoader mockClassLoader = new MockClassLoader(ClassLoader.getSystemClassLoader(), ImmutableMap.of(JavacOptions.COM_SUN_TOOLS_JAVAC_API_JAVAC_TOOL, MockJavac.class));
executionContext.getClassLoaderCache().injectClassLoader(ClassLoader.getSystemClassLoader(), ImmutableList.of(fakeJavacJar.toUri().toURL()), mockClassLoader);
Jsr199Javac javac = createJavac(/* withSyntaxError */
false, Optional.of(fakeJavacJar));
JavacExecutionContext javacExecutionContext = JavacExecutionContext.of(new JavacEventSinkToBuckEventBusBridge(executionContext.getBuckEventBus()), executionContext.getStdErr(), executionContext.getClassLoaderCache(), executionContext.getObjectMapper(), executionContext.getVerbosity(), executionContext.getCellPathResolver(), executionContext.getJavaPackageFinder(), createProjectFilesystem(), NoOpClassUsageFileWriter.instance(), executionContext.getEnvironment(), executionContext.getProcessExecutor(), ImmutableList.of(fakeJavacJar), Optional.empty());
boolean caught = false;
try {
javac.buildWithClasspath(javacExecutionContext, BuildTargetFactory.newInstance("//some:example"), ImmutableList.of(), ImmutableList.of(), SOURCE_PATHS, pathToSrcsList, Optional.empty(), JavacOptions.AbiGenerationMode.CLASS);
fail("Did not expect compilation to succeed");
} catch (UnsupportedOperationException ex) {
if (ex.toString().contains("abcdef")) {
caught = true;
}
}
assertTrue("mock Java compiler should throw", caught);
}
use of com.facebook.buck.step.ExecutionContext in project buck by facebook.
the class AbstractExecutionStepTest method testDescriptionGetters.
@Test
public void testDescriptionGetters() {
String description = "How I describe myself.";
Step step = new AbstractExecutionStep(description) {
@Override
public StepExecutionResult execute(ExecutionContext context) {
return StepExecutionResult.SUCCESS;
}
};
ExecutionContext context = TestExecutionContext.newInstance();
assertEquals(description, step.getShortName());
assertEquals(description, step.getDescription(context));
}
Aggregations