Skip to content

Commit a57e90c

Browse files
committed
Run JDK tests for both the Scala CLI launchers and coursier Scala installation's scala script
1 parent c1bd77a commit a57e90c

File tree

5 files changed

+87
-49
lines changed

5 files changed

+87
-49
lines changed

build.sc

+1
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,7 @@ trait CliIntegration extends SbtModule with ScalaCliPublishModule with HasTests
10401040
| def scalaSnapshot213 = "${TestDeps.scalaSnapshot213}"
10411041
| def scala3LtsPrefix = "${Scala.scala3LtsPrefix}"
10421042
| def scala3Lts = "${Scala.scala3Lts}"
1043+
| def scala3NextPrefix = "${Scala.scala3NextPrefix}"
10431044
| def scala3NextRc = "${Scala.scala3NextRc}"
10441045
| def scala3NextRcAnnounced = "${Scala.scala3NextRcAnnounced}"
10451046
| def scala3Next = "${Scala.scala3Next}"

modules/integration/src/test/scala/scala/cli/integration/CoursierScalaInstallationTestHelper.scala

+21-18
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import scala.util.Properties
1010
trait CoursierScalaInstallationTestHelper {
1111
def withScalaRunnerWrapper(
1212
root: os.Path,
13-
localCache: os.Path,
1413
localBin: os.Path,
15-
scalaVersion: String
14+
scalaVersion: String,
15+
localCache: Option[os.Path] = None,
16+
shouldCleanUp: Boolean = true
1617
)(f: os.Path => Unit): Unit = {
18+
val localCacheArgs = localCache.fold(Seq.empty[String])(c => Seq("--cache", c.toString))
1719
os.proc(
1820
TestUtil.cs,
1921
"install",
20-
"--cache",
21-
localCache,
22+
localCacheArgs,
2223
"--install-dir",
2324
localBin,
2425
s"scala:$scalaVersion"
@@ -74,21 +75,23 @@ trait CoursierScalaInstallationTestHelper {
7475
.call(cwd = root).out.trim()
7576
expect(wrapperVersion == cliVersion)
7677
f(launchScalaPath)
77-
// clean up cs local binaries
78-
val csPrebuiltBinaryDir =
79-
os.Path(underlyingScriptPath.toString().substring(
80-
0,
81-
underlyingScriptPath.toString().indexOf(scalaVersion) + scalaVersion.length
82-
))
83-
System.err.println(s"Cleaning up, trying to remove $csPrebuiltBinaryDir")
84-
try {
85-
os.remove.all(csPrebuiltBinaryDir)
78+
if (shouldCleanUp) {
79+
// clean up cs local binaries
80+
val csPrebuiltBinaryDir =
81+
os.Path(underlyingScriptPath.toString().substring(
82+
0,
83+
underlyingScriptPath.toString().indexOf(scalaVersion) + scalaVersion.length
84+
))
85+
System.err.println(s"Cleaning up, trying to remove $csPrebuiltBinaryDir")
86+
try {
87+
os.remove.all(csPrebuiltBinaryDir)
8688

87-
System.err.println(s"Cleanup complete. Removed $csPrebuiltBinaryDir")
88-
}
89-
catch {
90-
case ex: java.nio.file.FileSystemException =>
91-
System.err.println(s"Failed to remove $csPrebuiltBinaryDir: $ex")
89+
System.err.println(s"Cleanup complete. Removed $csPrebuiltBinaryDir")
90+
}
91+
catch {
92+
case ex: java.nio.file.FileSystemException =>
93+
System.err.println(s"Failed to remove $csPrebuiltBinaryDir: $ex")
94+
}
9295
}
9396
}
9497
}

modules/integration/src/test/scala/scala/cli/integration/RunJdkTestDefinitions.scala

+61-28
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,33 @@ trait RunJdkTestDefinitions { _: RunTestDefinitions =>
1111
s"zulu:$javaVersion"
1212
else javaVersion.toString
1313

14+
def canUseScalaInstallationWrapper: Boolean =
15+
actualScalaVersion.startsWith("3") && actualScalaVersion.split('.').drop(1).head.toInt >= 5
16+
1417
for {
1518
javaVersion <- Constants.allJavaVersions
1619
index = javaIndex(javaVersion)
20+
useScalaInstallationWrapper <-
21+
if (canUseScalaInstallationWrapper) Seq(false, true) else Seq(false)
22+
launcherString = if (useScalaInstallationWrapper) "coursier scala installation" else "Scala CLI"
23+
scalaRunnerWrapperVersion = actualScalaVersion match {
24+
case v if v == Constants.scala3NextRc => Constants.scala3NextRcAnnounced
25+
case v if v == Constants.scala3Next => Constants.scala3NextAnnounced
26+
case v => v
27+
}
28+
withLauncher = (root: os.Path) =>
29+
(f: Seq[os.Shellable] => Unit) =>
30+
if (useScalaInstallationWrapper)
31+
withScalaRunnerWrapper(
32+
root = root,
33+
localBin = root / "local-bin",
34+
scalaVersion = scalaRunnerWrapperVersion,
35+
shouldCleanUp = false
36+
)(launcher => f(Seq(launcher)))
37+
else
38+
f(Seq(TestUtil.cli))
1739
} {
18-
test(s"correct JVM is picked up when JAVA_HOME set to $index") {
40+
test(s"correct JVM is picked up by $launcherString when JAVA_HOME set to $index") {
1941
TestUtil.retryOnCi() {
2042
TestInputs(
2143
os.rel / "check_java_home.sc" ->
@@ -30,25 +52,33 @@ trait RunJdkTestDefinitions { _: RunTestDefinitions =>
3052
os.proc(TestUtil.cs, "java-home", "--jvm", index).call().out.trim(),
3153
os.pwd
3254
)
33-
val res = os.proc(TestUtil.cli, "run", ".", extraOptions)
55+
withLauncher(root) { launcher =>
56+
val res = os.proc(launcher, "run", ".", extraOptions)
3457
.call(cwd = root, env = Map("JAVA_HOME" -> javaHome.toString))
3558
expect(res.out.trim().contains(javaHome.toString))
59+
}
3660
}
3761
}
3862
}
3963

40-
test(s"hello world with --jvm $index") {
41-
val expectedMessage = "Hello, world!"
42-
TestInputs(
43-
os.rel / "hello_world.sc" -> s"println(\"$expectedMessage\")"
44-
).fromRoot { root =>
45-
val res = os.proc(TestUtil.cli, "run", ".", extraOptions, "--jvm", javaVersion)
46-
.call(cwd = root)
47-
expect(res.out.trim() == expectedMessage)
64+
test(s"hello world with $launcherString and --jvm $index") {
65+
TestUtil.retryOnCi() {
66+
val expectedMessage = "Hello, world!"
67+
TestInputs(
68+
os.rel / "hello_world.sc" -> s"println(\"$expectedMessage\")"
69+
).fromRoot { root =>
70+
withLauncher(root) { launcher =>
71+
val res = os.proc(launcher, "run", ".", extraOptions, "--jvm", javaVersion)
72+
.call(cwd = root)
73+
expect(res.out.trim() == expectedMessage)
74+
}
75+
}
4876
}
4977
}
5078

51-
test(s"correct JVM is picked up when Java $index is passed with --java-home") {
79+
test(
80+
s"correct JVM is picked up by $launcherString when Java $index is passed with --java-home"
81+
) {
5282
TestUtil.retryOnCi() {
5383
TestInputs(
5484
os.rel / "check_java_home.sc" ->
@@ -63,34 +93,37 @@ trait RunJdkTestDefinitions { _: RunTestDefinitions =>
6393
os.proc(TestUtil.cs, "java-home", "--jvm", index).call().out.trim(),
6494
os.pwd
6595
)
66-
val res =
67-
os.proc(TestUtil.cli, "run", ".", extraOptions, "--java-home", javaHome.toString)
96+
withLauncher(root) { launcher =>
97+
val res = os.proc(launcher, "run", ".", extraOptions, "--java-home", javaHome.toString)
6898
.call(cwd = root)
69-
expect(res.out.trim().contains(javaHome.toString))
99+
expect(res.out.trim().contains(javaHome.toString))
100+
}
70101
}
71102
}
72103
}
73104

74105
if (javaVersion >= Constants.bloopMinimumJvmVersion)
75-
test(s"Bloop runs correctly on JVM $index") {
106+
test(s"Bloop runs correctly with $launcherString on JVM $index") {
76107
TestUtil.retryOnCi() {
77108
val expectedMessage = "Hello, world!"
78109
TestInputs(os.rel / "check_java_home.sc" -> s"""println("$expectedMessage")""")
79110
.fromRoot { root =>
80111
os.proc(TestUtil.cli, "bloop", "exit", "--power").call(cwd = root)
81-
val res = os.proc(
82-
TestUtil.cli,
83-
"run",
84-
".",
85-
extraOptions,
86-
"--bloop-jvm",
87-
index,
88-
"--jvm",
89-
index
90-
)
91-
.call(cwd = root, stderr = os.Pipe)
92-
expect(res.err.trim().contains(javaVersion.toString))
93-
expect(res.out.trim() == expectedMessage)
112+
withLauncher(root) { launcher =>
113+
val res = os.proc(
114+
launcher,
115+
"run",
116+
".",
117+
extraOptions,
118+
"--bloop-jvm",
119+
index,
120+
"--jvm",
121+
index
122+
)
123+
.call(cwd = root, stderr = os.Pipe)
124+
expect(res.err.trim().contains(javaVersion.toString))
125+
expect(res.out.trim() == expectedMessage)
126+
}
94127
}
95128
}
96129
}

modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ abstract class RunTestDefinitions
2222
with RunSnippetTestDefinitions
2323
with RunScalaPyTestDefinitions
2424
with RunZipTestDefinitions
25-
with RunJdkTestDefinitions { _: TestScalaVersion =>
25+
with RunJdkTestDefinitions
26+
with CoursierScalaInstallationTestHelper { _: TestScalaVersion =>
2627
protected lazy val extraOptions: Seq[String] = scalaVersionArgs ++ TestUtil.extraOptions
2728
protected val emptyInputs: TestInputs = TestInputs(os.rel / ".placeholder" -> "")
2829

modules/integration/src/test/scala/scala/cli/integration/SipScalaTests.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,9 @@ class SipScalaTests extends ScalaCliSuite
842842
val scalaVersion = Constants.scala3NextRcAnnounced
843843
withScalaRunnerWrapper(
844844
root = root,
845-
localCache = localCache,
846845
localBin = localBin,
847-
scalaVersion = scalaVersion
846+
scalaVersion = scalaVersion,
847+
localCache = Some(localCache)
848848
) { launchScalaPath =>
849849
val r =
850850
os.proc(

0 commit comments

Comments
 (0)