Skip to content

Commit

Permalink
Merge pull request #83 from Stepwisepl/feature/add_failBuild_configur…
Browse files Browse the repository at this point in the history
…ation_property

Added failBuildOnMaxIssuesReached configuration property
  • Loading branch information
Ozsie authored Dec 29, 2020
2 parents 29d5f22 + 8debab9 commit ee0d9e5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.github.ozsie</groupId>
<artifactId>detekt-maven-plugin</artifactId>
<version>1.15.0</version>
<version>1.15.1-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>detekt-maven-plugin Maven Plugin</name>
Expand Down Expand Up @@ -91,6 +91,12 @@
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>io.gitlab.arturbosch.detekt</groupId>
<artifactId>detekt-tooling</artifactId>
<version>${detekt.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.gitlab.arturbosch.detekt</groupId>
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/com/github/ozsie/CheckMojo.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.ozsie

import io.github.detekt.tooling.api.MaxIssuesReached
import io.gitlab.arturbosch.detekt.cli.parseArguments
import io.gitlab.arturbosch.detekt.cli.runners.Runner
import org.apache.maven.plugins.annotations.LifecyclePhase
Expand All @@ -20,7 +21,21 @@ class CheckMojo : DetektMojo() {
}
val cliArgs = parseArguments(getCliSting().log().toTypedArray())
val foundInputDir = Files.isDirectory(Paths.get(input))
if (!skip && foundInputDir) return Runner(cliArgs, System.out, System.err).execute() else inputSkipLog(skip)
if (!skip && foundInputDir)
failBuildIfNeeded { Runner(cliArgs, System.out, System.err).execute() }
else
inputSkipLog(skip)
}

private fun failBuildIfNeeded(block: () -> Unit) {
try {
block()
} catch (e: MaxIssuesReached) {
if (failBuildOnMaxIssuesReached)
throw e
else
log.warn("Detekt check found issues. Ignoring because 'failBuildOnMaxIssuesReached' is set to false")
}
}

private fun inputSkipLog(skip: Boolean) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/github/ozsie/DetektMojo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ abstract class DetektMojo : AbstractMojo() {
@Parameter(property = "detekt.parallel", defaultValue = "false")
var parallel = false

@Parameter(property = "detekt.failBuildOnMaxIssuesReached", defaultValue = "true")
var failBuildOnMaxIssuesReached = true

@Parameter(property = "detekt.disableDefaultRuleset", defaultValue = "false")
var disableDefaultRuleset = false

Expand Down Expand Up @@ -105,7 +108,7 @@ abstract class DetektMojo : AbstractMojo() {
.useIf(config.isNotEmpty(), CONFIG, resolveConfig(mavenProject, config))
.useIf(configResource.isNotEmpty(), CONFIG_RESOURCE, configResource)
.useIf(input.isNotEmpty(), INPUT, input)
.useIf(report.isNotEmpty(), toArgList(report))
.useIf(report.isNotEmpty(), reportsToArgList(report))
.useIf(buildUponDefaultConfig, BUILD_UPON_DEFAULT_CONFIG)
.useIf(failFast, FAIL_FAST)
.useIf(disableDefaultRuleset, DISABLE_DEFAULT_RULE_SET)
Expand All @@ -121,7 +124,7 @@ abstract class DetektMojo : AbstractMojo() {
internal fun <T> ArrayList<T>.log(): ArrayList<T> = log(this@DetektMojo.log)
}

private fun toArgList(list: List<String>) = ArrayList<String>().apply {
private fun reportsToArgList(list: List<String>) = ArrayList<String>().apply {
list.forEach {
add(REPORT)
add(it)
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/github/ozsie/CheckMojoSpec.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.github.ozsie

import io.github.detekt.tooling.api.MaxIssuesReached
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.on
import kotlin.test.assertFailsWith
import kotlin.test.expect

class CheckMojoSpec : Spek({
val invalidPackageNamingDirectoryPath by lazy {
CheckMojoSpec::class.java.classLoader.getResource("code-samples/invalid-package-naming")!!
.file
}

given("a CheckMojo and 'skip' is true") {
val checkMojo = CheckMojo()
checkMojo.skip = true
Expand All @@ -29,4 +36,34 @@ class CheckMojoSpec : Spek({
}
}
}

given("a CheckMojo and 'failBuildOnMaxIssuesReached' is false") {
val checkMojo = CheckMojo().apply {
input = invalidPackageNamingDirectoryPath
failBuildOnMaxIssuesReached = false
failFast = true // fail on any issue
}
on("checkMojo.execute()") {
test("Unit is expected") {
expect(Unit) {
checkMojo.execute()
}
}
}
}

given("a CheckMojo and 'failBuildOnMaxIssuesReached' is true") {
val checkMojo = CheckMojo().apply {
input = invalidPackageNamingDirectoryPath
failBuildOnMaxIssuesReached = true
failFast = true // fail on any issue
}
on("checkMojo.execute()") {
test("Unit is expected") {
assertFailsWith(MaxIssuesReached::class, "Build failed with 1 weighted issues.") {
checkMojo.execute()
}
}
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package `code-samples`.`invalid-package-naming`

0 comments on commit ee0d9e5

Please sign in to comment.