Skip to content

Commit

Permalink
Fix passing jvm options
Browse files Browse the repository at this point in the history
  • Loading branch information
BarkingBad committed Dec 8, 2021
1 parent c3ebebe commit 0f3c819
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 14 deletions.
Expand Up @@ -60,6 +60,13 @@ class CoursierScalaTests:
assertTrue(output.mkString("\n").endsWith("scriptPath.sc"))
scriptPath()

def scriptEnvDashJDashD() =
val scriptPath = scripts("/scripting").find(_.getName == "envtest.sc").get.absPath
val args = scriptPath
val output = CoursierScalaTests.csScalaCmd("-J-Dkey=World", args)
assertEquals(output.mkString("\n"), "Hello World")
scriptEnvDashJDashD()

def version() =
val output = CoursierScalaTests.csScalaCmd("-version")
assertTrue(output.mkString("\n").contains(sys.env("DOTTY_BOOTSTRAPPED_VERSION")))
Expand All @@ -75,6 +82,11 @@ class CoursierScalaTests:
assertEquals(output.mkString("\n"), "Hello")
run()

def runDashJDashD() =
val output = CoursierScalaTests.csScalaCmd("-J-Dkey=World", "-classpath", scripts("/run").head.getParentFile.getParent, "-run", "run.envtest")
assertEquals(output.mkString("\n"), "Hello World")
runDashJDashD()

def notOnlyOptionsEqualsRun() =
val output = CoursierScalaTests.csScalaCmd("-classpath", scripts("/run").head.getParentFile.getParent, "run.myfile")
assertEquals(output.mkString("\n"), "Hello")
Expand Down Expand Up @@ -147,10 +159,12 @@ object CoursierScalaTests:
csCmd("dotty.tools.dotc.Main", options*)

