Skip to content

Commit

Permalink
Migrate test infrastructure off ErrorProneCompiler
Browse files Browse the repository at this point in the history
RELNOTES: N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=209777101
  • Loading branch information
cushon committed Aug 22, 2018
1 parent 495aabc commit 1c535d8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
Expand Up @@ -38,6 +38,9 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -61,7 +64,7 @@ public class CompilationTestHelper {
"-XDcompilePolicy=simple");

private final DiagnosticTestHelper diagnosticHelper;
private final BaseErrorProneCompiler compiler;
private final BaseErrorProneJavaCompiler compiler;
private final ByteArrayOutputStream outputStream;
private final ErrorProneInMemoryFileManager fileManager;
private final List<JavaFileObject> sources = new ArrayList<>();
Expand All @@ -77,19 +80,11 @@ private CompilationTestHelper(ScannerSupplier scannerSupplier, String checkName,
try {
fileManager.setLocation(StandardLocation.SOURCE_PATH, Collections.emptyList());
} catch (IOException e) {
e.printStackTrace();
throw new UncheckedIOException(e);
}
this.diagnosticHelper = new DiagnosticTestHelper(checkName);
this.outputStream = new ByteArrayOutputStream();
this.compiler =
BaseErrorProneCompiler.builder()
.report(scannerSupplier)
.redirectOutputTo(
new PrintWriter(
new BufferedWriter(new OutputStreamWriter(outputStream, UTF_8)),
/*autoFlush=*/ true))
.listenToDiagnostics(diagnosticHelper.collector)
.build();
this.compiler = new BaseErrorProneJavaCompiler(JavacTool.create(), scannerSupplier);
}

/**
Expand Down Expand Up @@ -298,10 +293,46 @@ private Result compile(Iterable<JavaFileObject> sources, String[] args) {
if (checkWellFormed) {
checkWellFormed(sources, args);
}
return compiler.run(args, fileManager, ImmutableList.copyOf(sources), null);
createAndInstallTempFolderForOutput(fileManager);
return compiler
.getTask(
new PrintWriter(
new BufferedWriter(new OutputStreamWriter(outputStream, UTF_8)),
/*autoFlush=*/ true),
fileManager,
diagnosticHelper.collector,
/* options= */ ImmutableList.copyOf(args),
/* classes= */ ImmutableList.of(),
sources)
.call()
? Result.OK
: Result.ERROR;
}

private static void createAndInstallTempFolderForOutput(
ErrorProneInMemoryFileManager fileManager) {
Path tempDirectory;
try {
tempDirectory =
Files.createTempDirectory(
fileManager.fileSystem().getRootDirectories().iterator().next(), "");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Arrays.stream(StandardLocation.values())
.filter(StandardLocation::isOutputLocation)
.forEach(
outputLocation -> {
try {
fileManager.setLocationFromPaths(outputLocation, ImmutableList.of(tempDirectory));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}

private void checkWellFormed(Iterable<JavaFileObject> sources, String[] args) {
createAndInstallTempFolderForOutput(fileManager);
JavaCompiler compiler = JavacTool.create();
OutputStream outputStream = new ByteArrayOutputStream();
String[] remainingArgs = null;
Expand Down
Expand Up @@ -131,4 +131,8 @@ public JavaFileObject forSourceLines(String fileName, String... lines) {
public boolean exists(String fileName) {
return Files.exists(resolvePath(fileName));
}

public FileSystem fileSystem() {
return fileSystem;
}
}
Expand Up @@ -332,9 +332,9 @@ public void expectNoDiagnoticsButDiagnosticsProducedFailsWithMatches() {

@Test
public void failureWithErrorAndNoDiagnosticFails() {
AssertionError expected =
InvalidCommandLineOptionException expected =
assertThrows(
AssertionError.class,
InvalidCommandLineOptionException.class,
() ->
compilationHelper
.expectNoDiagnostics()
Expand All @@ -343,11 +343,9 @@ public void failureWithErrorAndNoDiagnosticFails() {
ImmutableList.of("-Xep:ReturnTreeChecker:Squirrels")) // Bad flag crashes.
.ignoreJavacErrors()
.doTest());
assertThat(expected.getMessage())
.contains(
"Expected compilation result to be OK, but was CMDERR. No diagnostics were"
+ " emitted.");
assertThat(expected.getMessage()).contains("InvalidCommandLineOptionException");
assertThat(expected)
.hasMessageThat()
.contains("invalid flag: -Xep:ReturnTreeChecker:Squirrels");
}

@Test
Expand Down

0 comments on commit 1c535d8

Please sign in to comment.