Skip to content

Commit

Permalink
Fix kotlin tests (#24)
Browse files Browse the repository at this point in the history
Support kotlin tests

* Add KotlinTest to detect kotlin test classes
* Add scripted test for simple test and mixed tests
  • Loading branch information
tonilopezmr authored and pfn committed Jul 10, 2018
1 parent 931fa36 commit 2d50715
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Expand Up @@ -5,7 +5,7 @@ name := "kotlin-plugin"

organization := "com.hanhuy.sbt"

version := "1.0.8"
version := "1.0.9-SNAPSHOT"

scalacOptions ++= Seq("-deprecation","-Xlint","-feature")

Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/KotlinPlugin.scala
Expand Up @@ -68,6 +68,7 @@ object KotlinPlugin extends AutoPlugin {
classDirectory.value, streams.value)
}.dependsOn (compileInputs in (Compile,compile)).value,
compile := (compile dependsOn kotlinCompile).value,
kotlinSource := sourceDirectory.value / "kotlin"
kotlinSource := sourceDirectory.value / "kotlin",
definedTests in Test ++= KotlinTest.kotlinTests.value
)
}
40 changes: 40 additions & 0 deletions src/main/scala/KotlinTest.scala
@@ -0,0 +1,40 @@
package sbt

import sbt.Keys._
import sbt.classfile.Analyze
import sbt.classpath.ClasspathUtilities
import sbt.inc.{Analysis, IncOptions, IncrementalCompile}
import xsbti.compile.SingleOutput

object KotlinTest {
val kotlinTests = Def.task {
val out = ((target in Test).value ** "scala-*").get.head / "test-classes"
val srcs = ((sourceDirectory in Test).value ** "*.kt").get.toList
val xs = (out ** "*.class").get.toList

val loader = ClasspathUtilities.toLoader((fullClasspath in Test).value map {
_.data
})
val log = streams.value.log
val a0 = IncrementalCompile(
srcs.toSet, s => None,
(fs, changs, callback) => {
def readAPI(source: File, classes: Seq[Class[_]]): Set[String] = {
val (api, inherits) = ClassToAPI.process(classes)
callback.api(source, api)
inherits.map(_.getName)
}

Analyze(xs, srcs, log)(callback, loader, readAPI)
},
Analysis.Empty, f => None,
new SingleOutput {
def outputDirectory = out
},
log,
IncOptions.Default)._2
val frameworks = (loadedTestFrameworks in Test).value.values.toList
log.info(s"Compiling ${srcs.length} Kotlin source to ${out}...")
Tests.discover(frameworks, a0, log)._1
}
}
27 changes: 27 additions & 0 deletions src/sbt-test/kotlin/basic-tests/build.sbt
@@ -0,0 +1,27 @@
import sbt.complete.Parsers.spaceDelimited

import scala.xml.{NodeSeq, XML}

kotlinLib("stdlib")

libraryDependencies ++= Seq(
"com.novocode" % "junit-interface" % "0.11" % Test
)

lazy val checkTestPass = inputKey[Unit]("Check if a given test-report has one success test")
checkTestPass := {
val args: Seq[String] = spaceDelimited("<arg>").parsed
val testName = args.head

val xml = XML.load(s"target/test-reports/$testName.xml")
val totalTests = getInt(xml \\ "testsuite" \ "@tests")
val failures = getInt(xml \\ "testsuite" \ "@failures")
val errors = getInt(xml \\ "testsuite" \ "@errors")
val skipped = getInt(xml \\ "testsuite" \ "@skipped")

if (totalTests == 0 || failures > 0 || errors > 0 || skipped > 0) {
sys.error("Tests not passed")
}
}

