diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d40e45b..e24fcb3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -64,7 +64,7 @@ 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: @@ -72,7 +72,7 @@ jobs: - 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' @@ -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 }} @@ -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 }} diff --git a/build.sc b/build.sc index f056213..b536c25 100644 --- a/build.sc +++ b/build.sc @@ -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 @@ -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) { @@ -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 { @@ -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 = diff --git a/scripts/test-cli.sh b/scripts/test-cli.sh deleted file mode 100755 index efac67d..0000000 --- a/scripts/test-cli.sh +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env bash -set -ev - -launcher="$1" -scalaJsVersion="$2" - -if [ "$launcher" == "" -o "$scalaJsVersion" == "" ]; then - echo "Usage: $0 launcher scala-js-version" 1>&2 - exit 1 -fi - -echo "Using launcher $launcher, Scala.JS $scalaJsVersion" - -fail() { - echo "$1" >&2 - exit 2 -} - -# Actual test. -mkdir bin -cat > foo.scala <<'EOF' -object Foo { - def main(args: Array[String]): Unit = { - println(s"asdf ${1 + 1}") - new A - } - - class A -} -EOF - -cs launch scalac:2.13.6 -- \ - -classpath "$(cs fetch --intransitive org.scala-js::scalajs-library:$scalaJsVersion)" \ - -Xplugin:"$(cs fetch --intransitive org.scala-js:scalajs-compiler_2.13.6:$scalaJsVersion)" \ - -d bin foo.scala - -"$launcher" --stdlib "$(cs fetch --intransitive org.scala-js::scalajs-library:$scalaJsVersion)" -s -o test.js -mm Foo.main bin 2> test_stderr.txt || cat test_stderr.txt -grep -Fxq "Warning: using a single file as output (--output) is deprecated since Scala.js 1.3.0. Use --outputDir instead." test_stderr.txt \ - || fail "expected warning. Got: $(cat test_stderr.txt)" -test -s test.js || fail "scalajsld: empty output" -test -s test.js.map || fail "scalajsld: empty source map" - -node test.js > got-legacy.run -cat > want-legacy.run < got.run -cat > want.run < 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") + } +}