Skip to content

Commit

Permalink
Migrate tests in detekt-cli to junit (#4519)
Browse files Browse the repository at this point in the history
  • Loading branch information
marschwar committed Jan 24, 2022
1 parent 743706f commit 3c9f0e9
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 217 deletions.
3 changes: 1 addition & 2 deletions detekt-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ dependencies {
runtimeOnly(projects.detektRules)

testImplementation(projects.detektTestUtils)
testImplementation(libs.bundles.testImplementation)
testRuntimeOnly(libs.spek.runner)
testImplementation(libs.assertj)

formattingJar(projects.detektFormatting)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,45 @@ import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatCode
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import java.nio.file.Path
import java.nio.file.Paths

internal class CliArgsSpec : Spek({
internal class CliArgsSpec {

val projectPath = resourceAsPath("/").parent.parent.parent.parent.toAbsolutePath()
private val projectPath: Path = resourceAsPath("/").parent.parent.parent.parent.toAbsolutePath()

describe("Parsing the input path") {
@Nested
inner class `Parsing the input path` {

it("the current working directory is used if parameter is not set") {
@Test
fun `the current working directory is used if parameter is not set`() {
val cli = parseArguments(emptyArray())
assertThat(cli.inputPaths).hasSize(1)
assertThat(cli.inputPaths.first()).isEqualTo(Paths.get(System.getProperty("user.dir")))
}

it("a single value is converted to a path") {
@Test
fun `a single value is converted to a path`() {
val cli = parseArguments(arrayOf("--input", "$projectPath"))
assertThat(cli.inputPaths).hasSize(1)
assertThat(cli.inputPaths.first().toAbsolutePath()).isEqualTo(projectPath)
}

it("multiple input paths can be separated by comma") {
@Test
fun `multiple input paths can be separated by comma`() {
val mainPath = projectPath.resolve("src/main").toAbsolutePath()
val testPath = projectPath.resolve("src/test").toAbsolutePath()
val cli = parseArguments(arrayOf("--input", "$mainPath,$testPath"))
assertThat(cli.inputPaths).hasSize(2)
assertThat(cli.inputPaths.map(Path::toAbsolutePath)).containsExactlyInAnyOrder(mainPath, testPath)
}

it("reports an error if the input path does not exist") {
@Test
fun `reports an error if the input path does not exist`() {
val pathToNonExistentDirectory = projectPath.resolve("nonExistent")
val params = arrayOf("--input", "$pathToNonExistentDirectory")

Expand All @@ -47,9 +54,11 @@ internal class CliArgsSpec : Spek({
}
}

describe("parsing config parameters") {
@Nested
inner class `parsing config parameters` {

it("should fail on invalid config value") {
@Test
fun `should fail on invalid config value`() {
assertThatIllegalArgumentException()
.isThrownBy { parseArguments(arrayOf("--config", ",")).toSpec() }
assertThatExceptionOfType(ParameterException::class.java)
Expand All @@ -59,49 +68,57 @@ internal class CliArgsSpec : Spek({
}
}

describe("Valid combination of options") {
@Nested
inner class `Valid combination of options` {

describe("Baseline feature") {
@Nested
inner class `Baseline feature` {

it("reports an error when using --create-baseline without a --baseline file") {
@Test
fun `reports an error when using --create-baseline without a --baseline file`() {
assertThatCode { parseArguments(arrayOf("--create-baseline")) }
.isInstanceOf(HandledArgumentViolation::class.java)
.hasMessageContaining("Creating a baseline.xml requires the --baseline parameter to specify a path")
}

it("reports an error when using --baseline file does not exist") {
@Test
fun `reports an error when using --baseline file does not exist`() {
val nonExistingDirectory = projectPath.resolve("nonExistent").toString()
assertThatCode { parseArguments(arrayOf("--baseline", nonExistingDirectory)) }
.isInstanceOf(HandledArgumentViolation::class.java)
.hasMessageContaining("The file specified by --baseline should exist '$nonExistingDirectory'.")
}

it("reports an error when using --baseline file which is not a file") {
@Test
fun `reports an error when using --baseline file which is not a file`() {
val directory = resourceAsPath("/cases").toString()
assertThatCode { parseArguments(arrayOf("--baseline", directory)) }
.isInstanceOf(HandledArgumentViolation::class.java)
.hasMessageContaining("The path specified by --baseline should be a file '$directory'.")
}
}

it("throws HelpRequest on --help") {
@Test
fun `throws HelpRequest on --help`() {
assertThatExceptionOfType(HelpRequest::class.java)
.isThrownBy { parseArguments(arrayOf("--help")) }
}

it("throws HandledArgumentViolation on wrong options") {
@Test
fun `throws HandledArgumentViolation on wrong options`() {
assertThatExceptionOfType(HandledArgumentViolation::class.java)
.isThrownBy { parseArguments(arrayOf("--unknown-to-us-all")) }
}
}

describe("--all-rules and --fail-fast lead to all rules being activated") {
@Nested
inner class `--all-rules and --fail-fast lead to all rules being activated` {

arrayOf("--all-rules", "--fail-fast").forEach { flag ->
it("is true for flag $flag") {
val spec = parseArguments(arrayOf(flag)).toSpec()
assertThat(spec.rulesSpec.activateAllRules).isTrue()
}
@ParameterizedTest
@ValueSource(strings = ["--all-rules", "--fail-fast"])
fun `all rules active`(flag: String) {
val spec = parseArguments(arrayOf(flag)).toSpec()
assertThat(spec.rulesSpec.activateAllRules).isTrue()
}
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package io.gitlab.arturbosch.detekt.cli

import io.github.detekt.tooling.api.DetektCli
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.junit.jupiter.api.Test

class CliRunnerSpec : Spek({
class CliRunnerSpec {

test("cli module provides an implementation of DetektCli") {
@Test
fun `cli module provides an implementation of DetektCli`() {
assertThat(DetektCli.load()).isInstanceOf(CliRunner::class.java)
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,43 @@ import io.gitlab.arturbosch.detekt.cli.runners.ConfigExporter
import io.gitlab.arturbosch.detekt.cli.runners.Runner
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

class MainSpec : Spek({

describe("build runner") {

listOf(
arrayOf("--generate-config"),
arrayOf("--run-rule", "RuleSet: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") -> Runner::class
args.contains("--print-ast") -> AstPrinter::class
else -> Runner::class
}

it("returns [${expectedRunnerClass.simpleName}] when arguments are $args") {
val runner = buildRunner(args, NullPrintStream(), NullPrintStream())

assertThat(runner).isExactlyInstanceOf(expectedRunnerClass.java)
}
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import java.util.stream.Stream
import kotlin.reflect.KClass

class MainSpec {

@Nested
inner class `Build runner` {

fun runnerConfigs(): Stream<Arguments> {
return Stream.of(
Arguments.of(arrayOf("--generate-config"), ConfigExporter::class),
Arguments.of(arrayOf("--run-rule", "RuleSet:Rule"), Runner::class),
Arguments.of(arrayOf("--print-ast"), AstPrinter::class),
Arguments.of(arrayOf("--version"), VersionPrinter::class),
Arguments.of(emptyArray<String>(), Runner::class),
)
}

@ParameterizedTest
@MethodSource("runnerConfigs")
fun `builds correct runnner`(args: Array<String>, expectedRunnerClass: KClass<*>) {
val runner = buildRunner(args, NullPrintStream(), NullPrintStream())

assertThat(runner).isExactlyInstanceOf(expectedRunnerClass.java)
}
}

describe("Runner creates baselines") {
@Nested
inner class `Runner creates baselines` {

it("succeeds with --create-baseline and --baseline") {
@Test
fun `succeeds with --create-baseline and --baseline`() {
val out = StringPrintStream()
val err = StringPrintStream()

Expand All @@ -58,7 +61,8 @@ class MainSpec : Spek({
assertThat(err.toString()).isEmpty()
}

it("succeeds with --baseline if the path exists and is a file") {
@Test
fun `succeeds with --baseline if the path exists and is a file`() {
val out = StringPrintStream()
val err = StringPrintStream()

Expand All @@ -71,4 +75,4 @@ class MainSpec : Spek({
assertThat(err.toString()).isEmpty()
}
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,50 @@ package io.gitlab.arturbosch.detekt.cli
import io.github.detekt.test.utils.NullPrintStream
import io.gitlab.arturbosch.detekt.api.internal.PathFilters
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.nio.file.Paths

class PathFiltersSpec : Spek({
class PathFiltersSpec {

describe("parse different filter settings") {
@Nested
inner class `parse different filter settings` {

it("should load single filter") {
@Test
fun `should load single filter`() {
val filters = CliArgs { excludes = "**/one/**" }.toSpecFilters()
assertThat(filters?.isIgnored(Paths.get("/one/path"))).isTrue()
assertThat(filters?.isIgnored(Paths.get("/two/path"))).isFalse()
}

describe("parsing with different nullability combinations of path filters") {
it("returns an empty path filter when includes are empty and excludes are empty") {
@Nested
inner class `parsing with different nullability combinations of path filters` {
@Test
fun `returns an empty path filter when includes are empty and excludes are empty`() {
val pathFilter = PathFilters.of(emptyList(), emptyList())
assertThat(pathFilter).isNull()
}

it("parses includes correctly") {
@Test
fun `parses includes correctly`() {
val pathFilter = PathFilters.of(listOf("**/one/**", "**/two/**"), emptyList())
assertThat(pathFilter).isNotNull
assertThat(pathFilter?.isIgnored(Paths.get("/one/path"))).isFalse
assertThat(pathFilter?.isIgnored(Paths.get("/two/path"))).isFalse
assertThat(pathFilter?.isIgnored(Paths.get("/three/path"))).isTrue
}

it("parses excludes correctly") {
@Test
fun `parses excludes correctly`() {
val pathFilter = PathFilters.of(emptyList(), listOf("**/one/**", "**/two/**"))
assertThat(pathFilter).isNotNull
assertThat(pathFilter?.isIgnored(Paths.get("/one/path"))).isTrue
assertThat(pathFilter?.isIgnored(Paths.get("/two/path"))).isTrue
assertThat(pathFilter?.isIgnored(Paths.get("/three/path"))).isFalse
}

it("parses both includes and excludes correctly") {
@Test
fun `parses both includes and excludes correctly`() {
val pathFilter = PathFilters.of(listOf("**/one/**"), listOf("**/two/**"))
assertThat(pathFilter).isNotNull
assertThat(pathFilter?.isIgnored(Paths.get("/one/path"))).isFalse
Expand All @@ -48,40 +55,48 @@ class PathFiltersSpec : Spek({
}
}

describe("parsing with different separators") {
@Nested
inner class `parsing with different separators` {

it("should load multiple comma-separated filters with no spaces around commas") {
@Test
fun `should load multiple comma-separated filters with no spaces around commas`() {
val filters = CliArgs { excludes = "**/one/**,**/two/**,**/three" }.toSpecFilters()
assertSameFiltersIndependentOfSpacingAndSeparater(filters)
}

it("should load multiple semicolon-separated filters with no spaces around semicolons") {
@Test
fun `should load multiple semicolon-separated filters with no spaces around semicolons`() {
val filters = CliArgs { excludes = "**/one/**;**/two/**;**/three" }.toSpecFilters()
assertSameFiltersIndependentOfSpacingAndSeparater(filters)
}

it("should load multiple comma-separated filters with spaces around commas") {
@Test
fun `should load multiple comma-separated filters with spaces around commas`() {
val filters = CliArgs { excludes = "**/one/** ,**/two/**, **/three" }.toSpecFilters()
assertSameFiltersIndependentOfSpacingAndSeparater(filters)
}

it("should load multiple semicolon-separated filters with spaces around semicolons") {
@Test
fun `should load multiple semicolon-separated filters with spaces around semicolons`() {
val filters = CliArgs { excludes = "**/one/** ;**/two/**; **/three" }.toSpecFilters()
assertSameFiltersIndependentOfSpacingAndSeparater(filters)
}

it("should load multiple mixed-separated filters with no spaces around separators") {
@Test
fun `should load multiple mixed-separated filters with no spaces around separators`() {
val filters = CliArgs { excludes = "**/one/**,**/two/**;**/three" }.toSpecFilters()
assertSameFiltersIndependentOfSpacingAndSeparater(filters)
}

it("should load multiple mixed-separated filters with spaces around separators") {
@Test
fun `should load multiple mixed-separated filters with spaces around separators`() {
val filters = CliArgs { excludes = "**/one/** ,**/two/**; **/three" }.toSpecFilters()
assertSameFiltersIndependentOfSpacingAndSeparater(filters)
}
}

it("should ignore empty and blank filters") {
@Test
fun `should ignore empty and blank filters`() {
val filters = CliArgs { excludes = " ,,**/three" }.toSpecFilters()
assertThat(filters?.isIgnored(Paths.get("/three"))).isTrue()
assertThat(filters?.isIgnored(Paths.get("/root/three"))).isTrue()
Expand All @@ -90,7 +105,7 @@ class PathFiltersSpec : Spek({
assertThat(filters?.isIgnored(Paths.get("/three/path"))).isFalse()
}
}
})
}

private fun CliArgs.toSpecFilters(): PathFilters? {
val spec = this.createSpec(NullPrintStream(), NullPrintStream()).projectSpec
Expand Down

0 comments on commit 3c9f0e9

Please sign in to comment.