Ensured exit status is set on failure

This commit is contained in:
Erik C. Thauvin 2024-07-22 16:30:11 -07:00
parent b26240df76
commit 8b02e1e45a
Signed by: erik
GPG key ID: 776702A6A2DA330E
3 changed files with 147 additions and 135 deletions

View file

@ -45,3 +45,7 @@ public void detektBaseline() throws ExitStatusException, IOException, Interrupte
- [View Examples Project](https://github.com/rife2/bld-detekt/tree/main/examples) - [View Examples Project](https://github.com/rife2/bld-detekt/tree/main/examples)
Please check the [DetektOperation documentation](https://rife2.github.io/bld-detekt/rife/bld/extension/DetektOperation.html#method-summary) for all available configuration options. Please check the [DetektOperation documentation](https://rife2.github.io/bld-detekt/rife/bld/extension/DetektOperation.html#method-summary) for all available configuration options.
## Template Project
There is also a [Kotlin Template Project](https://github.com/rife2/kotlin-bld-example) with support for [Dokka](https://github.com/rife2/bld-dokka) and the Detekt extensions.

View file

@ -21,7 +21,6 @@ import rife.bld.extension.detekt.Report;
import rife.bld.extension.detekt.ReportId; import rife.bld.extension.detekt.ReportId;
import rife.bld.operations.AbstractProcessOperation; import rife.bld.operations.AbstractProcessOperation;
import rife.bld.operations.exceptions.ExitStatusException; import rife.bld.operations.exceptions.ExitStatusException;
import rife.tools.exceptions.FileUtilsErrorException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -60,6 +59,7 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
private final Collection<File> classpath_ = new ArrayList<>(); private final Collection<File> classpath_ = new ArrayList<>();
private final Collection<File> config_ = new ArrayList<>(); private final Collection<File> config_ = new ArrayList<>();
private final Collection<String> excludes_ = new ArrayList<>(); private final Collection<String> excludes_ = new ArrayList<>();
private final Collection<String> includes_ = new ArrayList<>();
private final Collection<File> input_ = new ArrayList<>(); private final Collection<File> input_ = new ArrayList<>();
private final Collection<File> plugins_ = new ArrayList<>(); private final Collection<File> plugins_ = new ArrayList<>();
private final Collection<Report> report_ = new ArrayList<>(); private final Collection<Report> report_ = new ArrayList<>();
@ -73,7 +73,6 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
private boolean debug_; private boolean debug_;
private boolean disableDefaultRuleSets_; private boolean disableDefaultRuleSets_;
private boolean generateConfig_; private boolean generateConfig_;
private final Collection<String> includes_ = new ArrayList<>();
private String jdkHome_; private String jdkHome_;
private String jvmTarget_; private String jvmTarget_;
private String languageVersion_; private String languageVersion_;
@ -350,13 +349,18 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
* *
* @throws InterruptedException when the operation was interrupted * @throws InterruptedException when the operation was interrupted
* @throws IOException when an exception occurred during the execution of the process * @throws IOException when an exception occurred during the execution of the process
* @throws FileUtilsErrorException when an exception occurred during the retrieval of the operation output
* @throws ExitStatusException when the exit status was changed during the operation * @throws ExitStatusException when the exit status was changed during the operation
*/ */
@Override @Override
public void execute() throws IOException, FileUtilsErrorException, InterruptedException, ExitStatusException { 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(); super.execute();
if (successful_ && LOGGER.isLoggable(Level.INFO)) { if (successful_ && LOGGER.isLoggable(Level.INFO) && !silent()){
if (createBaseline_) { if (createBaseline_) {
LOGGER.info("Detekt baseline generated successfully: " LOGGER.info("Detekt baseline generated successfully: "
+ "file://" + new File(baseline_).toURI().getPath()); + "file://" + new File(baseline_).toURI().getPath());
@ -365,6 +369,7 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
} }
} }
} }
}
/** /**
* Part of the {@link #execute} operation, constructs the command list * Part of the {@link #execute} operation, constructs the command list
@ -372,11 +377,8 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
*/ */
@Override @Override
protected List<String> executeConstructProcessCommandList() { protected List<String> executeConstructProcessCommandList() {
if (project_ == null) {
LOGGER.severe("A project must be specified.");
}
final List<String> args = new ArrayList<>(); final List<String> args = new ArrayList<>();
if (project_ != null) {
args.add(javaTool()); args.add(javaTool());
args.add("-cp"); args.add("-cp");
args.add(getDetektJarList(project_.libBldDirectory())); args.add(getDetektJarList(project_.libBldDirectory()));
@ -508,9 +510,10 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
}); });
} }
if (LOGGER.isLoggable(Level.FINE)) { if (LOGGER.isLoggable(Level.FINE) && !silent()) {
LOGGER.fine(String.join(" ", args.stream().filter(this::isNotBlank).toList())); LOGGER.fine(String.join(" ", args.stream().filter(this::isNotBlank).toList()));
} }
}
return args; return args;
} }
@ -551,9 +554,7 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
return this; return this;
} }
/* // Retrieves the matching JARs files from the given directory.
* Retrieves the matching JARs files from the given directory.
*/
private String getDetektJarList(File directory) { private String getDetektJarList(File directory) {
var jars = new ArrayList<String>(); var jars = new ArrayList<String>();

View file

@ -61,6 +61,7 @@ class DetektOperationTest {
} }
@Test @Test
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
void testCheckAllParameters() throws IOException { void testCheckAllParameters() throws IOException {
var args = Files.readAllLines(Paths.get("src", "test", "resources", "detekt-args.txt")); var args = Files.readAllLines(Paths.get("src", "test", "resources", "detekt-args.txt"));
@ -190,4 +191,10 @@ class DetektOperationTest {
.debug(true); .debug(true);
assertThatThrownBy(op::execute).isInstanceOf(ExitStatusException.class); assertThatThrownBy(op::execute).isInstanceOf(ExitStatusException.class);
} }
@Test
void testExecuteNoProject() {
var op = new DetektOperation();
assertThatCode(op::execute).isInstanceOf(ExitStatusException.class);
}
} }