private def csCmd(entry: String, options: String*): List[String] =
val newOptions = options match
case Nil => options
case _ => "--" +: options
execCmd("./cs", (s"""launch "org.scala-lang:scala3-compiler_3:${sys.env("DOTTY_BOOTSTRAPPED_VERSION")}" --main-class "$entry" --property "scala.usejavacp=true"""" +: newOptions)*)
val (jOpts, args) = options.partition(_.startsWith("-J"))
val newOptions = args match
case Nil => args
case _ => "--" +: args
val newJOpts = jOpts.map(s => s"--java-opt ${s.stripPrefix("-J")}").mkString(" ")
execCmd("./cs", (s"""launch "org.scala-lang:scala3-compiler_3:${sys.env("DOTTY_BOOTSTRAPPED_VERSION")}" $newJOpts --main-class "$entry" --property "scala.usejavacp=true"""" +: newOptions)*)

/** Get coursier script */
@BeforeClass def setup(): Unit =
Expand Down
4 changes: 4 additions & 0 deletions compiler/test-coursier/run/envtest.scala
@@ -0,0 +1,4 @@
package run

object envtest extends App:
println("Hello " + sys.props("key"))
4 changes: 4 additions & 0 deletions compiler/test-resources/scripting/envtest.sc
@@ -0,0 +1,4 @@
#!dist/target/pack/bin/scala

def main(args: Array[String]): Unit =
println("Hello " + util.Properties.propOrNull("key"))
4 changes: 4 additions & 0 deletions compiler/test-resources/scripting/envtest.scala
@@ -0,0 +1,4 @@
package run

object envtest extends App:
println("Hello " + sys.props("key"))
17 changes: 17 additions & 0 deletions compiler/test/dotty/tools/repl/EnvPropsTest.scala
@@ -0,0 +1,17 @@
package dotty.tools.repl

import org.junit.Assert._
import org.junit.Test

class EnvPropsTest extends ReplTest(options = Array("-J-Dkey=value", "-Dkey2=value2")) {
@Test def fromDashJDashD = fromInitialState { implicit s =>
run("util.Properties.propOrNull(\"key\")")
assertTrue(storedOutput().contains("val res0: String = value"))
}

@Test def fromDashD = fromInitialState { implicit s =>
run("util.Properties.propOrNull(\"key2\")")
assertTrue(storedOutput().contains("val res0: String = value2"))
}
}

36 changes: 27 additions & 9 deletions compiler/test/dotty/tools/scripting/BashScriptsTests.scala
Expand Up @@ -28,13 +28,13 @@ class BashScriptsTests:
printf("scalac path: [%s]\n", scalacPath)

lazy val expectedOutput = List(
"arg 0:[a]",
"arg 1:[b]",
"arg 2:[c]",
"arg 3:[-repl]",
"arg 4:[-run]",
"arg 5:[-script]",
"arg 6:[-debug]",
"arg 0:[a]",
"arg 1:[b]",
"arg 2:[c]",
"arg 3:[-repl]",
"arg 4:[-run]",
"arg 5:[-script]",
"arg 6:[-debug]",
)
lazy val testScriptArgs = Seq(
"a", "b", "c", "-repl", "-run", "-script", "-debug"
Expand All @@ -56,6 +56,24 @@ class BashScriptsTests:
if fail then
assert(stdout == expectedOutput)

/* verify `dist/bin/scala` with -J setting */
@Test def verifyScalaJProperty =
val commandline = Seq(scalaPath, "-J-Dkey=World", "-script", testFiles.find(_.getName == "envtest.sc").get.absPath).mkString(" ")
val (validTest, exitCode, stdout, stderr) = bashCommand(commandline)
assertEquals(stdout.mkString("\n"), "Hello World")

/* verify `dist/bin/scala` with -J setting */
@Test def verifyScalaJProperty =
val commandline = Seq(scalaPath, "-J-Dkey=World", "-script", testFiles.find(_.getName == "envtest.sc").get.absPath).mkString(" ")
val (validTest, exitCode, stdout, stderr) = bashCommand(commandline)
assertEquals(stdout.mkString("\n"), "Hello World")

/* verify `dist/bin/scala` with -J setting */
@Test def verifyScalaJProperty =
val commandline = Seq(scalaPath, "-J-Dkey=World", testFiles.find(_.getName == "envtest.scala").get.absPath).mkString(" ")
val (validTest, exitCode, stdout, stderr) = bashCommand(commandline)
assertEquals(stdout.mkString("\n"), "Hello World")

/* verify `dist/bin/scala` non-interference with command line args following script name */
@Test def verifyScalaArgs =
val commandline = (Seq("SCALA_OPTS= ", scalaPath, showArgsScript) ++ testScriptArgs).mkString(" ")
Expand All @@ -73,7 +91,7 @@ class BashScriptsTests:
assert(stdout == expectedOutput)

/*
* verify that scriptPath.sc sees a valid script.path property,
* verify that scriptPath.sc sees a valid script.path property,
* and that it's value is the path to "scriptPath.sc".
*/
@Test def verifyScriptPathProperty =
Expand Down Expand Up @@ -192,7 +210,7 @@ class BashScriptsTests:
val envPairs = testEnvPairs ++ additionalEnvPairs
val proc = Process(cmd, None, envPairs *)
val exitVal = proc ! ProcessLogger (
(out: String) => stdout ::= out,
(out: String) => stdout ::= out,
(err: String) => stderr ::= err
)
val validTest = exitVal == 0 && ! stderr.exists(_.contains("Permission denied"))
Expand Down
5 changes: 4 additions & 1 deletion dist/bin/scala
Expand Up @@ -31,7 +31,10 @@ source "$PROG_HOME/bin/common"
# exec here would prevent onExit from being called, leaving terminal in unusable state
compilerJavaClasspathArgs
[ -z "${ConEmuPID-}" -o -n "${cygwin-}" ] && export MSYSTEM= PWD= # workaround for #12405
eval "\"$JAVACMD\"" "-Dscala.home=$PROG_HOME" "-classpath \"$jvm_cp_args\"" "dotty.tools.MainGenericRunner" "-classpath \"$jvm_cp_args\"" "$@"
echo "$@" | xargs -d' ' -n 1 | grep "^[^-J]"
java_args=`echo "$@" | xargs -d' ' -n 1 | grep "^-J" | cut -c 3- | tr '\n' ' '`
args=`echo "$@" | xargs -d' ' -n 1 | grep "^[^-J]" | tr '\n' ' '`
eval "\"$JAVACMD\"" "$java_args" "-Dscala.home=$PROG_HOME" "-classpath \"$jvm_cp_args\"" "dotty.tools.MainGenericRunner" "-classpath \"$jvm_cp_args\"" "$args"
scala_exit_status=$?


Expand Down

0 comments on commit 0f3c819

Please sign in to comment.