Skip to content

Commit 6f352c5

Browse files
authored
Merge pull request #526 from dwijnand/munit
2 parents 0623ec2 + 384ea60 commit 6f352c5

File tree

10 files changed

+117
-50
lines changed

10 files changed

+117
-50
lines changed

build.sbt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,16 @@ val sbtplugin = project.enablePlugins(SbtPlugin).dependsOn(core).settings(
6363
val testFunctional = taskKey[Unit]("Run the functional test")
6464
val functionalTests = Project("functional-tests", file("functional-tests"))
6565
.dependsOn(core)
66+
.configs(IntegrationTest)
6667
.settings(
6768
libraryDependencies += "com.typesafe" % "config" % "1.4.0",
6869
libraryDependencies += "io.get-coursier" %% "coursier" % "2.0.0-RC6-25",
69-
testFunctional := (Compile / runMain).toTask(" com.typesafe.tools.mima.lib.UnitTests").value,
70-
IntegrationTest / test := (Compile / runMain).toTask(" com.typesafe.tools.mima.lib.IntegrationTests").value,
70+
libraryDependencies += "org.scalameta" %% "munit" % "0.7.9" % s"$Test;$IntegrationTest",
71+
testFrameworks += new TestFramework("munit.Framework"),
72+
//Test / run / fork := true,
73+
//Test / run / forkOptions := (Test / run / forkOptions).value.withWorkingDirectory((ThisBuild / baseDirectory).value),
74+
testFunctional := (Test / test).value,
75+
Defaults.itSettings,
7176
mimaFailOnNoPrevious := false,
7277
skip in publish := true,
7378
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.typesafe.tools.mima.lib
2+
3+
import munit._
4+
5+
class IntegrationTestSuite extends Suite {
6+
type TestValue = Unit
7+
8+
def munitTests() = for {
9+
test <- IntegrationTests.fromArgs(Nil).tests
10+
} yield new GenericTest(test.label, test.action, Set.empty, Location.empty)
11+
}

functional-tests/src/main/scala/com/typesafe/tools/mima/lib/AppRunTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import scala.util.{ Failure, Success, Try }
99

1010
/** Test running the App, using library v2. */
1111
object AppRunTest {
12-
def main(args: Array[String]): Unit = TestCase.testAll(args.toList)(testAppRun)
12+
def main(args: Array[String]): Unit = TestCase.argsToTests(args.toList, testAppRun).assert()
1313

1414
def testAppRun(testCase: TestCase) = {
1515
import testCase._

functional-tests/src/main/scala/com/typesafe/tools/mima/lib/CollectProblemsTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import scala.collection.JavaConverters._
1010
import scala.util.{ Failure, Success, Try }
1111

1212
object CollectProblemsTest {
13-
def main(args: Array[String]): Unit = TestCase.testAll(args.toList)(testCollectProblems)
13+
def main(args: Array[String]): Unit = TestCase.argsToTests(args.toList, testCollectProblems).assert()
1414

1515
def testCollectProblems(testCase: TestCase) = {
1616
import testCase._

functional-tests/src/main/scala/com/typesafe/tools/mima/lib/IntegrationTests.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ import scala.reflect.io.Directory
88
import scala.util.Try
99

1010
object IntegrationTests {
11-
def main(args: Array[String]): Unit = {
11+
def main(args: Array[String]): Unit = fromArgs(args.toList).assert()
12+
13+
def fromArgs(args: List[String]): Tests = {
1214
val dirs = Directory("functional-tests/src/it").dirs.filter(args match {
13-
case Array() => dir => dir.files.exists(_.name == "test.conf")
14-
case xs => xs.toSet.compose(_.name)
15-
}).toSeq.sortBy(_.path)
16-
TestPrinter.testAll(dirs)(dir => s"${dir.name}")(testIntegration)
15+
case Seq() => dir => dir.files.exists(_.name == "test.conf")
16+
case names => dir => names.contains(dir.name)
17+
}).toList.sortBy(_.path)
18+
Tests(dirs.map(dir => Test(dir.name, testIntegration(dir))))
1719
}
1820

19-
def testIntegration(baseDir: Directory) = {
21+
def testIntegration(baseDir: Directory): Try[Unit] = {
2022
val conf = ConfigFactory.parseFile((baseDir / "test.conf").jfile).resolve()
2123
val groupId = conf.getString("groupId")
2224
val artifactId = conf.getString("artifactId")
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.typesafe.tools.mima.lib
2+
3+
import scala.util.{ Failure, Success, Try }
4+
5+
object Test {
6+
def apply(label: String, action: => Unit): Test1 = Test1(label, () => action)
7+
8+
def pass = s"${Console.GREEN}\u2713${Console.RESET}" // check mark (green)
9+
def fail = s"${Console.RED}\u2717${Console.RESET}" // cross mark (red)
10+
11+
def testAll(tests: List[Test]): Try[Unit] = {
12+
tests.iterator.map(_.run()).foldLeft(Try(())) {
13+
case (res @ Failure(e1), Failure(e2)) => e1.addSuppressed(e2); res
14+
case (res @ Failure(_), _) => res
15+
case (res, _) => res
16+
}
17+
}
18+
19+
def run1(label: String, action: () => Unit): Try[Unit] = {
20+
Try(action()) match {
21+
case res @ Success(()) => println(s"+ $pass $label"); res
22+
case res @ Failure(ex) => println(s"- $fail $label: $ex"); res
23+
}
24+
}
25+
26+
if (System.out != Console.out) {
27+
System.out.println(" System.out identity ##: " + System.identityHashCode(System.out))
28+
System.out.println("Console.out identity ##: " + System.identityHashCode(scala.Console.out))
29+
System.out.println("cwd: " + new java.io.File("").getAbsoluteFile)
30+
}
31+
}
32+
33+
sealed trait Test {
34+
def assert(): Unit = run().get
35+
36+
def run(): Try[Unit] = this match {
37+
case Test1(l, a) => Test.run1(l, a)
38+
case Tests(ts) => Test.testAll(ts)
39+
}
40+
41+
override def toString = this match {
42+
case Test1(l, _) => s"Test($l)"
43+
case Tests(tests) => s"Tests(${tests.mkString(", ")})"
44+
}
45+
}
46+
47+
case class Test1(label: String, action: () => Unit) extends Test
48+
case class Tests(tests: List[Test1]) extends Test

functional-tests/src/main/scala/com/typesafe/tools/mima/lib/TestCase.scala

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,42 @@ object TestCase {
1818
val scala213 = "2.13.2"
1919
val hostScalaVersion = scala.util.Properties.versionNumberString
2020

21-
def testAll(argv: List[String])(test: TestCase => Try[Unit]): Unit =
22-
TestPrinter.testAll(fromArgs(argv))(tc => s"${tc.scalaBinaryVersion} / ${tc.name}")(test)
21+
def argsToTests(argv: List[String], runTestCase: TestCase => Try[Unit]): Tests =
22+
Tests(fromArgs(argv).map(tc => Test(s"${tc.scalaBinaryVersion} / ${tc.name}", runTestCase(tc))))
2323

2424
def fromArgs(argv: List[String]): List[TestCase] = {
2525
val Conf(svs, dirs0) = go(argv, Conf(Nil, Nil))
26-
val dirs = if (dirs0.nonEmpty) dirs0 else
26+
val dirs = if (dirs0.nonEmpty) dirs0.reverse else
2727
Directory("functional-tests/src/test").dirs
2828
.filter(_.files.exists(_.name == "problems.txt"))
2929
.toSeq.sortBy(_.path)
30-
val scalaCompilers = (if (svs.isEmpty) List(hostScalaVersion) else svs).map(new ScalaCompiler(_))
30+
val scalaCompilers = (if (svs.isEmpty) List(hostScalaVersion) else svs.reverse).map(new ScalaCompiler(_))
3131
val javaCompiler = ToolProvider.getSystemJavaCompiler
3232
for (sc <- scalaCompilers; dir <- dirs) yield new TestCase(dir, sc, javaCompiler)
3333
}
3434

3535
final case class Conf(scalaVersions: List[String], dirs: List[Directory])
3636

3737
@tailrec private def go(argv: List[String], conf: Conf): Conf = argv match {
38-
case "-213" :: xs => go(xs, conf.copy(scalaVersions = conf.scalaVersions :+ scala213))
39-
case "-212" :: xs => go(xs, conf.copy(scalaVersions = conf.scalaVersions :+ scala212))
40-
case "-211" :: xs => go(xs, conf.copy(scalaVersions = conf.scalaVersions :+ scala211))
41-
case "--scala-version" :: sv :: xs => go(xs, conf.copy(scalaVersions = conf.scalaVersions :+ sv))
42-
case "--cross" :: xs => go(xs, conf.copy(scalaVersions = List(scala213, scala212, scala211)))
43-
case s :: xs => go(xs, conf.copy(dirs = conf.dirs :+ Directory(s"functional-tests/src/test/$s")))
38+
case "-213" :: xs => go(xs, conf.copy(scalaVersions = scala213 :: conf.scalaVersions))
39+
case "-212" :: xs => go(xs, conf.copy(scalaVersions = scala212 :: conf.scalaVersions))
40+
case "-211" :: xs => go(xs, conf.copy(scalaVersions = scala211 :: conf.scalaVersions))
41+
case "--scala-version" :: sv :: xs => go(xs, conf.copy(scalaVersions = sv :: conf.scalaVersions))
42+
case "--cross" :: xs => go(xs, conf.copy(scalaVersions = List(scala211, scala212, scala213)))
43+
case s :: xs => go(xs, conf.copy(dirs = testDir(s) ::: conf.dirs))
4444
case Nil => conf
4545
}
46+
47+
def testDir(s: String) = {
48+
val base = Directory("functional-tests/src/test")
49+
base.dirs.find(_.name == s) match {
50+
case Some(d) => List(d)
51+
case _ => base.dirs.filter(_.name.contains(s)).toList.sortBy(_.path) match {
52+
case Nil => sys.error(s"No such directory: ${base / s}")
53+
case dirs => dirs
54+
}
55+
}
56+
}
4657
}
4758

4859
final class TestCase(val baseDir: Directory, val scalaCompiler: ScalaCompiler, val javaCompiler: JavaCompiler) {
@@ -110,4 +121,6 @@ final class TestCase(val baseDir: Directory, val scalaCompiler: ScalaCompiler, v
110121
case _ => p
111122
}
112123
}
124+
125+
override def toString = s"TestCase(baseDir=${baseDir.name}, scalaVersion=${scalaCompiler.version})"
113126
}

functional-tests/src/main/scala/com/typesafe/tools/mima/lib/TestPrinter.scala

Lines changed: 0 additions & 21 deletions
This file was deleted.

functional-tests/src/main/scala/com/typesafe/tools/mima/lib/UnitTests.scala

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import scala.reflect.io.Path
44
import scala.util.{ Failure, Success, Try }
55

66
object UnitTests {
7-
def main(args: Array[String]): Unit = {
8-
TestCase.testAll(args.toList) { testCase =>
9-
for {
10-
() <- testNameCheck(testCase)
11-
() <- CollectProblemsTest.testCollectProblems(testCase)
12-
() <- AppRunTest.testAppRun(testCase)
13-
} yield ()
14-
}
15-
}
7+
def main(args: Array[String]): Unit = TestCase.argsToTests(args.toList, runTestCase).assert()
8+
9+
def runTestCase(testCase: TestCase) = for {
10+
() <- testNameCheck(testCase)
11+
() <- CollectProblemsTest.testCollectProblems(testCase)
12+
() <- AppRunTest.testAppRun(testCase)
13+
} yield ()
1614

1715
def testNameCheck(testCase: TestCase): Try[Unit] = {
1816
val emptyProblemsTxt = blankFile(testCase.baseDir / "problems.txt")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.typesafe.tools.mima.lib
2+
3+
import munit._
4+
5+
class UnitTestSuite extends Suite {
6+
type TestValue = Unit
7+
8+
def munitTests() = for {
9+
test <- TestCase.argsToTests(Nil, UnitTests.runTestCase).tests
10+
} yield new GenericTest(test.label, test.action, Set.empty, Location.empty)
11+
}

0 commit comments

Comments
 (0)