-
-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw error instead of logging as error in analysis phase (#3193)
* Throw error instead of logging as error in analysis phase * Refactor to use IllegalStateException * Revert DetektError change
- Loading branch information
Chao Zhang
committed
Nov 5, 2020
1 parent
f0cb1aa
commit edaa3e9
Showing
6 changed files
with
115 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/Junk.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,10 @@ | ||
package io.gitlab.arturbosch.detekt.core | ||
|
||
import io.github.detekt.psi.absolutePath | ||
import io.gitlab.arturbosch.detekt.api.Finding | ||
import io.gitlab.arturbosch.detekt.api.RuleSetId | ||
import io.gitlab.arturbosch.detekt.api.internal.whichDetekt | ||
import io.gitlab.arturbosch.detekt.api.internal.whichJava | ||
import io.gitlab.arturbosch.detekt.api.internal.whichOS | ||
import org.jetbrains.kotlin.psi.KtFile | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
|
||
fun Path.exists(): Boolean = Files.exists(this) | ||
fun Path.isFile(): Boolean = Files.isRegularFile(this) | ||
fun Path.isDirectory(): Boolean = Files.isDirectory(this) | ||
|
||
fun MutableMap<String, List<Finding>>.mergeSmells(other: Map<String, List<Finding>>) { | ||
for ((key, findings) in other.entries) { | ||
merge(key, findings) { f1, f2 -> f1.plus(f2) } | ||
} | ||
} | ||
|
||
typealias FindingsResult = List<Map<RuleSetId, List<Finding>>> | ||
|
||
fun createErrorMessage(file: KtFile, error: Throwable): String = | ||
"Analyzing '${file.absolutePath()}' led to an exception.\n" + | ||
"The original exception message was: ${error.localizedMessage}\n" + | ||
"Running detekt '${whichDetekt() ?: "unknown"}' on Java '${whichJava()}' on OS '${whichOS()}'.\n" + | ||
"If the exception message does not help, please feel free to create an issue on our GitHub page." | ||
|
||
val NL: String = System.lineSeparator() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/AnalyzerSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.gitlab.arturbosch.detekt.core | ||
|
||
import io.github.detekt.test.utils.compileForTest | ||
import io.gitlab.arturbosch.detekt.api.CodeSmell | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.Debt | ||
import io.gitlab.arturbosch.detekt.api.Entity | ||
import io.gitlab.arturbosch.detekt.api.Issue | ||
import io.gitlab.arturbosch.detekt.api.Rule | ||
import io.gitlab.arturbosch.detekt.api.RuleSet | ||
import io.gitlab.arturbosch.detekt.api.RuleSetProvider | ||
import io.gitlab.arturbosch.detekt.api.Severity | ||
import io.gitlab.arturbosch.detekt.test.yamlConfig | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.Assertions.assertThatThrownBy | ||
import org.jetbrains.kotlin.psi.KtFile | ||
import org.spekframework.spek2.Spek | ||
import org.spekframework.spek2.style.specification.describe | ||
import java.util.concurrent.CompletionException | ||
|
||
class AnalyzerSpec : Spek({ | ||
|
||
describe("exceptions during analyze()") { | ||
|
||
it("analyze successfully when config has correct value type in config") { | ||
val testFile = path.resolve("Test.kt") | ||
val settings = createProcessingSettings(testFile, yamlConfig("configs/config-value-type-correct.yml")) | ||
val analyzer = Analyzer(settings, listOf(StyleRuleSetProvider()), emptyList()) | ||
|
||
assertThat(settings.use { analyzer.run(listOf(compileForTest(testFile))) }).isEmpty() | ||
} | ||
|
||
it("throw error explicitly when config has wrong value type in config") { | ||
val testFile = path.resolve("Test.kt") | ||
val settings = createProcessingSettings(testFile, yamlConfig("configs/config-value-type-wrong.yml")) | ||
val analyzer = Analyzer(settings, listOf(StyleRuleSetProvider()), emptyList()) | ||
|
||
assertThatThrownBy { | ||
settings.use { analyzer.run(listOf(compileForTest(testFile))) } | ||
}.isInstanceOf(IllegalStateException::class.java) | ||
} | ||
|
||
it("throw error explicitly in parallel when config has wrong value in config") { | ||
val testFile = path.resolve("Test.kt") | ||
val settings = createProcessingSettings( | ||
inputPath = testFile, | ||
config = yamlConfig("configs/config-value-type-wrong.yml"), | ||
spec = createNullLoggingSpec { | ||
execution { | ||
parallelParsing = true | ||
parallelAnalysis = true | ||
} | ||
} | ||
) | ||
val analyzer = Analyzer(settings, listOf(StyleRuleSetProvider()), emptyList()) | ||
|
||
assertThatThrownBy { settings.use { analyzer.run(listOf(compileForTest(testFile))) } } | ||
.isInstanceOf(CompletionException::class.java) | ||
.hasCauseInstanceOf(IllegalStateException::class.java) | ||
} | ||
} | ||
}) | ||
|
||
private class StyleRuleSetProvider : RuleSetProvider { | ||
override val ruleSetId: String = "style" | ||
override fun instance(config: Config) = RuleSet(ruleSetId, listOf(MaxLineLength(config))) | ||
} | ||
|
||
private class MaxLineLength(config: Config) : Rule(config) { | ||
override val issue = Issue(this::class.java.simpleName, Severity.Style, "", Debt.FIVE_MINS) | ||
private val lengthThreshold: Int = valueOrDefault("maxLineLength", 100) | ||
override fun visitKtFile(file: KtFile) { | ||
super.visitKtFile(file) | ||
for (line in file.text.splitToSequence(NL)) { | ||
if (line.length > lengthThreshold) { | ||
report(CodeSmell(issue, Entity.atPackageOrFirstDecl(file), issue.description)) | ||
} | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
detekt-core/src/test/resources/configs/config-value-type-correct.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
style: | ||
MaxLineLength: | ||
active: true | ||
maxLineLength: 120 |
4 changes: 4 additions & 0 deletions
4
detekt-core/src/test/resources/configs/config-value-type-wrong.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
style: | ||
MaxLineLength: | ||
active: true | ||
maxLineLength: 'abc' |