diff --git a/build.sc b/build.sc index 4e24282710..7a6579d5c4 100644 --- a/build.sc +++ b/build.sc @@ -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}" 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 index f76bba245b..4232e53e3f 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/RunJdkTestDefinitions.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/RunJdkTestDefinitions.scala @@ -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" -> @@ -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" -> @@ -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)) + } } } } @@ -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) + } } } } 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 192ec100d7..486b2f602a 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala @@ -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" -> "") 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(