Skip to content

Commit aca9622

Browse files
authored
Merge pull request #542 from dwijnand/bidirection
Tweak setup for forward compatibility testing
2 parents 3094ca0 + dd8b953 commit aca9622

File tree

6 files changed

+76
-38
lines changed

6 files changed

+76
-38
lines changed
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
package com.typesafe.tools.mima.lib
22

3+
import scala.reflect.io.{ Directory, Path }
34
import scala.util.{ Failure, Success, Try }
45

5-
/** Test running the App, using library v2. */
6+
/** Test running the App, using library v1 or v2. */
67
object AppRunTest {
7-
def main(args: Array[String]): Unit = TestCli.argsToTests(args.toList, testAppRun).unsafeRunTest()
8+
def testAppRun(testCase: TestCase, direction: Direction): Try[Unit] = {
9+
testAppRun1(testCase, direction.lhs(testCase), direction.rhs(testCase), direction.oracleFile)
10+
}
811

9-
def testAppRun(testCase: TestCase): Try[Unit] = for {
10-
() <- testCase.compileThem
11-
pending = testCase.versionedFile("testAppRun.pending").exists
12-
emptyPT = testCase.blankFile(testCase.versionedFile("problems.txt"))
13-
() <- testCase.runMain(testCase.outV1) // expect at least v1 to pass, even in the "pending" case
14-
() <- testCase.runMain(testCase.outV2) match {
15-
case Failure(t) if !pending && emptyPT => Failure(t)
16-
case Success(()) if !pending && !emptyPT => Failure(new Exception("expected running App to fail"))
17-
case _ => Success(())
12+
def testAppRun1(testCase: TestCase, v1: Directory, v2: Directory, oracleFile: Path): Try[Unit] = for {
13+
() <- testCase.compileBoth
14+
pending = testCase.versionedFile("testAppRun.pending").exists
15+
expectOk = testCase.blankFile(testCase.versionedFile(oracleFile))
16+
//() <- testCase.compileApp(v2) // compile app with v2
17+
//() <- testCase.runMain(v2) // sanity check 1: run app with v2
18+
() <- testCase.compileApp(v1) // recompile app with v1
19+
() <- testCase.runMain(v1) // sanity check 2: run app with v1
20+
() <- testCase.runMain(v2) match { // test: run app, compiled with v1, with v2
21+
case Failure(t) if !pending && expectOk => Failure(t)
22+
case Success(()) if !pending && !expectOk => Failure(new Exception("expected running App to fail"))
23+
case _ => Success(())
1824
}
1925
} yield ()
2026
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import scala.collection.JavaConverters._
99
import scala.util.{ Failure, Success, Try }
1010

1111
object CollectProblemsTest {
12-
def main(args: Array[String]): Unit = TestCli.argsToTests(args.toList, testCollectProblems).unsafeRunTest()
13-
14-
def testCollectProblems(testCase: TestCase): Try[Unit] = for {
15-
() <- testCase.compileThem
16-
problems = collectProblems(testCase.outV1.jfile, testCase.outV2.jfile)
17-
expected = readOracleFile(testCase.versionedFile("problems.txt").jfile)
12+
def testCollectProblems(testCase: TestCase, direction: Direction): Try[Unit] = for {
13+
() <- testCase.compileBoth
14+
problems = collectProblems(direction.lhs(testCase).jfile, direction.rhs(testCase).jfile)
15+
expected = readOracleFile(testCase.versionedFile(direction.oracleFile).jfile)
1816
() <- diffProblems(problems, expected)
1917
} yield ()
2018

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.typesafe.tools.mima.lib
2+
3+
sealed trait Direction {
4+
def lhs(testCase: TestCase) = this match {
5+
case Backwards => testCase.outV1
6+
case Forwards => testCase.outV2
7+
}
8+
def rhs(testCase: TestCase) = this match {
9+
case Backwards => testCase.outV2
10+
case Forwards => testCase.outV1
11+
}
12+
def oracleFile = this match {
13+
case Backwards => "problems.txt"
14+
case Forwards => "forwards.txt"
15+
}
16+
}
17+
case object Backwards extends Direction
18+
case object Forwards extends Direction

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.typesafe.tools.mima.lib
22

3-
import java.io.{ ByteArrayOutputStream, File, PrintStream }
3+
import java.io.{ ByteArrayOutputStream, PrintStream }
44
import java.net.{ URI, URLClassLoader }
55
import javax.tools._
66

@@ -24,16 +24,14 @@ final class TestCase(val baseDir: Directory, val scalaCompiler: ScalaCompiler, v
2424
val outV2 = (baseDir / s"target/scala-$scalaBinaryVersion/v2-classes").toDirectory
2525
val outApp = (baseDir / s"target/scala-$scalaBinaryVersion/app-classes").toDirectory
2626

27-
lazy val compileThem: Try[Unit] = for {
28-
() <- compileV1()
29-
() <- compileV2()
30-
() <- compileApp(outV1)
27+
lazy val compileBoth: Try[Unit] = for (() <- compileV1(); () <- compileV2()) yield ()
28+
def compileV1(): Try[Unit] = compileDir(srcV1, Nil, outV1)
29+
def compileV2(): Try[Unit] = compileDir(srcV2, Nil, outV2)
30+
def compileApp(outLib: Directory) = for {
31+
() <- compileBoth
32+
() <- compileDir(srcApp, List(outLib), outApp)
3133
} yield ()
3234

33-
def compileV1() = compileDir(srcV1, Nil, outV1)
34-
def compileV2() = compileDir(srcV2, Nil, outV2)
35-
def compileApp(outLib: Directory) = compileDir(srcApp, List(outLib), outApp)
36-
3735
def compileDir(srcDir: Directory, cp: List[Directory], out: Directory): Try[Unit] = for {
3836
() <- Try(recreateDir(out))
3937
() <- compileScala(srcDir, cp, out)

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ object TestCli {
1616
val testsDir = Directory("functional-tests/src/test")
1717

1818
def argsToTests(args: List[String], runTestCase: TestCase => Try[Unit]): Tests =
19-
Tests(fromArgs(args).map(testCaseToTest1(_, runTestCase)))
19+
testCasesToTests(fromArgs(args), runTestCase)
20+
21+
def testCasesToTests(testCases: List[TestCase], runTestCase: TestCase => Try[Unit]): Tests =
22+
Tests(testCases.map(testCaseToTest1(_, runTestCase)))
2023

2124
def testCaseToTest1(tc: TestCase, runTestCase: TestCase => Try[Unit]): Test1 =
2225
Test(s"${tc.scalaBinaryVersion} / ${tc.name}", runTestCase(tc))
@@ -25,7 +28,7 @@ object TestCli {
2528

2629
@tailrec def postProcessConf(conf: Conf): Conf = conf match {
2730
case Conf(Nil, _) => postProcessConf(conf.copy(scalaVersions = List(hostScalaVersion)))
28-
case Conf(_, Nil) => postProcessConf(conf.copy(dirs = allTestDirs()))
31+
case Conf(_, Nil) => postProcessConf(conf.copy(dirs = allTestDirs().ensuring(_.nonEmpty)))
2932
case _ => conf
3033
}
3134

@@ -40,7 +43,9 @@ object TestCli {
4043

4144
def getJavaCompiler = ToolProvider.getSystemJavaCompiler
4245

43-
def allTestDirs() = testsDir.dirs.filter(_.files.exists(_.name == "problems.txt")).toList.sortBy(_.path)
46+
def allTestDirs() = testsDir.dirs.filter(_.files.exists { f =>
47+
f.name == Backwards.oracleFile || f.name == Forwards.oracleFile
48+
}).toList.sortBy(_.path)
4449

4550
final case class Conf(scalaVersions: List[String], dirs: List[Directory])
4651

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.typesafe.tools.mima.lib
22

3+
import scala.reflect.io.Path
34
import scala.util.{ Failure, Success, Try }
45

56
import munit.GenericTest
@@ -9,17 +10,29 @@ object UnitTests {
910
def munitTests(): List[GenericTest[Unit]] = TestCli.argsToTests(Nil, runTestCase).munitTests
1011

1112
def runTestCase(testCase: TestCase): Try[Unit] = for {
12-
() <- testNameCheck(testCase)
13-
() <- CollectProblemsTest.testCollectProblems(testCase)
14-
() <- AppRunTest.testAppRun(testCase)
13+
() <- runTestCaseIf(testCase, Backwards)
14+
() <- runTestCaseIf(testCase, Forwards)
1515
} yield ()
1616

17-
def testNameCheck(testCase: TestCase): Try[Unit] = {
18-
val emptyProblemsTxt = testCase.blankFile(testCase.baseDir / "problems.txt")
19-
testCase.baseDir.name.takeRight(4).dropWhile(_ != '-') match {
20-
case "-ok" => if (emptyProblemsTxt) Success(()) else Failure(new Exception("OK test with non-empty problems.txt"))
21-
case "-nok" => if (emptyProblemsTxt) Failure(new Exception("NOK test with empty problems.txt")) else Success(())
22-
case _ => Failure(new Exception("Missing '-ok' or '-nok' suffix in project name"))
17+
def runTestCaseIf(testCase: TestCase, direction: Direction): Try[Unit] = {
18+
if ((testCase.baseDir / direction.oracleFile).exists)
19+
runTestCase1(testCase, direction)
20+
else
21+
Success(())
22+
}
23+
24+
def runTestCase1(testCase: TestCase, direction: Direction): Try[Unit] = for {
25+
() <- testNameCheck(testCase, direction.oracleFile)
26+
() <- CollectProblemsTest.testCollectProblems(testCase, direction)
27+
() <- AppRunTest.testAppRun(testCase, direction)
28+
} yield ()
29+
30+
def testNameCheck(testCase: TestCase, oracleFile: Path): Try[Unit] = {
31+
val emptyOracleFile = testCase.blankFile(testCase.baseDir / oracleFile)
32+
testCase.name.takeRight(4).dropWhile(_ != '-') match {
33+
case "-ok" => if (emptyOracleFile) Success(()) else Failure(new Exception(s"OK test with non-empty ${oracleFile.name}"))
34+
case "-nok" => if (emptyOracleFile) Failure(new Exception(s"NOK test with empty ${oracleFile.name}")) else Success(())
35+
case _ => Failure(new Exception(s"Missing '-ok' or '-nok' suffix in project name: ${testCase.name}"))
2336
}
2437
}
2538
}

0 commit comments

Comments
 (0)