Skip to content

Commit c9f21b8

Browse files
committed
removed platform-dependency in unit test; pass scriptArgs to precompiler jar
1 parent 12cce7b commit c9f21b8

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

compiler/src/dotty/tools/MainGenericRunner.scala

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ object MainGenericRunner {
105105
case "-run" :: fqName :: tail =>
106106
process(tail, settings.withExecuteMode(ExecuteMode.Run).withTargetToRun(fqName))
107107
case ("-cp" | "-classpath" | "--class-path") :: cp :: tail =>
108-
val globdir = cp.replaceAll("[\\/][^\\/]*$","") // slash/backslash agnostic
108+
val globdir = cp.replaceAll("[\\/][^\\/]*$", "") // slash/backslash agnostic
109109
val (tailargs, cpstr) = if globdir.nonEmpty && classpathSeparator != ";" || cp.contains(classpathSeparator) then
110110
(tail, cp)
111111
else
@@ -201,24 +201,21 @@ object MainGenericRunner {
201201
}
202202
errorFn("", res)
203203
case ExecuteMode.Script =>
204-
val targetScript = Paths.get(settings.targetScript)
205-
val targetJar = settings.targetScript.replaceAll("[.][^\\/]*$","")+".jar"
206-
val precompiledJar = Paths.get(targetJar)
207-
def mainClass = Jar(targetJar).mainClass match
208-
case Some(mc) =>
209-
mc
210-
case None =>
211-
""
212-
if precompiledJar.toFile.isFile && mainClass.nonEmpty && precompiledJar.toFile.lastModified >= targetScript.toFile.lastModified then
213-
// precompiledJar is newer than targetScript
214-
sys.props("script.path") = targetScript.toAbsolutePath.normalize.toString
204+
val targetScript = Paths.get(settings.targetScript).toFile
205+
val targetJar = settings.targetScript.replaceAll("[.][^\\/]*$", "")+".jar"
206+
val precompiledJar = Paths.get(targetJar).toFile
207+
def mainClass = Jar(targetJar).mainClass.getOrElse("") // throws exception if file not found
208+
val jarIsValid = precompiledJar.isFile && mainClass.nonEmpty && precompiledJar.lastModified >= targetScript.lastModified
209+
if jarIsValid then
210+
// precompiledJar exists, is newer than targetScript, and manifest defines a mainClass
211+
sys.props("script.path") = targetScript.toPath.toAbsolutePath.normalize.toString
215212
val scalaClasspath = ClasspathFromClassloader(Thread.currentThread().getContextClassLoader).split(classpathSeparator)
216213
val newClasspath = (settings.classPath.flatMap(_.split(classpathSeparator).filter(_.nonEmpty)) ++ removeCompiler(scalaClasspath) :+ ".").map(File(_).toURI.toURL)
217-
Jar(targetJar).mainClass match
218-
case Some(mc) =>
219-
ObjectRunner.runAndCatch(newClasspath :+ File(targetJar).toURI.toURL, mc, settings.residualArgs)
220-
case None =>
221-
Some(IllegalArgumentException(s"No main class defined in manifest in jar: $precompiledJar"))
214+
val mc = mainClass
215+
if mc.nonEmpty then
216+
ObjectRunner.runAndCatch(newClasspath :+ File(targetJar).toURI.toURL, mc, settings.scriptArgs)
217+
else
218+
Some(IllegalArgumentException(s"No main class defined in manifest in jar: $precompiledJar"))
222219
else
223220
val properArgs =
224221
List("-classpath", settings.classPath.mkString(classpathSeparator)).filter(Function.const(settings.classPath.nonEmpty))

compiler/test/dotty/tools/scripting/BashScriptsTests.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class BashScriptsTests:
5858

5959
/* verify `dist/bin/scala` non-interference with command line args following script name */
6060
@Test def verifyScalaArgs =
61-
val commandline = (Seq("SCALA_OPTS= ",scalaPath, showArgsScript) ++ testScriptArgs).mkString(" ")
61+
val commandline = (Seq("SCALA_OPTS= ", scalaPath, showArgsScript) ++ testScriptArgs).mkString(" ")
6262
val (validTest, exitCode, stdout, stderr) = bashCommand(commandline)
6363
if validTest then
6464
var fail = false
@@ -78,7 +78,7 @@ class BashScriptsTests:
7878
*/
7979
@Test def verifyScriptPathProperty =
8080
val scriptFile = testFiles.find(_.getName == "scriptPath.sc").get
81-
val expected = s"/${scriptFile.getName}"
81+
val expected = s"${scriptFile.getName}"
8282
printf("===> verify valid system property script.path is reported by script [%s]\n", scriptFile.getName)
8383
printf("calling scriptFile: %s\n", scriptFile)
8484
val (validTest, exitCode, stdout, stderr) = bashCommand(scriptFile.absPath)
@@ -105,7 +105,7 @@ class BashScriptsTests:
105105
if valid then printf(s"\n===> success: classpath begins with %s, as reported by [%s]\n", workingDirectory, scriptFile.getName)
106106
assert(valid, s"script ${scriptFile.absPath} did not report valid java.class.path first entry")
107107

108-
def existingPath: String = envOrElse("PATH","").norm
108+
def existingPath: String = envOrElse("PATH", "").norm
109109
def adjustedPath = s"$javaHome/bin$psep$scalaHome/bin$psep$existingPath"
110110
def pathEntries = adjustedPath.split(psep).toList
111111

@@ -120,7 +120,7 @@ class BashScriptsTests:
120120
def fixHome(s: String): String =
121121
s.startsWith("~") match {
122122
case false => s
123-
case true => s.replaceFirst("~",userHome)
123+
case true => s.replaceFirst("~", userHome)
124124
}
125125

126126
extension(s: String) {
@@ -146,7 +146,7 @@ class BashScriptsTests:
146146
def absPath: String = f.getAbsolutePath.norm
147147
}
148148

149-
lazy val psep: String = propOrElse("path.separator","")
149+
lazy val psep: String = propOrElse("path.separator", "")
150150
lazy val osname = propOrElse("os.name", "").toLowerCase
151151

152152
lazy val scalacPath = s"$workingDirectory/dist/target/pack/bin/scalac".norm
@@ -163,7 +163,7 @@ class BashScriptsTests:
163163
// else, SCALA_HOME if defined
164164
// else, not defined
165165
lazy val scalaHome =
166-
if scalacPath.isFile then scalacPath.replaceAll("/bin/scalac","")
166+
if scalacPath.isFile then scalacPath.replaceAll("/bin/scalac", "")
167167
else envOrElse("SCALA_HOME", "").norm
168168

169169
lazy val javaHome = envOrElse("JAVA_HOME", "").norm
@@ -172,7 +172,7 @@ class BashScriptsTests:
172172
("JAVA_HOME", javaHome),
173173
("SCALA_HOME", scalaHome),
174174
("PATH", adjustedPath),
175-
).filter { case (name,valu) => valu.nonEmpty }
175+
).filter { case (name, valu) => valu.nonEmpty }
176176

177177
lazy val whichBash: String =
178178
var whichBash = ""
@@ -183,7 +183,7 @@ class BashScriptsTests:
183183

184184
whichBash
185185

186-
def bashCommand(cmdstr: String, additionalEnvPairs:List[(String, String)] = Nil): (Boolean, Int, Seq[String], Seq[String]) = {
186+
def bashCommand(cmdstr: String, additionalEnvPairs: List[(String, String)] = Nil): (Boolean, Int, Seq[String], Seq[String]) = {
187187
var (stdout, stderr) = (List.empty[String], List.empty[String])
188188
if bashExe.toFile.exists then
189189
val cmd = Seq(bashExe, "-c", cmdstr)

0 commit comments

Comments
 (0)