Skip to content

Commit

Permalink
[K/JS] Fix file extension inside the JS KGP to run tests with ES modu…
Browse files Browse the repository at this point in the history
…les ^KT-63719 Fixed
  • Loading branch information
JSMonk authored and qodana-bot committed Dec 20, 2023
1 parent 87c8aa1 commit 262697d
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1815,4 +1815,50 @@ class Kotlin2JsIrGradlePluginIT : KGPBaseTest() {
}
}
}

@DisplayName("Check that nodeTest run tests with commonjs module kind")
@GradleTest
fun testFailedJsTestWithCommonJs(gradleVersion: GradleVersion) {
project("kotlin-js-project-failed-test", gradleVersion) {
buildGradle.appendText(
"""
|
|tasks.withType(KotlinJsCompile).configureEach {
| kotlinOptions { moduleKind = "commonjs" }
|}
""".trimMargin()
)
buildAndFail("nodeTest") {
assertTasksFailed(":nodeTest")

assertTestResults(
projectPath.resolve("TEST-all.xml"),
"nodeTest"
)
}
}
}

@DisplayName("Check that nodeTest run tests with ESM module kind")
@GradleTest
fun testFailedJsTestWithESM(gradleVersion: GradleVersion) {
project("kotlin-js-project-failed-test", gradleVersion) {
buildGradle.appendText(
"""
|
|tasks.withType(KotlinJsCompile).configureEach {
| kotlinOptions { moduleKind = "es" }
|}
""".trimMargin()
)
buildAndFail("nodeTest") {
assertTasksFailed(":nodeTest")

assertTestResults(
projectPath.resolve("TEST-all.xml"),
"nodeTest"
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<results>
<testsuite name="Tests" tests="1" skipped="0" failures="1" errors="0" timestamp="..." hostname="..." time="...">
<properties />
<testcase name="testHello[node]" classname="Tests" time="...">
<failure message="..." type="AssertionError">...</failure>
</testcase>
<system-out />
<system-err />
</testsuite>
</results>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile

plugins {
id("org.jetbrains.kotlin.js")
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-js")
testImplementation("org.jetbrains.kotlin:kotlin-test-js")
}

repositories {
mavenLocal()
mavenCentral()
}

kotlin {
js {
binaries.executable()
nodejs()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kotlin.tests.individualTaskReports=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright 2010-2019 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 com.example

fun getValue(): Int {
return 42
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright 2010-2021 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.
*/

import kotlin.test.Test
import kotlin.test.assertEquals

class Tests {
@Test
fun testHello() {
assertEquals(73, com.example.getValue())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import groovy.lang.Closure
import org.gradle.api.Action
import org.gradle.api.attributes.AttributeContainer
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.provider.Provider
import org.jetbrains.kotlin.gradle.dsl.JsModuleKind
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompilerOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinJsOptions
import org.jetbrains.kotlin.gradle.plugin.*
Expand Down Expand Up @@ -99,3 +101,15 @@ open class KotlinJsCompilation @Inject internal constructor(
}
}
}

internal val KotlinJsCompilation.fileExtension: Provider<String>
get() {
val isWasm = platformType == KotlinPlatformType.wasm
return compilerOptions.options.moduleKind.map { moduleKind ->
if (isWasm || moduleKind == JsModuleKind.MODULE_ES) {
"mjs"
} else {
"js"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinCompilationInfo
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinWithJavaTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.fileExtension
import org.jetbrains.kotlin.gradle.plugin.tcs
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
import org.jetbrains.kotlin.gradle.targets.js.KotlinWasmTargetType
Expand Down Expand Up @@ -41,7 +42,8 @@ internal open class BaseKotlin2JsCompileConfig<TASK : Kotlin2JsCompile>(

configureAdditionalFreeCompilerArguments(task, compilation)

val compilationTarget = compilation.tcs.compilation.target
val binaryCompilation = compilation.tcs.compilation
val compilationTarget = binaryCompilation.target
if (compilationTarget is KotlinJsTarget ||
(compilationTarget is KotlinWithJavaTarget<*, *> && compilationTarget.platformType == KotlinPlatformType.js)
) {
Expand All @@ -55,10 +57,14 @@ internal open class BaseKotlin2JsCompileConfig<TASK : Kotlin2JsCompile>(
@Suppress("DEPRECATION")
task.outputFileProperty.value(
task.destinationDirectory.flatMap { dir ->
if (task.compilerOptions.outputFile.orNull != null) {
task.compilerOptions.outputFile.map { File(it) }
} else {
task.compilerOptions.moduleName.map { name ->
when {
task.compilerOptions.outputFile.orNull != null -> task.compilerOptions.outputFile.map { File(it) }
binaryCompilation is KotlinJsCompilation -> task.compilerOptions.moduleName.flatMap { name ->
binaryCompilation.fileExtension.map {
dir.file("$name.$it").asFile
}
}
else -> task.compilerOptions.moduleName.map { name ->
dir.file(name + compilation.platformType.fileExtension).asFile
}
}
Expand Down

0 comments on commit 262697d

Please sign in to comment.