Skip to content

Commit

Permalink
Add --version to cli (#2383)
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Mar 3, 2020
1 parent aefe4e4 commit e896b00
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
Expand Up @@ -173,6 +173,12 @@ class CliArgs : Args {
)
var jvmTarget: JvmTarget = JvmTarget.DEFAULT

@Parameter(
names = ["--version"],
description = "Prints the detekt CLI version."
)
var showVersion: Boolean = false

val inputPaths: List<Path> by lazy {
MultipleExistingPathConverter().convert(input ?: System.getProperty("user.dir"))
}
Expand Down
Expand Up @@ -8,6 +8,7 @@ import io.gitlab.arturbosch.detekt.cli.runners.ConfigExporter
import io.gitlab.arturbosch.detekt.cli.runners.Executable
import io.gitlab.arturbosch.detekt.cli.runners.Runner
import io.gitlab.arturbosch.detekt.cli.runners.SingleRuleRunner
import io.gitlab.arturbosch.detekt.cli.runners.VersionPrinter
import java.io.PrintStream
import kotlin.system.exitProcess

Expand Down Expand Up @@ -48,6 +49,7 @@ fun buildRunner(
}
}
return when {
arguments.showVersion -> VersionPrinter(outputPrinter)
arguments.generateConfig -> ConfigExporter(arguments)
arguments.runRule != null -> SingleRuleRunner(arguments)
arguments.printAst -> AstPrinter(arguments)
Expand Down
@@ -0,0 +1,16 @@
package io.gitlab.arturbosch.detekt.cli.runners

import io.gitlab.arturbosch.detekt.core.whichDetekt
import java.io.PrintStream

class VersionPrinter(private val outputPrinter: PrintStream) : Executable {

override fun execute() {
val version = whichDetekt()
if (version != null) {
outputPrinter.println(version)
} else {
throw IllegalStateException("Can't find the detekt version")
}
}
}
Expand Up @@ -4,6 +4,7 @@ import io.gitlab.arturbosch.detekt.cli.runners.AstPrinter
import io.gitlab.arturbosch.detekt.cli.runners.ConfigExporter
import io.gitlab.arturbosch.detekt.cli.runners.Runner
import io.gitlab.arturbosch.detekt.cli.runners.SingleRuleRunner
import io.gitlab.arturbosch.detekt.cli.runners.VersionPrinter
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
Expand All @@ -22,10 +23,12 @@ class MainSpec : Spek({
arrayOf("--generate-config"),
arrayOf("--run-rule", "Rule"),
arrayOf("--print-ast"),
arrayOf("--version"),
emptyArray()
).forEach { args ->

val expectedRunnerClass = when {
args.contains("--version") -> VersionPrinter::class
args.contains("--generate-config") -> ConfigExporter::class
args.contains("--run-rule") -> SingleRuleRunner::class
args.contains("--print-ast") -> AstPrinter::class
Expand Down
@@ -0,0 +1,24 @@
package io.gitlab.arturbosch.detekt.cli.runners

import io.gitlab.arturbosch.detekt.core.Detektor
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.nio.charset.Charset

class VersionPrinterSpec : Spek({

describe("version printer") {

it("print the version") {
val byteArrayOutputStream = ByteArrayOutputStream()

VersionPrinter(PrintStream(byteArrayOutputStream)).execute()

assertThat(String(byteArrayOutputStream.toByteArray(), Charset.forName("UTF-8")))
.isEqualTo("1.6.0" + System.lineSeparator())
}
}
})
3 changes: 3 additions & 0 deletions detekt-cli/src/test/resources/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
DetektVersion: 1.6.0

0 comments on commit e896b00

Please sign in to comment.