Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to configure the plugin to fail on any severity #478

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions docs/maven-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ Description:

Parameters:

| Name | Type| Description |
| ------ | ------| -------- |
| **report.targetDir** | String | The directory where the individual report will be generated (default value is **${project.build.directory}/code-analysis**) |
| **report.summary.targetDir** | String | The directory where the summary report, containing links to the individual reports will be generated (Default value is **${session.executionRootDirectory}/target**)|
| **report.fail.on.error** | Boolean | Describes of the build should fail if high priority error is found (Default value is **true**)|
| **report.in.maven** | Boolean | Enable/Disable maven console logging of all messages (Default value is **true**)|
| Name | Type| Description |
|------------------------------| ------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **report.targetDir** | String | The directory where the individual report will be generated (default value is **${project.build.directory}/code-analysis**) |
| **report.summary.targetDir** | String | The directory where the summary report, containing links to the individual reports will be generated (Default value is **${session.executionRootDirectory}/target**) |
| **report.fail.on.error** | Boolean | Describes of the build should fail if high priority error is found (Default value is **true**) |
| **report.fail.on.warning** | Boolean | Describes of the build should fail if warning is found (Default value is **false**) |
| **report.fail.on.debug** | Boolean | Describes of the build should fail if info is found (Default value is **false**) |
| **report.in.maven** | Boolean | Enable/Disable maven console logging of all messages (Default value is **true**) |

## Customization

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import javax.xml.parsers.DocumentBuilder;
Expand Down Expand Up @@ -115,6 +117,18 @@ public class ReportMojo extends AbstractMojo {
@Parameter(property = "report.fail.on.error", defaultValue = "true")
private boolean failOnError;

/**
* Describes of the build should fail if medium priority error is found
*/
@Parameter(property = "report.fail.on.warning", defaultValue = "false")
private boolean failOnWarning;

/**
* Describes of the build should fail if low priority error is found
*/
@Parameter(property = "report.fail.on.debug", defaultValue = "false")
private boolean failOnDebug;

/**
* The directory where the summary report, containing links to the individual reports will be
* generated
Expand Down Expand Up @@ -142,6 +156,14 @@ public void setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
}

public void setFailOnWarning(boolean failOnWarning) {
this.failOnWarning = failOnWarning;
}

public void setFailOnDebug(boolean failOnDebug) {
this.failOnDebug = failOnDebug;
}

public void setSummaryReport(File summaryReport) {
this.summaryReportDirectory = summaryReport;
}
Expand Down Expand Up @@ -222,8 +244,8 @@ public void execute() throws MojoFailureException {
reportWarningsAndErrors(mergedReport, htmlOutputFileName);
}

// 9. Fail the build if the option is enabled and high priority warnings are found
if (failOnError) {
// 9. Fail the build if any level error is enabled and configured error levels are found
if (failOnError || failOnWarning || failOnDebug) {
failOnErrors(mergedReport);
}

Expand Down Expand Up @@ -342,12 +364,21 @@ private int countPriority(NodeList messages, String priority) {
}

private void failOnErrors(File mergedReport) throws MojoFailureException {
int errorCount = selectNodes(mergedReport, "/sca/file/message[@priority=1]").getLength();
List<String> errorPriorities = new ArrayList<>();
addLevel(failOnError, "@priority=1", errorPriorities);
addLevel(failOnWarning, "@priority=2", errorPriorities);
addLevel(failOnDebug, "@priority=3", errorPriorities);
String xpathExpression = "/sca/file/message[" + String.join(" or ", errorPriorities) + "]";
int errorCount = selectNodes(mergedReport, xpathExpression).getLength();
if (errorCount > 0) {
throw new MojoFailureException(String.format(
"%n" + "Code Analysis Tool has found %d error(s)! %n"
+ "Please fix the errors and rerun the build. %n",
selectNodes(mergedReport, "/sca/file/message[@priority=1]").getLength()));
throw new MojoFailureException(String.format("%n" + "Code Analysis Tool has found %d error(s)! %n"
+ "Please fix the errors and rerun the build. %n", errorCount));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message may need an update as well depending on the configuration. Users might think only fixing errors should be enough but if you configure it to fail on warnings/info this is no longer the case.

}
}

private static void addLevel(boolean level, String levelValue, List<String> levels) {
if (level) {
levels.add(levelValue);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@
package org.openhab.tools.analysis.report;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.Mockito.verify;
import static org.openhab.tools.analysis.report.ReportUtil.RESULT_FILE_NAME;

import java.io.File;
import java.util.stream.Stream;

import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

Expand Down Expand Up @@ -57,29 +62,32 @@ public void setUp() throws Exception {
}
}

@Test
public void assertReportIsCreatedAndBuildFails() throws Exception {
@ParameterizedTest
@MethodSource("provideReportParameters")
public void assertReportIsCreatedAndBuildFailsAsExpected(boolean failOnError, boolean failOnWarning,
boolean failOnDebug, boolean buildFailExpected) {
assertFalse(resultFile.exists());

subject.setFailOnError(true);
subject.setFailOnError(failOnError);
subject.setFailOnWarning(failOnWarning);
subject.setFailOnDebug(failOnDebug);
subject.setSummaryReport(null);
subject.setTargetDirectory(new File(TARGET_ABSOLUTE_DIR));

assertThrows(MojoFailureException.class, () -> subject.execute());
if (buildFailExpected) {
assertThrows(MojoFailureException.class, () -> subject.execute());
} else {
assertDoesNotThrow(() -> subject.execute());

}
assertTrue(resultFile.exists());
}

@Test
public void assertReportISCreatedAndBuildCompletes() throws MojoFailureException {
assertFalse(resultFile.exists());

subject.setFailOnError(false);
subject.setSummaryReport(null);
subject.setTargetDirectory(new File(TARGET_ABSOLUTE_DIR));

subject.execute();

assertTrue(resultFile.exists());
private static Stream<Arguments> provideReportParameters() {
return Stream.of(arguments(false, false, false, false), arguments(false, false, true, true),
arguments(false, true, false, true), arguments(false, true, true, true),
arguments(true, false, false, true), arguments(true, false, true, true),
arguments(true, true, false, true), arguments(true, true, true, true));
}

@Test
Expand Down