From 93e3bdc1ab529c1da38a6b6521307e20c0d80958 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Mon, 20 Mar 2017 13:26:18 +0300 Subject: [PATCH] Improve multi-platform integration test Compile the platform-specific code two times: once with the common file first and the platform file second, and once with the platform file first and the common file second in the compilation arguments. This is needed because some issues in the compiler are only reproducible in one of these two scenarios --- .../AbstractMultiPlatformIntegrationTest.kt | 52 +++++++++++-------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/compiler/tests/org/jetbrains/kotlin/multiplatform/AbstractMultiPlatformIntegrationTest.kt b/compiler/tests/org/jetbrains/kotlin/multiplatform/AbstractMultiPlatformIntegrationTest.kt index 99e2b7a6d17..91d1b0633d1 100644 --- a/compiler/tests/org/jetbrains/kotlin/multiplatform/AbstractMultiPlatformIntegrationTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/multiplatform/AbstractMultiPlatformIntegrationTest.kt @@ -17,11 +17,13 @@ package org.jetbrains.kotlin.multiplatform import org.jetbrains.kotlin.cli.AbstractCliTest +import org.jetbrains.kotlin.cli.common.CLICompiler import org.jetbrains.kotlin.cli.js.K2JSCompiler import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler import org.jetbrains.kotlin.cli.metadata.K2MetadataCompiler import org.jetbrains.kotlin.test.KotlinTestUtils import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase +import org.jetbrains.kotlin.test.util.trimTrailingWhitespacesAndAddNewlineAtEOF import java.io.File abstract class AbstractMultiPlatformIntegrationTest : KtUsefulTestCase() { @@ -38,39 +40,45 @@ abstract class AbstractMultiPlatformIntegrationTest : KtUsefulTestCase() { val jsDest = File(File(tmpdir, "js"), "output.js") val result = buildString { - val (commonOutput, commonExitCode) = AbstractCliTest.executeCompilerGrabOutput(K2MetadataCompiler(), listOf( - commonSrc.absolutePath, "-d", commonDest.absolutePath, "-Xskip-java-check" - )) appendln("-- Common --") - appendln("Exit code: $commonExitCode") - appendln("Output:") - appendln(commonOutput) + appendln(K2MetadataCompiler().compile(listOf(commonSrc), "-d", commonDest.absolutePath)) if (jvmSrc.exists()) { - val (jvmOutput, jvmExitCode) = AbstractCliTest.executeCompilerGrabOutput(K2JVMCompiler(), listOf( - jvmSrc.absolutePath, commonSrc.absolutePath, - "-d", jvmDest.absolutePath, - "-Xmulti-platform", "-Xskip-java-check" - )) + appendln() appendln("-- JVM --") - appendln("Exit code: $jvmExitCode") - appendln("Output:") - appendln(jvmOutput) + append(K2JVMCompiler().compileBothWays(commonSrc, jvmSrc, "-d", jvmDest.absolutePath)) } if (jsSrc.exists()) { - val (jsOutput, jsExitCode) = AbstractCliTest.executeCompilerGrabOutput(K2JSCompiler(), listOf( - jsSrc.absolutePath, commonSrc.absolutePath, - "-output", jsDest.absolutePath, - "-Xmulti-platform", "-Xskip-java-check" - )) + appendln() appendln("-- JS --") - appendln("Exit code: $jsExitCode") - appendln("Output:") - append(jsOutput) + append(K2JSCompiler().compileBothWays(commonSrc, jsSrc, "-output", jsDest.absolutePath)) } } KotlinTestUtils.assertEqualsToFile(File(root, "output.txt"), result.replace('\\', '/')) } + + private fun CLICompiler<*>.compileBothWays(commonSource: File, platformSource: File, vararg additionalArguments: String): String { + val platformFirst = compile(listOf(platformSource, commonSource), *additionalArguments).trimTrailingWhitespacesAndAddNewlineAtEOF() + val commonFirst = compile(listOf(commonSource, platformSource), *additionalArguments).trimTrailingWhitespacesAndAddNewlineAtEOF() + if (platformFirst != commonFirst) { + assertEquals( + "Compilation results are different when compiling [platform-specific, common] compared to when compiling [common, platform-specific]", + "// Compiling [platform-specific, common]\n\n$platformFirst", + "// Compiling [common, platform-specific]\n\n$commonFirst" + ) + } + return platformFirst + } + + private fun CLICompiler<*>.compile(sources: List, vararg additionalArguments: String): String = buildString { + val (output, exitCode) = AbstractCliTest.executeCompilerGrabOutput( + this@compile, + sources.map(File::getAbsolutePath) + listOf("-Xmulti-platform", "-Xskip-java-check") + additionalArguments + ) + appendln("Exit code: $exitCode") + appendln("Output:") + appendln(output) + }.trim() }