Search in sources :

Example 1 with Result

use of com.sun.tools.javac.main.Main.Result in project error-prone by google.

the class RefasterRuleCompiler method run.

private Result run(String[] argv, Context context) {
    try {
        argv = prepareCompilation(argv, context);
    } catch (InvalidCommandLineOptionException e) {
        System.err.println(e.getMessage());
        System.err.flush();
        return Result.CMDERR;
    }
    try {
        Result compileResult = new Main("RefasterRuleCompiler", new PrintWriter(new OutputStreamWriter(System.err, StandardCharsets.UTF_8))).compile(argv, context);
        System.err.flush();
        return compileResult;
    } catch (InvalidCommandLineOptionException e) {
        System.err.println(e.getMessage());
        System.err.flush();
        return Result.CMDERR;
    }
}
Also used : InvalidCommandLineOptionException(com.google.errorprone.InvalidCommandLineOptionException) OutputStreamWriter(java.io.OutputStreamWriter) Main(com.sun.tools.javac.main.Main) Result(com.sun.tools.javac.main.Main.Result) PrintWriter(java.io.PrintWriter)

Example 2 with Result

use of com.sun.tools.javac.main.Main.Result in project error-prone by google.

the class CompilationTestHelper method doTest.

/**
   * Performs a compilation and checks that the diagnostics and result match the expectations.
   */
// TODO(eaftan): any way to ensure that this is actually called?
public void doTest() {
    Preconditions.checkState(!sources.isEmpty(), "No source files to compile");
    List<String> allArgs = buildArguments(args);
    Result result = compile(sources, allArgs.toArray(new String[allArgs.size()]));
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticHelper.getDiagnostics()) {
        if (diagnostic.getCode().contains("error.prone.crash")) {
            fail(diagnostic.getMessage(Locale.ENGLISH));
        }
    }
    if (expectNoDiagnostics) {
        List<Diagnostic<? extends JavaFileObject>> diagnostics = diagnosticHelper.getDiagnostics();
        assertWithMessage(String.format("Expected no diagnostics produced, but found %d: %s", diagnostics.size(), diagnostics)).that(diagnostics.size()).isEqualTo(0);
    } else {
        for (JavaFileObject source : sources) {
            try {
                diagnosticHelper.assertHasDiagnosticOnAllMatchingLines(source, lookForCheckNameInDiagnostic);
            } catch (IOException e) {
                throw new IOError(e);
            }
        }
        assertTrue("Unused error keys: " + diagnosticHelper.getUnusedLookupKeys(), diagnosticHelper.getUnusedLookupKeys().isEmpty());
    }
    if (expectedResult.isPresent()) {
        assertWithMessage(String.format("Expected compilation result %s, but was %s", expectedResult.get(), result)).that(result).isEqualTo(expectedResult.get());
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) IOError(java.io.IOError) LookForCheckNameInDiagnostic(com.google.errorprone.DiagnosticTestHelper.LookForCheckNameInDiagnostic) Diagnostic(javax.tools.Diagnostic) IOException(java.io.IOException) Result(com.sun.tools.javac.main.Main.Result)

Example 3 with Result

use of com.sun.tools.javac.main.Main.Result in project error-prone by google.

the class CommandLineFlagTest method canEnableWithOverriddenSeverity.

@Test
public void canEnableWithOverriddenSeverity() throws Exception {
    ErrorProneTestCompiler compiler = builder.build();
    List<JavaFileObject> sources = compiler.fileManager().forResources(EmptyIfStatement.class, "testdata/EmptyIfStatementPositiveCases.java");
    Result exitCode = compiler.compile(sources);
    assertThat(exitCode).isEqualTo(Result.OK);
    assertThat(diagnosticHelper.getDiagnostics()).isEmpty();
    diagnosticHelper.clearDiagnostics();
    exitCode = compiler.compile(new String[] { "-Xep:EmptyIf:WARN" }, sources);
    assertThat(exitCode).isEqualTo(Result.OK);
    assertThat(diagnosticHelper.getDiagnostics()).isNotEmpty();
    assertThat(diagnosticHelper.getDiagnostics().toString()).contains("[EmptyIf]");
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Example 4 with Result

use of com.sun.tools.javac.main.Main.Result in project error-prone by google.

the class CommandLineFlagTest method canDisable.

@Test
public void canDisable() throws Exception {
    ErrorProneTestCompiler compiler = builder.report(ScannerSupplier.fromBugCheckerClasses(DisableableChecker.class)).build();
    List<JavaFileObject> sources = compiler.fileManager().forResources(getClass(), "CommandLineFlagTestFile.java");
    Result exitCode = compiler.compile(sources);
    assertThat(exitCode).isEqualTo(Result.ERROR);
    diagnosticHelper.clearDiagnostics();
    exitCode = compiler.compile(new String[] { "-Xep:DisableableChecker:OFF" }, sources);
    assertThat(exitCode).isEqualTo(Result.OK);
    assertThat(diagnosticHelper.getDiagnostics()).isEmpty();
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Example 5 with Result

use of com.sun.tools.javac.main.Main.Result in project error-prone by google.

the class CommandLineFlagTest method canEnableWithDefaultSeverity.

// We have to use one of the built-in checkers for the following two tests because there is no
// way to specify a custom checker and have it be off by default.
@Test
public void canEnableWithDefaultSeverity() throws Exception {
    ErrorProneTestCompiler compiler = builder.build();
    List<JavaFileObject> sources = compiler.fileManager().forResources(EmptyIfStatement.class, "testdata/EmptyIfStatementPositiveCases.java");
    Result exitCode = compiler.compile(sources);
    assertThat(exitCode).isEqualTo(Result.OK);
    assertThat(diagnosticHelper.getDiagnostics()).isEmpty();
    exitCode = compiler.compile(new String[] { "-Xep:EmptyIf" }, sources);
    assertThat(exitCode).isEqualTo(Result.ERROR);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Aggregations

Result (com.sun.tools.javac.main.Main.Result)36 Test (org.junit.Test)34 JavaFileObject (javax.tools.JavaFileObject)20 Diagnostic (javax.tools.Diagnostic)10 Matchers.containsString (org.hamcrest.Matchers.containsString)7 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)2 Path (java.nio.file.Path)2 ImmutableList (com.google.common.collect.ImmutableList)1 LookForCheckNameInDiagnostic (com.google.errorprone.DiagnosticTestHelper.LookForCheckNameInDiagnostic)1 InvalidCommandLineOptionException (com.google.errorprone.InvalidCommandLineOptionException)1 BadShiftAmount (com.google.errorprone.bugpatterns.BadShiftAmount)1 BugChecker (com.google.errorprone.bugpatterns.BugChecker)1 NonAtomicVolatileUpdate (com.google.errorprone.bugpatterns.NonAtomicVolatileUpdate)1 Main (com.sun.tools.javac.main.Main)1 IOError (java.io.IOError)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 List (java.util.List)1 JarEntry (java.util.jar.JarEntry)1