Ensure execution stops on failure

Bumped JUnit to version 5.10.3
Bumped PMD extension to version 1.1.2
This commit is contained in:
Erik C. Thauvin 2024-06-24 23:26:46 -07:00
parent a6747fdfe5
commit 8ff14ff9f9
Signed by: erik
GPG key ID: 776702A6A2DA330E
6 changed files with 93 additions and 87 deletions

View file

@ -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();

View file

@ -25,8 +25,8 @@ public class ExamplesBuild extends Project {
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 class ExamplesBuild extends Project {
}
@BuildCommand(summary = "Generates Jacoco Reports")
public void jacoco() throws IOException {
public void jacoco() throws Exception {
new JacocoReportOperation()
.fromProject(this)
.execute();

View file

@ -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

View file

@ -47,8 +47,8 @@ public class JacocoReportOperationBuild extends Project {
.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 class JacocoReportOperationBuild extends Project {
}
@BuildCommand(summary = "Runs PMD analysis")
public void pmd() {
public void pmd() throws Exception {
new PmdOperation()
.fromProject(this)
.failOnViolation(true)

View file

@ -82,10 +82,6 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* The project reference.
*/
private BaseProject project_;
/**
* The quiet flag.
*/
private boolean quiet_;
/**
* The report name.
*/
@ -252,67 +248,67 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* 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.");
}
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);
}
if (LOGGER.isLoggable(Level.INFO) && !quiet_) {
LOGGER.log(Level.INFO, "Execution Data: {0}", destFile_);
}
if (buildJacocoExec.exists()) {
execFiles_.add(buildJacocoExec);
}
}
if (sourceFiles_.isEmpty()) {
sourceFiles_.add(project_.srcMainJavaDirectory());
}
if (classFiles_.isEmpty()) {
classFiles_.add(project_.buildMainDirectory());
}
if (html_ == null) {
html_ = new File(buildJacocoReportsDir, "html");
}
if (xml_ == null) {
xml_ = new File(buildJacocoReportsDir, "jacocoTestReport.xml");
}
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);
throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
}
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();
}
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());
testOperation.execute();
if (LOGGER.isLoggable(Level.INFO) && !silent()) {
LOGGER.log(Level.INFO, "Execution Data: {0}", destFile_);
}
if (buildJacocoExec.exists()) {
execFiles_.add(buildJacocoExec);
}
}
if (sourceFiles_.isEmpty()) {
sourceFiles_.add(project_.srcMainJavaDirectory());
}
if (classFiles_.isEmpty()) {
classFiles_.add(project_.buildMainDirectory());
}
if (html_ == null) {
html_ = new File(buildJacocoReportsDir, "html");
}
if (xml_ == null) {
xml_ = new File(buildJacocoReportsDir, "jacocoTestReport.xml");
}
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 class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
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 class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
* @return this operation instance
*/
public JacocoReportOperation quiet(boolean quiet) {
quiet_ = quiet;
silent(quiet);
return this;
}
@ -470,7 +466,7 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
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 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
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());

View file

@ -18,6 +18,7 @@ package rife.bld.extension;
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.nio.file.Path;
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 @@ class JacocoReportOperationTest {
}
@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 @@ class JacocoReportOperationTest {
}
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;
}
}