Skip to content

Commit

Permalink
Concat configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Dec 2, 2021
1 parent f86edab commit 430327f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
Expand Up @@ -2,10 +2,12 @@ package io.gitlab.arturbosch.detekt.core.tooling

import io.github.detekt.tooling.api.DefaultConfigurationProvider
import io.github.detekt.tooling.api.spec.ProcessingSpec
import io.github.detekt.tooling.internal.getSafeResourceAsStream
import io.github.detekt.tooling.internal.openSafeStream
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.core.config.YamlConfig
import io.gitlab.arturbosch.detekt.core.settings.ExtensionFacade
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
Expand All @@ -20,15 +22,32 @@ class DefaultConfigProvider : DefaultConfigurationProvider {
override fun get(): Config = spec.getDefaultConfiguration()

override fun copy(targetLocation: Path) {
val configUrl = javaClass.getResource(DEFAULT_CONFIG_RESOURCES_PATH)!!
Files.copy(configUrl.openSafeStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING)
Files.copy(configInputStream(spec), targetLocation, StandardCopyOption.REPLACE_EXISTING)
}
}

fun ProcessingSpec.getDefaultConfiguration(): Config {
return requireNotNull(javaClass.getSafeResourceAsStream(DEFAULT_CONFIG_RESOURCES_PATH))
.reader()
.use(YamlConfig::load)
private fun configInputStream(spec: ProcessingSpec): InputStream {
val outputStream = ByteArrayOutputStream()

requireNotNull(spec.javaClass.getResourceAsStream("/default-detekt-config.yml"))
.use { it.copyTo(outputStream) }

ExtensionFacade(spec.extensionsSpec).pluginLoader
.getResourcesAsStream("config/config.yml")
.forEach { inputStream ->
outputStream.bufferedWriter().append('\n').flush()
inputStream.use { it.copyTo(outputStream) }
}

return ByteArrayInputStream(outputStream.toByteArray())
}

private const val DEFAULT_CONFIG_RESOURCES_PATH = "/default-detekt-config.yml"
private fun ClassLoader.getResourcesAsStream(name: String): Sequence<InputStream> {
return getResources(name)
.asSequence()
.map { it.openStream() }
}

fun ProcessingSpec.getDefaultConfiguration(): Config {
return YamlConfig.load(configInputStream(this).reader())
}
Expand Up @@ -6,10 +6,10 @@ import io.gitlab.arturbosch.detekt.core.createNullLoggingSpec
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import java.nio.file.Files

internal class DefaultConfigProviderSpec : Spek({

describe("defaultConfigProvider") {
describe("defaultConfigProvider without plugins") {
val spec by memoized {
createNullLoggingSpec {}
}
Expand All @@ -19,6 +19,7 @@ internal class DefaultConfigProviderSpec : Spek({

assertThat(config.parentPath).isNull()
assertThat(config.subConfig("build").valueOrNull<Int>("maxIssues")).isEqualTo(0)
assertThat(config.valueOrNull<Any>("sample")).isNull()
}

it("copy") {
Expand All @@ -29,4 +30,41 @@ internal class DefaultConfigProviderSpec : Spek({
.hasSameTextualContentAs(resourceAsPath("default-detekt-config.yml"))
}
}

describe("defaultConfigProvider without plugins") {
val spec by memoized {
createNullLoggingSpec {
extensions {
fromPaths { listOf(resourceAsPath("sample-rule-set.jar")) }
}
}
}

it("get") {
val config = DefaultConfigProvider().apply { init(spec) }.get()

assertThat(config.parentPath).isNull()
assertThat(config.subConfig("build").valueOrNull<Int>("maxIssues")).isEqualTo(0)
assertThat(config.valueOrNull<Any>("sample")).isNotNull()
}

it("copy") {
val path = createTempFileForTest("test", "test")
DefaultConfigProvider().apply { init(spec) }.copy(path)

val actual = String(Files.readAllBytes(path), Charsets.UTF_8)
val expected = String(Files.readAllBytes(resourceAsPath("default-detekt-config.yml")), Charsets.UTF_8) +
"""
|
|sample:
| TooManyFunctions:
| active: true
| TooManyFunctionsTwo:
| active: true
|
""".trimMargin()

assertThat(actual).isEqualTo(expected)
}
}
})
Binary file modified detekt-core/src/test/resources/sample-rule-set.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions detekt-sample-extensions/src/main/resources/config/config.yml
@@ -0,0 +1,5 @@
sample:
TooManyFunctions:
active: true
TooManyFunctionsTwo:
active: true

0 comments on commit 430327f

Please sign in to comment.