Gradle, native: Introduce separate binary type for tests

Earlier native tests were represented by the same binary type as product
executables. We also used to create test tasks in the same manner as run
tasks for product executables. Such tasks could be obtained used a property
of an executable binary with type Exec.

But now we have a separate class for test tasks. Also we probably will want
to create several tasks for the same test. So we cannot use the same run task
property for both test and product executables. Also representing test and
product executables by the same binary type leads to the fact that assemble
task execution causes building not only product binaries but also the test
ones.

This patch solves the issues described above by introducing a separate binary
type for tests. Such a test binary can be created in the same manner as other
binaries:

    kotlin.macosX64 {
        binaries {
            test("integration") { ... }
        }
    }

One test binary and a task executing it is created by the plugin out of the box.
This test binary replaces a test executable used to be created earlier.

Issue #KT-31609 Fixed
This commit is contained in:
Ilya Matveev
2019-05-17 20:48:39 +07:00
parent 2db8409d85
commit 48b1f71cef
16 changed files with 366 additions and 104 deletions
@@ -942,7 +942,6 @@ class NewMultiplatformIT : BaseGradleIT() {
"fooReleaseExecutable" to "foo",
"barReleaseExecutable" to "bar",
"bazReleaseExecutable" to "my-baz",
"testDebugExecutable" to "test",
"test2ReleaseExecutable" to "test2",
"releaseStatic" to "native_binary",
"releaseShared" to "native_binary"
@@ -1136,19 +1135,73 @@ class NewMultiplatformIT : BaseGradleIT() {
fun testNativeTests() = with(Project("new-mpp-native-tests", gradleVersion)) {
val testTasks = listOf("macos64Test", "linux64Test", "mingw64Test")
val hostTestTask = "${nativeHostTargetName}Test"
val suffix = if (isWindows) "exe" else "kexe"
val defaultOutputFile = "build/bin/$nativeHostTargetName/debugTest/test.$suffix"
val anotherOutputFile = "build/bin/$nativeHostTargetName/anotherDebugTest/another.$suffix"
build("tasks") {
assertSuccessful()
println(output)
testTasks.forEach {
// We need to create tasks for all hosts
assertTrue(output.contains("$it - "), "There is no test task '$it' in the task list.")
}
}
// Check that tests are not built during the ":assemble" execution
build("assemble") {
assertSuccessful()
assertNoSuchFile(defaultOutputFile)
assertNoSuchFile(anotherOutputFile)
}
build("check") {
assertSuccessful()
assertTasksExecuted(":$hostTestTask")
assertFileExists(defaultOutputFile)
assertTestResults("testProject/new-mpp-native-tests/TEST-TestKt.xml", hostTestTask)
}
build("linkAnotherDebugTest${nativeHostTargetName}") {
assertSuccessful()
assertFileExists(anotherOutputFile)
}
// Check that test binaries can be accessed in a buildscript.
build("checkNewGetters") {
assertSuccessful()
listOf("test.$suffix", "another.$suffix").forEach {
assertContains("Get test: $it")
assertContains("Find test: $it")
}
}
// Check that accessing a test as an executable fails or returns null and shows the corresponding warning.
build("checkOldGet") {
assertFailed()
assertContains(
"""
|Probably you are accessing the default test binary using the 'binaries.getExecutable("test", DEBUG)' method.
|Since 1.3.40 tests are represented by a separate binary type. To get the default test binary, use:
|
| binaries.getTest(DEBUG)
""".trimMargin()
)
}
build("checkOldFind") {
assertSuccessful()
assertContains(
"""
|Probably you are accessing the default test binary using the 'binaries.findExecutable("test", DEBUG)' method.
|Since 1.3.40 tests are represented by a separate binary type. To get the default test binary, use:
|
| binaries.findTest(DEBUG)
""".trimMargin()
)
assertContains("Find test: null")
}
}
@Test
@@ -53,6 +53,7 @@ kotlin {
executable("test2", [RELEASE]) {
compilation = compilations["test"]
freeCompilerArgs.add("-tr")
}
sharedLib([RELEASE])
@@ -44,6 +44,7 @@ kotlin {
executable("test2") {
compilation = compilations["test"]
freeCompilerArgs.add("-tr")
}
sharedLib(listOf(RELEASE))
@@ -24,5 +24,56 @@ kotlin {
fromPreset(presets.macosX64, 'macos64')
fromPreset(presets.linuxX64, 'linux64')
fromPreset(presets.mingwX64, 'mingw64')
configure([macos64, linux64, mingw64]) {
compilations.create("anotherTest")
binaries {
test("another", [DEBUG]) {
compilation = compilations.anotherTest
}
}
}
}
sourceSets {
anotherTest
macos64AnotherTest.dependsOn(anotherTest)
linux64AnotherTest.dependsOn(anotherTest)
mingw64AnotherTest.dependsOn(anotherTest)
}
}
// Check that getting a test binary in an old way fails showing the corresponding warning
task checkOldGet {
doLast {
kotlin.targets {
configure([macos64, linux64, mingw64]) {
println("Get test: ${binaries.getExecutable("test", DEBUG)}")
}
}
}
}
// Check that finding a test binary in an old way returns null showing the corresponding warning.
task checkOldFind {
doLast {
kotlin.targets {
configure([macos64, linux64, mingw64]) {
println("Find test: ${binaries.findExecutable("test", DEBUG)}")
}
}
}
}
task checkNewGetters {
doLast {
kotlin.targets {
configure([macos64, linux64, mingw64]) {
println("Get test: ${binaries.getTest(DEBUG).outputFile.name}")
println("Find test: ${binaries.findTest(DEBUG).outputFile.name}")
println("Get test: ${binaries.getTest("another", DEBUG).outputFile.name}")
println("Find test: ${binaries.findTest("another", DEBUG).outputFile.name}")
}
}
}
}
@@ -0,0 +1,13 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.foo.test
import kotlin.test.*
@Test
fun anotherTest() {
println("Another test")
}