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_;
@ -348,20 +347,26 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
/** /**
* Performs the operation. * Performs the operation.
* *
* @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 {
super.execute(); if (project_ == null) {
if (successful_ && LOGGER.isLoggable(Level.INFO)) { if (LOGGER.isLoggable(Level.SEVERE) && !silent()) {
if (createBaseline_) { LOGGER.severe("A project must be specified.");
LOGGER.info("Detekt baseline generated successfully: " }
+ "file://" + new File(baseline_).toURI().getPath()); throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
} else { } else {
LOGGER.info("Detekt operation finished successfully."); super.execute();
if (successful_ && LOGGER.isLoggable(Level.INFO) && !silent()){
if (createBaseline_) {
LOGGER.info("Detekt baseline generated successfully: "
+ "file://" + new File(baseline_).toURI().getPath());
} else {
LOGGER.info("Detekt operation finished successfully.");
}
} }
} }
} }
@ -372,144 +377,142 @@ 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<>();
args.add(javaTool()); if (project_ != null) {
args.add("-cp"); args.add(javaTool());
args.add(getDetektJarList(project_.libBldDirectory())); args.add("-cp");
args.add("io.gitlab.arturbosch.detekt.cli.Main"); args.add(getDetektJarList(project_.libBldDirectory()));
args.add("io.gitlab.arturbosch.detekt.cli.Main");
// all-rules // all-rules
if (allRules_) { if (allRules_) {
args.add("--all-rules"); args.add("--all-rules");
} }
// auto-correct // auto-correct
if (autoCorrect_) { if (autoCorrect_) {
args.add("--auto-correct"); args.add("--auto-correct");
} }
// base-path // base-path
if (isNotBlank(basePath_)) { if (isNotBlank(basePath_)) {
args.add("--base-path"); args.add("--base-path");
args.add(basePath_); args.add(basePath_);
} }
// baseline // baseline
if (isNotBlank(baseline_)) { if (isNotBlank(baseline_)) {
args.add("--baseline"); args.add("--baseline");
args.add(baseline_); args.add(baseline_);
} }
// build-upon-default-config // build-upon-default-config
if (buildUponDefaultConfig_) { if (buildUponDefaultConfig_) {
args.add("--build-upon-default-config"); args.add("--build-upon-default-config");
} }
// classpath // classpath
if (!classpath_.isEmpty()) { if (!classpath_.isEmpty()) {
args.add("--classpath"); args.add("--classpath");
args.add(String.join(File.pathSeparator, classpath_.stream().map(File::getAbsolutePath).toList())); args.add(String.join(File.pathSeparator, classpath_.stream().map(File::getAbsolutePath).toList()));
} }
// config // config
if (!config_.isEmpty()) { if (!config_.isEmpty()) {
args.add("-config"); args.add("-config");
args.add(String.join(";", config_.stream().map(File::getAbsolutePath).toList())); args.add(String.join(";", config_.stream().map(File::getAbsolutePath).toList()));
} }
// config-resource // config-resource
if (isNotBlank(configResource_)) { if (isNotBlank(configResource_)) {
args.add("--config-resource"); args.add("--config-resource");
args.add(configResource_); args.add(configResource_);
} }
// create-baseline // create-baseline
if (createBaseline_) { if (createBaseline_) {
args.add("--create-baseline"); args.add("--create-baseline");
} }
// debug // debug
if (debug_) { if (debug_) {
args.add("--debug"); args.add("--debug");
} }
// disable-default-rulesets // disable-default-rulesets
if (disableDefaultRuleSets_) { if (disableDefaultRuleSets_) {
args.add("--disable-default-rulesets"); args.add("--disable-default-rulesets");
} }
// excludes // excludes
if (!excludes_.isEmpty()) { if (!excludes_.isEmpty()) {
args.add("--excludes"); args.add("--excludes");
args.add(String.join(",", excludes_)); args.add(String.join(",", excludes_));
} }
// generate-config // generate-config
if (generateConfig_) { if (generateConfig_) {
args.add("--generate-config"); args.add("--generate-config");
} }
// includes // includes
if (!includes_.isEmpty()) { if (!includes_.isEmpty()) {
args.add("--includes"); args.add("--includes");
args.add(String.join(",", includes_)); args.add(String.join(",", includes_));
} }
// input // input
if (!input_.isEmpty()) { if (!input_.isEmpty()) {
args.add("--input"); args.add("--input");
args.add(String.join(",", input_.stream().map(File::getAbsolutePath).toList())); args.add(String.join(",", input_.stream().map(File::getAbsolutePath).toList()));
} }
// jdk-home // jdk-home
if (isNotBlank(jdkHome_)) { if (isNotBlank(jdkHome_)) {
args.add("--jdk-home"); args.add("--jdk-home");
args.add(jdkHome_); args.add(jdkHome_);
} }
// jvm-target // jvm-target
if (isNotBlank(jvmTarget_)) { if (isNotBlank(jvmTarget_)) {
args.add("--jvm-target"); args.add("--jvm-target");
args.add(jvmTarget_); args.add(jvmTarget_);
} }
// language-version // language-version
if (isNotBlank(languageVersion_)) { if (isNotBlank(languageVersion_)) {
args.add("--language-version"); args.add("--language-version");
args.add(languageVersion_); args.add(languageVersion_);
} }
// max-issues // max-issues
if (maxIssues_ > 0) { if (maxIssues_ > 0) {
args.add("--max-issues"); args.add("--max-issues");
args.add(String.valueOf(maxIssues_)); args.add(String.valueOf(maxIssues_));
} }
// parallel // parallel
if (parallel_) { if (parallel_) {
args.add("--parallel"); args.add("--parallel");
} }
// plugins // plugins
if (!plugins_.isEmpty()) { if (!plugins_.isEmpty()) {
args.add("--plugins"); args.add("--plugins");
args.add(String.join(",", plugins_.stream().map(File::getAbsolutePath).toList())); args.add(String.join(",", plugins_.stream().map(File::getAbsolutePath).toList()));
} }
// report // report
if (!report_.isEmpty()) { if (!report_.isEmpty()) {
report_.forEach(it -> { report_.forEach(it -> {
args.add("--report"); args.add("--report");
args.add(it.id().name().toLowerCase() + ":" + it.path()); args.add(it.id().name().toLowerCase() + ":" + it.path());
}); });
} }
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"));
@ -108,10 +109,10 @@ class DetektOperationTest {
for (var i = 1; i < 6; i++) { for (var i = 1; i < 6; i++) {
assertThat(op.classPath()).as("classPath[" + i + ']').hasSize(5).contains(new File("path" + i)); assertThat(op.classPath()).as("classPath[" + i + ']').hasSize(5).contains(new File("path" + i));
assertThat(op.config()).as("config[" + i + ']').hasSize(5).contains(new File("config" + i)); assertThat(op.config()).as("config[" + i + ']').hasSize(5).contains(new File("config" + i));
assertThat(op.includes()).as("includes[" + i + ']').hasSize(5).contains("includes" + i); assertThat(op.includes()).as("includes[" + i + ']').hasSize(5).contains("includes" + i);
assertThat(op.input()).as("input[" + i + ']').hasSize(5).contains(new File("input" + i)); assertThat(op.input()).as("input[" + i + ']').hasSize(5).contains(new File("input" + i));
assertThat(op.plugins()).as("plugins[" + i + ']').hasSize(5).contains(new File("jar" + i)); assertThat(op.plugins()).as("plugins[" + i + ']').hasSize(5).contains(new File("jar" + i));
} }
var params = op.executeConstructProcessCommandList(); var params = op.executeConstructProcessCommandList();
@ -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);
}
} }