|
| 1 | +package scala.cli.integration |
| 2 | + |
| 3 | +import com.eed3si9n.expecty.Expecty.expect |
| 4 | + |
| 5 | +import scala.util.Properties |
| 6 | + |
| 7 | +trait RunJdkTestDefinitions { _: RunTestDefinitions => |
| 8 | + def javaIndex(javaVersion: Int): String = |
| 9 | + // TODO just passing the version number on arm64 should be enough, needs a fix in cs |
| 10 | + if (Properties.isMac && TestUtil.isM1 && (javaVersion < 11 || javaVersion == 16)) |
| 11 | + s"zulu:$javaVersion" |
| 12 | + else javaVersion.toString |
| 13 | + |
| 14 | + def canUseScalaInstallationWrapper: Boolean = |
| 15 | + actualScalaVersion.startsWith("3") && actualScalaVersion.split('.').drop(1).head.toInt >= 5 |
| 16 | + |
| 17 | + for { |
| 18 | + javaVersion <- Constants.allJavaVersions |
| 19 | + 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)) |
| 39 | + } { |
| 40 | + test(s"correct JVM is picked up by $launcherString when JAVA_HOME set to $index") { |
| 41 | + TestUtil.retryOnCi() { |
| 42 | + TestInputs( |
| 43 | + os.rel / "check_java_home.sc" -> |
| 44 | + s"""assert( |
| 45 | + | System.getProperty("java.version").startsWith("$javaVersion") || |
| 46 | + | System.getProperty("java.version").startsWith("1.$javaVersion") |
| 47 | + |) |
| 48 | + |println(System.getProperty("java.home"))""".stripMargin |
| 49 | + ).fromRoot { root => |
| 50 | + val javaHome = |
| 51 | + os.Path( |
| 52 | + os.proc(TestUtil.cs, "java-home", "--jvm", index).call().out.trim(), |
| 53 | + os.pwd |
| 54 | + ) |
| 55 | + withLauncher(root) { launcher => |
| 56 | + val res = os.proc(launcher, "run", ".", extraOptions) |
| 57 | + .call(cwd = root, env = Map("JAVA_HOME" -> javaHome.toString)) |
| 58 | + expect(res.out.trim().contains(javaHome.toString)) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 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 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (!Properties.isWin || !useScalaInstallationWrapper) // TODO make this pass on Windows |
| 80 | + test( |
| 81 | + s"correct JVM is picked up by $launcherString when Java $index is passed with --java-home" |
| 82 | + ) { |
| 83 | + TestUtil.retryOnCi() { |
| 84 | + TestInputs( |
| 85 | + os.rel / "check_java_home.sc" -> |
| 86 | + s"""assert( |
| 87 | + | System.getProperty("java.version").startsWith("$javaVersion") || |
| 88 | + | System.getProperty("java.version").startsWith("1.$javaVersion") |
| 89 | + |) |
| 90 | + |println(System.getProperty("java.home"))""".stripMargin |
| 91 | + ).fromRoot { root => |
| 92 | + val javaHome = |
| 93 | + os.Path( |
| 94 | + os.proc(TestUtil.cs, "java-home", "--jvm", index).call().out.trim(), |
| 95 | + os.pwd |
| 96 | + ) |
| 97 | + withLauncher(root) { launcher => |
| 98 | + val res = |
| 99 | + os.proc(launcher, "run", ".", extraOptions, "--java-home", javaHome.toString) |
| 100 | + .call(cwd = root) |
| 101 | + expect(res.out.trim().contains(javaHome.toString)) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (javaVersion >= Constants.bloopMinimumJvmVersion) |
| 108 | + test(s"Bloop runs correctly with $launcherString on JVM $index") { |
| 109 | + TestUtil.retryOnCi() { |
| 110 | + val expectedMessage = "Hello, world!" |
| 111 | + TestInputs(os.rel / "check_java_home.sc" -> s"""println("$expectedMessage")""") |
| 112 | + .fromRoot { root => |
| 113 | + os.proc(TestUtil.cli, "bloop", "exit", "--power").call(cwd = root) |
| 114 | + withLauncher(root) { launcher => |
| 115 | + val res = os.proc( |
| 116 | + launcher, |
| 117 | + "run", |
| 118 | + ".", |
| 119 | + extraOptions, |
| 120 | + "--bloop-jvm", |
| 121 | + index, |
| 122 | + "--jvm", |
| 123 | + index |
| 124 | + ) |
| 125 | + .call(cwd = root, stderr = os.Pipe) |
| 126 | + expect(res.err.trim().contains(javaVersion.toString)) |
| 127 | + expect(res.out.trim() == expectedMessage) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments