-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use testkit library to test plugin
- Loading branch information
1 parent
b095ddc
commit d376bec
Showing
8 changed files
with
149 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 0 additions & 76 deletions
76
src/test/kotlin/org/danilopianini/gradle/test/TestingDSL.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,30 @@ | ||
package org.danilopianini.gradle.test | ||
|
||
import com.uchuhimo.konf.Config | ||
import com.uchuhimo.konf.source.yaml | ||
import io.github.classgraph.ClassGraph | ||
import io.github.mirkofelice.api.Testkit | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.file.shouldBeAFile | ||
import io.kotest.matchers.file.shouldExist | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.string.shouldContain | ||
import io.kotest.matchers.string.shouldNotContain | ||
import org.gradle.internal.impldep.org.junit.rules.TemporaryFolder | ||
import org.gradle.testkit.runner.BuildResult | ||
import org.gradle.testkit.runner.GradleRunner | ||
import org.gradle.testkit.runner.TaskOutcome | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import java.io.File | ||
|
||
class Tests : StringSpec( | ||
{ | ||
val scan = ClassGraph() | ||
.enableAllInfo() | ||
.acceptPackages(Tests::class.java.`package`.name) | ||
.scan() | ||
scan.getResourcesWithLeafName("test.yaml") | ||
.flatMap { resource -> | ||
log.debug("Found test list in {}", resource) | ||
val yamlFile = File(resource.classpathElementFile.absolutePath + "/" + resource.path) | ||
val testConfiguration = Config { addSpec(Root) }.from.yaml.inputStream(resource.open()) | ||
testConfiguration[Root.tests].map { it to yamlFile.parentFile } | ||
} | ||
.forEach { (test, location) -> | ||
log.debug("Test to be executed: {} from {}", test, location) | ||
val testFolder = folder { | ||
location.copyRecursively(this.root) | ||
} | ||
log.debug("Test has been copied into {} and is ready to get executed", testFolder) | ||
test.description { | ||
val result = GradleRunner.create() | ||
.withProjectDir(testFolder.root) | ||
.withPluginClasspath() | ||
.withArguments(test.configuration.tasks + test.configuration.options) | ||
.run { if (test.expectation.failure.isEmpty()) build() else buildAndFail() } | ||
println(result.tasks) | ||
println(result.output) | ||
test.expectation.output_contains.forEach { | ||
result.output shouldContain it | ||
} | ||
test.expectation.output_doesnt_contain.forEach { | ||
result.output shouldNotContain it | ||
} | ||
test.expectation.success.forEach { | ||
result.outcomeOf(it) shouldBe TaskOutcome.SUCCESS | ||
} | ||
test.expectation.failure.forEach { | ||
result.outcomeOf(it) shouldBe TaskOutcome.FAILED | ||
} | ||
test.expectation.not_executed.forEach { | ||
result.task(it) shouldBe null | ||
} | ||
test.expectation.file_exists.forEach { | ||
val file = File("${testFolder.root.absolutePath}/${it.name}").apply { | ||
shouldExist() | ||
shouldBeAFile() | ||
} | ||
it.validate(file) | ||
} | ||
} | ||
} | ||
}, | ||
) { | ||
companion object { | ||
val log: Logger = LoggerFactory.getLogger(Tests::class.java) | ||
class Tests : StringSpec({ | ||
|
||
private fun BuildResult.outcomeOf(name: String) = checkNotNull(task(":$name")?.outcome) { | ||
"Task $name was not present among the executed tasks" | ||
} | ||
val projectName = "publish-on-central" | ||
val sep = File.separator | ||
val baseFolder = Testkit.DEFAULT_TEST_FOLDER + "org${sep}danilopianini${sep}gradle${sep}test$sep" | ||
|
||
private fun folder(closure: TemporaryFolder.() -> Unit) = TemporaryFolder().apply { | ||
create() | ||
closure() | ||
} | ||
fun Testkit.projectTest(folder: String) = this.test(projectName, baseFolder + folder) | ||
|
||
"Test ktjs" { | ||
Testkit.projectTest("ktjs") | ||
} | ||
|
||
"Test ktmultiplatform" { | ||
Testkit.projectTest("ktmultiplatform") | ||
} | ||
|
||
"Test multiproject" { | ||
Testkit.projectTest("multiproject") | ||
} | ||
|
||
"Test test0" { | ||
Testkit.projectTest("test0") | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.