From 35505007d2653c50598306a5056d81b740040f0b Mon Sep 17 00:00:00 2001 From: Pavel Punegov <32519625+PavelPunegov@users.noreply.github.com> Date: Mon, 16 Oct 2017 11:45:50 +0300 Subject: [PATCH] TestRunner support in test's build.gradle * Fixes to tests * RunKonanTest runs tests build with TestRunner * Standalone tests --- backend.native/tests/build.gradle | 2 +- .../tests/codegen/basics/check_type.kt | 2 +- .../tests/codegen/bridges/linkTest_lib.kt | 2 +- .../tests/codegen/bridges/linkTest_main.kt | 7 +- .../tests/codegen/bridges/test13.kt | 2 +- .../tests/codegen/cycles/cycle_for.kt | 2 +- .../delegatedOverride_lib.kt | 2 +- .../delegatedOverride_main.kt | 8 +- .../tests/codegen/enum/linkTest_lib.kt | 2 +- .../tests/codegen/enum/linkTest_main.kt | 8 +- .../inline/defaultArgs_linkTest_lib.kt | 2 +- .../inline/defaultArgs_linkTest_main.kt | 8 +- .../tests/codegen/innerClass/qualifiedThis.kt | 2 +- .../innerClass/secondaryConstructor.kt | 2 +- .../tests/codegen/innerClass/superOuter.kt | 2 +- .../tests/codegen/kclass/kclass0.kt | 5 +- .../propertyCallableReference/linkTest_lib.kt | 2 +- .../linkTest_main.kt | 8 +- backend.native/tests/mangling/mangling.kt | 6 +- backend.native/tests/mangling/manglinglib.kt | 2 - backend.native/tests/runtime/basic/driver0.kt | 4 + backend.native/tests/runtime/basic/entry1.kt | 2 - backend.native/tests/runtime/basic/entry2.kt | 2 - .../tests/runtime/basic/initializers2.kt | 6 +- .../tests/runtime/basic/libentry2.kt | 2 - .../serialization/deserialize_members.kt | 8 +- .../tests/serialization/serialize_members.kt | 2 +- .../org/jetbrains/kotlin/KonanTest.groovy | 115 +++++++++++++++++- .../src/main/kotlin/konan/test/TestRunner.kt | 4 +- 29 files changed, 149 insertions(+), 72 deletions(-) create mode 100644 backend.native/tests/runtime/basic/driver0.kt diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 9b79082d0fb..a51d950e9c0 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -2213,7 +2213,7 @@ task interop_echo_server(type: RunInteropKonanTest) { } if (isMac()) { - task interop_opengl_teapot(type: RunKonanTest) { + task interop_opengl_teapot(type: RunStandaloneKonanTest) { disabled = (project.testTarget == 'wasm32') // No interop for wasm yet. run = false source = "interop/basics/opengl_teapot.kt" diff --git a/backend.native/tests/codegen/basics/check_type.kt b/backend.native/tests/codegen/basics/check_type.kt index a5c1906194c..00d6fab7aa0 100644 --- a/backend.native/tests/codegen/basics/check_type.kt +++ b/backend.native/tests/codegen/basics/check_type.kt @@ -1,4 +1,4 @@ -package codegen.basics +package codegen.basics.check_type import kotlin.test.* diff --git a/backend.native/tests/codegen/bridges/linkTest_lib.kt b/backend.native/tests/codegen/bridges/linkTest_lib.kt index 9dbb36d9f43..3df1d13e0e4 100644 --- a/backend.native/tests/codegen/bridges/linkTest_lib.kt +++ b/backend.native/tests/codegen/bridges/linkTest_lib.kt @@ -1,4 +1,4 @@ -package codegen.bridges.linkTest.a +package a interface A { fun foo(): T diff --git a/backend.native/tests/codegen/bridges/linkTest_main.kt b/backend.native/tests/codegen/bridges/linkTest_main.kt index d2e992c6967..38c4dabf20a 100644 --- a/backend.native/tests/codegen/bridges/linkTest_main.kt +++ b/backend.native/tests/codegen/bridges/linkTest_main.kt @@ -1,11 +1,8 @@ -package codegen.bridges.linkTest_main - -import kotlin.test.* -import codegen.bridges.linkTest.a.* +import a.* class B: C() -@Test fun runTest() { +fun main(args: Array) { val b = B() println(b.foo()) val c: C = b diff --git a/backend.native/tests/codegen/bridges/test13.kt b/backend.native/tests/codegen/bridges/test13.kt index 4dcab8b1cd6..ae2e8b02c54 100644 --- a/backend.native/tests/codegen/bridges/test13.kt +++ b/backend.native/tests/codegen/bridges/test13.kt @@ -18,7 +18,7 @@ open class B: A() { } } -fun main(args : Array) { +@Test fun runTest() { val b = B() val a = A() b.bar(42) diff --git a/backend.native/tests/codegen/cycles/cycle_for.kt b/backend.native/tests/codegen/cycles/cycle_for.kt index 1f5a976fcb6..3b57370660e 100644 --- a/backend.native/tests/codegen/cycles/cycle_for.kt +++ b/backend.native/tests/codegen/cycles/cycle_for.kt @@ -10,6 +10,6 @@ fun cycle_for(arr: Array) : Int { return sum } -@Test fun main() { +@Test fun runTest() { if (cycle_for(Array(4, init = { it })) != 6) throw Error() } \ No newline at end of file diff --git a/backend.native/tests/codegen/delegatedProperty/delegatedOverride_lib.kt b/backend.native/tests/codegen/delegatedProperty/delegatedOverride_lib.kt index 432224e40ec..0aceea0ffdd 100644 --- a/backend.native/tests/codegen/delegatedProperty/delegatedOverride_lib.kt +++ b/backend.native/tests/codegen/delegatedProperty/delegatedOverride_lib.kt @@ -1,4 +1,4 @@ -package codegen.delegatedProperty.delegatedOverride.a +package a import kotlin.reflect.KProperty diff --git a/backend.native/tests/codegen/delegatedProperty/delegatedOverride_main.kt b/backend.native/tests/codegen/delegatedProperty/delegatedOverride_main.kt index 1692cb5174e..ba4fadc4e25 100644 --- a/backend.native/tests/codegen/delegatedProperty/delegatedOverride_main.kt +++ b/backend.native/tests/codegen/delegatedProperty/delegatedOverride_main.kt @@ -1,8 +1,4 @@ -package codegen.delegatedProperty.delegatedOverride_main - -import kotlin.test.* - -import codegen.delegatedProperty.delegatedOverride.a.* +import a.* open class C: B() { override val x: Int = 156 @@ -15,7 +11,7 @@ open class C: B() { } } -@Test fun runTest() { +fun main(args: Array) { val c = C() c.foo() } diff --git a/backend.native/tests/codegen/enum/linkTest_lib.kt b/backend.native/tests/codegen/enum/linkTest_lib.kt index baa41c46be2..3653236c9cd 100644 --- a/backend.native/tests/codegen/enum/linkTest_lib.kt +++ b/backend.native/tests/codegen/enum/linkTest_lib.kt @@ -1,4 +1,4 @@ -package codegen.enum.linkTest.a +package a enum class A(val x: Int) { Z1(42), diff --git a/backend.native/tests/codegen/enum/linkTest_main.kt b/backend.native/tests/codegen/enum/linkTest_main.kt index 0790e8d7cfb..cb4be8e436a 100644 --- a/backend.native/tests/codegen/enum/linkTest_main.kt +++ b/backend.native/tests/codegen/enum/linkTest_main.kt @@ -1,10 +1,6 @@ -package codegen.enum.linkTest_main +import a.* -import kotlin.test.* - -import codegen.enum.linkTest.a.* - -@Test fun runTest() { +fun main(args: Array) { println(A.Z1.x) println(A.valueOf("Z2").x) println(A.values()[2].x) diff --git a/backend.native/tests/codegen/inline/defaultArgs_linkTest_lib.kt b/backend.native/tests/codegen/inline/defaultArgs_linkTest_lib.kt index e8621e0d1ce..04a3e5e7349 100644 --- a/backend.native/tests/codegen/inline/defaultArgs_linkTest_lib.kt +++ b/backend.native/tests/codegen/inline/defaultArgs_linkTest_lib.kt @@ -1,3 +1,3 @@ -package codegen.inline.defaultArgs_linkTest.a +package a inline fun foo(x: Int, y: Int = 117) = x + y \ No newline at end of file diff --git a/backend.native/tests/codegen/inline/defaultArgs_linkTest_main.kt b/backend.native/tests/codegen/inline/defaultArgs_linkTest_main.kt index 9fe0f656610..19a3db05739 100644 --- a/backend.native/tests/codegen/inline/defaultArgs_linkTest_main.kt +++ b/backend.native/tests/codegen/inline/defaultArgs_linkTest_main.kt @@ -1,10 +1,6 @@ -package codegen.inline.defaultArgs_linkTest_main +import a.* -import kotlin.test.* - -import codegen.inline.defaultArgs_linkTest.a.* - -@Test fun runTest() { +fun main(args: Array) { println(foo(5)) println(foo(5, 42)) } \ No newline at end of file diff --git a/backend.native/tests/codegen/innerClass/qualifiedThis.kt b/backend.native/tests/codegen/innerClass/qualifiedThis.kt index 837c11e920a..20179a9164d 100644 --- a/backend.native/tests/codegen/innerClass/qualifiedThis.kt +++ b/backend.native/tests/codegen/innerClass/qualifiedThis.kt @@ -45,6 +45,6 @@ class A: ABase() { // implicit label @A fun box() = A().B().bar(D()) -fun main(args : Array) { +@Test fun runTest() { println(box()) } diff --git a/backend.native/tests/codegen/innerClass/secondaryConstructor.kt b/backend.native/tests/codegen/innerClass/secondaryConstructor.kt index 1ab240b1bbd..4df0d490764 100644 --- a/backend.native/tests/codegen/innerClass/secondaryConstructor.kt +++ b/backend.native/tests/codegen/innerClass/secondaryConstructor.kt @@ -19,6 +19,6 @@ class Outer(val x: Int) { } } -fun main(args : Array) { +@Test fun runTest() { Outer(42).Inner().InnerInner("zzz") } diff --git a/backend.native/tests/codegen/innerClass/superOuter.kt b/backend.native/tests/codegen/innerClass/superOuter.kt index 7b86c358d00..036424790b9 100644 --- a/backend.native/tests/codegen/innerClass/superOuter.kt +++ b/backend.native/tests/codegen/innerClass/superOuter.kt @@ -12,6 +12,6 @@ open class Outer(val outer: String) { fun box() = Outer("Fail").value() -fun main(args : Array) { +@Test fun runTest() { println(box()) } \ No newline at end of file diff --git a/backend.native/tests/codegen/kclass/kclass0.kt b/backend.native/tests/codegen/kclass/kclass0.kt index 0827989450a..0e42b8ac3ea 100644 --- a/backend.native/tests/codegen/kclass/kclass0.kt +++ b/backend.native/tests/codegen/kclass/kclass0.kt @@ -1,7 +1,10 @@ +package codegen.kclass.kclass0 + +import kotlin.test.* import kotlin.reflect.KClass @Test fun runTest() { - main(emptyArray()) + main(emptyArray()) } fun main(args: Array) { diff --git a/backend.native/tests/codegen/propertyCallableReference/linkTest_lib.kt b/backend.native/tests/codegen/propertyCallableReference/linkTest_lib.kt index 10e980b7165..7595b1acde4 100644 --- a/backend.native/tests/codegen/propertyCallableReference/linkTest_lib.kt +++ b/backend.native/tests/codegen/propertyCallableReference/linkTest_lib.kt @@ -1,3 +1,3 @@ -package codegen.propertyCallableReference.linkTest.a +package a class A(val x: Int) \ No newline at end of file diff --git a/backend.native/tests/codegen/propertyCallableReference/linkTest_main.kt b/backend.native/tests/codegen/propertyCallableReference/linkTest_main.kt index 7fb5069cc1e..5af7b724a75 100644 --- a/backend.native/tests/codegen/propertyCallableReference/linkTest_main.kt +++ b/backend.native/tests/codegen/propertyCallableReference/linkTest_main.kt @@ -1,10 +1,6 @@ -package codegen.propertyCallableReference.linkTest_main +import a.A -import kotlin.test.* - -import codegen.propertyCallableReference.linkTest.a.A - -@Test fun runTest() { +fun main(args: Array) { val p1 = A::x println(p1.get(A(42))) val a = A(117) diff --git a/backend.native/tests/mangling/mangling.kt b/backend.native/tests/mangling/mangling.kt index 8ea7bd17325..0da5a5bc04e 100644 --- a/backend.native/tests/mangling/mangling.kt +++ b/backend.native/tests/mangling/mangling.kt @@ -1,7 +1,3 @@ -package mangling.mangling - -import kotlin.test.* - fun test_direct() { val mutListInt = mutableListOf(1, 2, 3, 4) val mutListNum = mutableListOf(9, 10, 11, 12) @@ -32,7 +28,7 @@ fun test_multiple_constructors() { mangle3(number) } -@Test fun runTest() { +fun main(args: Array) { test_direct() test_param() test_multiple_constructors() diff --git a/backend.native/tests/mangling/manglinglib.kt b/backend.native/tests/mangling/manglinglib.kt index ac062ebe75b..4c75ddd7b04 100644 --- a/backend.native/tests/mangling/manglinglib.kt +++ b/backend.native/tests/mangling/manglinglib.kt @@ -1,5 +1,3 @@ -package mangling.mangling - public fun mangle1(l: List) { println("Int direct $l") } diff --git a/backend.native/tests/runtime/basic/driver0.kt b/backend.native/tests/runtime/basic/driver0.kt new file mode 100644 index 00000000000..4b603a1274d --- /dev/null +++ b/backend.native/tests/runtime/basic/driver0.kt @@ -0,0 +1,4 @@ +// TODO: remove kotlin_native.io once overrides are in place. +fun main(args : Array) { + println("Hello, world!") +} \ No newline at end of file diff --git a/backend.native/tests/runtime/basic/entry1.kt b/backend.native/tests/runtime/basic/entry1.kt index 7a5e552274b..30878af591a 100644 --- a/backend.native/tests/runtime/basic/entry1.kt +++ b/backend.native/tests/runtime/basic/entry1.kt @@ -1,5 +1,3 @@ -package runtime.basic.entry1 - fun fail() { println("Test failed, this is a wrong main() function.") } diff --git a/backend.native/tests/runtime/basic/entry2.kt b/backend.native/tests/runtime/basic/entry2.kt index 353f9b83715..ce15f37213d 100644 --- a/backend.native/tests/runtime/basic/entry2.kt +++ b/backend.native/tests/runtime/basic/entry2.kt @@ -1,5 +1,3 @@ -package runtime.basic.entry2 - fun main(args: Array) { fail() } diff --git a/backend.native/tests/runtime/basic/initializers2.kt b/backend.native/tests/runtime/basic/initializers2.kt index 3149e8c618e..51ccc478c14 100644 --- a/backend.native/tests/runtime/basic/initializers2.kt +++ b/backend.native/tests/runtime/basic/initializers2.kt @@ -1,7 +1,3 @@ -package runtime.basic.initializers2 - -import kotlin.test.* - class A(val msg: String) { init { println("init $msg") @@ -13,7 +9,7 @@ val globalValue1 = 1 val globalValue2 = A("globalValue2") val globalValue3 = A("globalValue3") -@Test fun runTest() { +fun main(args: Array) { println(globalValue1.toString()) println(globalValue2.toString()) println(globalValue3.toString()) diff --git a/backend.native/tests/runtime/basic/libentry2.kt b/backend.native/tests/runtime/basic/libentry2.kt index 0e8177220d1..b9d0e5ae40e 100644 --- a/backend.native/tests/runtime/basic/libentry2.kt +++ b/backend.native/tests/runtime/basic/libentry2.kt @@ -1,5 +1,3 @@ -package runtime.basic.entry2 - fun fail() { println("Test failed, this is a wrong main() function.") } diff --git a/backend.native/tests/serialization/deserialize_members.kt b/backend.native/tests/serialization/deserialize_members.kt index 3d0bb5fb878..a81f6661c92 100644 --- a/backend.native/tests/serialization/deserialize_members.kt +++ b/backend.native/tests/serialization/deserialize_members.kt @@ -1,10 +1,6 @@ -package serialization.deserialize_members +import foo.bar.* -import kotlin.test.* - -import serialization.serialize_members.* - -@Test fun runTest() { +fun main(args: Array) { val c = C() val d = C.D() val e = C.D.E() diff --git a/backend.native/tests/serialization/serialize_members.kt b/backend.native/tests/serialization/serialize_members.kt index e10a3450082..f9da87c3781 100644 --- a/backend.native/tests/serialization/serialize_members.kt +++ b/backend.native/tests/serialization/serialize_members.kt @@ -1,4 +1,4 @@ -package serialization.serialize_members +package foo.bar class R { inline fun bar(t: T) { diff --git a/buildSrc/plugins/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy b/buildSrc/plugins/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy index a05b22cbe74..38e2355bd45 100644 --- a/buildSrc/plugins/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy +++ b/buildSrc/plugins/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy @@ -17,7 +17,6 @@ package org.jetbrains.kotlin import groovy.json.JsonOutput -import org.gradle.api.DefaultTask import org.gradle.api.tasks.JavaExec import org.gradle.api.tasks.TaskAction import org.gradle.process.ExecResult @@ -296,7 +295,117 @@ class TestFailedException extends RuntimeException { } } -class RunKonanTest extends KonanTest { +abstract class ExtKonanTest extends KonanTest { + + ExtKonanTest() { + super() + } + + @Override + String buildExePath() { + // a single executable for all tests + return "$outputDirectory/program.tr" + } + + // The same as its super() version but doesn't create a new dir for each test + @Override + void createOutputDirectory() { + if (outputDirectory != null) { + return + } + + def outputSourceSet = project.sourceSets.findByName(getOutputSourceSetName()) + if (outputSourceSet != null) { + outputDirectory = outputSourceSet.output.getDirs().getSingleFile().absolutePath + project.file(outputDirectory).mkdirs() + } else { + outputDirectory = getTemporaryDir().absolutePath + } + } +} + +/** + * Builds tests with TestRunner enabled + */ +class BuildKonanTest extends ExtKonanTest { + + public List compileList + public List excludeList + + @Override + List buildCompileList() { + assert compileList != null + + // convert exclude list to paths + def excludeFiles = new ArrayList() + excludeList.each { excludeFiles.add(project.file(it).absolutePath) } + + // create list of tests to compile + def compileFiles = new ArrayList() + compileList.each { + project.file(it).eachFileRecurse { + if (it.isFile() && it.name.endsWith(".kt") && !excludeFiles.contains(it.absolutePath)) { + compileFiles.add(it.absolutePath) + } + } + } + compileFiles + } + + @Override + void compileTest(List filesToCompile, String exe) { + flags = flags ?: [] + // compile with test runner enabled + flags.add("-tr") + runCompiler(filesToCompile, exe, flags) + } + + @TaskAction + @Override + void executeTest() { + // only build tests + createOutputDirectory() + def program = buildExePath() + compileTest(buildCompileList(), program) + } +} + +/** + * Runs test built with Konan's TestRunner + */ +class RunKonanTest extends ExtKonanTest { + + RunKonanTest() { + super() + dependsOn(project.tasks['buildKonanTests']) + } + + @Override + void compileTest(List filesToCompile, String exe) { + // tests should be already compiled + } + + @TaskAction + @Override + void executeTest() { + arguments = arguments ?: [] + // Print only test's output + arguments.add("--ktest_logger=SILENT") + arguments.add("--ktest_filter=" + convertToPattern(source)) + super.executeTest() + } + + private String convertToPattern(String source) { + return source.replace('/', '.') + .replace(".kt", "") + .concat(".*") + } +} + +/** + * Compiles and executes test as a standalone binary + */ +class RunStandaloneKonanTest extends KonanTest { void compileTest(List filesToCompile, String exe) { runCompiler(filesToCompile, exe, flags?:[]) } @@ -379,7 +488,7 @@ class LinkKonanTest extends KonanTest { } } -class RunExternalTestGroup extends RunKonanTest { +class RunExternalTestGroup extends RunStandaloneKonanTest { def groupDirectory = "." def outputSourceSetName = "testOutputExternal" diff --git a/runtime/src/main/kotlin/konan/test/TestRunner.kt b/runtime/src/main/kotlin/konan/test/TestRunner.kt index bd24024f8e0..622064746fb 100644 --- a/runtime/src/main/kotlin/konan/test/TestRunner.kt +++ b/runtime/src/main/kotlin/konan/test/TestRunner.kt @@ -48,8 +48,8 @@ object TestRunner { * --gtest_list_tests * --ktest_list_tests - Show all available tests. * - * --gtest_filter=POSTIVE_PATTERNS[-NEGATIVE_PATTERNS] - * --ktest_filter=POSTIVE_PATTERNS[-NEGATIVE_PATTERNS] - Run only the tests whose name matches one of the + * --gtest_filter=POSITIVE_PATTERNS[-NEGATIVE_PATTERNS] + * --ktest_filter=POSITIVE_PATTERNS[-NEGATIVE_PATTERNS] - Run only the tests whose name matches one of the * positive patterns but none of the negative patterns. * '?' matches any single character; '*' matches any * substring; ':' separates two patterns.