Skip to content

Commit 68d3ce3

Browse files
Run tests from Scala (#20)
1 parent 988e0b9 commit 68d3ce3

File tree

4 files changed

+156
-90
lines changed

4 files changed

+156
-90
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
with:
2222
jvm: "temurin:17"
2323
- name: Test CLI
24-
run: ./mill -i ci.testCli
24+
run: ./mill -i 'tests[_].test'
2525

2626
publish:
2727
needs: test
@@ -64,15 +64,15 @@ jobs:
6464
- run: |
6565
./mill -i "native[$scalaJsVersion].writeNativeImageScript" generate.sh "" && \
6666
./generate.sh && \
67-
./mill -i "native[$scalaJsVersion].testNative" && \
67+
./mill -i "tests[$scalaJsVersion].test.native" && \
6868
./mill -i "native[$scalaJsVersion].copyToArtifacts" artifacts/
6969
if: runner.os != 'Windows'
7070
env:
7171
scalaJsVersion: ${{ matrix.scalaJsVersion }}
7272
- run: |
7373
@call ./mill.bat -i "native[%scalaJsVersion%].writeNativeImageScript" generate.bat ""
7474
@call generate.bat
75-
@call ./mill.bat -i "native[%scalaJsVersion%].testNative"
75+
@call ./mill.bat -i "tests[%scalaJsVersion%].test.native"
7676
@call ./mill.bat -i "native[%scalaJsVersion%].copyToArtifacts" artifacts/
7777
shell: cmd
7878
if: runner.os == 'Windows'
@@ -108,7 +108,7 @@ jobs:
108108
- run: |
109109
./mill -i "native-static[$scalaJsVersion].writeNativeImageScript" generate.sh "" && \
110110
./generate.sh && \
111-
./mill -i "native-static[$scalaJsVersion].testNative" && \
111+
./mill -i "tests[$scalaJsVersion].test.nativeStatic" && \
112112
./mill -i "native-static[$scalaJsVersion].copyToArtifacts" artifacts/
113113
env:
114114
scalaJsVersion: ${{ matrix.scalaJsVersion }}
@@ -142,7 +142,7 @@ jobs:
142142
- run: |
143143
./mill -i "native-mostly-static[$scalaJsVersion].writeNativeImageScript" generate.sh "" && \
144144
./generate.sh && \
145-
./mill -i "native-mostly-static[$scalaJsVersion].testNative" && \
145+
./mill -i "tests[$scalaJsVersion].test.nativeStatic" && \
146146
./mill -i "native-mostly-static[$scalaJsVersion].copyToArtifacts" artifacts/
147147
env:
148148
scalaJsVersion: ${{ matrix.scalaJsVersion }}

build.sc

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import mill._
1010
import mill.scalalib._
1111
import coursier.core.Version
1212

13+
import java.io.File
14+
1315
import scala.concurrent.duration._
1416
import scala.util.Properties.isWin
1517

@@ -142,19 +144,12 @@ class ScalaJsCliNativeImage(val scalaJsVersion0: String) extends ScalaModule wit
142144
suffix = nameSuffix
143145
)
144146
}
145-
146-
def testNative() = T.command {
147-
val path = nativeImage().path
148-
System.err.println(s"Testing ${path.relativeTo(os.pwd)}")
149-
val cwd = T.dest / "workdir"
150-
os.makeDir.all(cwd)
151-
os.proc(bash, os.pwd / "scripts" / "test-cli.sh", path, scalaJsVersion)
152-
.call(cwd = cwd, stdin = os.Inherit, stdout = os.Inherit)
153-
}
154147
}
155148

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

151+
def native0 = native
152+
158153
def csDockerVersion = "2.1.0-M5-18-gfebf9838c"
159154

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

189+
object tests extends Cross[Tests](scalaJsVersions: _*)
190+
class Tests(val scalaJsVersion0: String) extends ScalaModule {
191+
def scalaVersion = scala213
192+
193+
object test extends Tests {
194+
def ivyDeps = super.ivyDeps() ++ Seq(
195+
ivy"org.scalameta::munit:0.7.29",
196+
ivy"com.lihaoyi::os-lib:0.8.1",
197+
ivy"com.lihaoyi::pprint:0.8.0"
198+
)
199+
def testFramework = "munit.Framework"
200+
201+
private final class TestHelper(
202+
launcherTask: T[PathRef]
203+
) {
204+
def test(args: String*) = {
205+
val argsTask = T.task {
206+
val launcher = launcherTask().path
207+
val extraArgs = Seq(
208+
s"-Dtest.scala-js-cli.path=$launcher",
209+
s"-Dtest.scala-js-cli.scala-js-version=$scalaJsVersion0"
210+
)
211+
args ++ extraArgs
212+
}
213+
T.command {
214+
testTask(argsTask, T.task(Seq.empty[String]))()
215+
}
216+
}
217+
}
218+
219+
def test(args: String*) =
220+
jvm(args: _*)
221+
def jvm(args: String*) =
222+
new TestHelper(cli(scalaJsVersion0).standaloneLauncher).test(args: _*)
223+
def native(args: String*) =
224+
new TestHelper(native0(scalaJsVersion0).nativeImage).test(args: _*)
225+
def nativeStatic(args: String*) =
226+
new TestHelper(`native-static`(scalaJsVersion0).nativeImage).test(args: _*)
227+
def nativeMostlyStatic(args: String*) =
228+
new TestHelper(`native-mostly-static`(scalaJsVersion0).nativeImage).test(args: _*)
229+
230+
private def updateRef(ref: PathRef): PathRef = {
231+
val rawPath = ref.path.toString.replace(
232+
File.separator + scalaJsVersion0 + File.separator,
233+
File.separator
234+
)
235+
PathRef(os.Path(rawPath))
236+
}
237+
def sources = T.sources {
238+
super.sources().flatMap { ref =>
239+
Seq(updateRef(ref), ref)
240+
}
241+
}
242+
def resources = T.sources {
243+
super.resources().flatMap { ref =>
244+
Seq(updateRef(ref), ref)
245+
}
246+
}
247+
}
248+
}
249+
194250
def ghOrg = "scala-cli"
195251
def ghName = "scala-js-cli"
196252
trait ScalaJsCliPublishModule extends PublishModule {
@@ -341,23 +397,6 @@ object ci extends Module {
341397

342398
Upload.upload("scala-cli", "scala-js-cli", ghToken, tag, dryRun = false, overwrite = overwriteAssets)(launchers: _*)
343399
}
344-
345-
def testCli() = {
346-
val tasks = scalaJsVersions.map { scalaJsVer =>
347-
cli(scalaJsVer).standaloneLauncher.map((scalaJsVer, _))
348-
}
349-
T.command {
350-
val workDir = T.dest
351-
val launchers = T.sequence(tasks)()
352-
for ((scalaJsVer, launcher) <- launchers) {
353-
System.err.println(s"Testing Scala.JS $scalaJsVer")
354-
val cwd = workDir / scalaJsVer
355-
os.makeDir.all(cwd)
356-
os.proc(bash, os.pwd / "scripts" / "test-cli.sh", launcher.path, scalaJsVer)
357-
.call(cwd = cwd, stdin = os.Inherit, stdout = os.Inherit)
358-
}
359-
}
360-
}
361400
}
362401

