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

Support disabling config validation via tooling spec #4937

Merged
merged 1 commit into from Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -41,7 +41,7 @@ internal fun CliArgs.createSpec(output: Appendable, error: Appendable): Processi

config {
useDefaultConfig = args.buildUponDefaultConfig
shouldValidateBeforeAnalysis = false
shouldValidateBeforeAnalysis = null
knownPatterns = emptyList()
// ^^ cli does not have these properties yet; specified in yaml config for now
configPaths = config?.let { MultipleExistingPathConverter().convert(it) }.orEmpty()
Expand Down
Expand Up @@ -11,9 +11,11 @@ import io.gitlab.arturbosch.detekt.core.reporting.red
import io.gitlab.arturbosch.detekt.core.reporting.yellow

internal fun checkConfiguration(settings: ProcessingSettings, baseline: Config) {
val props = settings.config.subConfig("config")
val shouldValidate = props.valueOrDefault("validation", true)

var shouldValidate = settings.spec.configSpec.shouldValidateBeforeAnalysis
if (shouldValidate == null) {
val props = settings.config.subConfig("config")
shouldValidate = props.valueOrDefault("validation", true)
}
if (shouldValidate) {
val validators =
loadExtensions<ConfigValidator>(settings) + DefaultPropertiesConfigValidator(settings, baseline)
Expand Down
Expand Up @@ -20,6 +20,28 @@ class SupportConfigValidationSpec {
private val testDir = createTempDirectoryForTest("detekt-sample")
private val spec = createNullLoggingSpec {}

@Test
fun `passes because config validation is disabled by tooling spec`() {
val config = yamlConfigFromContent(
"""
unknown_property:
unknown_var: ""
"""
)
createProcessingSettings(
testDir,
config,
spec = createNullLoggingSpec {
config {
shouldValidateBeforeAnalysis = false
}
}
).use {
assertThatCode { checkConfiguration(it, spec.getDefaultConfiguration()) }
.doesNotThrowAnyException()
}
}

@Test
fun `fails when unknown properties are found`() {
val config = yamlConfigFromContent(
Expand Down
6 changes: 3 additions & 3 deletions detekt-tooling/api/detekt-tooling.api
Expand Up @@ -100,7 +100,7 @@ public abstract interface class io/github/detekt/tooling/api/spec/ConfigSpec {
public abstract fun getConfigPaths ()Ljava/util/Collection;
public abstract fun getKnownPatterns ()Ljava/util/Collection;
public abstract fun getResources ()Ljava/util/Collection;
public abstract fun getShouldValidateBeforeAnalysis ()Z
public abstract fun getShouldValidateBeforeAnalysis ()Ljava/lang/Boolean;
public abstract fun getUseDefaultConfig ()Z
}

Expand Down Expand Up @@ -233,12 +233,12 @@ public final class io/github/detekt/tooling/dsl/ConfigSpecBuilder : io/github/de
public final fun getConfigPaths ()Ljava/util/Collection;
public final fun getKnownPatterns ()Ljava/util/Collection;
public final fun getResources ()Ljava/util/Collection;
public final fun getShouldValidateBeforeAnalysis ()Z
public final fun getShouldValidateBeforeAnalysis ()Ljava/lang/Boolean;
public final fun getUseDefaultConfig ()Z
public final fun setConfigPaths (Ljava/util/Collection;)V
public final fun setKnownPatterns (Ljava/util/Collection;)V
public final fun setResources (Ljava/util/Collection;)V
public final fun setShouldValidateBeforeAnalysis (Z)V
public final fun setShouldValidateBeforeAnalysis (Ljava/lang/Boolean;)V
public final fun setUseDefaultConfig (Z)V
}

Expand Down
Expand Up @@ -10,7 +10,7 @@ interface ConfigSpec {
*
* Unknown properties to detekt will get reported as errors.
*/
val shouldValidateBeforeAnalysis: Boolean
val shouldValidateBeforeAnalysis: Boolean?

/**
* Property patterns which should be excluded from validation.
Expand Down
Expand Up @@ -7,7 +7,7 @@ import java.nio.file.Path
@ProcessingModelDsl
class ConfigSpecBuilder : Builder<ConfigSpec> {

var shouldValidateBeforeAnalysis: Boolean = true
var shouldValidateBeforeAnalysis: Boolean? = null
var knownPatterns: Collection<String> = emptyList()

var useDefaultConfig: Boolean = false // false to be backwards compatible in 1.X
Expand All @@ -24,7 +24,7 @@ class ConfigSpecBuilder : Builder<ConfigSpec> {
}

private data class ConfigModel(
override val shouldValidateBeforeAnalysis: Boolean,
override val shouldValidateBeforeAnalysis: Boolean?,
override val knownPatterns: Collection<String>,
override val useDefaultConfig: Boolean,
override val resources: Collection<URL>,
Expand Down