Search in sources :

Example 26 with Result

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

the class ErrorProneCompilerIntegrationTest method invalidFlagCausesCmdErrResult.

@Test
public void invalidFlagCausesCmdErrResult() throws Exception {
    String[] args = { "-Xep:" };
    Result exitCode = compiler.compile(args, Arrays.asList(compiler.fileManager().forSourceLines("Test.java", "public class Test {", "  public Test() {}", "}")));
    outputStream.flush();
    assertThat(outputStream.toString(), exitCode, is(Result.CMDERR));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Example 27 with Result

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

the class ErrorProneCompilerIntegrationTest method severityIsResetOnNextCompilation.

@Test
public void severityIsResetOnNextCompilation() throws Exception {
    String[] testFile = { "public class Test {", "  void doIt (int i) {", "    i = i;", "  }", "}" };
    String[] args = { "-Xep:SelfAssignment:WARN" };
    Result exitCode = compiler.compile(args, Arrays.asList(compiler.fileManager().forSourceLines("Test.java", testFile)));
    outputStream.flush();
    Matcher<? super Iterable<Diagnostic<? extends JavaFileObject>>> matcher = hasItem(diagnosticMessage(containsString("[SelfAssignment]")));
    assertThat(outputStream.toString(), exitCode, is(Result.OK));
    assertTrue("Warning should be found. " + diagnosticHelper.describe(), matcher.matches(diagnosticHelper.getDiagnostics()));
    // Should reset to default severity (ERROR)
    exitCode = compiler.compile(Arrays.asList(compiler.fileManager().forSourceLines("Test.java", testFile)));
    outputStream.flush();
    assertThat(outputStream.toString(), exitCode, is(Result.ERROR));
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Diagnostic(javax.tools.Diagnostic) Matchers.containsString(org.hamcrest.Matchers.containsString) Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Example 28 with Result

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

the class ErrorProneCompilerIntegrationTest method cannotSuppressGeneratedError.

@Test
public void cannotSuppressGeneratedError() throws Exception {
    String[] generatedFile = { "@javax.annotation.Generated(\"Foo\")", "class Generated {", "  public Generated() {", "    if (true);", "  }", "}" };
    String[] args = { "-Xep:EmptyIf:ERROR", "-XepDisableWarningsInGeneratedCode" };
    Result exitCode = compiler.compile(args, Arrays.asList(compiler.fileManager().forSourceLines("Generated.java", generatedFile)));
    outputStream.flush();
    assertThat(diagnosticHelper.getDiagnostics()).hasSize(1);
    assertThat(diagnosticHelper.getDiagnostics().get(0).getMessage(Locale.ENGLISH)).contains("[EmptyIf]");
    assertThat(outputStream.toString(), exitCode, is(Result.ERROR));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Example 29 with Result

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

the class CommandLineFlagTest method malformedFlag.

/* Tests for new-style ("-Xep:") flags */
@Test
public void malformedFlag() throws Exception {
    ErrorProneTestCompiler compiler = builder.build();
    List<String> badArgs = Arrays.asList(// too many parts
    "-Xep:Foo:WARN:jfkdlsdf", // no check name
    "-Xep:", // nonexistent severity level
    "-Xep:Foo:FJDKFJSD");
    Result exitCode;
    for (String badArg : badArgs) {
        exitCode = compiler.compile(new String[] { badArg }, Collections.<JavaFileObject>emptyList());
        assertThat(exitCode).isEqualTo(Result.CMDERR);
        assertThat(output.toString()).contains("invalid flag");
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Example 30 with Result

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

the class CommandLineFlagTest method cantOverrideNonexistentCheck.

@Test
public void cantOverrideNonexistentCheck() throws Exception {
    ErrorProneTestCompiler compiler = builder.build();
    List<JavaFileObject> sources = compiler.fileManager().forResources(getClass(), "CommandLineFlagTestFile.java");
    List<String> badOptions = Arrays.asList("-Xep:BogusChecker:ERROR", "-Xep:BogusChecker:WARN", "-Xep:BogusChecker:OFF", "-Xep:BogusChecker");
    for (String badOption : badOptions) {
        Result exitCode = compiler.compile(new String[] { badOption }, sources);
        assertThat(exitCode).isEqualTo(Result.CMDERR);
        assertThat(output.toString()).contains("BogusChecker is not a valid checker name");
    }
}
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