|
| 1 | +//> using toolkit 0.2.1 |
| 2 | + |
| 3 | +import scala.util.Try |
| 4 | +import scala.Console.* |
| 5 | +import scala.concurrent.duration.Duration |
| 6 | + |
| 7 | +class CrossPlatformTests extends munit.FunSuite: |
| 8 | + requireCmd("scala-cli") |
| 9 | + test("jvm")(publishAndRun("jvm", "1.0.0-SNAPSHOT")) |
| 10 | + test("js")(publishAndRun("js", "1.0.0-SNAPSHOT", "--js")) |
| 11 | + test("native")(publishAndRun("native", "1.0.0-SNAPSHOT", "--native", "--native-version", "0.4.17")) |
| 12 | + |
| 13 | + override val munitTimeout = Duration(90, "s") |
| 14 | + |
| 15 | + private def publishAndRun(platform: String, version: String, extraOpts: String*): Unit = |
| 16 | + val toolkitFile = if platform == "js" then os.pwd / "Toolkit.js.scala" else os.pwd / "Toolkit.scala" |
| 17 | + val toolkitTestFile = os.pwd / "ToolkitTest.scala" |
| 18 | + publish(toolkitFile, version, extraOpts) |
| 19 | + publish(toolkitTestFile, version, extraOpts ++ Seq("--dependency", s"org.scala-lang::toolkit::$version")) |
| 20 | + (listTests("shared") ++ listTests(platform)).foreach(runTest(platform, _, version, extraOpts*)) |
| 21 | + |
| 22 | + private def publish(file: os.Path, version: String, extraOpts: Seq[String]): Unit = |
| 23 | + os.proc( |
| 24 | + Seq("scala-cli", "--power", "publish", "local", "--cross"), |
| 25 | + extraOpts, |
| 26 | + Seq("--organization", "org.scala-lang"), |
| 27 | + Seq("--project-version", version), |
| 28 | + file |
| 29 | + ).call(stderr = os.Pipe) // mute warnings |
| 30 | + |
| 31 | + private def listTests(folderName: String): Seq[os.Path] = |
| 32 | + os.list(os.pwd / "tests" / folderName).filter(_.ext == "sc") |
| 33 | + |
| 34 | + private def runTest(platform: String, testFile: os.Path, toolkitVersion: String, extraOpts: String*): Unit = |
| 35 | + val testName = s"$platform.${testFile.last}" |
| 36 | + val expectedOutput = getExpectedOutput(testFile) |
| 37 | + println(s"Running $testName") |
| 38 | + |
| 39 | + val testProcess = os.proc("scala-cli", "run", "--toolkit", toolkitVersion, extraOpts, testFile).call(check = false) |
| 40 | + val output = testProcess.out.lines().map(_.trim) |
| 41 | + |
| 42 | + if testProcess.exitCode != 0 then |
| 43 | + println(s"${RED}$testName failed$RESET") |
| 44 | + else if output != expectedOutput then |
| 45 | + println(s"${RED}$testName failed$RESET") |
| 46 | + println(s"${RED} Expected output: $expectedOutput$RESET") |
| 47 | + println(s"${RED} Output: ${output}$RESET") |
| 48 | + end runTest |
| 49 | + |
| 50 | + private def getExpectedOutput(test: os.Path): Seq[String] = |
| 51 | + val OutputLine = "(.*)//\\$ (.*)".r |
| 52 | + os.read(test).linesIterator.collect { case OutputLine(_, content) => content.trim }.toSeq |
| 53 | + |
| 54 | + private def requireCmd(cmd: String): Unit = |
| 55 | + if os.proc("which", cmd).call(check = false).exitCode != 0 then |
| 56 | + println(s"${RED}Please install $cmd$RESET") |
| 57 | + sys.exit(1) |
0 commit comments