Gradle plugin: fix tests.

This commit is contained in:
Zalim Bashorov
2014-12-23 18:26:50 +03:00
parent 35e2a46ae0
commit a05ba64f93
4 changed files with 65 additions and 19 deletions
@@ -3,15 +3,16 @@ package org.jetbrains.kotlin.gradle
import com.google.common.io.Files import com.google.common.io.Files
import java.io.File import java.io.File
import java.io.InputStream import java.io.InputStream
import java.util.Scanner
import org.junit.Before import org.junit.Before
import org.junit.After import org.junit.After
import kotlin.test.assertTrue import kotlin.test.assertTrue
import kotlin.test.assertFalse import kotlin.test.assertFalse
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.fail import kotlin.test.fail
import org.gradle.api.logging.LogLevel 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") { open class BaseGradleIT(resourcesRoot: String = "src/test/resources") {
@@ -42,20 +43,25 @@ open class BaseGradleIT(resourcesRoot: String = "src/test/resources") {
} }
fun CompiledProject.assertSuccessful(): CompiledProject { 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 return this
} }
fun CompiledProject.assertContains(vararg expected: String): CompiledProject { fun CompiledProject.assertContains(vararg expected: String): CompiledProject {
for (str in expected) { 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 return this
} }
fun CompiledProject.assertNotContains(vararg expected: String): CompiledProject { fun CompiledProject.assertNotContains(vararg expected: String): CompiledProject {
for (str in expected) { 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 return this
} }
@@ -95,6 +101,8 @@ open class BaseGradleIT(resourcesRoot: String = "src/test/resources") {
listOf("/bin/bash", "./gradlew") + tailParameters listOf("/bin/bash", "./gradlew") + tailParameters
} }
private fun String.normalize() = this.replaceAll("\r\n", "\n").replaceAll("\n", SYSTEM_LINE_SEPARATOR)
private fun isWindows(): Boolean { private fun isWindows(): Boolean {
return System.getProperty("os.name")!!.contains("Windows") return System.getProperty("os.name")!!.contains("Windows")
} }
@@ -12,12 +12,15 @@ class Kotlin2JsGradlePluginIT : BaseGradleIT() {
assertSuccessful() assertSuccessful()
assertReportExists() assertReportExists()
assertContains(":libraryProject:jarSources\n", assertContains(
":mainProject:compileKotlin2Js\n", ":mainProject:copyKotlinJs\n", ":libraryProject:jarSources\n",
":libraryProject:compileKotlin2Js\n", ":libraryProject:copyKotlinJs\n") ":mainProject:compileKotlin2Js\n",
":libraryProject:compileKotlin2Js\n"
)
listOf("mainProject/web/js/app.js", "mainProject/web/js/lib/kotlin.js", listOf("mainProject/web/js/app.js",
"libraryProject/build/kotlin2js/main/app.js", "libraryProject/build/kotlin2js/main/kotlin.js", "mainProject/web/js/lib/kotlin.js",
"libraryProject/build/kotlin2js/main/test-library.js",
"mainProject/web/js/app.js.map" "mainProject/web/js/app.js.map"
).forEach { assertFileExists(it) } ).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 // 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 // into Rhino and running assertions on that. See https://github.com/abesto/kotlin/commit/120ec1bda3d95630189d4d33d0b2afb4253b5186
// for the (original) discussion on this. // for the (original) discussion on this.
assertFileContains("libraryProject/build/kotlin2js/main/app.js", "Counter: Kotlin.createClass") assertFileContains("libraryProject/build/kotlin2js/main/test-library.js", "Counter: Kotlin.createClass")
assertFileContains("mainProject/web/js/app.js", "var counter = new Counter(counterText);") assertFileContains("mainProject/web/js/app.js", "var counter = new Kotlin.modules['test-library'].example.library.Counter(counterText);")
} }
project.build("build") { project.build("build") {
assertSuccessful() 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") { project.build("clean") {
assertSuccessful() assertSuccessful()
assertReportExists() assertReportExists()
assertContains(":mainProject:cleanCompileKotlin2Js\n", ":mainProject:cleanCopyKotlinJs\n") assertContains(":mainProject:cleanCompileKotlin2Js\n")
assertNoSuchFile("mainProject/web/js/app.js") assertNoSuchFile("mainProject/web/js/app.js")
assertNoSuchFile("mainProject/web/js/lib/kotlin.js")
// Test that we don't accidentally remove the containing directory // Test that we don't accidentally remove the containing directory
// This would fail if we used the default clean task of the copy task // This would fail if we used the default clean task of the copy task
@@ -53,15 +58,16 @@ class Kotlin2JsGradlePluginIT : BaseGradleIT() {
project.build("clean") { project.build("clean") {
assertSuccessful() assertSuccessful()
assertReportExists() assertReportExists()
assertContains(":mainProject:cleanCompileKotlin2Js UP-TO-DATE", ":mainProject:cleanCopyKotlinJs UP-TO-DATE") assertContains(":mainProject:cleanCompileKotlin2Js UP-TO-DATE")
} }
} }
Test fun testNoOutputFileFails() { Test fun testNoOutputFileFails() {
val project = Project("kotlin2JsNoOutputFileProject", "1.6") val project = Project("kotlin2JsNoOutputFileProject", "1.6")
project.build("build") { project.build("build") {
assertFailed()
assertReportExists() assertReportExists()
assertContains("compileKotlin2Js.kotlinOptions.outputFile must be set to a string") assertContains("compileKotlin2Js.kotlinOptions.outputFile should be specified.")
} }
} }
} }
@@ -32,4 +32,23 @@ artifacts {
compile jarSources 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)
@@ -30,4 +30,17 @@ compileKotlin2Js.kotlinOptions.outputFile = "${projectDir}/web/js/app.js"
compileKotlin2Js.kotlinOptions.suppressWarnings = true compileKotlin2Js.kotlinOptions.suppressWarnings = true
compileKotlin2Js.kotlinOptions.verbose = 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/"))
}
}
}
}