Skip to content

Commit

Permalink
Ensure execution stops on failure
Browse files Browse the repository at this point in the history
Bumped JUnit to version 5.10.3
Bumped PMD extension to version 1.1.2
  • Loading branch information
ethauvin committed Jul 3, 2024
1 parent a6747fd commit 8ff14ff
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions examples/src/bld/java/com/example/ExamplesBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ 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) {
new ExamplesBuild().start(args);
}

@BuildCommand(summary = "Generates Jacoco Reports")
public void jacoco() throws IOException {
public void jacoco() throws Exception {
new JacocoReportOperation()
.fromProject(this)
.execute();
Expand Down
2 changes: 1 addition & 1 deletion lib/bld/bld-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
108 changes: 52 additions & 56 deletions src/main/java/rife/bld/extension/JacocoReportOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -252,67 +248,67 @@ public Collection<File> 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);
}

/**
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -381,7 +377,7 @@ public JacocoReportOperation name(String name) {
* @return this operation instance
*/
public JacocoReportOperation quiet(boolean quiet) {
quiet_ = quiet;
silent(quiet);
return this;
}

Expand Down Expand Up @@ -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());
}
Expand All @@ -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());
Expand Down
32 changes: 21 additions & 11 deletions src/test/java/rife/bld/extension/JacocoReportOperationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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;
}
}

0 comments on commit 8ff14ff

Please sign in to comment.