diff --git a/README.md b/README.md index d723eba..282ec85 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ To run the tests and generate the code coverage reports, add the floowing to you ```java @BuildCommand(summary = "Generates Jacoco Reports") -public void jacoco() throws IOException { +public void jacoco() throws Exception { new JacocoReportOperation() .fromProject(this) .execute(); diff --git a/examples/src/bld/java/com/example/ExamplesBuild.java b/examples/src/bld/java/com/example/ExamplesBuild.java index a58fb0d..0c4f60a 100644 --- a/examples/src/bld/java/com/example/ExamplesBuild.java +++ b/examples/src/bld/java/com/example/ExamplesBuild.java @@ -25,8 +25,8 @@ public ExamplesBuild() { repositories = List.of(MAVEN_LOCAL, MAVEN_CENTRAL); scope(test) - .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 2))) - .include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 2))); + .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 3))) + .include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 3))); } public static void main(String[] args) { @@ -34,7 +34,7 @@ public static void main(String[] args) { } @BuildCommand(summary = "Generates Jacoco Reports") - public void jacoco() throws IOException { + public void jacoco() throws Exception { new JacocoReportOperation() .fromProject(this) .execute(); diff --git a/lib/bld/bld-wrapper.properties b/lib/bld/bld-wrapper.properties index 54c6328..0fbcd36 100644 --- a/lib/bld/bld-wrapper.properties +++ b/lib/bld/bld-wrapper.properties @@ -1,6 +1,6 @@ bld.downloadExtensionJavadoc=false bld.downloadExtensionSources=true bld.downloadLocation= -bld.extension-pmd=com.uwyn.rife2:bld-pmd:1.1.0 +bld.extension-pmd=com.uwyn.rife2:bld-pmd:1.1.2 bld.repositories=MAVEN_CENTRAL,MAVEN_LOCAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES bld.version=1.9.1 diff --git a/src/bld/java/rife/bld/extension/JacocoReportOperationBuild.java b/src/bld/java/rife/bld/extension/JacocoReportOperationBuild.java index dd75f21..61bc5ea 100644 --- a/src/bld/java/rife/bld/extension/JacocoReportOperationBuild.java +++ b/src/bld/java/rife/bld/extension/JacocoReportOperationBuild.java @@ -47,8 +47,8 @@ public JacocoReportOperationBuild() { .include(dependency("org.jacoco", "jacoco", jacocoVersion).exclude("*", "org.jacoco.doc")) .include(dependency("com.uwyn.rife2", "bld", version(1, 9, 1))); scope(test) - .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 2))) - .include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 2))) + .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 3))) + .include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 3))) .include(dependency("org.assertj", "assertj-core", version(3, 26, 0))); javadocOperation() @@ -91,7 +91,7 @@ public static void main(String[] args) { } @BuildCommand(summary = "Runs PMD analysis") - public void pmd() { + public void pmd() throws Exception { new PmdOperation() .fromProject(this) .failOnViolation(true) diff --git a/src/main/java/rife/bld/extension/JacocoReportOperation.java b/src/main/java/rife/bld/extension/JacocoReportOperation.java index 75c633b..b055eab 100644 --- a/src/main/java/rife/bld/extension/JacocoReportOperation.java +++ b/src/main/java/rife/bld/extension/JacocoReportOperation.java @@ -82,10 +82,6 @@ public class JacocoReportOperation extends AbstractOperation execFiles() { * Performs the operation execution that can be wrapped by the {@code #executeOnce} call. */ @Override - public void execute() throws IOException { - if ((project_ == null) && LOGGER.isLoggable(Level.SEVERE)) { - LOGGER.severe("A project must be specified."); - } else { - var buildJacocoReportsDir = Path.of(project_.buildDirectory().getPath(), "reports", "jacoco", "test").toFile(); - var buildJacocoExecDir = Path.of(project_.buildDirectory().getPath(), "jacoco").toFile(); - var buildJacocoExec = Path.of(buildJacocoExecDir.getPath(), "jacoco.exec").toFile(); - - if (destFile_ == null) { - destFile_ = Path.of(buildJacocoExecDir.getPath(), "jacoco.exec").toFile(); + public void execute() throws Exception { + if ((project_ == null)) { + if (LOGGER.isLoggable(Level.SEVERE) && !silent()) { + LOGGER.severe("A project must be specified."); } + throw new ExitStatusException(ExitStatusException.EXIT_FAILURE); + } - if (execFiles_.isEmpty()) { - var testOperation = project_.testOperation().fromProject(project_); - testOperation.javaOptions().javaAgent(Path.of(project_.libBldDirectory().getPath(), - "org.jacoco.agent-" + JaCoCo.VERSION.substring(0, JaCoCo.VERSION.lastIndexOf('.')) - + "-runtime.jar").toFile(), "destfile=" + destFile_.getPath()); - try { - testOperation.execute(); - } catch (InterruptedException | ExitStatusException e) { - throw new IOException(e); - } + var buildJacocoReportsDir = Path.of(project_.buildDirectory().getPath(), "reports", "jacoco", "test").toFile(); + var buildJacocoExecDir = Path.of(project_.buildDirectory().getPath(), "jacoco").toFile(); + var buildJacocoExec = Path.of(buildJacocoExecDir.getPath(), "jacoco.exec").toFile(); - if (LOGGER.isLoggable(Level.INFO) && !quiet_) { - LOGGER.log(Level.INFO, "Execution Data: {0}", destFile_); - } + if (destFile_ == null) { + destFile_ = Path.of(buildJacocoExecDir.getPath(), "jacoco.exec").toFile(); + } - if (buildJacocoExec.exists()) { - execFiles_.add(buildJacocoExec); - } - } + if (execFiles_.isEmpty()) { + var testOperation = project_.testOperation().fromProject(project_); + testOperation.javaOptions().javaAgent(Path.of(project_.libBldDirectory().getPath(), + "org.jacoco.agent-" + JaCoCo.VERSION.substring(0, JaCoCo.VERSION.lastIndexOf('.')) + + "-runtime.jar").toFile(), "destfile=" + destFile_.getPath()); - if (sourceFiles_.isEmpty()) { - sourceFiles_.add(project_.srcMainJavaDirectory()); - } + testOperation.execute(); - if (classFiles_.isEmpty()) { - classFiles_.add(project_.buildMainDirectory()); + if (LOGGER.isLoggable(Level.INFO) && !silent()) { + LOGGER.log(Level.INFO, "Execution Data: {0}", destFile_); } - if (html_ == null) { - html_ = new File(buildJacocoReportsDir, "html"); + if (buildJacocoExec.exists()) { + execFiles_.add(buildJacocoExec); } + } - if (xml_ == null) { - xml_ = new File(buildJacocoReportsDir, "jacocoTestReport.xml"); - } + if (sourceFiles_.isEmpty()) { + sourceFiles_.add(project_.srcMainJavaDirectory()); + } - if (csv_ == null) { - csv_ = new File(buildJacocoReportsDir, "jacocoTestReport.csv"); - } + if (classFiles_.isEmpty()) { + classFiles_.add(project_.buildMainDirectory()); + } - //noinspection ResultOfMethodCallIgnored - buildJacocoReportsDir.mkdirs(); - //noinspection ResultOfMethodCallIgnored - buildJacocoExecDir.mkdirs(); + if (html_ == null) { + html_ = new File(buildJacocoReportsDir, "html"); + } + + if (xml_ == null) { + xml_ = new File(buildJacocoReportsDir, "jacocoTestReport.xml"); + } - var loader = loadExecFiles(); - var bundle = analyze(loader.getExecutionDataStore()); - writeReports(bundle, loader); + if (csv_ == null) { + csv_ = new File(buildJacocoReportsDir, "jacocoTestReport.csv"); } + + //noinspection ResultOfMethodCallIgnored + buildJacocoReportsDir.mkdirs(); + //noinspection ResultOfMethodCallIgnored + buildJacocoExecDir.mkdirs(); + + var loader = loadExecFiles(); + var bundle = analyze(loader.getExecutionDataStore()); + writeReports(bundle, loader); } /** @@ -349,11 +345,11 @@ public JacocoReportOperation html(String html) { private ExecFileLoader loadExecFiles() throws IOException { var loader = new ExecFileLoader(); - if (execFiles_.isEmpty() && LOGGER.isLoggable(Level.WARNING) && !quiet_) { + if (execFiles_.isEmpty() && LOGGER.isLoggable(Level.WARNING) && !silent()) { LOGGER.warning("No execution data files provided."); } else { for (var f : execFiles_) { - if (LOGGER.isLoggable(Level.INFO) && !quiet_) { + if (LOGGER.isLoggable(Level.INFO) && !silent()) { LOGGER.log(Level.INFO, "Loading execution data: {0}", f.getAbsolutePath()); } @@ -381,7 +377,7 @@ public JacocoReportOperation name(String name) { * @return this operation instance */ public JacocoReportOperation quiet(boolean quiet) { - quiet_ = quiet; + silent(quiet); return this; } @@ -470,7 +466,7 @@ public JacocoReportOperation tabWidth(int tabWidth) { private void writeReports(IBundleCoverage bundle, ExecFileLoader loader) throws IOException { - if (LOGGER.isLoggable(Level.INFO) && !quiet_) { + if (LOGGER.isLoggable(Level.INFO) && !silent()) { LOGGER.log(Level.INFO, "Analyzing {0} classes.", bundle.getClassCounter().getTotalCount()); } @@ -479,7 +475,7 @@ private void writeReports(IBundleCoverage bundle, ExecFileLoader loader) loader.getExecutionDataStore().getContents()); visitor.visitBundle(bundle, sourceLocator()); visitor.visitEnd(); - if (LOGGER.isLoggable(Level.INFO) && !quiet_) { + if (LOGGER.isLoggable(Level.INFO) && !silent()) { LOGGER.log(Level.INFO, "XML Report: file://{0}", xml_.toURI().getPath()); LOGGER.log(Level.INFO, "CSV Report: file://{0}", csv_.toURI().getPath()); LOGGER.log(Level.INFO, "HTML Report: file://{0}index.html", html_.toURI().getPath()); diff --git a/src/test/java/rife/bld/extension/JacocoReportOperationTest.java b/src/test/java/rife/bld/extension/JacocoReportOperationTest.java index aaccc02..30821d0 100644 --- a/src/test/java/rife/bld/extension/JacocoReportOperationTest.java +++ b/src/test/java/rife/bld/extension/JacocoReportOperationTest.java @@ -18,6 +18,7 @@ import org.junit.jupiter.api.Test; import rife.bld.Project; +import rife.bld.operations.exceptions.ExitStatusException; import java.io.File; import java.io.IOException; @@ -26,6 +27,7 @@ import java.util.Objects; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; class JacocoReportOperationTest { final File csv; @@ -52,7 +54,13 @@ static void deleteOnExit(File folder) { } @Test - void executeTest() throws IOException { + void executeFailureTest() { + var op = new JacocoReportOperation().fromProject(new Project()); + assertThatCode(op::execute).isInstanceOf(ExitStatusException.class); + } + + @Test + void executeTest() throws Exception { newJacocoReportOperation().execute(); assertThat(csv).exists(); @@ -64,18 +72,20 @@ void executeTest() throws IOException { } assertThat(Path.of(html.getPath(), "com.example", "Examples.java.html")).exists(); - deleteOnExit(tempDir.toFile()); } JacocoReportOperation newJacocoReportOperation() { - var o = new JacocoReportOperation(); - o.fromProject(new Project()); - o.csv(csv); - o.html(html); - o.xml(xml); - o.classFiles(new File("src/test/resources/Examples.class")); - o.sourceFiles(new File("examples/src/main/java")); - o.execFiles(new File("src/test/resources/jacoco.exec")); - return o; + var op = new JacocoReportOperation() + .fromProject(new Project()) + .csv(csv) + .html(html) + .xml(xml) + .classFiles(new File("src/test/resources/Examples.class")) + .sourceFiles(new File("examples/src/main/java")) + .execFiles(new File("src/test/resources/jacoco.exec")); + + deleteOnExit(tempDir.toFile()); + + return op; } }