Skip to content

Commit

Permalink
Run JDK tests for both the Scala CLI launchers and coursier Scala i…
Browse files Browse the repository at this point in the history
…nstallation's `scala` script
  • Loading branch information
Gedochao committed Dec 19, 2024
1 parent c1bd77a commit b0d4619
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 48 deletions.
1 change: 1 addition & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,7 @@ trait CliIntegration extends SbtModule with ScalaCliPublishModule with HasTests
| def scalaSnapshot213 = "${TestDeps.scalaSnapshot213}"
| def scala3LtsPrefix = "${Scala.scala3LtsPrefix}"
| def scala3Lts = "${Scala.scala3Lts}"
| def scala3NextPrefix = "${Scala.scala3NextPrefix}"
| def scala3NextRc = "${Scala.scala3NextRc}"
| def scala3NextRcAnnounced = "${Scala.scala3NextRcAnnounced}"
| def scala3Next = "${Scala.scala3Next}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import scala.util.Properties
trait CoursierScalaInstallationTestHelper {
def withScalaRunnerWrapper(
root: os.Path,
localCache: os.Path,
localBin: os.Path,
scalaVersion: String
scalaVersion: String,
localCache: Option[os.Path] = None,
shouldCleanUp: Boolean = true
)(f: os.Path => Unit): Unit = {
val localCacheArgs = localCache.fold(Seq.empty[String])(c => Seq("--cache", c.toString))
os.proc(
TestUtil.cs,
"install",
"--cache",
localCache,
localCacheArgs,
"--install-dir",
localBin,
s"scala:$scalaVersion"
Expand Down Expand Up @@ -74,21 +75,23 @@ trait CoursierScalaInstallationTestHelper {
.call(cwd = root).out.trim()
expect(wrapperVersion == cliVersion)
f(launchScalaPath)
// clean up cs local binaries
val csPrebuiltBinaryDir =
os.Path(underlyingScriptPath.toString().substring(
0,
underlyingScriptPath.toString().indexOf(scalaVersion) + scalaVersion.length
))
System.err.println(s"Cleaning up, trying to remove $csPrebuiltBinaryDir")
try {
os.remove.all(csPrebuiltBinaryDir)
if (shouldCleanUp) {
// clean up cs local binaries
val csPrebuiltBinaryDir =
os.Path(underlyingScriptPath.toString().substring(
0,
underlyingScriptPath.toString().indexOf(scalaVersion) + scalaVersion.length
))
System.err.println(s"Cleaning up, trying to remove $csPrebuiltBinaryDir")
try {
os.remove.all(csPrebuiltBinaryDir)

System.err.println(s"Cleanup complete. Removed $csPrebuiltBinaryDir")
}
catch {
case ex: java.nio.file.FileSystemException =>
System.err.println(s"Failed to remove $csPrebuiltBinaryDir: $ex")
System.err.println(s"Cleanup complete. Removed $csPrebuiltBinaryDir")
}
catch {
case ex: java.nio.file.FileSystemException =>
System.err.println(s"Failed to remove $csPrebuiltBinaryDir: $ex")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,33 @@ trait RunJdkTestDefinitions { _: RunTestDefinitions =>
s"zulu:$javaVersion"
else javaVersion.toString

def canUseScalaInstallationWrapper: Boolean =
actualScalaVersion.startsWith("3") && actualScalaVersion.split('.').drop(1).head.toInt >= 5

for {
javaVersion <- Constants.allJavaVersions
index = javaIndex(javaVersion)
useScalaInstallationWrapper <-
if (canUseScalaInstallationWrapper) Seq(false, true) else Seq(false)
launcherString = if (useScalaInstallationWrapper) "coursier scala installation" else "Scala CLI"
scalaRunnerWrapperVersion = actualScalaVersion match {
case v if v == Constants.scala3NextRc => Constants.scala3NextRcAnnounced
case v if v == Constants.scala3Next => Constants.scala3NextAnnounced
case v => v
}
withLauncher = (root: os.Path) =>
(f: Seq[os.Shellable] => Unit) =>
if (useScalaInstallationWrapper)
withScalaRunnerWrapper(
root = root,
localBin = root / "local-bin",
scalaVersion = scalaRunnerWrapperVersion,
shouldCleanUp = false
)(launcher => f(Seq(launcher)))
else
f(Seq(TestUtil.cli))
} {
test(s"correct JVM is picked up when JAVA_HOME set to $index") {
test(s"correct JVM is picked up by $launcherString when JAVA_HOME set to $index") {
TestUtil.retryOnCi() {
TestInputs(
os.rel / "check_java_home.sc" ->
Expand All @@ -30,25 +52,33 @@ trait RunJdkTestDefinitions { _: RunTestDefinitions =>
os.proc(TestUtil.cs, "java-home", "--jvm", index).call().out.trim(),
os.pwd
)
val res = os.proc(TestUtil.cli, "run", ".", extraOptions)
withLauncher(root) { launcher =>
val res = os.proc(launcher, "run", ".", extraOptions)
.call(cwd = root, env = Map("JAVA_HOME" -> javaHome.toString))
expect(res.out.trim().contains(javaHome.toString))
}
}
}
}

test(s"hello world with --jvm $index") {
val expectedMessage = "Hello, world!"
TestInputs(
os.rel / "hello_world.sc" -> s"println(\"$expectedMessage\")"
).fromRoot { root =>
val res = os.proc(TestUtil.cli, "run", ".", extraOptions, "--jvm", javaVersion)
.call(cwd = root)
expect(res.out.trim() == expectedMessage)
test(s"hello world with $launcherString and --jvm $index") {
TestUtil.retryOnCi() {
val expectedMessage = "Hello, world!"
TestInputs(
os.rel / "hello_world.sc" -> s"println(\"$expectedMessage\")"
).fromRoot { root =>
withLauncher(root) { launcher =>
val res = os.proc(launcher, "run", ".", extraOptions, "--jvm", javaVersion)
.call(cwd = root)
expect(res.out.trim() == expectedMessage)
}
}
}
}

test(s"correct JVM is picked up when Java $index is passed with --java-home") {
test(
s"correct JVM is picked up by $launcherString when Java $index is passed with --java-home"
) {
TestUtil.retryOnCi() {
TestInputs(
os.rel / "check_java_home.sc" ->
Expand All @@ -63,10 +93,11 @@ trait RunJdkTestDefinitions { _: RunTestDefinitions =>
os.proc(TestUtil.cs, "java-home", "--jvm", index).call().out.trim(),
os.pwd
)
val res =
os.proc(TestUtil.cli, "run", ".", extraOptions, "--java-home", javaHome.toString)
withLauncher(root) { launcher =>
val res = os.proc(launcher, "run", ".", extraOptions, "--java-home", javaHome.toString)
.call(cwd = root)
expect(res.out.trim().contains(javaHome.toString))
expect(res.out.trim().contains(javaHome.toString))
}
}
}
}
Expand All @@ -78,19 +109,21 @@ trait RunJdkTestDefinitions { _: RunTestDefinitions =>
TestInputs(os.rel / "check_java_home.sc" -> s"""println("$expectedMessage")""")
.fromRoot { root =>
os.proc(TestUtil.cli, "bloop", "exit", "--power").call(cwd = root)
val res = os.proc(
TestUtil.cli,
"run",
".",
extraOptions,
"--bloop-jvm",
index,
"--jvm",
index
)
.call(cwd = root, stderr = os.Pipe)
expect(res.err.trim().contains(javaVersion.toString))
expect(res.out.trim() == expectedMessage)
withLauncher(root) { launcher =>
val res = os.proc(
launcher,
"run",
".",
extraOptions,
"--bloop-jvm",
index,
"--jvm",
index
)
.call(cwd = root, stderr = os.Pipe)
expect(res.err.trim().contains(javaVersion.toString))
expect(res.out.trim() == expectedMessage)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ abstract class RunTestDefinitions
with RunSnippetTestDefinitions
with RunScalaPyTestDefinitions
with RunZipTestDefinitions
with RunJdkTestDefinitions { _: TestScalaVersion =>
with RunJdkTestDefinitions
with CoursierScalaInstallationTestHelper { _: TestScalaVersion =>
protected lazy val extraOptions: Seq[String] = scalaVersionArgs ++ TestUtil.extraOptions
protected val emptyInputs: TestInputs = TestInputs(os.rel / ".placeholder" -> "")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -842,9 +842,9 @@ class SipScalaTests extends ScalaCliSuite
val scalaVersion = Constants.scala3NextRcAnnounced
withScalaRunnerWrapper(
root = root,
localCache = localCache,
localBin = localBin,
scalaVersion = scalaVersion
scalaVersion = scalaVersion,
localCache = Some(localCache)
) { launchScalaPath =>
val r =
os.proc(
Expand Down

0 comments on commit b0d4619

Please sign in to comment.