diff --git a/pom.xml b/pom.xml index 81b9a0a..1219618 100644 --- a/pom.xml +++ b/pom.xml @@ -48,7 +48,6 @@ 3.5.1 - 1.2.1 1.6.0 1.3.2 @@ -113,16 +112,17 @@ - org.jetbrains.spek - spek-api - ${spek-api.version} + org.jetbrains.kotlin + kotlin-test + ${kotlin.version} + test + + + + org.jetbrains.kotlin + kotlin-test-junit5 + ${kotlin.version} test - - - org.jetbrains.kotlin - kotlin-reflect - - com.beust @@ -130,18 +130,6 @@ ${jcommander.version} - - org.jetbrains.spek - spek-junit-platform-engine - ${spek-api.version} - test - - - org.jetbrains.kotlin - kotlin-reflect - - - org.jetbrains.kotlin kotlin-reflect @@ -278,7 +266,7 @@ ${maven-surefire-plugin.version} - **/*Spec.* + **/*Test.* @@ -288,15 +276,9 @@ ${junit-platform-surefire-provider.version} - org.jetbrains.spek - spek-junit-platform-engine - ${spek-api.version} - - - org.jetbrains.kotlin - kotlin-runtime - - + org.jetbrains.kotlin + kotlin-test-junit5 + ${kotlin.version} @@ -375,6 +357,11 @@ JCenter Repository https://jcenter.bintray.com/ + + maven + Maven + https://repo1.maven.org/maven2/ + @@ -383,6 +370,11 @@ JCenter Repository https://jcenter.bintray.com/ + + maven + Maven + https://repo1.maven.org/maven2/ + diff --git a/src/test/java/com/github/ozsie/CheckMojoSpec.kt b/src/test/java/com/github/ozsie/CheckMojoSpec.kt deleted file mode 100644 index 882c06b..0000000 --- a/src/test/java/com/github/ozsie/CheckMojoSpec.kt +++ /dev/null @@ -1,93 +0,0 @@ -package com.github.ozsie - -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 kotlin.test.assertFailsWith -import kotlin.test.expect - -class CheckMojoSpec : Spek({ - val codeSamplesDirectory = CheckMojoSpec::class.java.classLoader.getResource("code-samples")!!.file - val invalidPackageNamingDirectoryPath = "$codeSamplesDirectory/invalid-package-naming" - - given("a CheckMojo and 'skip' is true") { - val checkMojo = CheckMojo() - checkMojo.skip = true - on("checkMojo.execute()") { - test("unit is expected") { - expect(Unit) { - checkMojo.execute() - } - } - } - } - - given("a CheckMojo and 'skip' is false") { - val checkMojo = CheckMojo() - checkMojo.skip = false - on("checkMojo.execute()") { - test("Unit is expected") { - expect(Unit) { - checkMojo.execute() - } - } - } - } - - given("a CheckMojo and 'failBuildOnMaxIssuesReached' is false") { - val checkMojo = CheckMojo().apply { - input = invalidPackageNamingDirectoryPath - failBuildOnMaxIssuesReached = false - } - on("checkMojo.execute()") { - test("Unit is expected") { - expect(Unit) { - checkMojo.execute() - } - } - } - } - - given("a CheckMojo and 'failBuildOnMaxIssuesReached' is true") { - val checkMojo = CheckMojo().apply { - input = invalidPackageNamingDirectoryPath - failBuildOnMaxIssuesReached = true - } - on("checkMojo.execute()") { - test("Unit is expected") { - assertFailsWith(MaxIssuesReached::class, "Build failed with 1 weighted issues.") { - checkMojo.execute() - } - } - } - } - - given("multiple valid comma separated input directories are supplied") { - val checkMojo = CheckMojo().apply { - input = "$codeSamplesDirectory/valid,$codeSamplesDirectory/valid2" - failBuildOnMaxIssuesReached = true - } - on("checkMojo.execute()") { - test("detekt analyses the specified directories") { - assertFailsWith(MaxIssuesReached::class, "Build failed with 2 weighted issues.") { - checkMojo.execute() - } - } - } - } - - given("a mix of valid and invalid comma separated input directories are supplied") { - val checkMojo = CheckMojo().apply { - input = "$codeSamplesDirectory/valid,invalidDirectory" - failBuildOnMaxIssuesReached = false - } - on("checkMojo.execute()") { - test("detekt analysis is aborted") { - expect(Unit) { - checkMojo.execute() - } - } - } - } -}) diff --git a/src/test/java/com/github/ozsie/CheckMojoTest.kt b/src/test/java/com/github/ozsie/CheckMojoTest.kt new file mode 100644 index 0000000..ca239bd --- /dev/null +++ b/src/test/java/com/github/ozsie/CheckMojoTest.kt @@ -0,0 +1,73 @@ +package com.github.ozsie + +import io.github.detekt.tooling.api.MaxIssuesReached +import kotlin.test.assertFailsWith +import kotlin.test.expect +import org.junit.jupiter.api.Test + +class CheckMojoTest { + private val codeSamplesDirectory = CheckMojoTest::class.java.classLoader.getResource("code-samples")!!.file + private val invalidPackageNamingDirectoryPath = "$codeSamplesDirectory/invalid-package-naming" + + @Test + fun `a CheckMojo and 'skip' is true Unit is expected`() { + val checkMojo = CheckMojo() + checkMojo.skip = true + expect(Unit) { + checkMojo.execute() + } + } + + @Test + fun `a CheckMojo and 'skip' is false Unit is expected`() { + val checkMojo = CheckMojo() + checkMojo.skip = false + expect(Unit) { + checkMojo.execute() + } + } + + @Test + fun `a CheckMojo and 'failBuildOnMaxIssuesReached' is false Unit is exptected`() { + val checkMojo = CheckMojo().apply { + input = invalidPackageNamingDirectoryPath + failBuildOnMaxIssuesReached = false + } + expect(Unit) { + checkMojo.execute() + } + } + + @Test + fun `a CheckMojo and 'failBuildOnMaxIssuesReached' is true Unit is exptected`() { + val checkMojo = CheckMojo().apply { + input = invalidPackageNamingDirectoryPath + failBuildOnMaxIssuesReached = true + } + assertFailsWith(MaxIssuesReached::class, "Build failed with 1 weighted issues.") { + checkMojo.execute() + } + } + + @Test + fun `multiple valid comma separated input directories are supplied detekt analyses the specified directories`() { + val checkMojo = CheckMojo().apply { + input = "$codeSamplesDirectory/valid,$codeSamplesDirectory/valid2" + failBuildOnMaxIssuesReached = true + } + assertFailsWith(MaxIssuesReached::class, "Build failed with 2 weighted issues.") { + checkMojo.execute() + } + } + + @Test + fun `a mix of valid and invalid comma separated input directories are supplied detekt analysis is aborted`() { + val checkMojo = CheckMojo().apply { + input = "$codeSamplesDirectory/valid,invalidDirectory" + failBuildOnMaxIssuesReached = false + } + expect(Unit) { + checkMojo.execute() + } + } +} diff --git a/src/test/java/com/github/ozsie/CheckWithTypeResolutionMojoSpec.kt b/src/test/java/com/github/ozsie/CheckWithTypeResolutionMojoSpec.kt deleted file mode 100644 index 4aa1c17..0000000 --- a/src/test/java/com/github/ozsie/CheckWithTypeResolutionMojoSpec.kt +++ /dev/null @@ -1,90 +0,0 @@ -package com.github.ozsie - -import com.github.ozsie.test.CheckWithTypeResolutionMojoTestFactory -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 kotlin.test.assertFailsWith -import kotlin.test.expect - -class CheckWithTypeResolutionMojoSpec : Spek({ - given("a CheckMojo and 'skip' is true") { - val checkMojo = CheckWithTypeResolutionMojo() - checkMojo.skip = true - on("checkMojo.execute()") { - test("unit is expected") { - expect(Unit) { - checkMojo.execute() - } - } - } - } - - given("a CheckMojo and 'skip' is false") { - val checkMojo = CheckWithTypeResolutionMojo() - checkMojo.skip = false - on("checkMojo.execute()") { - test("Unit is expected") { - expect(Unit) { - checkMojo.execute() - } - } - } - } - - given("a CheckMojo and 'failBuildOnMaxIssuesReached' is false") { - val checkMojo = CheckWithTypeResolutionMojoTestFactory.createWithInvalidPackageNamingStructure { - failBuildOnMaxIssuesReached = false - } - - on("checkMojo.execute()") { - test("Unit is expected") { - expect(Unit) { - checkMojo.execute() - } - } - } - } - - given("a CheckMojo and 'failBuildOnMaxIssuesReached' is true") { - val checkMojo = CheckWithTypeResolutionMojoTestFactory.createWithInvalidPackageNamingStructure { - failBuildOnMaxIssuesReached = true - } - on("checkMojo.execute()") { - test("Unit is expected") { - assertFailsWith(MaxIssuesReached::class, "Build failed with 1 weighted issues.") { - checkMojo.execute() - } - } - } - } - - given("classpath parameter") { - val checkMojo = CheckWithTypeResolutionMojoTestFactory.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 = CheckWithTypeResolutionMojoTestFactory.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/CheckWithTypeResolutionMojoTest.kt b/src/test/java/com/github/ozsie/CheckWithTypeResolutionMojoTest.kt new file mode 100644 index 0000000..7228165 --- /dev/null +++ b/src/test/java/com/github/ozsie/CheckWithTypeResolutionMojoTest.kt @@ -0,0 +1,70 @@ +package com.github.ozsie + +import com.github.ozsie.test.CheckWithTypeResolutionMojoTestFactory +import io.github.detekt.tooling.api.MaxIssuesReached +import org.junit.jupiter.api.Test +import kotlin.test.assertFailsWith +import kotlin.test.expect + +class CheckWithTypeResolutionMojoTest { + @Test + fun `a CheckMojo and 'skip' is true`() { + val checkMojo = CheckWithTypeResolutionMojo() + checkMojo.skip = true + expect(Unit) { + checkMojo.execute() + } + } + + @Test + fun `a CheckMojo and 'skip' is false`() { + val checkMojo = CheckWithTypeResolutionMojo() + checkMojo.skip = false + expect(Unit) { + checkMojo.execute() + } + } + + @Test + fun `a CheckMojo and 'failBuildOnMaxIssuesReached' is false`() { + val checkMojo = CheckWithTypeResolutionMojoTestFactory.createWithInvalidPackageNamingStructure { + failBuildOnMaxIssuesReached = false + } + + expect(Unit) { + checkMojo.execute() + } + } + + @Test + fun `a CheckMojo and 'failBuildOnMaxIssuesReached' is true`() { + val checkMojo = CheckWithTypeResolutionMojoTestFactory.createWithInvalidPackageNamingStructure { + failBuildOnMaxIssuesReached = true + } + assertFailsWith(MaxIssuesReached::class, "Build failed with 1 weighted issues.") { + checkMojo.execute() + } + } + + @Test + fun `classpath parameter`() { + val checkMojo = CheckWithTypeResolutionMojoTestFactory.createWithNoRuleExecution { + classPath = "/tmp/provided" + } + + expect("/tmp/provided") { + checkMojo.execute() + checkMojo.cliArgs.classpath + } + } + + @Test + fun `no classpath parameter`() { + val checkMojo = CheckWithTypeResolutionMojoTestFactory.createWithNoRuleExecution() + + expect("/tmp/default${java.io.File.pathSeparatorChar}/tmp/default2") { + checkMojo.execute() + checkMojo.cliArgs.classpath + } + } +} diff --git a/src/test/java/com/github/ozsie/CreateBaselineMojoSpec.kt b/src/test/java/com/github/ozsie/CreateBaselineMojoSpec.kt deleted file mode 100644 index a2629c9..0000000 --- a/src/test/java/com/github/ozsie/CreateBaselineMojoSpec.kt +++ /dev/null @@ -1,58 +0,0 @@ -package com.github.ozsie - -import com.beust.jcommander.ParameterException -import org.jetbrains.spek.api.Spek -import org.jetbrains.spek.api.dsl.given -import org.jetbrains.spek.api.dsl.on -import kotlin.test.assertFailsWith -import kotlin.test.expect - -class CreateBaselineMojoSpec : Spek({ - given("a CreateBaselineMojo and 'skip' is true") { - val createBaselineMojo = CreateBaselineMojo() - createBaselineMojo.skip = true - on("createBaselineMojo.execute()") { - test("unit is expected") { - expect(Unit) { - createBaselineMojo.execute() - } - } - } - } - - given("a CreateBaselineMojo and 'skip' is false") { - val createBaselineMojo = CreateBaselineMojo() - createBaselineMojo.skip = false - on("createBaselineMojo.execute()") { - test("unit is expected") { - expect(Unit) { - createBaselineMojo.execute() - } - } - } - } - - given("a CBMojo and 'skip' is true") { - val createBaselineMojo = CBMojo() - createBaselineMojo.skip = true - on("createBaselineMojo.execute()") { - test("unit is expected") { - expect(Unit) { - createBaselineMojo.execute() - } - } - } - } - - given("a CBMojo and 'skip' is false") { - val createBaselineMojo = CBMojo() - createBaselineMojo.skip = false - on("createBaselineMojo.execute()") { - test("unit is expected") { - expect(Unit) { - createBaselineMojo.execute() - } - } - } - } -}) diff --git a/src/test/java/com/github/ozsie/CreateBaselineMojoTest.kt b/src/test/java/com/github/ozsie/CreateBaselineMojoTest.kt new file mode 100644 index 0000000..c12c933 --- /dev/null +++ b/src/test/java/com/github/ozsie/CreateBaselineMojoTest.kt @@ -0,0 +1,42 @@ +package com.github.ozsie + +import org.junit.jupiter.api.Test +import kotlin.test.expect + +class CreateBaselineMojoTest { + @Test + fun `a CreateBaselineMojo and 'skip' is true Unit is expected`() { + val createBaselineMojo = CreateBaselineMojo() + createBaselineMojo.skip = true + expect(Unit) { + createBaselineMojo.execute() + } + } + + @Test + fun `a CreateBaselineMojo and 'skip' is false Unit is expected`() { + val createBaselineMojo = CreateBaselineMojo() + createBaselineMojo.skip = false + expect(Unit) { + createBaselineMojo.execute() + } + } + + @Test + fun `a CBMojo and 'skip' is true Unit is expected`() { + val createBaselineMojo = CBMojo() + createBaselineMojo.skip = true + expect(Unit) { + createBaselineMojo.execute() + } + } + + @Test + fun `a CBMojo and 'skip' is false Unit is expected`() { + val createBaselineMojo = CBMojo() + createBaselineMojo.skip = false + expect(Unit) { + createBaselineMojo.execute() + } + } +} diff --git a/src/test/java/com/github/ozsie/DetektMojoSpec.kt b/src/test/java/com/github/ozsie/DetektMojoSpec.kt deleted file mode 100644 index 1d7b7a9..0000000 --- a/src/test/java/com/github/ozsie/DetektMojoSpec.kt +++ /dev/null @@ -1,113 +0,0 @@ -package com.github.ozsie - -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.plugin.logging.Log -import org.apache.maven.plugin.logging.SystemStreamLog -import org.apache.maven.project.MavenProject -import org.jetbrains.spek.api.Spek -import org.jetbrains.spek.api.dsl.given -import org.jetbrains.spek.api.dsl.on -import kotlin.test.assertEquals - -object DetektMojoSpec : Spek({ - given("an ArrayList") { - val arrayList = arrayListOf("a", "b", "c") - - on("useIf") { - arrayList.useIf(true, "d") - test("length should match") { - assertEquals(4, arrayList.size) - } - } - } - - given("a StringBuilder") { - val sb = StringBuilder() - - val plugin = Plugin().apply { - dependencies = mutableListOf(Dependency().apply { - groupId = "x.y" - artifactId = "y" - version = "1" - }) - } - - on("buildPluginPaths") { - sb.buildPluginPaths(arrayListOf("x.y:y"), plugin, "~") - test("sb should contain path to plugin") { - assertEquals("~/x/y/y/1/y-1.jar;", sb.toString()) - } - } - } - - given("a Dependency") { - val dependency = Dependency().apply { - groupId = "x.y" - artifactId = "z" - version = "1" - } - - on("asPath") { - val path = dependency asPath "~" - test("path should equal path to plugin") { - assertEquals("~/x/y/z/1/z-1.jar", path) - } - } - - on("getIdentifier") { - val identifier = dependency.getIdentifier() - test("identifier should equal groupId:artifactId") { - assertEquals("x.y:z", identifier) - } - } - } - - given("a String") { - val groupId = "x.y.z" - on("asPath") { - val path = groupId.asPath() - test(". should be replaced by / in path") { - assertEquals("x/y/z", path) - } - } - } - - given("an ArrayList") { - val mavenProject: MavenProject = mock { - on { - getPlugin(any()) - } doReturn ( - Plugin().apply { - dependencies = mutableListOf(Dependency().apply { - groupId = "a.b" - artifactId = "b" - version = "1" - }) - } - ) - } - - val stringList = arrayListOf("x.y:y", "a.b:b", "c.d:d") - on("buildPluginPaths") { - val pluginPath = stringList.buildPluginPaths(mavenProject, "~/.m2", SystemStreamLog()) - test("StringBuilder.buildPLuginPaths should be called") { - assertEquals("~/.m2/a/b/b/1/b-1.jar", pluginPath) - } - } - } - - given("an ArrayList") { - val log: Log = mock {} - val stringList = arrayListOf("x.y:y", "a.b:b", "c.d:d") - on("log") { - val listAfterLog = stringList.log(log) - test("list should not be modified") { - assertEquals(stringList, listAfterLog) - } - } - } -}) diff --git a/src/test/java/com/github/ozsie/DetektMojoTest.kt b/src/test/java/com/github/ozsie/DetektMojoTest.kt new file mode 100644 index 0000000..7d00be0 --- /dev/null +++ b/src/test/java/com/github/ozsie/DetektMojoTest.kt @@ -0,0 +1,97 @@ +package com.github.ozsie + +import com.nhaarman.mockito_kotlin.any +import com.nhaarman.mockito_kotlin.doReturn +import com.nhaarman.mockito_kotlin.mock +import org.junit.jupiter.api.Test +import org.apache.maven.model.Dependency +import org.apache.maven.model.Plugin +import org.apache.maven.plugin.logging.Log +import org.apache.maven.plugin.logging.SystemStreamLog +import org.apache.maven.project.MavenProject +import kotlin.test.assertEquals + +object DetektMojoTest { + @Test + fun `an ArrayList useIf length should match`() { + val arrayList = arrayListOf("a", "b", "c") + arrayList.useIf(true, "d") + assertEquals(4, arrayList.size) + } + + @Test + fun `a StringBuilder buildPluginPaths sb should contain path to plugin`() { + val sb = StringBuilder() + + val plugin = Plugin().apply { + dependencies = mutableListOf(Dependency().apply { + groupId = "x.y" + artifactId = "y" + version = "1" + }) + } + + sb.buildPluginPaths(arrayListOf("x.y:y"), plugin, "~") + assertEquals("~/x/y/y/1/y-1.jar;", sb.toString()) + } + + @Test + fun `a Dependency path should equal path to plugin`() { + val dependency = Dependency().apply { + groupId = "x.y" + artifactId = "z" + version = "1" + } + + val path = dependency asPath "~" + assertEquals("~/x/y/z/1/z-1.jar", path) + } + + @Test + fun `a Dependency identifier should equal groupId artifactId`() { + val dependency = Dependency().apply { + groupId = "x.y" + artifactId = "z" + version = "1" + } + + val identifier = dependency.getIdentifier() + assertEquals("x.y:z", identifier) + } + + @Test + fun `a String asPath dot should be replaced by slash in path`() { + val groupId = "x.y.z" + val path = groupId.asPath() + assertEquals("x/y/z", path) + } + + @Test + fun `an ArrayList of String buildPluginPaths StringBuilder buildPluginPaths should be called`() { + val mavenProject: MavenProject = mock { + on { + getPlugin(any()) + } doReturn ( + Plugin().apply { + dependencies = mutableListOf(Dependency().apply { + groupId = "a.b" + artifactId = "b" + version = "1" + }) + } + ) + } + + val stringList = arrayListOf("x.y:y", "a.b:b", "c.d:d") + val pluginPath = stringList.buildPluginPaths(mavenProject, "~/.m2", SystemStreamLog()) + assertEquals("~/.m2/a/b/b/1/b-1.jar", pluginPath) + } + + @Test + fun `an ArrayList of T log list should not be modified`() { + val log: Log = mock {} + val stringList = arrayListOf("x.y:y", "a.b:b", "c.d:d") + val listAfterLog = stringList.log(log) + assertEquals(stringList, listAfterLog) + } +} diff --git a/src/test/java/com/github/ozsie/GenerateConfigMojoSpec.kt b/src/test/java/com/github/ozsie/GenerateConfigMojoSpec.kt deleted file mode 100644 index 5519f58..0000000 --- a/src/test/java/com/github/ozsie/GenerateConfigMojoSpec.kt +++ /dev/null @@ -1,60 +0,0 @@ -package com.github.ozsie - -import org.jetbrains.spek.api.Spek -import org.jetbrains.spek.api.dsl.given -import org.jetbrains.spek.api.dsl.on -import java.io.File -import kotlin.test.assertTrue -import kotlin.test.expect - -class GenerateConfigMojoSpec : Spek({ - given("a GenerateConfigMojo and 'skip' is true") { - val generateConfigMojo = GenerateConfigMojo() - generateConfigMojo.skip = true - on("generateConfigMojo.execute()") { - test("unit is expected") { - expect(Unit) { - generateConfigMojo.execute() - } - } - } - } - - given("a GenerateConfigMojo and 'skip' is false") { - val generateConfigMojo = GenerateConfigMojo() - generateConfigMojo.skip = false - on("generateConfigMojo.execute()") { - test("Config file is generated") { - generateConfigMojo.execute() - val file = File("detekt.yml") - assertTrue(file.exists()) - file.deleteOnExit() - } - } - } - - given("a GCMojo and 'skip' is true") { - val generateConfigMojo = GCMojo() - generateConfigMojo.skip = true - on("generateConfigMojo.execute()") { - test("unit is expected") { - expect(Unit) { - generateConfigMojo.execute() - } - } - } - } - - given("a GCMojo and 'skip' is false") { - val generateConfigMojo = GCMojo() - generateConfigMojo.skip = false - on("generateConfigMojo.execute()") { - test("Config file is generated") { - generateConfigMojo.execute() - val file = File("detekt.yml") - assertTrue(file.exists()) - file.deleteOnExit() - } - } - } -}) diff --git a/src/test/java/com/github/ozsie/GenerateConfigMojoTest.kt b/src/test/java/com/github/ozsie/GenerateConfigMojoTest.kt new file mode 100644 index 0000000..9858953 --- /dev/null +++ b/src/test/java/com/github/ozsie/GenerateConfigMojoTest.kt @@ -0,0 +1,46 @@ +package com.github.ozsie + +import java.io.File +import org.junit.jupiter.api.Test +import kotlin.test.assertTrue +import kotlin.test.expect + +class GenerateConfigMojoTest { + @Test + fun `a GenerateConfigMojo and 'skip' is true`() { + val generateConfigMojo = GenerateConfigMojo() + generateConfigMojo.skip = true + expect(Unit) { + generateConfigMojo.execute() + } + } + + @Test + fun `a GenerateConfigMojo and 'skip' is false`() { + val generateConfigMojo = GenerateConfigMojo() + generateConfigMojo.skip = false + generateConfigMojo.execute() + val file = File("detekt.yml") + assertTrue(file.exists()) + file.deleteOnExit() + } + + @Test + fun `a GCMojo and 'skip' is true`() { + val generateConfigMojo = GCMojo() + generateConfigMojo.skip = true + expect(Unit) { + generateConfigMojo.execute() + } + } + + @Test + fun `a GCMojo and 'skip' is false`() { + val generateConfigMojo = GCMojo() + generateConfigMojo.skip = false + generateConfigMojo.execute() + val file = File("detekt.yml") + assertTrue(file.exists()) + file.deleteOnExit() + } +} diff --git a/src/test/java/com/github/ozsie/ResolveConfigSpec.kt b/src/test/java/com/github/ozsie/ResolveConfigSpec.kt deleted file mode 100644 index aee1ea2..0000000 --- a/src/test/java/com/github/ozsie/ResolveConfigSpec.kt +++ /dev/null @@ -1,135 +0,0 @@ -package com.github.ozsie - -import org.jetbrains.spek.api.Spek -import org.jetbrains.spek.api.dsl.given -import org.jetbrains.spek.api.dsl.on -import java.io.File -import java.io.FileNotFoundException -import java.nio.file.Files -import java.nio.file.Paths -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith -import kotlin.test.assertTrue - -const val REMOTE_HOST = "https://raw.githubusercontent.com" -const val REMOTE_REPO = "$REMOTE_HOST/Ozsie/detekt-maven-plugin/fd0de6d59e6ae1e062a9d2b030a171da1d3225ab" -const val REMOTE_CONFIG_URL = "$REMOTE_REPO/src/test/resources/resolve-config/remote/remote-config.yml" -// Note: while annoying, the use of File() in the asserts should maximize cross-platform compatibility -object ResolveConfigSpec : Spek({ - - given("a test project") { - val project = projectWithBasedirAt("resolve-config") - val basedir = project.basedir - given("a relative config name") { - val config = "one.yml" - on("resolveConfig") { - val result = resolveConfig(project, config) - test("resolves the file name to an absolute path") { - assertEquals( - File(basedir, "one.yml").absolutePath, - result - ) - } - } - } - given("an absolute config name") { - val config = resolveTestResourcePath("resolve-config/nested/three.yml").absolutePath - on("resolveConfig") { - val result = resolveConfig(project, config) - test("returns the input") { - assertEquals(config, result) - } - } - } - given("semicolon-separated config names") { - val config = "one.yml;two.yml" - on("resolveConfig") { - val result = resolveConfig(project, config) - test("resolves all config files") { - assertEquals( - "${File(basedir, "one.yml")};${File(basedir, "two.yml")}", - result - ) - } - } - } - given("comma-separated config names") { - val config = "one.yml,two.yml" - on("resolveConfig") { - val result = resolveConfig(project, config) - test("resolves all config files") { - assertEquals( - "${File(basedir, "one.yml")};${File(basedir, "two.yml")}", - result - ) - } - } - } - given("a non-existent path") { - val config = "fake.yml" - on("resolveConfig") { - test("FileNotFound exception is thrown") { - assertFailsWith { resolveConfig(project, config) } - } - } - } - } - - given("a nested project") { - val project = projectWithBasedirAt("resolve-config/nested") - val basedir = project.basedir - val parentDir = basedir.parentFile - - given("a file in the parent project") { - val config = "one.yml" - on("resolveConfig") { - val result = resolveConfig(project, config) - test("resolves to the parent directory") { - assertEquals( - File(parentDir, "one.yml").absolutePath, - result - ) - } - } - } - given("multiple config names") { - val config = "one.yml;three.yml" - on("resolveConfig") { - val result = resolveConfig(project, config) - test("resolves resolves files in parent and child") { - assertEquals( - "${File(parentDir, "one.yml")};${File(basedir, "three.yml")}", - result - ) - } - } - } - } - - given("remote config file") { - val project = projectWithBasedirAt("resolve-config") - - given("remote file exists") { - on("resolveConfig") { - val result = resolveConfig(project, REMOTE_CONFIG_URL) - val expected = project.basedir.absolutePath + EXPORTED_FILE_LOCATION - test("resolves the remote file") { - assertEquals(expected, result) - assertTrue(Files.exists(Paths.get(project.basedir.absolutePath + EXPORTED_FILE_LOCATION))) - } - } - } - - given("remote file does not exist") { - val config = "$REMOTE_REPO/does-not-exist.yml" - on("resolveConfig") { - val exception = assertFailsWith { - resolveConfig(project, config) - } - test("resolves the remote file") { - assertEquals(config, exception.message) - } - } - } - } -}) diff --git a/src/test/java/com/github/ozsie/ResolveConfigTest.kt b/src/test/java/com/github/ozsie/ResolveConfigTest.kt new file mode 100644 index 0000000..02a3144 --- /dev/null +++ b/src/test/java/com/github/ozsie/ResolveConfigTest.kt @@ -0,0 +1,111 @@ +package com.github.ozsie + +import java.io.File +import java.io.FileNotFoundException +import java.nio.file.Files +import java.nio.file.Paths +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertTrue + +const val REMOTE_HOST = "https://raw.githubusercontent.com" +const val REMOTE_REPO = "$REMOTE_HOST/Ozsie/detekt-maven-plugin/fd0de6d59e6ae1e062a9d2b030a171da1d3225ab" +const val REMOTE_CONFIG_URL = "$REMOTE_REPO/src/test/resources/resolve-config/remote/remote-config.yml" +// Note: while annoying, the use of File() in the asserts should maximize cross-platform compatibility + +class ResolveConfigTestProjectTest { + private val project = projectWithBasedirAt("resolve-config") + private val basedir = project.basedir + + @Test + fun `a relative config name resolveConfig resolves the file name to an absolute path`() { + val config = "one.yml" + val result = resolveConfig(project, config) + assertEquals( + File(basedir, "one.yml").absolutePath, + result + ) + } + + @Test + fun `an absolute config name returns the input`() { + val config = resolveTestResourcePath("resolve-config/nested/three.yml").absolutePath + val result = resolveConfig(project, config) + assertEquals(config, result) + } + + @Test + fun `semicolon-separated config names resolves all config files`() { + val config = "one.yml;two.yml" + val result = resolveConfig(project, config) + assertEquals( + "${File(basedir, "one.yml")};${File(basedir, "two.yml")}", + result + ) + } + + @Test + fun `comma-separated config names resolves all config files`() { + val config = "one.yml,two.yml" + val result = resolveConfig(project, config) + assertEquals( + "${File(basedir, "one.yml")};${File(basedir, "two.yml")}", + result + ) + } + + @Test + fun `a non-existent path FileNotFound exception is thrown`() { + val config = "fake.yml" + assertFailsWith { resolveConfig(project, config) } + } +} + +class ResolveConfigTestNestedProjectTest { + private val project = projectWithBasedirAt("resolve-config/nested") + private val basedir = project.basedir + private val parentDir = basedir.parentFile + + @Test + fun `a file in the parent project resolves to the parent directory`() { + val config = "one.yml" + val result = resolveConfig(project, config) + assertEquals( + File(parentDir, "one.yml").absolutePath, + result + ) + } + + @Test + fun `multiple config names`() { + val config = "one.yml;three.yml" + val result = resolveConfig(project, config) + assertEquals( + "${File(parentDir, "one.yml")};${File(basedir, "three.yml")}", + result + ) + } +} + +class ResolveConfigRemoteConfigFileTest { + private val project = projectWithBasedirAt("resolve-config") + + @Test + fun `remote file exists resolves the remote file`() { + val result = resolveConfig(project, REMOTE_CONFIG_URL) + val expected = project.basedir.absolutePath + EXPORTED_FILE_LOCATION + assertEquals(expected, result) + assertTrue(Files.exists(Paths.get(project.basedir.absolutePath + EXPORTED_FILE_LOCATION))) + } + + @Test + fun `remote file does not exist resolves the remote file`() { + val config = "$REMOTE_REPO/does-not-exist.yml" + val exception = assertFailsWith { + resolveConfig(project, config) + } + assertEquals(config, exception.message) + } +} + diff --git a/src/test/java/com/github/ozsie/TestResources.kt b/src/test/java/com/github/ozsie/TestResources.kt index 9d52449..ebae5f2 100644 --- a/src/test/java/com/github/ozsie/TestResources.kt +++ b/src/test/java/com/github/ozsie/TestResources.kt @@ -14,7 +14,7 @@ import kotlin.test.assertTrue * @throws AssertionError if resource does not exist */ internal fun resolveTestResourcePath(path: String): File { - val url = ResolveConfigSpec::class.java.classLoader.getResource(path) + val url = ResolveConfigTestProjectTest::class.java.classLoader.getResource(path) assertNotNull(url, "Cannot find test resource path $path") return File(url.toURI()) } diff --git a/src/test/java/com/github/ozsie/test/CheckMojoTestFactory.kt b/src/test/java/com/github/ozsie/test/CheckMojoTestFactory.kt deleted file mode 100644 index e5d239b..0000000 --- a/src/test/java/com/github/ozsie/test/CheckMojoTestFactory.kt +++ /dev/null @@ -1,69 +0,0 @@ -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/java/com/github/ozsie/test/CheckWithTypeResolutionMojoTestFactory.kt b/src/test/java/com/github/ozsie/test/CheckWithTypeResolutionMojoTestFactory.kt index ceb7c97..ff87951 100644 --- a/src/test/java/com/github/ozsie/test/CheckWithTypeResolutionMojoTestFactory.kt +++ b/src/test/java/com/github/ozsie/test/CheckWithTypeResolutionMojoTestFactory.kt @@ -1,7 +1,7 @@ package com.github.ozsie.test import com.github.ozsie.CTRMojo -import com.github.ozsie.CheckWithTypeResolutionMojoSpec +import com.github.ozsie.CheckWithTypeResolutionMojoTest import com.nhaarman.mockito_kotlin.any import com.nhaarman.mockito_kotlin.doReturn import com.nhaarman.mockito_kotlin.mock @@ -13,18 +13,18 @@ import java.nio.file.Paths object CheckWithTypeResolutionMojoTestFactory { private val invalidPackageNamingDirectoryPath by lazy { - val uri = CheckWithTypeResolutionMojoSpec::class.java.classLoader - .getResource("code-samples/invalid-package-naming")!!.toURI() + val uri = CheckWithTypeResolutionMojoTest::class.java.classLoader + .getResource("code-samples/invalid-package-naming")?.toURI() ?: error("Failed to load resource") Paths.get(uri).toString() } private val validPackageNamingDirectoryPath by lazy { - val uri = CheckWithTypeResolutionMojoSpec::class.java.classLoader - .getResource("code-samples/valid")!!.toURI() + val uri = CheckWithTypeResolutionMojoTest::class.java.classLoader + .getResource("code-samples/valid")?.toURI() ?: error("Failed to load resource") Paths.get(uri).toString() } - fun create(block: CTRMojo.() -> Unit = {}): CTRMojo { + private fun create(block: CTRMojo.() -> Unit = {}): CTRMojo { return CTRMojo().apply { input = validPackageNamingDirectoryPath mavenProject = createMockMavenProject()