Skip to content

Commit 6ebd438

Browse files
committed
CompilationTestHelper clean-up
Ensure that test compilations always set an empty -sourcepath, and use UTF-8 everywhere. MOE_MIGRATED_REVID=141339766
1 parent d82da81 commit 6ebd438

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

test_helpers/src/main/java/com/google/errorprone/CompilationTestHelper.java

+27-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.errorprone;
1818

1919
import static com.google.common.truth.Truth.assertWithMessage;
20+
import static java.nio.charset.StandardCharsets.UTF_8;
2021
import static org.junit.Assert.assertTrue;
2122
import static org.junit.Assert.fail;
2223

@@ -30,19 +31,23 @@
3031
import com.sun.tools.javac.api.JavacTool;
3132
import com.sun.tools.javac.main.Main.Result;
3233
import com.sun.tools.javac.util.Context;
34+
import java.io.BufferedWriter;
3335
import java.io.ByteArrayOutputStream;
3436
import java.io.IOError;
3537
import java.io.IOException;
3638
import java.io.OutputStream;
39+
import java.io.OutputStreamWriter;
3740
import java.io.PrintWriter;
3841
import java.util.ArrayList;
3942
import java.util.Arrays;
43+
import java.util.Collections;
4044
import java.util.List;
4145
import java.util.Locale;
4246
import javax.tools.Diagnostic;
4347
import javax.tools.JavaCompiler;
4448
import javax.tools.JavaCompiler.CompilationTask;
4549
import javax.tools.JavaFileObject;
50+
import javax.tools.StandardLocation;
4651

4752
/**
4853
* Helps test Error Prone bug checkers and compilations.
@@ -69,12 +74,20 @@ public class CompilationTestHelper {
6974

7075
private CompilationTestHelper(ScannerSupplier scannerSupplier, String checkName, Class<?> clazz) {
7176
this.fileManager = new ErrorProneInMemoryFileManager(clazz);
77+
try {
78+
fileManager.setLocation(StandardLocation.SOURCE_PATH, Collections.emptyList());
79+
} catch (IOException e) {
80+
e.printStackTrace();
81+
}
7282
this.diagnosticHelper = new DiagnosticTestHelper(checkName);
7383
this.outputStream = new ByteArrayOutputStream();
7484
this.compiler =
7585
BaseErrorProneCompiler.builder()
7686
.report(scannerSupplier)
77-
.redirectOutputTo(new PrintWriter(outputStream, /*autoFlush=*/ true))
87+
.redirectOutputTo(
88+
new PrintWriter(
89+
new BufferedWriter(new OutputStreamWriter(outputStream, UTF_8)),
90+
/*autoFlush=*/ true))
7891
.listenToDiagnostics(diagnosticHelper.collector)
7992
.build();
8093
}
@@ -178,7 +191,7 @@ public CompilationTestHelper expectNoDiagnostics() {
178191
this.expectNoDiagnostics = true;
179192
return this;
180193
}
181-
194+
182195
/**
183196
* By default, the compilation helper will not run Error Prone on
184197
* compilations that fail with javac errors. This behaviour can be
@@ -210,7 +223,7 @@ public CompilationTestHelper expectResult(Result result) {
210223
expectedResult = Optional.of(result);
211224
return this;
212225
}
213-
226+
214227
/**
215228
* Expects an error message matching {@code matcher} at the line below a comment matching the key.
216229
* For example, given the source
@@ -256,7 +269,7 @@ public void doTest() {
256269
} catch (IOException e) {
257270
throw new IOError(e);
258271
}
259-
}
272+
}
260273
assertTrue("Unused error keys: " + diagnosticHelper.getUnusedLookupKeys(),
261274
diagnosticHelper.getUnusedLookupKeys().isEmpty());
262275
}
@@ -286,13 +299,16 @@ private void checkWellFormed(Iterable<JavaFileObject> sources, String[] args) {
286299
} catch (InvalidCommandLineOptionException e) {
287300
fail("Exception during argument processing: " + e);
288301
}
289-
CompilationTask task = compiler.getTask(
290-
new PrintWriter(outputStream, /*autoFlush=*/true),
291-
fileManager,
292-
null,
293-
buildArguments(Arrays.asList(remainingArgs)),
294-
null,
295-
sources);
302+
CompilationTask task =
303+
compiler.getTask(
304+
new PrintWriter(
305+
new BufferedWriter(new OutputStreamWriter(outputStream, UTF_8)),
306+
/*autoFlush=*/ true),
307+
fileManager,
308+
null,
309+
buildArguments(Arrays.asList(remainingArgs)),
310+
null,
311+
sources);
296312
boolean result = task.call();
297313
assertWithMessage(String.format(
298314
"Test program failed to compile with non Error Prone error: %s",

0 commit comments

Comments
 (0)