def getInt(path: NodeSeq): Int = path.text.toInt
9 changes: 9 additions & 0 deletions src/sbt-test/kotlin/basic-tests/project/plugins.sbt
@@ -0,0 +1,9 @@
{
val ver = System.getProperty("plugin.version")
if (ver == null)
throw new RuntimeException("""
|The system property 'plugin.version' is not defined.
|Specify this property using scriptedLaunchOpts -Dplugin.version."""
.stripMargin)
else addSbtPlugin("com.hanhuy.sbt" % "kotlin-plugin" % ver)
}
6 changes: 6 additions & 0 deletions src/sbt-test/kotlin/basic-tests/src/main/kotlin/simple.kt
@@ -0,0 +1,6 @@
package demo

fun main(args: Array<String>) {
println("Hello, world!")
}

11 changes: 11 additions & 0 deletions src/sbt-test/kotlin/basic-tests/src/test/kotlin/SimpleTest.kt
@@ -0,0 +1,11 @@
import org.junit.Test
import junit.framework.TestCase.assertEquals

class SimpleTest {

@Test
fun `should works`() {
assertEquals(4, 2 + 2)
}

}
3 changes: 3 additions & 0 deletions src/sbt-test/kotlin/basic-tests/test
@@ -0,0 +1,3 @@
> compile
> test
> "checkTestPass SimpleTest"
27 changes: 27 additions & 0 deletions src/sbt-test/kotlin/mixed-tests/build.sbt
@@ -0,0 +1,27 @@
import sbt.complete.Parsers.spaceDelimited

import scala.xml.{NodeSeq, XML}

kotlinLib("stdlib")

libraryDependencies ++= Seq(
"com.novocode" % "junit-interface" % "0.11" % Test
)

lazy val checkTestPass = inputKey[Unit]("Check if a given test-report has one success test")
checkTestPass := {
val args: Seq[String] = spaceDelimited("<arg>").parsed
val testName = args.head

val xml = XML.load(s"target/test-reports/$testName.xml")
val totalTests = getInt(xml \\ "testsuite" \ "@tests")
val failures = getInt(xml \\ "testsuite" \ "@failures")
val errors = getInt(xml \\ "testsuite" \ "@errors")
val skipped = getInt(xml \\ "testsuite" \ "@skipped")

if (totalTests == 0 || failures > 0 || errors > 0 || skipped > 0) {
sys.error("Tests not passed")
}
}

def getInt(path: NodeSeq): Int = path.text.toInt
9 changes: 9 additions & 0 deletions src/sbt-test/kotlin/mixed-tests/project/plugins.sbt
@@ -0,0 +1,9 @@
{
val ver = System.getProperty("plugin.version")
if (ver == null)
throw new RuntimeException("""
|The system property 'plugin.version' is not defined.
|Specify this property using scriptedLaunchOpts -Dplugin.version."""
.stripMargin)
else addSbtPlugin("com.hanhuy.sbt" % "kotlin-plugin" % ver)
}
@@ -0,0 +1,9 @@
package demo;

public class JavaCalculator {

public int sum(int a, int b) {
return a + b;
}

}
7 changes: 7 additions & 0 deletions src/sbt-test/kotlin/mixed-tests/src/main/kotlin/Calculator.kt
@@ -0,0 +1,7 @@
package demo

class Calculator(private val calculator: JavaCalculator) {

fun sum(a: Int, b: Int): Int = calculator.sum(a, b)

}
14 changes: 14 additions & 0 deletions src/sbt-test/kotlin/mixed-tests/src/test/kotlin/MixedTest.kt
@@ -0,0 +1,14 @@
import org.junit.Test
import junit.framework.TestCase.assertEquals
import demo.Calculator
import demo.JavaCalculator

class MixedTest {

@Test
fun `should sum 2 plus 2`() {
val calculator = Calculator(JavaCalculator())
assertEquals(4, calculator.sum(2, 2))
}

}
3 changes: 3 additions & 0 deletions src/sbt-test/kotlin/mixed-tests/test
@@ -0,0 +1,3 @@
> compile
> test
> "checkTestPass MixedTest"

0 comments on commit 2d50715

Please sign in to comment.