@Test public void testInvalidAnnotationsOnInterface() { CommandSpec2YamlProcessor processor = new CommandSpec2YamlProcessor(); Compilation compilation = javac() .withProcessors(processor) .compile(JavaFileObjects.forResource( "picocli/examples/annotatedmethods/InvalidAnnotatedInterfaceMethodOptions.java")); assertThat(compilation).failed(); ImmutableList<Diagnostic<? extends JavaFileObject>> errors = compilation.errors(); assertEquals("expected error count", 3, errors.size()); for (Diagnostic<? extends JavaFileObject> diag : errors) { MatcherAssert.assertThat(diag.getMessage(Locale.ENGLISH), containsString("Invalid picocli annotation on interface field")); } //assertThat(compilation).hadErrorContaining("Invalid picocli annotation on interface field"); }
/** * Returns the file with name {@code fileName} in package {@code packageName} if one was * generated. * * <p>For example: * * <pre> * {@code Optional<JavaFileObject>} fooClassFile = * compilation.generatedFile(CLASS_OUTPUT, "com.google.myapp", "Foo.class"); * </pre> * * @throws IllegalStateException for {@linkplain #status() failed compilations}, since the state * of the generated files is undefined in that case */ public Optional<JavaFileObject> generatedFile( Location location, String packageName, String fileName) { return generatedFile( location, packageName.isEmpty() ? fileName : packageName.replace('.', '/') + '/' + fileName); }
boolean succeeded = task.call(); Compilation compilation = new Compilation( this, files, diagnosticCollector.getDiagnostics(), fileManager.getOutputFiles()); if (compilation.status().equals(Status.FAILURE) && compilation.errors().isEmpty()) { throw new CompilationFailureException(compilation);
/** Asserts that the compilation succeeded. */ public void succeeded() { if (actual().status().equals(FAILURE)) { failWithRawMessage( actual().describeFailureDiagnostics() + actual().describeGeneratedSourceFiles()); } }
assertEquals(compilation.generatedSourceFiles().size(), 1);
@Nonnull public CompilationAssert isFailed() { isNotNull(); final Compilation.Status actual = this.actual.status(); assertThat(actual) .overridingErrorMessage("Compilation was expected to fail, but was succeeded") .isEqualTo(Compilation.Status.FAILURE); return this; }
/** Asserts that the compilation failed. */ public void failed() { if (actual().status().equals(SUCCESS)) { failWithRawMessage( "Compilation was expected to fail, but contained no errors.\n\n" + actual().describeGeneratedSourceFiles()); } }
/** * Returns the file at {@code path} if one was generated. * * <p>For example: * * <pre> * {@code Optional<JavaFileObject>} fooClassFile = * compilation.generatedFile(CLASS_OUTPUT, "com/google/myapp/Foo.class"); * </pre> * * @throws IllegalStateException for {@linkplain #status() failed compilations}, since the state * of the generated files is undefined in that case */ public Optional<JavaFileObject> generatedFile(Location location, String path) { // We're relying on the implementation of location.getName() to be equivalent to the first // part of the path. String expectedFilename = String.format("%s/%s", location.getName(), path); return generatedFiles() .stream() .filter(generated -> generated.toUri().getPath().endsWith(expectedFilename)) .findFirst(); }
ImmutableList<Diagnostic<? extends JavaFileObject>> diagnosticsOfKind(Kind kind, Kind... more) { ImmutableSet<Kind> kinds = Sets.immutableEnumSet(kind, more); return diagnostics() .stream() .filter(diagnostic -> kinds.contains(diagnostic.getKind())) .collect(toImmutableList()); }
/** * Source files generated during compilation. * * @throws IllegalStateException for {@linkplain #status() failed compilations}, since the state * of the generated files is undefined in that case */ public ImmutableList<JavaFileObject> generatedSourceFiles() { return generatedFiles() .stream() .filter(generatedFile -> generatedFile.getKind().equals(JavaFileObject.Kind.SOURCE)) .collect(toImmutableList()); }
/** Returns a description of the why the compilation failed. */ String describeFailureDiagnostics() { ImmutableList<Diagnostic<? extends JavaFileObject>> errors = errors(); if (errors.isEmpty()) { return "Compilation produced no errors.\n"; } StringBuilder message = new StringBuilder("Compilation produced the following errors:\n"); errors.stream().forEach(error -> message.append(error).append('\n')); // If we compiled with -Werror we should output the warnings too if (compiler.options().contains("-Werror")) { warnings().stream().forEach(warning -> message.append(warning).append('\n')); } return message.toString(); }
/** Returns a description of the why the compilation failed. */ String describeFailureDiagnostics() { ImmutableList<Diagnostic<? extends JavaFileObject>> diagnostics = diagnostics(); if (diagnostics.isEmpty()) { return "Compilation produced no diagnostics.\n"; } StringBuilder message = new StringBuilder("Compilation produced the following diagnostics:\n"); diagnostics.forEach(diagnostic -> message.append(diagnostic).append('\n')); return message.toString(); }
Compilation compilation = new Compilation( Compiler.javac(), files,
/** * Returns the file with name {@code fileName} in package {@code packageName} if one was * generated. * * <p>For example: * * <pre> * {@code Optional<JavaFileObject>} fooClassFile = * compilation.generatedFile(CLASS_OUTPUT, "com.google.myapp", "Foo.class"); * </pre> * * @throws IllegalStateException for {@linkplain #status() failed compilations}, since the state * of the generated files is undefined in that case */ public Optional<JavaFileObject> generatedFile( Location location, String packageName, String fileName) { return generatedFile( location, packageName.isEmpty() ? fileName : packageName.replace('.', '/') + '/' + fileName); }
boolean succeeded = task.call(); Compilation compilation = new Compilation( this, files, diagnosticCollector.getDiagnostics(), fileManager.getOutputFiles()); if (compilation.status().equals(Status.FAILURE) && compilation.errors().isEmpty()) { throw new CompilationFailureException(compilation);
/** Asserts that the compilation succeeded. */ public void succeeded() { if (actual().status().equals(FAILURE)) { failWithoutActual( simpleFact( actual().describeFailureDiagnostics() + actual().describeGeneratedSourceFiles())); } }
assertEquals(compilation.generatedSourceFiles().size(), 7);
@Nonnull public CompilationAssert isSucceeded() { isNotNull(); final Compilation.Status actual = this.actual.status(); assertThat(actual) .overridingErrorMessage("Compilation was expected to succeed, but was failed") .isEqualTo(Compilation.Status.SUCCESS); return this; }
/** Asserts that the compilation failed. */ public void failed() { if (actual().status().equals(SUCCESS)) { failWithoutActual( simpleFact( "Compilation was expected to fail, but contained no errors.\n\n" + actual().describeGeneratedSourceFiles())); } }
/** * Returns the file at {@code path} if one was generated. * * <p>For example: * * <pre> * {@code Optional<JavaFileObject>} fooClassFile = * compilation.generatedFile(CLASS_OUTPUT, "com/google/myapp/Foo.class"); * </pre> * * @throws IllegalStateException for {@linkplain #status() failed compilations}, since the state * of the generated files is undefined in that case */ public Optional<JavaFileObject> generatedFile(Location location, String path) { // We're relying on the implementation of location.getName() to be equivalent to the first // part of the path. String expectedFilename = String.format("%s/%s", location.getName(), path); return generatedFiles() .stream() .filter(generated -> generated.toUri().getPath().endsWith(expectedFilename)) .findFirst(); }