@@ -11,10 +11,13 @@ import org.junit.Test
11
11
import vulpix .TestConfiguration
12
12
13
13
14
- /** Runs all tests contained in `compiler/test-resources/scripting/` */
14
+ /** Verifies correct handling of command line arguments by `dist/bin/scala` and `dist/bin/scalac`.
15
+ * +. arguments following a script path must be treated as script arguments
16
+ * +. preserve script command line arguments.
17
+ */
15
18
class BashScriptsTests :
16
19
// classpath tests managed by scripting.ClasspathTests.scala
17
- def testFiles = scripts(" /scripting" ).filter { ! _.getName.startsWith( " classpath " ) }
20
+ def testFiles = scripts(" /scripting" )
18
21
19
22
lazy val expectedOutput = List (
20
23
" arg 0:[a]" ,
@@ -28,10 +31,10 @@ class BashScriptsTests:
28
31
lazy val testScriptArgs = Seq (
29
32
" a" , " b" , " c" , " -repl" , " -run" , " -script" , " -debug"
30
33
)
31
- lazy val (bashExe,bashPath) =
34
+ lazy val (bashExe, bashPath) =
32
35
val bexe = getBashPath
33
36
val bpath = Paths .get(bexe)
34
- printf(" bashExe: [%s]\n " , bexe)
37
+ // printf("bashExe: [%s]\n", bexe)
35
38
(bexe, bpath)
36
39
37
40
val showArgsScript = testFiles.find(_.getName == " showArgs.sc" ).get.absPath
@@ -41,13 +44,11 @@ class BashScriptsTests:
41
44
42
45
/* verify `dist/bin/scalac` */
43
46
@ Test def verifyScalacArgs =
44
- printf(" scalacPath[%s]\n " ,scalacPath)
47
+ printf(" scalacPath[%s]\n " , scalacPath)
45
48
val commandline = (Seq (scalacPath, " -script" , showArgsScript) ++ testScriptArgs).mkString(" " )
46
49
if bashPath.toFile.exists then
47
50
var cmd = Array (bashExe, " -c" , commandline)
48
- val output = for {
49
- line <- Process (cmd).lazyLines_!
50
- } yield line
51
+ val output = Process (cmd).lazyLines_!
51
52
var fail = false
52
53
printf(" \n " )
53
54
for (line, expect) <- output zip expectedOutput do
@@ -68,7 +69,7 @@ class BashScriptsTests:
68
69
} yield line
69
70
var fail = false
70
71
printf(" \n " )
71
- var mismatches = List .empty[(String ,String )]
72
+ var mismatches = List .empty[(String , String )]
72
73
for (line, expect) <- output zip expectedOutput do
73
74
printf(" expected: %-17s\n actual : %s\n " , expect, line)
74
75
if line != expect then
@@ -77,24 +78,81 @@ class BashScriptsTests:
77
78
if fail then
78
79
assert(output == expectedOutput)
79
80
80
- extension (str : String ) def dropExtension =
81
+ /*
82
+ * verify that scriptPath.sc sees a valid script.path property.
83
+ */
84
+ @ Test def verifyScriptPathProperty =
85
+ val scriptFile = testFiles.find(_.getName == " scriptPath.sc" ).get
86
+ val expected = s " / ${scriptFile.getName}"
87
+ printf(" ===> verify valid system property script.path is reported by script [%s]\n " , scriptFile.getName)
88
+ val (exitCode, stdout, stderr) = bashCommand(scriptFile.absPath)
89
+ if exitCode == 0 && ! stderr.exists(_.contains(" Permission denied" )) then
90
+ // var cmd = Array(bashExe, "-c", scriptFile.absPath)
91
+ // val stdout = Process(cmd).lazyLines_!
92
+ stdout.foreach { printf(" ######### [%s]\n " , _) }
93
+ val valid = stdout.exists { _.endsWith(expected) }
94
+ if valid then printf(" # valid script.path reported by [%s]\n " , scriptFile.getName)
95
+ assert(valid, s " script ${scriptFile.absPath} did not report valid script.path value " )
96
+
97
+ /*
98
+ * verify SCALA_OPTS can specify an @argsfile when launching a scala script in `dist/bin/scala`.
99
+ */
100
+ @ Test def verifyScalaOpts =
101
+ val scriptFile = testFiles.find(_.getName == " classpathReport.sc" ).get
102
+ printf(" ===> verify valid system property script.path is reported by script [%s]\n " , scriptFile.getName)
103
+ val argsfile = createArgsFile() // avoid problems caused by drive letter
104
+ val envPairs = List ((" SCALA_OPTS" , s " @ $argsfile" ))
105
+ val (exitCode, stdout, stderr) = bashCommand(scriptFile.absPath, envPairs:_* )
106
+ if exitCode != 0 || stderr.exists(_.contains(" Permission denied" )) then
107
+ stderr.foreach { System .err.printf(" stderr [%s]\n " , _) }
108
+ printf(" unable to execute script, return value is %d\n " , exitCode)
109
+ else
110
+ // val stdout: Seq[String] = Process(cmd, cwd, envPairs:_*).lazyLines_!.toList
111
+ val expected = s " ${cwd.toString}"
112
+ val List (line1 : String , line2 : String ) = stdout.take(2 )
113
+ val valid = line2.dropWhile( _ != ' ' ).trim.startsWith(expected)
114
+ if valid then printf(s " \n ===> success: classpath begins with %s, as reported by [%s] \n " , cwd, scriptFile.getName)
115
+ assert(valid, s " script ${scriptFile.absPath} did not report valid java.class.path first entry " )
116
+
117
+ lazy val cwd = Paths .get(dotty.tools.dotc.config.Properties .userDir).toFile
118
+
119
+ def createArgsFile (): String =
120
+ val utfCharset = java.nio.charset.StandardCharsets .UTF_8 .name
121
+ val text = s " -classpath ${cwd.absPath}"
122
+ val path = Files .createTempFile(" scriptingTest" , " .args" )
123
+ Files .write(path, text.getBytes(utfCharset))
124
+ path.toFile.getAbsolutePath.replace('\\ ' , '/' )
125
+
126
+ extension (str : String ) def dropExtension : String =
81
127
str.reverse.dropWhile(_ != '.' ).drop(1 ).reverse
82
128
83
- extension(f : File ) def absPath =
129
+ extension(f : File ) def absPath : String =
84
130
f.getAbsolutePath.replace('\\ ' , '/' )
85
131
86
132
lazy val osname = Option (sys.props(" os.name" )).getOrElse(" " ).toLowerCase
87
133
88
134
def getBashPath : String =
89
135
var whichBash = " "
90
- printf(" osname[%s]\n " , osname)
136
+ // printf("osname[%s]\n", osname)
91
137
if osname.startsWith(" windows" ) then
92
138
whichBash = which(" bash.exe" )
93
139
else
94
140
whichBash = which(" bash" )
95
141
96
142
whichBash
97
143
144
+ def bashCommand (cmdstr : String , envPairs : (String , String )* ): (Int , Seq [String ], Seq [String ]) = {
145
+ import scala .sys .process ._
146
+ val cmd = Seq (bashExe, " -c" , cmdstr)
147
+ val proc = Process (cmd, None , envPairs * )
148
+ var (stdout, stderr) = (List .empty[String ], List .empty[String ])
149
+ val exitVal = proc ! ProcessLogger (
150
+ (out : String ) => stdout ::= out,
151
+ (err : String ) => stderr ::= err
152
+ )
153
+ (exitVal, stdout.reverse, stderr.reverse)
154
+ }
155
+
98
156
def execCmd (command : String , options : String * ): Seq [String ] =
99
157
val cmd = (command :: options.toList).toSeq
100
158
for {
0 commit comments