Ensured exit status is set on failure

This commit is contained in:
Erik C. Thauvin 2024-07-22 17:13:41 -07:00
parent 6faea5a8f4
commit 8a5b9c9431
Signed by: erik
GPG key ID: 776702A6A2DA330E
2 changed files with 47 additions and 28 deletions

View file

@ -18,10 +18,14 @@ package rife.bld.extension;
import rife.bld.BaseProject; import rife.bld.BaseProject;
import rife.bld.operations.AbstractProcessOperation; import rife.bld.operations.AbstractProcessOperation;
import rife.bld.operations.exceptions.ExitStatusException;
import java.nio.file.Path; import java.io.File;
import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* Mutation testing and coverage with <a href="https://pitest.org">PIT</a>. * Mutation testing and coverage with <a href="https://pitest.org">PIT</a>.
@ -34,17 +38,12 @@ public class PitestOperation extends AbstractProcessOperation<PitestOperation> {
* False constant. * False constant.
*/ */
protected static final String FALSE = "false"; protected static final String FALSE = "false";
/**
* Source directories command line option.
*/
protected static final String SOURCE_DIRS = "--sourceDirs";
/** /**
* True constant. * True constant.
*/ */
protected static final String TRUE = "true"; protected static final String TRUE = "true";
/** private static final Logger LOGGER = Logger.getLogger(PitestOperation.class.getName());
* The PIT options. private static final String SOURCE_DIRS = "--sourceDirs";
*/
private final Map<String, String> options_ = new ConcurrentHashMap<>(); private final Map<String, String> options_ = new ConcurrentHashMap<>();
private BaseProject project_; private BaseProject project_;
@ -330,33 +329,45 @@ public class PitestOperation extends AbstractProcessOperation<PitestOperation> {
return this; return this;
} }
@Override
public void execute() throws IOException, InterruptedException, ExitStatusException {
if (project_ == null) {
if (LOGGER.isLoggable(Level.SEVERE) && !silent()) {
LOGGER.severe("A project must be specified.");
}
throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
} else {
super.execute();
}
}
/** /**
* Part of the {@link #execute} operation, constructs the command list * Part of the {@link #execute} operation, constructs the command list
* to use for building the process. * to use for building the process.
*/ */
@Override @Override
protected List<String> executeConstructProcessCommandList() { protected List<String> executeConstructProcessCommandList() {
if (project_ == null) {
throw new IllegalArgumentException("A project must be specified.");
} else if (!options_.containsKey(SOURCE_DIRS)) {
options_.put(SOURCE_DIRS, project_.srcDirectory().getPath());
}
final List<String> args = new ArrayList<>(); final List<String> args = new ArrayList<>();
args.add(javaTool());
args.add("-cp"); if (project_ != null) {
args.add(String.format("%s:%s:%s:%s", Path.of(project_.libTestDirectory().getPath(), "*"), args.add(javaTool());
Path.of(project_.libCompileDirectory().getPath(), "*"), project_.buildMainDirectory(), args.add("-cp");
project_.buildTestDirectory())); args.add(String.format("%s:%s:%s:%s", new File(project_.libTestDirectory(), "*"),
args.add("org.pitest.mutationtest.commandline.MutationCoverageReport"); new File(project_.libCompileDirectory(), "*"), project_.buildMainDirectory(),
project_.buildTestDirectory()));
args.add("org.pitest.mutationtest.commandline.MutationCoverageReport");
options_.forEach((k, v) -> { if (!options_.containsKey(SOURCE_DIRS)) {
args.add(k); options_.put(SOURCE_DIRS, project_.srcDirectory().getPath());
if (!v.isEmpty()) {
args.add(v);
} }
});
options_.forEach((k, v) -> {
args.add(k);
if (!v.isEmpty()) {
args.add(v);
}
});
}
return args; return args;
} }

View file

@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test;
import rife.bld.BaseProject; import rife.bld.BaseProject;
import rife.bld.Project; import rife.bld.Project;
import rife.bld.WebProject; import rife.bld.WebProject;
import rife.bld.operations.exceptions.ExitStatusException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
@ -29,7 +30,8 @@ import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatCode;
import static rife.bld.extension.PitestOperation.*; import static rife.bld.extension.PitestOperation.FALSE;
import static rife.bld.extension.PitestOperation.TRUE;
class PitestOperationTest { class PitestOperationTest {
private static final String AS_LIST = "as list"; private static final String AS_LIST = "as list";
@ -287,6 +289,12 @@ class PitestOperationTest {
"--sourceDirs c:\\myProject\\src"); "--sourceDirs c:\\myProject\\src");
} }
@Test
void executeNoProject() {
var op = new PitestOperation();
assertThatCode(op::execute).isInstanceOf(ExitStatusException.class);
}
@Test @Test
void exportLineCoverage() { void exportLineCoverage() {
var op = new PitestOperation() var op = new PitestOperation()
@ -547,12 +555,12 @@ class PitestOperationTest {
var op = new PitestOperation() var op = new PitestOperation()
.fromProject(new BaseProject()) .fromProject(new BaseProject())
.sourceDirs(FOO, BAR); .sourceDirs(FOO, BAR);
assertThat(op.options().get(SOURCE_DIRS)).isEqualTo(FOOBAR); assertThat(op.options().get("--sourceDirs")).isEqualTo(FOOBAR);
op = new PitestOperation() op = new PitestOperation()
.fromProject(new Project()) .fromProject(new Project())
.sourceDirs(List.of(FOO, BAR)); .sourceDirs(List.of(FOO, BAR));
assertThat(op.options().get(SOURCE_DIRS)).as(AS_LIST).isEqualTo(FOOBAR); assertThat(op.options().get("--sourceDirs")).as(AS_LIST).isEqualTo(FOOBAR);
} }
@Test @Test