diff --git a/build.sc b/build.sc index 1402aeddb8..7a6579d5c4 100644 --- a/build.sc +++ b/build.sc @@ -1030,12 +1030,17 @@ trait CliIntegration extends SbtModule with ScalaCliPublishModule with HasTests | |/** Build-time constants. Generated by mill. */ |object Constants { + | def allJavaVersions = Seq(${Java.allJavaVersions.sorted.mkString(", ")}) | def bspVersion = "${Deps.bsp4j.dep.version}" + | def bloopMinimumJvmVersion = ${Java.minimumBloopJava} + | def minimumInternalJvmVersion = ${Java.minimumInternalJava} + | def defaultJvmVersion = ${Java.defaultJava} | def scala212 = "${Scala.scala212}" | def scala213 = "${Scala.scala213}" | 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}" diff --git a/modules/integration/src/test/scala/scala/cli/integration/BspTestDefinitions.scala b/modules/integration/src/test/scala/scala/cli/integration/BspTestDefinitions.scala index 82c1d7b60c..c3f4bed829 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/BspTestDefinitions.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/BspTestDefinitions.scala @@ -1908,19 +1908,19 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg test("BSP respects JAVA_HOME") { TestUtil.retryOnCi() { - val javaVersion = "22" + val javaVersion = "23" val inputs = TestInputs(os.rel / "check-java.sc" -> s"""assert(System.getProperty("java.version").startsWith("$javaVersion")) |println(System.getProperty("java.home"))""".stripMargin) inputs.fromRoot { root => os.proc(TestUtil.cli, "bloop", "exit", "--power").call(cwd = root) - val java22Home = + val java23Home = os.Path( os.proc(TestUtil.cs, "java-home", "--jvm", s"zulu:$javaVersion").call().out.trim(), os.pwd ) os.proc(TestUtil.cli, "setup-ide", "check-java.sc") - .call(cwd = root, env = Map("JAVA_HOME" -> java22Home.toString())) + .call(cwd = root, env = Map("JAVA_HOME" -> java23Home.toString())) val ideOptionsPath = root / Constants.workspaceDirName / "ide-options-v2.json" expect(ideOptionsPath.toNIO.toFile.exists()) val ideEnvsPath = root / Constants.workspaceDirName / "ide-envs.json" @@ -1948,7 +1948,7 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg test("BSP respects --java-home") { TestUtil.retryOnCi() { - val javaVersion = "22" + val javaVersion = "23" val inputs = TestInputs(os.rel / "check-java.sc" -> s"""assert(System.getProperty("java.version").startsWith("$javaVersion")) |println(System.getProperty("java.home"))""".stripMargin) diff --git a/modules/integration/src/test/scala/scala/cli/integration/CoursierScalaInstallationTestHelper.scala b/modules/integration/src/test/scala/scala/cli/integration/CoursierScalaInstallationTestHelper.scala index 2eb53db2e8..0eb31e5ca3 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/CoursierScalaInstallationTestHelper.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/CoursierScalaInstallationTestHelper.scala @@ -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" @@ -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") + } } } } diff --git a/modules/integration/src/test/scala/scala/cli/integration/RunJdkTestDefinitions.scala b/modules/integration/src/test/scala/scala/cli/integration/RunJdkTestDefinitions.scala new file mode 100644 index 0000000000..bb93fbea58 --- /dev/null +++ b/modules/integration/src/test/scala/scala/cli/integration/RunJdkTestDefinitions.scala @@ -0,0 +1,133 @@ +package scala.cli.integration + +import com.eed3si9n.expecty.Expecty.expect + +import scala.util.Properties + +trait RunJdkTestDefinitions { _: RunTestDefinitions => + def javaIndex(javaVersion: Int): String = + // TODO just passing the version number on arm64 should be enough, needs a fix in cs + if (Properties.isMac && TestUtil.isM1 && (javaVersion < 11 || javaVersion == 16)) + 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 by $launcherString when JAVA_HOME set to $index") { + TestUtil.retryOnCi() { + TestInputs( + os.rel / "check_java_home.sc" -> + s"""assert( + | System.getProperty("java.version").startsWith("$javaVersion") || + | System.getProperty("java.version").startsWith("1.$javaVersion") + |) + |println(System.getProperty("java.home"))""".stripMargin + ).fromRoot { root => + val javaHome = + os.Path( + os.proc(TestUtil.cs, "java-home", "--jvm", index).call().out.trim(), + os.pwd + ) + 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 $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) + } + } + } + } + + if (!Properties.isWin || !useScalaInstallationWrapper) // TODO make this pass on Windows + 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" -> + s"""assert( + | System.getProperty("java.version").startsWith("$javaVersion") || + | System.getProperty("java.version").startsWith("1.$javaVersion") + |) + |println(System.getProperty("java.home"))""".stripMargin + ).fromRoot { root => + val javaHome = + os.Path( + os.proc(TestUtil.cs, "java-home", "--jvm", index).call().out.trim(), + os.pwd + ) + withLauncher(root) { launcher => + val res = + os.proc(launcher, "run", ".", extraOptions, "--java-home", javaHome.toString) + .call(cwd = root) + expect(res.out.trim().contains(javaHome.toString)) + } + } + } + } + + if (javaVersion >= Constants.bloopMinimumJvmVersion) + test(s"Bloop runs correctly with $launcherString on JVM $index") { + TestUtil.retryOnCi() { + val expectedMessage = "Hello, world!" + TestInputs(os.rel / "check_java_home.sc" -> s"""println("$expectedMessage")""") + .fromRoot { root => + os.proc(TestUtil.cli, "bloop", "exit", "--power").call(cwd = root) + 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) + } + } + } + } + } +} diff --git a/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala b/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala index 767ad9767d..486b2f602a 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala @@ -21,7 +21,9 @@ abstract class RunTestDefinitions with RunScalacCompatTestDefinitions with RunSnippetTestDefinitions with RunScalaPyTestDefinitions - with RunZipTestDefinitions { _: TestScalaVersion => + with RunZipTestDefinitions + with RunJdkTestDefinitions + with CoursierScalaInstallationTestHelper { _: TestScalaVersion => protected lazy val extraOptions: Seq[String] = scalaVersionArgs ++ TestUtil.extraOptions protected val emptyInputs: TestInputs = TestInputs(os.rel / ".placeholder" -> "") diff --git a/modules/integration/src/test/scala/scala/cli/integration/SipScalaTests.scala b/modules/integration/src/test/scala/scala/cli/integration/SipScalaTests.scala index 813ec100d6..daee0f49ba 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/SipScalaTests.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/SipScalaTests.scala @@ -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( diff --git a/project/deps.sc b/project/deps.sc index 1f5291af07..24b1d92c0c 100644 --- a/project/deps.sc +++ b/project/deps.sc @@ -80,6 +80,9 @@ object Java { def minimumBloopJava = 17 def minimumInternalJava = 16 def defaultJava = minimumBloopJava + def mainJavaVersions = Seq(8, 11, 17, 21, 23) + def allJavaVersions = + (mainJavaVersions ++ Seq(minimumBloopJava, minimumInternalJava, defaultJava)).distinct } // Dependencies used in integration test fixtures