@@ -31,10 +31,10 @@ class BashScriptsTests:
3131 lazy val testScriptArgs = Seq (
3232 " a" , " b" , " c" , " -repl" , " -run" , " -script" , " -debug"
3333 )
34- lazy val (bashExe,bashPath) =
34+ lazy val (bashExe, bashPath) =
3535 val bexe = getBashPath
3636 val bpath = Paths .get(bexe)
37- printf(" bashExe: [%s]\n " , bexe)
37+ // printf("bashExe: [%s]\n", bexe)
3838 (bexe, bpath)
3939
4040 val showArgsScript = testFiles.find(_.getName == " showArgs.sc" ).get.absPath
@@ -44,7 +44,7 @@ class BashScriptsTests:
4444
4545 /* verify `dist/bin/scalac` */
4646 @ Test def verifyScalacArgs =
47- printf(" scalacPath[%s]\n " ,scalacPath)
47+ printf(" scalacPath[%s]\n " , scalacPath)
4848 val commandline = (Seq (scalacPath, " -script" , showArgsScript) ++ testScriptArgs).mkString(" " )
4949 if bashPath.toFile.exists then
5050 var cmd = Array (bashExe, " -c" , commandline)
@@ -69,7 +69,7 @@ class BashScriptsTests:
6969 } yield line
7070 var fail = false
7171 printf(" \n " )
72- var mismatches = List .empty[(String ,String )]
72+ var mismatches = List .empty[(String , String )]
7373 for (line, expect) <- output zip expectedOutput do
7474 printf(" expected: %-17s\n actual : %s\n " , expect, line)
7575 if line != expect then
@@ -85,12 +85,14 @@ class BashScriptsTests:
8585 val scriptFile = testFiles.find(_.getName == " scriptPath.sc" ).get
8686 val expected = s " / ${scriptFile.getName}"
8787 printf(" ===> verify valid system property script.path is reported by script [%s]\n " , scriptFile.getName)
88- var cmd = Array (bashExe, " -c" , scriptFile.absPath)
89- val output = Process (cmd).lazyLines_!
90- output.foreach { printf(" [%s]\n " ,_) }
91- val valid = output.exists { _.endsWith(expected) }
92- if valid then printf(" # valid script.path reported by [%s]\n " ,scriptFile.getName)
93- assert(valid, s " script ${scriptFile.absPath} did not report valid script.path value " )
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 " )
9496
9597 /*
9698 * verify SCALA_OPTS can specify an @argsfile when launching a scala script in `dist/bin/scala`.
@@ -99,23 +101,27 @@ class BashScriptsTests:
99101 val scriptFile = testFiles.find(_.getName == " classpathReport.sc" ).get
100102 printf(" ===> verify valid system property script.path is reported by script [%s]\n " , scriptFile.getName)
101103 val argsfile = createArgsFile() // avoid problems caused by drive letter
102- val envPairs = List ((" SCALA_OPTS" ,s " @ $argsfile" ))
103- var cmd = Array (bashExe, " -c" , scriptFile.absPath)
104- val output : Seq [String ] = Process (cmd,cwd,envPairs:_* ).lazyLines_!.toList
105- val expected = s " ${cwd.toString}"
106- val List (line1 : String , line2 : String ) = output.take(2 )
107- val valid = line2.dropWhile( _ != ' ' ).trim.startsWith(expected)
108- if valid then printf(s " \n ===> success: classpath begins with %s, as reported by [%s] \n " ,cwd, scriptFile.getName)
109- assert(valid, s " script ${scriptFile.absPath} did not report valid java.class.path first entry " )
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 " )
110116
111117 lazy val cwd = Paths .get(dotty.tools.dotc.config.Properties .userDir).toFile
112118
113119 def createArgsFile (): String =
114120 val utfCharset = java.nio.charset.StandardCharsets .UTF_8 .name
115121 val text = s " -classpath ${cwd.absPath}"
116- val path = Files .createTempFile(" scriptingTest" ," .args" )
122+ val path = Files .createTempFile(" scriptingTest" , " .args" )
117123 Files .write(path, text.getBytes(utfCharset))
118- path.toFile.getAbsolutePath.replace('\\ ' ,'/' )
124+ path.toFile.getAbsolutePath.replace('\\ ' , '/' )
119125
120126 extension (str : String ) def dropExtension : String =
121127 str.reverse.dropWhile(_ != '.' ).drop(1 ).reverse
@@ -127,14 +133,26 @@ class BashScriptsTests:
127133
128134 def getBashPath : String =
129135 var whichBash = " "
130- printf(" osname[%s]\n " , osname)
136+ // printf("osname[%s]\n", osname)
131137 if osname.startsWith(" windows" ) then
132138 whichBash = which(" bash.exe" )
133139 else
134140 whichBash = which(" bash" )
135141
136142 whichBash
137143
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+
138156 def execCmd (command : String , options : String * ): Seq [String ] =
139157 val cmd = (command :: options.toList).toSeq
140158 for {
0 commit comments