Gradle plugin: fix tests.
This commit is contained in:
+13
-5
@@ -3,15 +3,16 @@ package org.jetbrains.kotlin.gradle
|
||||
import com.google.common.io.Files
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.util.Scanner
|
||||
import org.junit.Before
|
||||
import org.junit.After
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotEquals
|
||||
import kotlin.test.fail
|
||||
import org.gradle.api.logging.LogLevel
|
||||
import kotlin.test.assertFalse
|
||||
|
||||
private val SYSTEM_LINE_SEPARATOR = System.getProperty("line.separator")
|
||||
|
||||
open class BaseGradleIT(resourcesRoot: String = "src/test/resources") {
|
||||
|
||||
@@ -42,20 +43,25 @@ open class BaseGradleIT(resourcesRoot: String = "src/test/resources") {
|
||||
}
|
||||
|
||||
fun CompiledProject.assertSuccessful(): CompiledProject {
|
||||
assertEquals(resultCode, 0, "Gradle build failed")
|
||||
assertEquals(0, resultCode, "Gradle build failed")
|
||||
return this
|
||||
}
|
||||
|
||||
fun CompiledProject.assertFailed(): CompiledProject {
|
||||
assertNotEquals(0, resultCode, "Expected that Gradle build failed")
|
||||
return this
|
||||
}
|
||||
|
||||
fun CompiledProject.assertContains(vararg expected: String): CompiledProject {
|
||||
for (str in expected) {
|
||||
assertTrue(output.contains(str), "Should contain '$str', actual output: $output")
|
||||
assertTrue(output.contains(str.normalize()), "Should contain '$str', actual output: $output")
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
fun CompiledProject.assertNotContains(vararg expected: String): CompiledProject {
|
||||
for (str in expected) {
|
||||
assertFalse(output.contains(str), "Should not contain '$str', actual output: $output")
|
||||
assertFalse(output.contains(str.normalize()), "Should not contain '$str', actual output: $output")
|
||||
}
|
||||
return this
|
||||
}
|
||||
@@ -95,6 +101,8 @@ open class BaseGradleIT(resourcesRoot: String = "src/test/resources") {
|
||||
listOf("/bin/bash", "./gradlew") + tailParameters
|
||||
}
|
||||
|
||||
private fun String.normalize() = this.replaceAll("\r\n", "\n").replaceAll("\n", SYSTEM_LINE_SEPARATOR)
|
||||
|
||||
private fun isWindows(): Boolean {
|
||||
return System.getProperty("os.name")!!.contains("Windows")
|
||||
}
|
||||
|
||||
+18
-12
@@ -12,12 +12,15 @@ class Kotlin2JsGradlePluginIT : BaseGradleIT() {
|
||||
assertSuccessful()
|
||||
assertReportExists()
|
||||
|
||||
assertContains(":libraryProject:jarSources\n",
|
||||
":mainProject:compileKotlin2Js\n", ":mainProject:copyKotlinJs\n",
|
||||
":libraryProject:compileKotlin2Js\n", ":libraryProject:copyKotlinJs\n")
|
||||
assertContains(
|
||||
":libraryProject:jarSources\n",
|
||||
":mainProject:compileKotlin2Js\n",
|
||||
":libraryProject:compileKotlin2Js\n"
|
||||
)
|
||||
|
||||
listOf("mainProject/web/js/app.js", "mainProject/web/js/lib/kotlin.js",
|
||||
"libraryProject/build/kotlin2js/main/app.js", "libraryProject/build/kotlin2js/main/kotlin.js",
|
||||
listOf("mainProject/web/js/app.js",
|
||||
"mainProject/web/js/lib/kotlin.js",
|
||||
"libraryProject/build/kotlin2js/main/test-library.js",
|
||||
"mainProject/web/js/app.js.map"
|
||||
).forEach { assertFileExists(it) }
|
||||
|
||||
@@ -26,21 +29,23 @@ class Kotlin2JsGradlePluginIT : BaseGradleIT() {
|
||||
// TODO It would be better to test these by behavior instead of implementation, for example by loading the files
|
||||
// into Rhino and running assertions on that. See https://github.com/abesto/kotlin/commit/120ec1bda3d95630189d4d33d0b2afb4253b5186
|
||||
// for the (original) discussion on this.
|
||||
assertFileContains("libraryProject/build/kotlin2js/main/app.js", "Counter: Kotlin.createClass")
|
||||
assertFileContains("mainProject/web/js/app.js", "var counter = new Counter(counterText);")
|
||||
assertFileContains("libraryProject/build/kotlin2js/main/test-library.js", "Counter: Kotlin.createClass")
|
||||
assertFileContains("mainProject/web/js/app.js", "var counter = new Kotlin.modules['test-library'].example.library.Counter(counterText);")
|
||||
}
|
||||
|
||||
project.build("build") {
|
||||
assertSuccessful()
|
||||
assertContains(":mainProject:compileKotlin2Js UP-TO-DATE", ":libraryProject:compileTestKotlin2Js UP-TO-DATE")
|
||||
assertContains(
|
||||
":mainProject:compileKotlin2Js UP-TO-DATE",
|
||||
":libraryProject:compileTestKotlin2Js UP-TO-DATE"
|
||||
)
|
||||
}
|
||||
|
||||
project.build("clean") {
|
||||
assertSuccessful()
|
||||
assertReportExists()
|
||||
assertContains(":mainProject:cleanCompileKotlin2Js\n", ":mainProject:cleanCopyKotlinJs\n")
|
||||
assertContains(":mainProject:cleanCompileKotlin2Js\n")
|
||||
assertNoSuchFile("mainProject/web/js/app.js")
|
||||
assertNoSuchFile("mainProject/web/js/lib/kotlin.js")
|
||||
|
||||
// Test that we don't accidentally remove the containing directory
|
||||
// This would fail if we used the default clean task of the copy task
|
||||
@@ -53,15 +58,16 @@ class Kotlin2JsGradlePluginIT : BaseGradleIT() {
|
||||
project.build("clean") {
|
||||
assertSuccessful()
|
||||
assertReportExists()
|
||||
assertContains(":mainProject:cleanCompileKotlin2Js UP-TO-DATE", ":mainProject:cleanCopyKotlinJs UP-TO-DATE")
|
||||
assertContains(":mainProject:cleanCompileKotlin2Js UP-TO-DATE")
|
||||
}
|
||||
}
|
||||
|
||||
Test fun testNoOutputFileFails() {
|
||||
val project = Project("kotlin2JsNoOutputFileProject", "1.6")
|
||||
project.build("build") {
|
||||
assertFailed()
|
||||
assertReportExists()
|
||||
assertContains("compileKotlin2Js.kotlinOptions.outputFile must be set to a string")
|
||||
assertContains("compileKotlin2Js.kotlinOptions.outputFile should be specified.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+20
-1
@@ -32,4 +32,23 @@ artifacts {
|
||||
compile jarSources
|
||||
}
|
||||
|
||||
compileKotlin2Js.kotlinOptions.outputFile = "${buildDir}/kotlin2js/main/app.js"
|
||||
def outDir = "${buildDir}/kotlin2js/main/"
|
||||
|
||||
compileKotlin2Js.kotlinOptions.outputFile = outDir + "test-library.js"
|
||||
|
||||
jar {
|
||||
from sourceSets.main.allSource
|
||||
include "**/*.kt"
|
||||
|
||||
from outDir
|
||||
include "**/*.js"
|
||||
|
||||
manifest {
|
||||
attributes(
|
||||
"Specification-Title": "Kotlin JavaScript Lib",
|
||||
"Kotlin-JS-Module-Name": "test-library"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
jar.dependsOn(compileKotlin2Js)
|
||||
|
||||
+14
-1
@@ -30,4 +30,17 @@ compileKotlin2Js.kotlinOptions.outputFile = "${projectDir}/web/js/app.js"
|
||||
compileKotlin2Js.kotlinOptions.suppressWarnings = true
|
||||
compileKotlin2Js.kotlinOptions.verbose = true
|
||||
|
||||
copyKotlinJs.into "${projectDir}/web/js/lib"
|
||||
task copyJsFilesFromDependencies(dependsOn: build) {
|
||||
configurations.compile.each { File file ->
|
||||
copy {
|
||||
includeEmptyDirs = false
|
||||
|
||||
from zipTree(file.absolutePath)
|
||||
into "${projectDir}/web/js/lib"
|
||||
include { fileTreeElement ->
|
||||
def path = fileTreeElement.path
|
||||
path.endsWith(".js") && (path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user