Skip to content

Commit

Permalink
Fix generate-config
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Mar 20, 2022
1 parent 4e23be3 commit e55d034
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
Expand Up @@ -20,7 +20,7 @@ class CliRunner : DetektCli {

val specialRunner = when {
arguments.showVersion -> VersionPrinter(outputChannel)
arguments.generateConfig -> ConfigExporter(arguments, outputChannel, errorChannel)
arguments.generateConfig -> ConfigExporter(arguments, outputChannel)
arguments.printAst -> AstPrinter(arguments, outputChannel)
else -> null
}
Expand Down
Expand Up @@ -54,7 +54,7 @@ fun buildRunner(
val arguments = parseArguments(args)
return when {
arguments.showVersion -> VersionPrinter(outputPrinter)
arguments.generateConfig -> ConfigExporter(arguments, outputPrinter, errorPrinter)
arguments.generateConfig -> ConfigExporter(arguments, outputPrinter)
arguments.printAst -> AstPrinter(arguments, outputPrinter)
else -> Runner(arguments, outputPrinter, errorPrinter)
}
Expand Down
@@ -1,19 +1,24 @@
package io.gitlab.arturbosch.detekt.cli.runners

import io.github.detekt.tooling.api.DefaultConfigurationProvider
import io.github.detekt.tooling.api.spec.ProcessingSpec
import io.gitlab.arturbosch.detekt.cli.CliArgs
import io.gitlab.arturbosch.detekt.cli.createSpec
import io.gitlab.arturbosch.detekt.cli.MultipleExistingPathConverter
import java.nio.file.Paths

class ConfigExporter(
private val arguments: CliArgs,
private val outputPrinter: Appendable,
private val errorPrinter: Appendable,
) : Executable {

override fun execute() {
val configPath = Paths.get(arguments.config ?: "detekt.yml")
val spec = arguments.createSpec(outputPrinter, errorPrinter)
val spec = ProcessingSpec {
extensions {
disableDefaultRuleSets = arguments.disableDefaultRuleSets
fromPaths { arguments.plugins?.let { MultipleExistingPathConverter().convert(it) }.orEmpty() }
}
}
DefaultConfigurationProvider.load(spec.extensionsSpec).copy(configPath)
outputPrinter.appendLine("Successfully copied default config to ${configPath.toAbsolutePath()}")
}
Expand Down
Expand Up @@ -4,18 +4,17 @@ import io.github.detekt.test.utils.NullPrintStream
import io.github.detekt.test.utils.createTempFileForTest
import io.gitlab.arturbosch.detekt.cli.parseArguments
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.nio.file.Files

class ConfigExporterSpec {

@Test
fun `should export the given config`() {
val tmpConfig = createTempFileForTest("ConfigPrinterSpec", ".yml")
val tmpConfig = createTempFileForTest("ConfigPrinterSpec", ".yml").also { Files.delete(it) }
val cliArgs = parseArguments(arrayOf("--config", tmpConfig.toString()))

ConfigExporter(cliArgs, NullPrintStream(), NullPrintStream()).execute()
ConfigExporter(cliArgs, NullPrintStream()).execute()

assertThat(Files.readAllLines(tmpConfig)).isNotEmpty
}
Expand Down

0 comments on commit e55d034

Please sign in to comment.