@@ -22,27 +22,35 @@ class ClasspathTests:
22
22
23
23
// only interested in classpath test scripts
24
24
def testFiles = scripts(" /scripting" ).filter { _.getName.matches(" classpath.*[.]sc" ) }
25
+ val testScriptName = " classpathReport.sc"
26
+ def testScript = testFiles.find { _.getName == testScriptName } match
27
+ case None => sys.error(s " test script not found: ${testScriptName}" )
28
+ case Some (file) => file
29
+
30
+ def packBinScalaExists : Boolean =
31
+ val scalaScriptPath = Paths .get(" dist/target/pack/bin/scala" )
32
+ scalaScriptPath.toFile.exists
25
33
26
34
/*
27
- * Call test scripts
35
+ * verify java command line generated by scalac.
28
36
*/
29
37
@ Test def scalacEchoTest =
30
- for scriptFile <- testFiles do
31
- val relpath = scriptFile.toPath.relpath.norm
32
- printf(" ===> test script name [%s]\n " ,relpath)
33
- printf(" %s\n " ,relpath)
34
- printf(" bash is [%s]\n " ,bashExe)
35
-
38
+ val relpath = testScript.toPath.relpath.norm
39
+ printf(" ===> scalacEchoTest for script [%s]\n " ,relpath)
40
+ printf(" bash is [%s]\n " ,bashExe)
41
+
42
+ if packBinScalaExists then
36
43
val echoTest = " SCALAC_ECHO_TEST=1"
37
- val bashCmdline = s " SCALA_OPTS= $echoTest dist/target/pack/bin/scala -classpath 'lib/* ' $relpath"
44
+ val bashCmdline = s " SCALA_OPTS= $echoTest dist/target/pack/bin/scala -classpath ' $wildcardEntry ' $relpath"
38
45
39
46
// ask [dist/bin/scalac] to echo generated command line so we can verify some things
40
47
val cmd = Array (bashExe," -c" ,bashCmdline)
41
48
42
- cmd.foreach { printf(" [%s]\n " ,_) }
49
+ // cmd.foreach { printf("[%s]\n",_) }
43
50
44
51
val javaCommandLine = exec(cmd:_* ).mkString(" " ).split(" " ).filter { _.trim.nonEmpty }
45
- javaCommandLine.foreach { printf(" scalac-java-command[%s]\n " ,_) }
52
+ printf(" \n ==================== isWin[%s], cygwin[%s], mingw[%s], msys[%s]\n " ,isWin,cygwin,mingw,msys)
53
+ javaCommandLine.foreach { printf(" java-command[%s]\n " ,_) }
46
54
47
55
val output = scala.collection.mutable.Queue (javaCommandLine:_* )
48
56
output.dequeueWhile( _ != " dotty.tools.scripting.Main" )
@@ -66,7 +74,6 @@ class ClasspathTests:
66
74
// PR #10761: verify that [dist/bin/scala] -classpath processing adds $psep to wildcard if Windows
67
75
val classpathValue = consumeNext
68
76
printf(" classpath value [%s]\n " ,classpathValue)
69
- if isWin then printf(" cygwin[%s], mingw[%s], msys[%s]\n " ,cygwin,mingw,msys)
70
77
assert( ! winshell || classpathValue.contains(psep) )
71
78
72
79
// expecting -script next
@@ -77,12 +84,57 @@ class ClasspathTests:
77
84
printf(" last: %s\n relp: %s\n " ,javaCommandLine.last,relpath)
78
85
assert(javaCommandLine.last == relpath,s " unexpected output passed to scripting.Main " )
79
86
87
+ /*
88
+ * verify classpath reported by called script.
89
+ */
90
+ @ Test def hashbangClasspathVerifyTest =
91
+ val relpath = testScript.toPath.relpath.norm
92
+ printf(" ===> hashbangClasspathVerifyTest for script [%s]\n " ,relpath)
93
+ printf(" bash is [%s]\n " ,bashExe)
94
+
95
+ if false && packBinScalaExists then
96
+ val bashCmdline = s " SCALA_OPTS= $relpath"
97
+ val cmd = Array (bashExe," -c" ,bashCmdline)
98
+
99
+ cmd.foreach { printf(" [%s]\n " ,_) }
100
+
101
+ // test script reports the classpath it sees
102
+ val scriptOutput = exec(cmd:_* )
103
+ val scriptCwd = findTaggedLine(" cwd" ,scriptOutput)
104
+ printf(" script ran in directory [%s]\n " ,scriptCwd)
105
+ val scriptCp = findTaggedLine(" classpath" ,scriptOutput)
106
+
107
+ val hashbangClasspathJars = scriptCp.split(psep).map { _.getName }.sorted.distinct
108
+ val packlibJars = listJars(s " $scriptCwd/dist/target/pack/lib " ).sorted.distinct
109
+
110
+ // verify that the classpath set in the hashbang line is effective
111
+ if hashbangClasspathJars.size != packlibJars.size then
112
+ printf(" %d test script jars in classpath\n " ,hashbangClasspathJars.size)
113
+ printf(" %d jar files in dist/target/pack/lib\n " ,packlibJars.size)
114
+
115
+ assert(hashbangClasspathJars.size == packlibJars.size)
80
116
117
+
118
+ // ////////////// end of tests ////////////////
81
119
lazy val cwd = Paths .get(" ." ).toAbsolutePath
120
+ lazy val wildcardEntry = " dist/target/pack/lib/*"
121
+
122
+ def listJars (dir : String ) =
123
+ val packlibDir = Paths .get(dir).toFile
124
+ if packlibDir.isDirectory then
125
+ packlibDir.listFiles.toList.map { _.getName }.filter { _.endsWith(" .jar" ) }
126
+ else
127
+ Nil
82
128
83
129
import scala .jdk .CollectionConverters ._
84
130
lazy val env : Map [String ,String ] = System .getenv.asScala.toMap
85
131
132
+ // script output expected as "<tag>: <value>"
133
+ def findTaggedLine (tag : String , lines : Seq [String ]): String =
134
+ lines.find { _.startsWith(tag) } match
135
+ case None => sys.error(s " no $tag: found in script output " )
136
+ case Some (cwd) => cwd.dropWhile( _ != ' ' ).trim // discard tag
137
+
86
138
def exec (cmd : String * ): Seq [String ] = Process (cmd).lazyLines_!.toList
87
139
88
140
def which (str: String ) =
@@ -125,6 +177,8 @@ extension(p:Path)
125
177
def norm : String = p.toString.replace('\\ ' ,'/' )
126
178
127
179
extension(path : String )
180
+ def getName : String = norm.replaceAll(" .*/" ," " )
181
+
128
182
// Normalize path separator, convert relative path to absolute
129
183
def norm : String =
130
184
path.replace('\\ ' , '/' ) match {
0 commit comments