363402
private def bash =

scripts/test-cli.sh

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.scalajs.cli.tests
2+
3+
class Tests extends munit.FunSuite {
4+
5+
val launcher = sys.props.getOrElse(
6+
"test.scala-js-cli.path",
7+
sys.error("test.scala-js-cli.path Java property not set")
8+
)
9+
val scalaJsVersion = sys.props.getOrElse(
10+
"test.scala-js-cli.scala-js-version",
11+
sys.error("test.scala-js-cli.scala-js-version Java property not set")
12+
)
13+
14+
test("tests") {
15+
val dir = os.temp.dir()
16+
os.write(
17+
dir / "foo.scala",
18+
"""object Foo {
19+
| def main(args: Array[String]): Unit = {
20+
| println(s"asdf ${1 + 1}")
21+
| new A
22+
| }
23+
|
24+
| class A
25+
|}
26+
|""".stripMargin
27+
)
28+
29+
os.makeDir.all(dir / "bin")
30+
os.proc(
31+
"cs",
32+
"launch",
33+
"scalac:2.13.6",
34+
"--",
35+
"-classpath",
36+
os.proc("cs", "fetch", "--intransitive", s"org.scala-js::scalajs-library:$scalaJsVersion").call(cwd = dir).out.trim(),
37+
s"-Xplugin:${os.proc("cs", "fetch", "--intransitive", s"org.scala-js:scalajs-compiler_2.13.6:$scalaJsVersion").call(cwd = dir).out.trim()}",
38+
"-d", "bin", "foo.scala"
39+
).call(cwd = dir, stdin = os.Inherit, stdout = os.Inherit)
40+
41+
val res = os.proc(
42+
launcher,
43+
"--stdlib",
44+
os.proc("cs", "fetch", "--intransitive", s"org.scala-js::scalajs-library:$scalaJsVersion").call(cwd = dir).out.trim(),
45+
"-s", "-o", "test.js", "-mm", "Foo.main", "bin"
46+
).call(cwd = dir, stderr = os.Pipe)
47+
val expectedInOutput = "Warning: using a single file as output (--output) is deprecated since Scala.js 1.3.0. Use --outputDir instead."
48+
assert(res.err.text().contains(expectedInOutput))
49+
50+
val testJsSize = os.size(dir / "test.js")
51+
val testJsMapSize = os.size(dir / "test.js.map")
52+
assert(testJsSize > 0)
53+
assert(testJsMapSize > 0)
54+
55+
val runRes = os.proc("node", "test.js").call(cwd = dir)
56+
val runOutput = runRes.out.trim()
57+
assert(runOutput == "asdf 2")
58+
59+
os.makeDir.all(dir / "test-output")
60+
os.proc(
61+
launcher,
62+
"--stdlib",
63+
os.proc("cs", "fetch", "--intransitive", s"org.scala-js::scalajs-library:$scalaJsVersion").call(cwd = dir).out.trim(),
64+
"-s",
65+
"--outputDir",
66+
"test-output",
67+
"--moduleSplitStyle",
68+
"SmallestModules",
69+
"--moduleKind",
70+
"CommonJSModule",
71+
"-mm",
72+
"Foo.main",
73+
"bin"
74+
).call(cwd = dir, stdin = os.Inherit, stdout = os.Inherit)
75+
76+
val jsFileCount = os.list(dir / "test-output").count { p =>
77+
p.last.endsWith(".js") && os.isFile(p)
78+
}
79+
assert(jsFileCount > 1)
80+
81+
val splitRunRes = os.proc("node", "test-output/main.js")
82+
.call(cwd = dir)
83+
val splitRunOutput =splitRunRes.out.trim()
84+
assert(splitRunOutput == "asdf 2")
85+
}
86+
}

0 commit comments

Comments
 (0)