@@ -24,6 +24,7 @@ import java.net.URI
24
24
import scala .reflect .io .AbstractFile
25
25
import scala .collection .mutable
26
26
import scala .reflect .internal .util .ScalaClassLoader
27
+ import java .net .URLClassLoader
27
28
28
29
object FileManager {
29
30
def getLogFile (dir : File , fileBase : String , kind : String ): File =
@@ -97,91 +98,41 @@ object FileManager {
97
98
if (diff.getDeltas.isEmpty) " "
98
99
else difflib.DiffUtils .generateUnifiedDiff(originalName, revisedName, original.asJava, diff, 1 ).asScala.mkString(" \n " )
99
100
}
100
-
101
- // only used by script-based partest, not recommended for anything else
102
- def classPathFromTrifecta (library : Path , reflect : Path , compiler : Path ) = {
103
- val usingJars = library.getAbsolutePath endsWith " .jar"
104
- // basedir for jars or classfiles on core classpath
105
- val baseDir = SFile (library).parent
106
-
107
- def relativeToLibrary (what : String ): Path = {
108
- if (usingJars) (baseDir / s " $what.jar " )
109
- else (baseDir.parent / " classes" / what)
110
- }
111
-
112
- // all jars or dirs with prefix `what`
113
- def relativeToLibraryAll (what : String ): Iterator [Path ] = (
114
- if (usingJars) jarsWithPrefix(baseDir, what)
115
- else dirsWithPrefix(baseDir.parent / " classes" toDirectory, what)
116
- )
117
-
118
- List [Path ](
119
- library, reflect, compiler,
120
- relativeToLibrary(" scala-actors" ),
121
- relativeToLibrary(" scala-parser-combinators" ),
122
- relativeToLibrary(" scala-xml" ),
123
- relativeToLibrary(" scala-scaladoc" ),
124
- relativeToLibrary(" scala-interactive" ),
125
- relativeToLibrary(" scalap" ),
126
- PathSettings .diffUtils.fold(sys.error, identity)
127
- ) ++ relativeToLibraryAll(" scala-partest" )
128
- }
129
-
130
- // find library/reflect/compiler jar or subdir under build/$stage/classes/
131
- // TODO: make more robust -- for now, only matching on prefix of jar file so it works for ivy/maven-resolved versioned jars
132
- // can we use the ClassLoader to find the jar/directory that contains a characteristic class file?
133
- def fromClassPath (name : String , testClassPath : List [Path ]): Path = {
134
- // the old approach:
135
- def fallback =
136
- testClassPath find (f =>
137
- (f.extension == " jar" && f.getName.startsWith(s " scala- $name" ))
138
- || (f.absolutePathSegments endsWith Seq (" classes" , name))
139
- ) getOrElse sys.error(s " Provided compilationPath does not contain a Scala $name element. \n Looked in: ${testClassPath.mkString(" :" )}" )
140
-
141
- // more precise:
142
- try {
143
- val classLoader = ScalaClassLoader fromURLs (testClassPath map (_.toURI.toURL))
144
- val canaryClass =
145
- name match {
146
- case " library" => Class .forName(" scala.Unit" , false , classLoader)
147
- case " reflect" => Class .forName(" scala.reflect.api.Symbols" , false , classLoader)
148
- case " compiler" => Class .forName(" scala.tools.nsc.Main" , false , classLoader)
149
- }
150
- val path = Path (canaryClass.getProtectionDomain.getCodeSource.getLocation.getPath)
151
- if (path.extension == " jar" || path.absolutePathSegments.endsWith(Seq (" classes" , name))) path
152
- else fallback
153
- } catch {
154
- case everything : Exception => fallback
155
- }
156
- }
157
101
}
158
102
159
- class FileManager private (val testClassPath : List [Path ],
160
- val libraryUnderTest : Path ,
161
- val reflectUnderTest : Path ,
162
- val compilerUnderTest : Path ) {
163
- def this (testClassPath : List [Path ]) {
164
- this (testClassPath,
165
- FileManager .fromClassPath(" library" , testClassPath),
166
- FileManager .fromClassPath(" reflect" , testClassPath),
167
- FileManager .fromClassPath(" compiler" , testClassPath))
168
- }
103
+ class FileManager (val testClassLoader : URLClassLoader ) {
104
+ lazy val libraryUnderTest : Path = findArtifact(" library" )
105
+ lazy val reflectUnderTest : Path = findArtifact(" reflect" )
106
+ lazy val compilerUnderTest : Path = findArtifact(" compiler" )
169
107
170
- protected [nest] def this (libraryUnderTest : Path , reflectUnderTest : Path , compilerUnderTest : Path ) {
171
- this (FileManager .classPathFromTrifecta(libraryUnderTest, reflectUnderTest, compilerUnderTest), libraryUnderTest, reflectUnderTest, compilerUnderTest)
172
- }
108
+ lazy val testClassPath = testClassLoader.getURLs().map(url => Path (new File (url.toURI))).toList
173
109
174
- protected [nest] def this (testClassPath : List [Path ], trifecta : ( Path , Path , Path ) ) {
175
- this (testClassPath, trifecta._1, trifecta._2, trifecta._3 )
110
+ def this (testClassPath : List [Path ]) {
111
+ this (new URLClassLoader (testClassPath.toArray map (_.toURI.toURL)) )
176
112
}
177
113
178
- lazy val testClassLoader = ScalaClassLoader fromURLs (testClassPath map (_.toURI.toURL))
179
-
180
114
def distKind = {
181
115
val p = libraryUnderTest.getAbsolutePath
182
116
if (p endsWith " build/quick/classes/library" ) " quick"
183
117
else if (p endsWith " build/pack/lib/scala-library.jar" ) " pack"
184
118
else if (p endsWith " dists/latest/lib/scala-library.jar" ) " latest"
185
119
else " installed"
186
120
}
121
+
122
+ // find library/reflect/compiler jar or subdir under build/$stage/classes/
123
+ private def findArtifact (name : String ): Path = {
124
+ val canaryClass =
125
+ name match {
126
+ case " library" => Class .forName(" scala.Unit" , false , testClassLoader)
127
+ case " reflect" => Class .forName(" scala.reflect.api.Symbols" , false , testClassLoader)
128
+ case " compiler" => Class .forName(" scala.tools.nsc.Main" , false , testClassLoader)
129
+ }
130
+
131
+ val path = Path (canaryClass.getProtectionDomain.getCodeSource.getLocation.getPath)
132
+ if (path.extension == " jar"
133
+ || path.absolutePathSegments.endsWith(Seq (" classes" , name))) path
134
+ else sys.error(
135
+ s """ Test Classloader does not contain a $name jar or classes/ $name directory.
136
+ |Looked in: $testClassPath""" )
137
+ }
187
138
}
0 commit comments