Skip to content

Commit

Permalink
Run tests from Scala (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Nov 14, 2022
1 parent 988e0b9 commit 68d3ce3
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 90 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
jvm: "temurin:17"
- name: Test CLI
run: ./mill -i ci.testCli
run: ./mill -i 'tests[_].test'

publish:
needs: test
Expand Down Expand Up @@ -64,15 +64,15 @@ jobs:
- run: |
./mill -i "native[$scalaJsVersion].writeNativeImageScript" generate.sh "" && \
./generate.sh && \
./mill -i "native[$scalaJsVersion].testNative" && \
./mill -i "tests[$scalaJsVersion].test.native" && \
./mill -i "native[$scalaJsVersion].copyToArtifacts" artifacts/
if: runner.os != 'Windows'
env:
scalaJsVersion: ${{ matrix.scalaJsVersion }}
- run: |
@call ./mill.bat -i "native[%scalaJsVersion%].writeNativeImageScript" generate.bat ""
@call generate.bat
@call ./mill.bat -i "native[%scalaJsVersion%].testNative"
@call ./mill.bat -i "tests[%scalaJsVersion%].test.native"
@call ./mill.bat -i "native[%scalaJsVersion%].copyToArtifacts" artifacts/
shell: cmd
if: runner.os == 'Windows'
Expand Down Expand Up @@ -108,7 +108,7 @@ jobs:
- run: |
./mill -i "native-static[$scalaJsVersion].writeNativeImageScript" generate.sh "" && \
./generate.sh && \
./mill -i "native-static[$scalaJsVersion].testNative" && \
./mill -i "tests[$scalaJsVersion].test.nativeStatic" && \
./mill -i "native-static[$scalaJsVersion].copyToArtifacts" artifacts/
env:
scalaJsVersion: ${{ matrix.scalaJsVersion }}
Expand Down Expand Up @@ -142,7 +142,7 @@ jobs:
- run: |
./mill -i "native-mostly-static[$scalaJsVersion].writeNativeImageScript" generate.sh "" && \
./generate.sh && \
./mill -i "native-mostly-static[$scalaJsVersion].testNative" && \
./mill -i "tests[$scalaJsVersion].test.nativeStatic" && \
./mill -i "native-mostly-static[$scalaJsVersion].copyToArtifacts" artifacts/
env:
scalaJsVersion: ${{ matrix.scalaJsVersion }}
Expand Down
91 changes: 65 additions & 26 deletions build.sc
Expand Up @@ -10,6 +10,8 @@ import mill._
import mill.scalalib._
import coursier.core.Version

import java.io.File

import scala.concurrent.duration._
import scala.util.Properties.isWin

Expand Down Expand Up @@ -142,19 +144,12 @@ class ScalaJsCliNativeImage(val scalaJsVersion0: String) extends ScalaModule wit
suffix = nameSuffix
)
}

def testNative() = T.command {
val path = nativeImage().path
System.err.println(s"Testing ${path.relativeTo(os.pwd)}")
val cwd = T.dest / "workdir"
os.makeDir.all(cwd)
os.proc(bash, os.pwd / "scripts" / "test-cli.sh", path, scalaJsVersion)
.call(cwd = cwd, stdin = os.Inherit, stdout = os.Inherit)
}
}

object native extends Cross[ScalaJsCliNativeImage](scalaJsVersions: _*)

def native0 = native

def csDockerVersion = "2.1.0-M5-18-gfebf9838c"

class ScalaJsCliStaticNativeImage(scalaJsVersion0: String) extends ScalaJsCliNativeImage(scalaJsVersion0) {
Expand Down Expand Up @@ -191,6 +186,67 @@ class ScalaJsCliMostlyStaticNativeImage(scalaJsVersion0: String) extends ScalaJs
}
object `native-mostly-static` extends Cross[ScalaJsCliMostlyStaticNativeImage](scalaJsVersions: _*)

object tests extends Cross[Tests](scalaJsVersions: _*)
class Tests(val scalaJsVersion0: String) extends ScalaModule {
def scalaVersion = scala213

object test extends Tests {
def ivyDeps = super.ivyDeps() ++ Seq(
ivy"org.scalameta::munit:0.7.29",
ivy"com.lihaoyi::os-lib:0.8.1",
ivy"com.lihaoyi::pprint:0.8.0"
)
def testFramework = "munit.Framework"

private final class TestHelper(
launcherTask: T[PathRef]
) {
def test(args: String*) = {
val argsTask = T.task {
val launcher = launcherTask().path
val extraArgs = Seq(
s"-Dtest.scala-js-cli.path=$launcher",
s"-Dtest.scala-js-cli.scala-js-version=$scalaJsVersion0"
)
args ++ extraArgs
}
T.command {
testTask(argsTask, T.task(Seq.empty[String]))()
}
}
}

def test(args: String*) =
jvm(args: _*)
def jvm(args: String*) =
new TestHelper(cli(scalaJsVersion0).standaloneLauncher).test(args: _*)
def native(args: String*) =
new TestHelper(native0(scalaJsVersion0).nativeImage).test(args: _*)
def nativeStatic(args: String*) =
new TestHelper(`native-static`(scalaJsVersion0).nativeImage).test(args: _*)
def nativeMostlyStatic(args: String*) =
new TestHelper(`native-mostly-static`(scalaJsVersion0).nativeImage).test(args: _*)

private def updateRef(ref: PathRef): PathRef = {
val rawPath = ref.path.toString.replace(
File.separator + scalaJsVersion0 + File.separator,
File.separator
)
PathRef(os.Path(rawPath))
}
def sources = T.sources {
super.sources().flatMap { ref =>
Seq(updateRef(ref), ref)
}
}
def resources = T.sources {
super.resources().flatMap { ref =>
Seq(updateRef(ref), ref)
}
}
}
}

