Skip to content

Commit 4b9d73b

Browse files
committed
KTL-2176: Change the logic of comparing compiled code to comparing executed code for js.
We had a problem that with each new version of Kotlin the js tests were failed. That happened because we compared translated js code. With current changes we decided to compare not the translated code but the result of its execution.
1 parent 237a466 commit 4b9d73b

File tree

12 files changed

+88
-26
lines changed

12 files changed

+88
-26
lines changed

src/main/kotlin/com/compiler/server/compiler/components/KotlinToJSTranslator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class KotlinToJSTranslator(
1515
private val kotlinEnvironment: KotlinEnvironment,
1616
) {
1717
companion object {
18-
private const val JS_IR_CODE_BUFFER = "playground.output?.buffer_1;\n"
18+
internal const val JS_IR_CODE_BUFFER = "playground.output?.buffer_1;\n"
1919

20-
private val JS_IR_OUTPUT_REWRITE = """
20+
internal val JS_IR_OUTPUT_REWRITE = """
2121
if (typeof get_output !== "undefined") {
2222
get_output();
2323
output = new BufferedOutput();

src/test/kotlin/com/compiler/server/ResourceE2ECompileTest.kt

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package com.compiler.server
22

3+
import com.compiler.server.base.startNodeJsApp
4+
import com.compiler.server.compiler.components.KotlinToJSTranslator.Companion.JS_IR_CODE_BUFFER
5+
import com.compiler.server.compiler.components.KotlinToJSTranslator.Companion.JS_IR_OUTPUT_REWRITE
36
import com.compiler.server.generator.generateSingleProject
4-
import com.compiler.server.model.*
7+
import com.compiler.server.model.ExecutionResult
8+
import com.compiler.server.model.JunitExecutionResult
9+
import com.compiler.server.model.JvmExecutionResult
10+
import com.compiler.server.model.ProjectType
11+
import com.compiler.server.model.TranslationJSResult
12+
import com.compiler.server.model.TranslationWasmResult
513
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
614
import org.junit.jupiter.api.Test
715
import org.springframework.beans.factory.annotation.Value
@@ -12,6 +20,9 @@ import org.springframework.http.MediaType
1220
import org.springframework.web.client.RestTemplate
1321
import java.io.File
1422
import java.net.InetAddress
23+
import kotlin.io.path.absolutePathString
24+
import kotlin.io.path.createTempDirectory
25+
import kotlin.io.path.writeText
1526

1627
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
1728
class ResourceE2ECompileTest : BaseResourceCompileTest {
@@ -50,21 +61,28 @@ class ResourceE2ECompileTest : BaseResourceCompileTest {
5061
checkResourceExamples(
5162
listOf(
5263
testDirJVM,
53-
// testDirJS we are currently disable these tests (see KTL-2176)
64+
testDirJS
5465
)
5566
) { result, file ->
56-
val json = jacksonObjectMapper().writeValueAsString(result)
57-
val out = file.path.replace("test-compile-data", "test-compile-output").replace("\\.kt$".toRegex(), ".json")
67+
val (actualResult, extension) = when (result) {
68+
// For the JS it has no sense to compare compiled code,
69+
// because it differs with each new compiler version,
70+
// but it makes sense to compare the results of the execution such code.
71+
is TranslationJSResult -> Pair(executeCompiledJsCode(result), ".txt")
72+
else -> Pair(jacksonObjectMapper().writeValueAsString(result), ".json")
73+
}
74+
val out =
75+
file.path.replace("test-compile-data", "test-compile-output").replace("\\.kt$".toRegex(), extension)
5876

5977
val outFile = File(out)
6078

6179
if (outFile.exists()) {
6280
val text = outFile.readText()
63-
if (text != json) {
81+
if (text != actualResult) {
6482
if (!file.isInconsistentOutput()) {
6583
return@checkResourceExamples """
6684
Expected: $text
67-
Actual: $json
85+
Actual: $actualResult
6886
""".trimIndent()
6987
}
7088

@@ -75,7 +93,7 @@ class ResourceE2ECompileTest : BaseResourceCompileTest {
7593
} else {
7694
println("New file: ${outFile.path}")
7795
File(outFile.parent).mkdirs()
78-
outFile.writeText(json)
96+
outFile.writeText(actualResult)
7997
}
8098

8199
null
@@ -102,3 +120,38 @@ private fun File.isInconsistentOutput(): Boolean {
102120
"LocalDate.now()",
103121
).any { code.contains(it) }
104122
}
123+
124+
/**
125+
* The output of the JS compilation is JavaScript code that includes some additional logic
126+
* for redirecting output specifically for the playground.
127+
* In this method, we strip out that extra logic and execute the JavaScript code directly,
128+
* because we're not concerned about whether the compiled code has changed —
129+
* what's important is whether the result of its execution has changed.
130+
*/
131+
private fun executeCompiledJsCode(result: TranslationJSResult): String {
132+
val jsCode = result.jsCode ?: ""
133+
134+
val cleanedCode = jsCode.replace(JS_IR_CODE_BUFFER, "")
135+
.replace(JS_IR_OUTPUT_REWRITE, "")
136+
// In pure Node JS there is no `alert` function like in browser,
137+
// that is why we need to add some custom override for alerts here.
138+
.replace("alert('", "console.log('ALERT: ")
139+
140+
return executeJsCode(cleanedCode)
141+
}
142+
143+
private fun executeJsCode(jsCode: String): String {
144+
val tmpDir = createTempDirectory()
145+
val jsMain = tmpDir.resolve("playground.js")
146+
jsMain.writeText(jsCode)
147+
148+
val textResult = startNodeJsApp(
149+
System.getenv("kotlin.wasm.node.path"),
150+
jsMain.normalize().absolutePathString()
151+
)
152+
153+
tmpDir.toFile().deleteRecursively()
154+
return textResult
155+
}
156+
157+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.compiler.server.base
2+
3+
import java.io.IOException
4+
5+
6+
@Throws(IOException::class, InterruptedException::class)
7+
fun startNodeJsApp(
8+
pathToBinNode: String?,
9+
pathToAppScript: String?
10+
): String {
11+
val processBuilder = ProcessBuilder()
12+
processBuilder.command(pathToBinNode, pathToAppScript)
13+
val process = processBuilder.start()
14+
val inputStream = process.inputStream
15+
process.waitFor()
16+
return inputStream.reader().readText()
17+
}

src/test/kotlin/com/compiler/server/generator/TestProjectRunner.kt

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.compiler.server.generator
33
import com.compiler.server.base.filterOnlyErrors
44
import com.compiler.server.base.hasErrors
55
import com.compiler.server.base.renderErrorDescriptors
6+
import com.compiler.server.base.startNodeJsApp
67
import com.compiler.server.model.*
78
import com.compiler.server.service.KotlinProjectExecutor
89
import model.Completion
@@ -227,17 +228,4 @@ class TestProjectRunner {
227228
Assertions.assertTrue(textResult.contains(contains), "Actual: ${textResult}. \n Expected: $contains")
228229
return result
229230
}
230-
231-
@Throws(IOException::class, InterruptedException::class)
232-
fun startNodeJsApp(
233-
pathToBinNode: String?,
234-
pathToAppScript: String?
235-
): String {
236-
val processBuilder = ProcessBuilder()
237-
processBuilder.command(pathToBinNode, pathToAppScript)
238-
val process = processBuilder.start()
239-
val inputStream = process.inputStream
240-
process.waitFor()
241-
return inputStream.reader().readText()
242-
}
243231
}

src/test/resources/test-compile-output/js/KotlinByExampleSnippetsTests/01_dynamic/0.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a == a
2+
97 == 97
3+
bbb
4+
2 + 2 = 4
5+
'2' + 2 = 22

src/test/resources/test-compile-output/js/KotlinByExampleSnippetsTests/02_js_function/0.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALERT: alert from Kotlin!

src/test/resources/test-compile-output/js/KotlinByExampleSnippetsTests/02_js_function/1.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Jane","hobby":"movies"}

src/test/resources/test-compile-output/js/KotlinByExampleSnippetsTests/03_external/0.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALERT: Hi!

0 commit comments

Comments
 (0)