Created
December 16, 2020 22:04
-
-
Save SergejIsbrecht/fbbe1d835a223560244179cdd718b117 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| * Copyright 2016-2020 JetBrains s.r.o. | |
| * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | |
| */ | |
| package kotlinx.validation | |
| import org.gradle.testkit.runner.BuildResult | |
| import org.gradle.testkit.runner.GradleRunner | |
| import org.gradle.testkit.runner.TaskOutcome | |
| import org.junit.Test | |
| import java.io.File | |
| import kotlin.test.assertEquals | |
| internal class FuncTest : BaseKotlinGradleTest() { | |
| @Test | |
| fun myfancytest() { | |
| val runner = test { | |
| buildGradleKts { | |
| resolve("resources/examples/default/build.gradle.kts") | |
| } | |
| runner { | |
| debug = true | |
| arguments.add(":apiCheck") | |
| } | |
| } | |
| runner.build().apply { | |
| assertTaskSuccess(":apiCheck") | |
| } | |
| } | |
| } | |
| internal fun BuildResult.assertTaskSuccess(task: String) { | |
| assertTaskOutcome(TaskOutcome.SUCCESS, task) | |
| } | |
| internal fun BuildResult.assertTaskFailure(task: String) { | |
| assertTaskOutcome(TaskOutcome.FAILED, task) | |
| } | |
| private fun BuildResult.assertTaskOutcome(taskOutcome: TaskOutcome, taskName: String) { | |
| assertEquals(taskOutcome, task(taskName)?.outcome) | |
| } | |
| internal fun BaseKotlinGradleTest.test(fn: BaseKotlinScope.() -> Unit): GradleRunner { | |
| val baseKotlinScope = BaseKotlinScope() | |
| fn(baseKotlinScope) | |
| // do side-effects with data provided by `baseKotlinScope | |
| // write files "build.gradle.kts with merged files | |
| // copy over arguments to GradleRunner | |
| return GradleRunner.create() // | |
| .withArguments(baseKotlinScope.runner.arguments) | |
| .withPluginClasspath() | |
| .withProjectDir(this.testProjectDir.root) | |
| } | |
| internal fun BaseKotlinScope.buildGradleKts(fn: AppendableScope.() -> Unit) { | |
| val appendableScope = AppendableScope() | |
| fn(appendableScope) | |
| gradleKtsScope = appendableScope | |
| } | |
| internal fun BaseKotlinScope.runner(fn: Runner.() -> Unit) { | |
| val runner = Runner() | |
| fn(runner) | |
| this.runner = runner | |
| } | |
| internal fun AppendableScope.resolve(fileName: String) { | |
| // resolve file and add to `AppendableScope.files | |
| // files.add() | |
| } | |
| internal class BaseKotlinScope { | |
| var gradleKtsScope: AppendableScope = AppendableScope() | |
| var runner: Runner = Runner() | |
| } | |
| internal class AppendableScope { | |
| val files: MutableList<File> = mutableListOf() | |
| } | |
| internal class Runner { | |
| var debug = false | |
| val arguments: MutableList<String> = mutableListOf() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment