Skip to content

Commit b9773d6

Browse files
authored
Merge pull request #12467 from philwalk/fix-scripting-args-with-tests
fix for capturing script args as soon as script file is detected
2 parents c415033 + 138285d commit b9773d6

File tree

4 files changed

+134
-27
lines changed

4 files changed

+134
-27
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env scala
2+
3+
// precise output format expected by BashScriptsTests.scala
4+
def main(args: Array[String]): Unit =
5+
for (a,i) <- args.zipWithIndex do
6+
printf(s"arg %2d:[%s]\n",i,a)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package dotty
2+
package tools
3+
package scripting
4+
5+
import java.io.File
6+
import java.nio.file.{Path, Paths, Files}
7+
import scala.sys.process._
8+
9+
import org.junit.Test
10+
11+
import vulpix.TestConfiguration
12+
13+
14+
/** Runs all tests contained in `compiler/test-resources/scripting/` */
15+
class BashScriptsTests:
16+
// classpath tests managed by scripting.ClasspathTests.scala
17+
def testFiles = scripts("/scripting").filter { ! _.getName.startsWith("classpath") }
18+
19+
lazy val expectedOutput = List(
20+
"arg 0:[a]",
21+
"arg 1:[b]",
22+
"arg 2:[c]",
23+
"arg 3:[-repl]",
24+
"arg 4:[-run]",
25+
"arg 5:[-script]",
26+
"arg 6:[-debug]",
27+
)
28+
lazy val testScriptArgs = Seq(
29+
"a", "b", "c", "-repl", "-run", "-script", "-debug"
30+
)
31+
lazy val (bashExe,bashPath) =
32+
val bexe = getBashPath
33+
val bpath = Paths.get(bexe)
34+
printf("bashExe: [%s]\n", bexe)
35+
(bexe, bpath)
36+
37+
val showArgsScript = testFiles.find(_.getName == "showArgs.sc").get.absPath
38+
39+
val scalacPath = which("scalac")
40+
val scalaPath = which("scala")
41+
42+
/* verify `dist/bin/scalac` */
43+
@Test def verifyScalacArgs =
44+
val commandline = (Seq(scalacPath, "-script", showArgsScript) ++ testScriptArgs).mkString(" ")
45+
if bashPath.toFile.exists then
46+
var cmd = Array(bashExe, "-c", commandline)
47+
val output = for {
48+
line <- Process(cmd).lazyLines_!
49+
} yield line
50+
var fail = false
51+
printf("\n")
52+
for (line, expect) <- output zip expectedOutput do
53+
printf("expected: %-17s| actual: %s\n", line, expect)
54+
if line != expect then
55+
fail = true
56+
57+
if fail then
58+
assert(output == expectedOutput)
59+
60+
/* verify `dist/bin/scala` */
61+
@Test def verifyScalaArgs =
62+
val commandline = (Seq(scalaPath, showArgsScript) ++ testScriptArgs).mkString(" ")
63+
if bashPath.toFile.exists then
64+
var cmd = Array(bashExe, "-c", commandline)
65+
val output = for {
66+
line <- Process(cmd).lazyLines_!
67+
} yield line
68+
var fail = false
69+
printf("\n")
70+
for (line, expect) <- output zip expectedOutput do
71+
printf("expected: %-17s| actual: %s\n", line, expect)
72+
if line != expect then
73+
fail = true
74+
75+
if fail then
76+
assert(output == expectedOutput)
77+
78+
extension (str: String) def dropExtension =
79+
str.reverse.dropWhile(_ != '.').drop(1).reverse
80+
81+
extension(f: File) def absPath =
82+
f.getAbsolutePath.replace('\\', '/')
83+
84+
lazy val osname = Option(sys.props("os.name")).getOrElse("").toLowerCase
85+
86+
def getBashPath: String =
87+
var whichBash = ""
88+
printf("osname[%s]\n", osname)
89+
if osname.startsWith("windows") then
90+
whichBash = which("bash.exe")
91+
else
92+
whichBash = which("bash")
93+
94+
whichBash
95+
96+
def execCmd(command: String, options: String *): Seq[String] =
97+
val cmd = (command :: options.toList).toSeq
98+
for {
99+
line <- Process(cmd).lazyLines_!
100+
} yield line

dist/bin/scala

100755100644
Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -118,34 +118,35 @@ while [[ $# -gt 0 ]]; do
118118
shift ;;
119119

120120
*)
121-
if [ "${execute_mode-}" == 'script' ]; then
122-
addScript "$1"
123-
else
124-
# script if extension .scala or .sc, or if has scala hashbang line
125-
126-
# no -f test, issue meaningful error message (file not found)
127-
if [[ "$1" == *.scala || "$1" == *.sc ]]; then
128-
setExecuteMode 'script' # execute_script=true
129-
130-
# -f test needed before we examine the hashbang line
131-
elif [[ (-f "$1" && `head -n 1 -- "$1" | grep '#!.*scala'`) ]]; then
132-
setExecuteMode 'script' # execute_script=true
133-
fi
121+
# script if extension .scala or .sc, or if has scala hashbang line
122+
# no -f test, issue meaningful error message (file not found)
123+
if [[ "$1" == *.scala || "$1" == *.sc ]]; then
124+
setExecuteMode 'script' # execute_script=true
125+
126+
# -f test needed before we examine the hashbang line
127+
elif [[ (-f "$1" && `head -n 1 -- "$1" | grep '#!.*scala'`) ]]; then
128+
setExecuteMode 'script' # execute_script=true
129+
fi
134130

135-
if [ "${execute_mode-}" == 'script' ]; then
136-
target_script="$1"
137-
if [ ! -f $target_script ]; then
138-
# likely a typo or missing script file, quit early
139-
echo "not found: $target_script" 1>&2
140-
scala_exit_status=2
141-
onExit
142-
fi
143-
else
144-
# all unrecognized args appearing prior to a script name
145-
addResidual "$1"
131+
if [ "${execute_mode-}" == 'script' ]; then
132+
target_script="$1"
133+
shift
134+
if [ ! -f $target_script ]; then
135+
# likely a typo or missing script file, quit early
136+
echo "not found: $target_script" 1>&2
137+
scala_exit_status=2
138+
onExit
146139
fi
140+
# all are script args
141+
while [[ $# -gt 0 ]]; do
142+
addScript "${1}"
143+
shift
144+
done
145+
else
146+
# all unrecognized args appearing prior to a script name
147+
addResidual "$1"
148+
shift
147149
fi
148-
shift
149150
;;
150151

151152
esac

dist/bin/scalac

100755100644
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ case "$1" in
4242
-Oshort) addJava "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" && shift ;;
4343
-repl) PROG_NAME="$ReplMain" && shift ;;
4444
-script) PROG_NAME="$ScriptingMain" && target_script="$2" && shift && shift
45-
while [[ $# -gt 0 ]]; do addScripting "$1" && shift ; done ;;
45+
while [[ $# -gt 0 ]]; do addScript "$1" && shift ; done ;;
4646
-compile) PROG_NAME="$CompilerMain" && shift ;;
4747
-decompile) PROG_NAME="$DecompilerMain" && shift ;;
4848
-print-tasty) PROG_NAME="$DecompilerMain" && addScala "-print-tasty" && shift ;;
@@ -64,7 +64,7 @@ compilerJavaClasspathArgs
6464

6565
if [ "$PROG_NAME" == "$ScriptingMain" ]; then
6666
setScriptName="-Dscript.path=$target_script"
67-
scripting_string="-script $target_script ${scripting_args[@]}"
67+
scripting_string="-script $target_script ${script_args[@]}"
6868
fi
6969

7070
[ -n "$script_trace" ] && set -x

0 commit comments

Comments
 (0)