Skip to content

Commit

Permalink
Remove YamlConfig.loadResource()
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Nov 20, 2021
1 parent 7b8d273 commit 65bf336
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.gitlab.arturbosch.detekt.core.config

import io.github.detekt.tooling.api.spec.ConfigSpec
import io.github.detekt.tooling.api.spec.ProcessingSpec
import io.github.detekt.tooling.internal.openSafeStream
import io.gitlab.arturbosch.detekt.api.Config
import java.net.URI
import java.net.URL
Expand Down Expand Up @@ -29,10 +30,10 @@ internal fun ProcessingSpec.loadConfiguration(): Config = with(configSpec) {

private fun parseResourceConfig(urls: Collection<URL>): Config =
if (urls.size == 1) {
YamlConfig.loadResource(urls.first())
YamlConfig.load(urls.first().openSafeStream().reader())
} else {
urls.asSequence()
.map { YamlConfig.loadResource(it) }
.map { YamlConfig.load(it.openSafeStream().reader()) }
.reduce { composite, config -> CompositeConfig(config, composite) }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.gitlab.arturbosch.detekt.core.config

import io.github.detekt.tooling.internal.getSafeResourceAsStream
import io.gitlab.arturbosch.detekt.api.Config

internal object DefaultConfig {

const val RESOURCE_NAME = "default-detekt-config.yml"

fun newInstance(): Config {
val configUrl = javaClass.getResource("/$RESOURCE_NAME")!!
return YamlConfig.loadResource(configUrl)
val configReader = javaClass.getSafeResourceAsStream("/$RESOURCE_NAME")!!.reader()
return YamlConfig.load(configReader)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import io.gitlab.arturbosch.detekt.api.Config.Companion.CONFIG_SEPARATOR
import io.gitlab.arturbosch.detekt.api.Notification
import org.yaml.snakeyaml.Yaml
import java.io.Reader
import java.net.URL
import java.nio.file.Path

/**
Expand Down Expand Up @@ -58,21 +57,6 @@ class YamlConfig internal constructor(
}.reader()
)

/**
* Factory method to load a yaml configuration from a URL.
*/
fun loadResource(url: URL): Config = load(
url.openConnection()
/*
* Due to https://bugs.openjdk.java.net/browse/JDK-6947916 and https://bugs.openjdk.java.net/browse/JDK-8155607,
* it is necessary to disallow caches to maintain stability on JDK 8. Otherwise, simultaneous invocations of
* Detekt in the same VM can fail spuriously. A similar bug is referenced in
* https://github.com/detekt/detekt/issues/3396. The performance regression is likely unnoticeable.
*/
.apply { if (System.getProperty("java.specification.version") == "1.8") useCaches = false }
.getInputStream().reader()
)

/**
* Constructs a [YamlConfig] from any [Reader].
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.gitlab.arturbosch.detekt.core.tooling

import io.github.detekt.tooling.api.DefaultConfigurationProvider
import io.github.detekt.tooling.internal.openSafeStream
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.core.config.DefaultConfig
import java.nio.file.Files
Expand All @@ -13,6 +14,6 @@ class DefaultConfigProvider : DefaultConfigurationProvider {

override fun copy(targetLocation: Path) {
val configUrl = javaClass.getResource("/${DefaultConfig.RESOURCE_NAME}")!!
Files.copy(configUrl.openStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING)
Files.copy(configUrl.openSafeStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package io.gitlab.arturbosch.detekt.core.config

import io.github.detekt.test.utils.resourceAsPath
import io.github.detekt.tooling.internal.getSafeResourceAsStream
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.test.yamlConfig
import io.gitlab.arturbosch.detekt.test.yamlConfigFromContent
Expand Down Expand Up @@ -58,11 +59,11 @@ class YamlConfigSpec : Spek({
describe("loading empty configurations") {

it("empty yaml file is equivalent to empty config") {
YamlConfig.loadResource(javaClass.getResource("/empty.yml"))
YamlConfig.load(javaClass.getSafeResourceAsStream("/empty.yml")!!.reader())
}

it("single item in yaml file is valid") {
YamlConfig.loadResource(javaClass.getResource("/oneitem.yml"))
YamlConfig.load(javaClass.getSafeResourceAsStream("/oneitem.yml")!!.reader())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.core.config.YamlConfig
import java.io.StringReader

fun yamlConfig(name: String) = YamlConfig.loadResource(resource(name).toURL())
fun yamlConfig(name: String) = YamlConfig.load(resource(name).toURL().openStream().reader())

fun yamlConfigFromContent(content: String): Config =
YamlConfig.load(StringReader(content.trimIndent()))
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.detekt.tooling.internal

import java.io.InputStream
import java.net.URL

fun URL.openSafeStream(): InputStream {
return openConnection()
/*
* Due to https://bugs.openjdk.java.net/browse/JDK-6947916 and https://bugs.openjdk.java.net/browse/JDK-8155607,
* it is necessary to disallow caches to maintain stability on JDK 8. Otherwise, simultaneous invocations of
* Detekt in the same VM can fail spuriously. A similar bug is referenced in
* https://github.com/detekt/detekt/issues/3396. The performance regression is likely unnoticeable.
*/
.apply { if (System.getProperty("java.specification.version") == "1.8") useCaches = false }
.getInputStream()
}

fun <T> Class<T>.getSafeResourceAsStream(name: String): InputStream? {
return getResource(name)?.openSafeStream()
}

0 comments on commit 65bf336

Please sign in to comment.