Added tests

This commit is contained in:
Erik C. Thauvin 2023-08-11 19:50:04 -07:00
parent 26c1f11e84
commit 541ed74d9f
7 changed files with 68 additions and 35 deletions

View file

@ -53,8 +53,6 @@ public class JacocoReportOperationBuild extends Project {
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 0)))
.include(dependency("org.assertj:assertj-joda-time:2.2.0"));
testOperation().mainClass("rife.bld.extension.JacocoReportOperationTest");
javadocOperation()
.javadocOptions()
.docLint(NO_MISSING)
@ -62,7 +60,7 @@ public class JacocoReportOperationBuild extends Project {
publishOperation()
.repository(version.isSnapshot() ? repository("rife2-snapshot") : repository("rife2"))
// .repository(MAVEN_LOCAL)
// .repository(MAVEN_LOCAL)
.info()
.groupId("com.uwyn.rife2")
.artifactId("bld-jacoco-report")
@ -84,7 +82,7 @@ public class JacocoReportOperationBuild extends Project {
}
@BuildCommand(summary = "Runs PMD analysis")
public void pmd() throws Exception {
public void pmd() {
new PmdOperation()
.fromProject(this)
.failOnViolation(true)

View file

@ -142,34 +142,38 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
@Override
public void execute() throws IOException {
if ((project == null) && LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.severe("A project and version must be specified.");
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 (execFiles.isEmpty()) {
// project.testOperation().fromProject(project).javaOptions().javaAgent(
// Path.of(project.libBldDirectory().getPath(), "org.jacoco.agent-"
// + JaCoCo.VERSION.substring(0, JaCoCo.VERSION.lastIndexOf('.')) + "-runtime.jar").toFile(),
// "destfile=" + Path.of(buildJacocoExecDir.getPath(), "jacoco.exec"));
project.testOperation().fromProject(project).javaOptions().add("-javaagent:" +
Path.of(project.libBldDirectory().getPath(), "org.jacoco.agent-"
+ JaCoCo.VERSION.substring(0, JaCoCo.VERSION.lastIndexOf('.')) + "-runtime.jar")
+ "=destfile=" + Path.of(buildJacocoExecDir.getPath(), "jacoco.exec"));
try {
project.testOperation().execute();
} catch (InterruptedException | ExitStatusException e) {
throw new IOException(e);
project.testOperation().fromProject(project).javaOptions().add("-javaagent:" +
Path.of(project.libBldDirectory().getPath(), "org.jacoco.agent-"
+ JaCoCo.VERSION.substring(0, JaCoCo.VERSION.lastIndexOf('.')) + "-runtime.jar")
+ "=destfile=" + Path.of(buildJacocoExecDir.getPath(), "jacoco.exec"));
try {
project.testOperation().execute();
} catch (InterruptedException | ExitStatusException e) {
throw new IOException(e);
}
if (buildJacocoExec.exists()) {
execFiles.add(buildJacocoExec);
}
}
if (sourceFiles.isEmpty()) {
sourceFiles.add(project.srcDirectory());
sourceFiles.add(project.srcTestDirectory());
sourceFiles.add(project.srcMainJavaDirectory());
}
if (classFiles.isEmpty()) {
classFiles.add(project.buildMainDirectory());
classFiles.add(project.buildTestDirectory());
}
if (html == null) {
@ -184,10 +188,6 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
csv = new File(buildJacocoReportsDir, "jacocoTestReport.csv");
}
if (execFiles.isEmpty() && (buildJacocoExec.exists())) {
execFiles.add(buildJacocoExec);
}
//noinspection ResultOfMethodCallIgnored
buildJacocoReportsDir.mkdirs();
@ -267,7 +267,7 @@ public class JacocoReportOperation extends AbstractOperation<JacocoReportOperati
}
/**
* Sets the locations of the source files.
* Sets the locations of the source files. (e.g., {@code src/main/java})
**/
public JacocoReportOperation sourceFiles(File... sourceFiles) {
this.sourceFiles.addAll(Arrays.stream(sourceFiles).toList());

View file

@ -17,29 +17,64 @@
package rife.bld.extension;
import org.junit.jupiter.api.Test;
import rife.bld.Project;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import static org.assertj.core.api.Assertions.assertThat;
class JacocoReportOperationTest {
final File csv;
final File html;
final Path tempDir;
final File xml;
JacocoReportOperationTest() throws IOException {
tempDir = Files.createTempDirectory("jacoco-test");
csv = (new File(tempDir.toFile(), "jacoco.csv"));
html = (new File(tempDir.toFile(), "html"));
xml = (new File(tempDir.toFile(), "jacoco.xml"));
}
static void deleteOnExit(File folder) {
folder.deleteOnExit();
for (var f : Objects.requireNonNull(folder.listFiles())) {
if (f.isDirectory()) {
deleteOnExit(f);
} else {
f.deleteOnExit();
}
}
}
@Test
void executeTest() throws IOException {
newJacocoReportOperation().execute();
assertThat(csv).exists();
assertThat(html).isDirectory();
assertThat(xml).exists();
try (var lines = Files.lines(xml.toPath())) {
assertThat(lines.anyMatch(s ->
s.contains("<counter type=\"INSTRUCTION\" missed=\"0\" covered=\"3\"/>"))).isTrue();
}
// deleteOnExit(tempDir.toFile());
}
JacocoReportOperation newJacocoReportOperation() {
var o = new JacocoReportOperation();
o.csv(new File(tempDir.toFile(), "jacoco.csv"));
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;
}
@Test
void executeTest() {
assertThat(true).isTrue(); // TODO
}
}

Binary file not shown.

Binary file not shown.