def ghOrg = "scala-cli"
def ghName = "scala-js-cli"
trait ScalaJsCliPublishModule extends PublishModule {
Expand Down Expand Up @@ -341,23 +397,6 @@ object ci extends Module {

Upload.upload("scala-cli", "scala-js-cli", ghToken, tag, dryRun = false, overwrite = overwriteAssets)(launchers: _*)
}

def testCli() = {
val tasks = scalaJsVersions.map { scalaJsVer =>
cli(scalaJsVer).standaloneLauncher.map((scalaJsVer, _))
}
T.command {
val workDir = T.dest
val launchers = T.sequence(tasks)()
for ((scalaJsVer, launcher) <- launchers) {
System.err.println(s"Testing Scala.JS $scalaJsVer")
val cwd = workDir / scalaJsVer
os.makeDir.all(cwd)
os.proc(bash, os.pwd / "scripts" / "test-cli.sh", launcher.path, scalaJsVer)
.call(cwd = cwd, stdin = os.Inherit, stdout = os.Inherit)
}
}
}
}

private def bash =
Expand Down
59 changes: 0 additions & 59 deletions scripts/test-cli.sh

This file was deleted.

86 changes: 86 additions & 0 deletions tests/test/src/org/scalajs/cli/tests/Tests.scala
@@ -0,0 +1,86 @@
package org.scalajs.cli.tests

class Tests extends munit.FunSuite {

val launcher = sys.props.getOrElse(
"test.scala-js-cli.path",
sys.error("test.scala-js-cli.path Java property not set")
)
val scalaJsVersion = sys.props.getOrElse(
"test.scala-js-cli.scala-js-version",
sys.error("test.scala-js-cli.scala-js-version Java property not set")
)

test("tests") {
val dir = os.temp.dir()
os.write(
dir / "foo.scala",
"""object Foo {
| def main(args: Array[String]): Unit = {
| println(s"asdf ${1 + 1}")
| new A
| }
|
| class A
|}
|""".stripMargin
)

os.makeDir.all(dir / "bin")
os.proc(
"cs",
"launch",
"scalac:2.13.6",
"--",
"-classpath",
os.proc("cs", "fetch", "--intransitive", s"org.scala-js::scalajs-library:$scalaJsVersion").call(cwd = dir).out.trim(),
s"-Xplugin:${os.proc("cs", "fetch", "--intransitive", s"org.scala-js:scalajs-compiler_2.13.6:$scalaJsVersion").call(cwd = dir).out.trim()}",
"-d", "bin", "foo.scala"
).call(cwd = dir, stdin = os.Inherit, stdout = os.Inherit)

val res = os.proc(
launcher,
"--stdlib",
os.proc("cs", "fetch", "--intransitive", s"org.scala-js::scalajs-library:$scalaJsVersion").call(cwd = dir).out.trim(),
"-s", "-o", "test.js", "-mm", "Foo.main", "bin"
).call(cwd = dir, stderr = os.Pipe)
val expectedInOutput = "Warning: using a single file as output (--output) is deprecated since Scala.js 1.3.0. Use --outputDir instead."
assert(res.err.text().contains(expectedInOutput))

val testJsSize = os.size(dir / "test.js")
val testJsMapSize = os.size(dir / "test.js.map")
assert(testJsSize > 0)
assert(testJsMapSize > 0)

val runRes = os.proc("node", "test.js").call(cwd = dir)
val runOutput = runRes.out.trim()
assert(runOutput == "asdf 2")

os.makeDir.all(dir / "test-output")
os.proc(
launcher,
"--stdlib",
os.proc("cs", "fetch", "--intransitive", s"org.scala-js::scalajs-library:$scalaJsVersion").call(cwd = dir).out.trim(),
"-s",
"--outputDir",
"test-output",
"--moduleSplitStyle",
"SmallestModules",
"--moduleKind",
"CommonJSModule",
"-mm",
"Foo.main",
"bin"
).call(cwd = dir, stdin = os.Inherit, stdout = os.Inherit)

val jsFileCount = os.list(dir / "test-output").count { p =>
p.last.endsWith(".js") && os.isFile(p)
}
assert(jsFileCount > 1)

val splitRunRes = os.proc("node", "test-output/main.js")
.call(cwd = dir)
val splitRunOutput =splitRunRes.out.trim()
assert(splitRunOutput == "asdf 2")
}
}

0 comments on commit 68d3ce3

Please sign in to comment.