1
1
/** Adapted from https://github.com/sbt/sbt/blob/0.13/compile/interface/src/test/scala/xsbt/ScalaCompilerForUnitTesting.scala */
2
2
package xsbt
3
3
4
- import xsbti .compile .SingleOutput
4
+ import xsbti .compile .{ CompileProgress , SingleOutput }
5
5
import java .io .File
6
6
import xsbti ._
7
7
import sbt .io .IO
8
8
import xsbti .api .{ ClassLike , Def , DependencyContext }
9
9
import DependencyContext ._
10
10
import xsbt .api .SameAPI
11
11
import sbt .internal .util .ConsoleLogger
12
+ import dotty .tools .io .PlainFile .toPlainFile
13
+ import dotty .tools .xsbt .CompilerBridge
12
14
13
15
import TestCallback .ExtractedClassDependencies
14
16
@@ -32,8 +34,8 @@ class ScalaCompilerForUnitTesting {
32
34
* Compiles given source code using Scala compiler and returns API representation
33
35
* extracted by ExtractAPI class.
34
36
*/
35
- def extractApisFromSrcs (reuseCompilerInstance : Boolean )( srcs : List [String ]* ): Seq [Seq [ClassLike ]] = {
36
- val (tempSrcFiles, analysisCallback) = compileSrcs(srcs.toList, reuseCompilerInstance )
37
+ def extractApisFromSrcs (srcs : List [String ]* ): Seq [Seq [ClassLike ]] = {
38
+ val (tempSrcFiles, analysisCallback) = compileSrcs(srcs.toList)
37
39
tempSrcFiles.map(analysisCallback.apis)
38
40
}
39
41
@@ -91,7 +93,7 @@ class ScalaCompilerForUnitTesting {
91
93
* file system-independent way of testing dependencies between source code "files".
92
94
*/
93
95
def extractDependenciesFromSrcs (srcs : List [List [String ]]): ExtractedClassDependencies = {
94
- val (_, testCallback) = compileSrcs(srcs, reuseCompilerInstance = true )
96
+ val (_, testCallback) = compileSrcs(srcs)
95
97
96
98
val memberRefDeps = testCallback.classDependencies collect {
97
99
case (target, src, DependencyByMemberRef ) => (src, target)
@@ -117,79 +119,53 @@ class ScalaCompilerForUnitTesting {
117
119
* useful to compile macros, which cannot be used in the same compilation run that
118
120
* defines them.
119
121
*
120
- * The `reuseCompilerInstance` parameter controls whether the same Scala compiler instance
121
- * is reused between compiling source groups. Separate compiler instances can be used to
122
- * test stability of API representation (with respect to pickling) or to test handling of
123
- * binary dependencies.
124
- *
125
122
* The sequence of temporary files corresponding to passed snippets and analysis
126
123
* callback is returned as a result.
127
124
*/
128
- def compileSrcs (groupedSrcs : List [List [String ]],
129
- reuseCompilerInstance : Boolean ): (Seq [File ], TestCallback ) = {
130
- // withTemporaryDirectory { temp =>
131
- {
125
+ def compileSrcs (groupedSrcs : List [List [String ]]): (Seq [File ], TestCallback ) = {
132
126
val temp = IO .createTemporaryDirectory
133
127
val analysisCallback = new TestCallback
134
128
val classesDir = new File (temp, " classes" )
135
129
classesDir.mkdir()
136
130
137
- lazy val commonCompilerInstanceAndCtx = prepareCompiler(classesDir, analysisCallback, classesDir.toString)
131
+ val bridge = new CompilerBridge
138
132
139
133
val files = for ((compilationUnit, unitId) <- groupedSrcs.zipWithIndex) yield {
140
- // use a separate instance of the compiler for each group of sources to
141
- // have an ability to test for bugs in instability between source and pickled
142
- // representation of types
143
- val (compiler, ctx) = if (reuseCompilerInstance) commonCompilerInstanceAndCtx else
144
- prepareCompiler(classesDir, analysisCallback, classesDir.toString)
145
- val run = compiler.newRun(ctx)
146
- val srcFiles = compilationUnit.toSeq.zipWithIndex map {
147
- case (src, i) =>
134
+ val srcFiles = compilationUnit.toSeq.zipWithIndex.map {
135
+ (src, i) =>
148
136
val fileName = s " Test- $unitId- $i.scala "
149
137
prepareSrcFile(temp, fileName, src)
150
138
}
151
- val srcFilePaths = srcFiles.map(srcFile => srcFile.getAbsolutePath).toList
152
139
153
- run.compile(srcFilePaths)
140
+ val virtualSrcFiles = srcFiles.map(file => TestVirtualFile (file.toPath)).toArray
141
+ val classesDirPath = classesDir.getAbsolutePath.toString
142
+ val output = new SingleOutput :
143
+ def getOutputDirectory () = classesDir
144
+
145
+ bridge.run(
146
+ virtualSrcFiles.toArray,
147
+ new TestDependencyChanges ,
148
+ Array (" -Yforce-sbt-phases" , " -classpath" , classesDirPath, " -usejavacp" , " -d" , classesDirPath),
149
+ output,
150
+ analysisCallback,
151
+ new TestReporter ,
152
+ new CompileProgress {},
153
+ new TestLogger
154
+ )
154
155
155
- // srcFilePaths.foreach(f => new File(f).delete)
156
156
srcFiles
157
157
}
158
158
(files.flatten.toSeq, analysisCallback)
159
- }
160
159
}
161
160
162
161
def compileSrcs (srcs : String * ): (Seq [File ], TestCallback ) = {
163
- compileSrcs(List (srcs.toList), reuseCompilerInstance = true )
162
+ compileSrcs(List (srcs.toList))
164
163
}
165
164
166
165
private def prepareSrcFile (baseDir : File , fileName : String , src : String ): File = {
167
166
val srcFile = new File (baseDir, fileName)
168
167
IO .write(srcFile, src)
169
168
srcFile
170
169
}
171
-
172
- private def prepareCompiler (outputDir : File , analysisCallback : AnalysisCallback , classpath : String = " ." ) = {
173
- val args = Array .empty[String ]
174
-
175
- import dotty .tools .dotc .{Compiler , Driver }
176
- import dotty .tools .dotc .core .Contexts ._
177
-
178
- val driver = new TestDriver
179
- val ctx = (new ContextBase ).initialCtx.fresh.setSbtCallback(analysisCallback)
180
- driver.getCompiler(Array (" -classpath" , classpath, " -usejavacp" , " -d" , outputDir.getAbsolutePath), ctx)
181
- }
182
-
183
- private object ConsoleReporter extends Reporter {
184
- def reset (): Unit = ()
185
- def hasErrors : Boolean = false
186
- def hasWarnings : Boolean = false
187
- def printWarnings (): Unit = ()
188
- def problems (): Array [xsbti.Problem ] = Array .empty
189
- def log (problem : xsbti.Problem ): Unit = println(problem.message)
190
- def comment (pos : Position , msg : String ): Unit = ()
191
- def printSummary (): Unit = ()
192
- }
193
-
194
170
}
195
171
0 commit comments