From 4470f5ede6ad60ad0c9d7a0477e0e85ec596543b Mon Sep 17 00:00:00 2001 From: Sebastian Sigl Date: Sat, 23 Jul 2022 13:54:53 +0200 Subject: [PATCH 1/2] Add default classpath Closes #144 --- baseline.xml | 2 +- src/main/java/com/github/ozsie/CheckMojo.kt | 18 ++++- .../java/com/github/ozsie/CheckMojoSpec.kt | 49 +++++++++---- .../github/ozsie/test/CheckMojoTestFactory.kt | 69 +++++++++++++++++++ .../resources/code-samples/valid/ValidFile.kt | 1 + 5 files changed, 120 insertions(+), 19 deletions(-) create mode 100644 src/test/java/com/github/ozsie/test/CheckMojoTestFactory.kt create mode 100644 src/test/resources/code-samples/valid/ValidFile.kt diff --git a/baseline.xml b/baseline.xml index 586363a..2665ddc 100644 --- a/baseline.xml +++ b/baseline.xml @@ -2,7 +2,7 @@ - InvalidPackageDeclaration:WildcardImportViolated.kt$package `code-samples`.`invalid-package-naming` + PackageNaming:ValidFile.kt$package `code-samples`.`valid` PackageNaming:WildcardImportViolated.kt$package `code-samples`.`invalid-package-naming` diff --git a/src/main/java/com/github/ozsie/CheckMojo.kt b/src/main/java/com/github/ozsie/CheckMojo.kt index 07b304e..6c7fe07 100644 --- a/src/main/java/com/github/ozsie/CheckMojo.kt +++ b/src/main/java/com/github/ozsie/CheckMojo.kt @@ -1,6 +1,7 @@ package com.github.ozsie import io.github.detekt.tooling.api.MaxIssuesReached +import io.gitlab.arturbosch.detekt.cli.CliArgs import io.gitlab.arturbosch.detekt.cli.parseArguments import io.gitlab.arturbosch.detekt.cli.runners.Runner import org.apache.maven.plugins.annotations.LifecyclePhase @@ -15,18 +16,29 @@ import java.nio.file.Paths threadSafe = true, requiresDependencyCollection = ResolutionScope.TEST) class CheckMojo : DetektMojo() { + lateinit var cliArgs: CliArgs + override fun execute() { getCliSting().forEach { log.debug("Applying $it") } - val cliArgs = parseArguments(getCliSting().log().toTypedArray()) + this.cliArgs = parseArguments(getCliSting().log().toTypedArray()) + val foundInputDir = Files.isDirectory(Paths.get(input)) - if (!skip && foundInputDir) + if (!skip && foundInputDir) { + setDefaultClasspathIfNotSet(cliArgs) failBuildIfNeeded { Runner(cliArgs, System.out, System.err).execute() } - else + } else inputSkipLog(skip) } + private fun setDefaultClasspathIfNotSet(cliArgs: CliArgs) { + if (cliArgs.classpath.isNullOrBlank()) { + cliArgs.classpath = mavenProject?.compileClasspathElements?.joinToString( + java.io.File.pathSeparatorChar.toString()) + } + } + private fun failBuildIfNeeded(block: () -> Unit) { try { block() diff --git a/src/test/java/com/github/ozsie/CheckMojoSpec.kt b/src/test/java/com/github/ozsie/CheckMojoSpec.kt index 046cc9d..8e29034 100644 --- a/src/test/java/com/github/ozsie/CheckMojoSpec.kt +++ b/src/test/java/com/github/ozsie/CheckMojoSpec.kt @@ -1,22 +1,14 @@ package com.github.ozsie +import com.github.ozsie.test.CheckMojoTestFactory import io.github.detekt.tooling.api.MaxIssuesReached import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.dsl.given import org.jetbrains.spek.api.dsl.on -import java.net.URI -import java.nio.file.Paths import kotlin.test.assertFailsWith import kotlin.test.expect - class CheckMojoSpec : Spek({ - val invalidPackageNamingDirectoryPath by lazy { - val uri = CheckMojoSpec::class.java.classLoader - .getResource("code-samples/invalid-package-naming")!!.toURI() - Paths.get(uri).toString() - } - given("a CheckMojo and 'skip' is true") { val checkMojo = CheckMojo() checkMojo.skip = true @@ -42,11 +34,11 @@ class CheckMojoSpec : Spek({ } given("a CheckMojo and 'failBuildOnMaxIssuesReached' is false") { - val checkMojo = CheckMojo().apply { - input = invalidPackageNamingDirectoryPath + val checkMojo = CheckMojoTestFactory.createWithInvalidPackageNamingStructure { failBuildOnMaxIssuesReached = false - failFast = true // fail on any issue + failFast = true } + on("checkMojo.execute()") { test("Unit is expected") { expect(Unit) { @@ -57,10 +49,9 @@ class CheckMojoSpec : Spek({ } given("a CheckMojo and 'failBuildOnMaxIssuesReached' is true") { - val checkMojo = CheckMojo().apply { - input = invalidPackageNamingDirectoryPath + val checkMojo = CheckMojoTestFactory.createWithInvalidPackageNamingStructure { failBuildOnMaxIssuesReached = true - failFast = true // fail on any issue + failFast = true } on("checkMojo.execute()") { test("Unit is expected") { @@ -70,4 +61,32 @@ class CheckMojoSpec : Spek({ } } } + + given("classpath parameter") { + val checkMojo = CheckMojoTestFactory.createWithNoRuleExecution { + classPath = "/tmp/provided" + } + + on("checkMojo.execute()") { + test("uses provider value") { + expect("/tmp/provided") { + checkMojo.execute() + checkMojo.cliArgs.classpath + } + } + } + } + + given("no classpath parameter") { + val checkMojo = CheckMojoTestFactory.createWithNoRuleExecution() + + on("checkMojo.execute()") { + test("uses default compileClasspathElements") { + expect("/tmp/default${java.io.File.pathSeparatorChar}/tmp/default2") { + checkMojo.execute() + checkMojo.cliArgs.classpath + } + } + } + } }) diff --git a/src/test/java/com/github/ozsie/test/CheckMojoTestFactory.kt b/src/test/java/com/github/ozsie/test/CheckMojoTestFactory.kt new file mode 100644 index 0000000..e5d239b --- /dev/null +++ b/src/test/java/com/github/ozsie/test/CheckMojoTestFactory.kt @@ -0,0 +1,69 @@ +package com.github.ozsie.test + +import com.github.ozsie.CheckMojo +import com.github.ozsie.CheckMojoSpec +import com.nhaarman.mockito_kotlin.any +import com.nhaarman.mockito_kotlin.doReturn +import com.nhaarman.mockito_kotlin.mock +import org.apache.maven.model.Dependency +import org.apache.maven.model.Plugin +import org.apache.maven.project.MavenProject +import java.nio.file.Paths + +object CheckMojoTestFactory { + + private val invalidPackageNamingDirectoryPath by lazy { + val uri = CheckMojoSpec::class.java.classLoader + .getResource("code-samples/invalid-package-naming")!!.toURI() + Paths.get(uri).toString() + } + + private val validPackageNamingDirectoryPath by lazy { + val uri = CheckMojoSpec::class.java.classLoader + .getResource("code-samples/valid")!!.toURI() + Paths.get(uri).toString() + } + + fun create(block: CheckMojo.() -> Unit = {}): CheckMojo { + return CheckMojo().apply { + input = validPackageNamingDirectoryPath + mavenProject = createMockMavenProject() + block(this) + } + } + + fun createWithInvalidPackageNamingStructure(block: CheckMojo.() -> Unit): CheckMojo { + return CheckMojo().apply { + input = invalidPackageNamingDirectoryPath + block(this) + } + } + + fun createWithNoRuleExecution(block: CheckMojo.() -> Unit = {}): CheckMojo { + return create { + disableDefaultRuleSets = true + block(this) + } + } + + private fun createMockMavenProject(): MavenProject { + return mock { + on { + compileClasspathElements + } doReturn listOf("/tmp/default", "/tmp/default2") + on { + getPlugin(any()) + } doReturn ( + Plugin().apply { + dependencies = mutableListOf( + Dependency().apply { + groupId = "a.b" + artifactId = "b" + version = "1" + } + ) + } + ) + } + } +} diff --git a/src/test/resources/code-samples/valid/ValidFile.kt b/src/test/resources/code-samples/valid/ValidFile.kt new file mode 100644 index 0000000..08a4178 --- /dev/null +++ b/src/test/resources/code-samples/valid/ValidFile.kt @@ -0,0 +1 @@ +package `code-samples`.`valid` From 6740e8844b1b7ab26e6b6cbca126330b84a8073e Mon Sep 17 00:00:00 2001 From: Sebastian Sigl Date: Sat, 23 Jul 2022 13:57:20 +0200 Subject: [PATCH 2/2] Delete type resolution block from readme --- README.md | 43 ------------------------------------------- 1 file changed, 43 deletions(-) diff --git a/README.md b/README.md index ed732f3..f0800e5 100644 --- a/README.md +++ b/README.md @@ -187,49 +187,6 @@ This will generate a baseline file for each module named as `baseline- ``` -## Using Type Resolution - -See [Issue #144](https://github.com/Ozsie/detekt-maven-plugin/issues/144) for an explanation. - -```xml - - - - org.apache.maven.plugins - maven-dependency-plugin - 3.2.0 - - - generate-classpath-var - package - build-classpath - - generated.classpath - true - - - - - - com.github.ozsie - detekt-maven-plugin - 1.21.0 - - baseline.xml - ${generated.classpath} - 17 - - - - verify - check - - - - - -``` - ### Goals #### check