summaryrefslogtreecommitdiff
path: root/src/partest/scala/tools/partest/PartestTask.scala
blob: 51c77d386a2bcea1d8ea4322c152640642635216 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*                     __                                               *\
**     ________ ___   / /  ___     Scala Parallel Testing               **
**    / __/ __// _ | / /  / _ |    (c) 2007-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

package scala.tools
package partest

import scala.util.Properties.setProp
import scala.tools.nsc.io.{ Directory, Path => SPath }
import nsc.util.ClassPath
import util.PathResolver
import scala.tools.ant.sabbus.CompilationPathProperty
import java.io.File
import java.lang.reflect.Method
import org.apache.tools.ant.Task
import org.apache.tools.ant.types.{Path, Reference, FileSet}
import org.apache.tools.ant.types.Commandline.Argument

/** An Ant task to execute the Scala test suite (NSC).
 *
 *  This task can take the following parameters as attributes:
 *  - `srcdir`,
 *  - `classpath`,
 *  - `classpathref`,
 *  - `showlog`,
 *  - `showdiff`,
 *  - `erroronfailed`,
 *  - `javacmd`,
 *  - `javaccmd`,
 *  - `scalacopts`,
 *  - `timeout`,
 *  - `debug`,
 *  - `junitreportdir`.
 *
 *  It also takes the following parameters as nested elements:
 *  - `compilationpath`.
 *  - `postests`,
 *  - `negtests`,
 *  - `runtests`,
 *  - `jvmtests`,
 *  - `residenttests`,
 *  - `buildmanagertests`,
 *  - `shootouttests`,
 *  - `scalaptests`,
 *  - `scalachecktests`,
 *  - `specializedtests`,
 *  - `instrumentedtests`,
 *  - `presentationtests`,
 *  - `scripttests`.
 *
 * @author Philippe Haller
 */
class PartestTask extends Task with CompilationPathProperty {

  def addConfiguredPosTests(input: FileSet) {
    posFiles = Some(input)
  }

  def addConfiguredNegTests(input: FileSet) {
    negFiles = Some(input)
  }

  def addConfiguredRunTests(input: FileSet) {
    runFiles = Some(input)
  }

  def addConfiguredJvmTests(input: FileSet) {
    jvmFiles = Some(input)
  }

  def addConfiguredResidentTests(input: FileSet) {
    residentFiles = Some(input)
  }

  def addConfiguredBuildManagerTests(input: FileSet) {
    buildManagerFiles = Some(input)
  }

  def addConfiguredScalacheckTests(input: FileSet) {
    scalacheckFiles = Some(input)
  }

  def addConfiguredScriptTests(input: FileSet) {
    scriptFiles = Some(input)
  }

  def addConfiguredShootoutTests(input: FileSet) {
    shootoutFiles = Some(input)
  }

  def addConfiguredScalapTests(input: FileSet) {
    scalapFiles = Some(input)
  }

  def addConfiguredSpecializedTests(input: FileSet) {
    specializedFiles = Some(input)
  }

  def addConfiguredInstrumentedTests(input: FileSet) {
    instrumentedFiles = Some(input)
  }

  def addConfiguredPresentationTests(input: FileSet) {
    presentationFiles = Some(input)
  }

  def addConfiguredAntTests(input: FileSet) {
    antFiles = Some(input)
  }


  def setSrcDir(input: String) {
    srcDir = Some(input)
  }

  def setClasspath(input: Path) {
    if (classpath.isEmpty)
      classpath = Some(input)
    else
      classpath.get.append(input)
  }

  def createClasspath(): Path = {
    if (classpath.isEmpty) classpath = Some(new Path(getProject()))
    classpath.get.createPath()
  }

  def setClasspathref(input: Reference) {
    createClasspath().setRefid(input)
  }

  def setShowLog(input: Boolean) {
    showLog = input
  }

  def setShowDiff(input: Boolean) {
    showDiff = input
  }

  def setErrorOnFailed(input: Boolean) {
    errorOnFailed = input
  }

  def setJavaCmd(input: File) {
    javacmd = Some(input)
  }

  def setJavacCmd(input: File) {
    javaccmd = Some(input)
  }

  def setScalacOpts(input: String) {
    val s = input.split(' ').map { s => val a = new Argument; a.setValue(s); a }
    scalacArgs = Some(scalacArgs.getOrElse(Seq()) ++ s)
  }

  def createCompilerArg(): Argument = {
    val a = new Argument
    scalacArgs = Some(scalacArgs.getOrElse(Seq()) :+ a)
    a
  }

  def setTimeout(delay: String) {
    timeout = Some(delay)
  }

  def setDebug(input: Boolean) {
    debug = input
  }

  def setJUnitReportDir(input: File) {
    jUnitReportDir = Some(input)
  }

  private var classpath: Option[Path] = None
  private var srcDir: Option[String] = None
  private var javacmd: Option[File] = None
  private var javaccmd: Option[File] = None
  private var showDiff: Boolean = false
  private var showLog: Boolean = false
  private var posFiles: Option[FileSet] = None
  private var negFiles: Option[FileSet] = None
  private var runFiles: Option[FileSet] = None
  private var jvmFiles: Option[FileSet] = None
  private var residentFiles: Option[FileSet] = None
  private var buildManagerFiles: Option[FileSet] = None
  private var scalacheckFiles: Option[FileSet] = None
  private var scriptFiles: Option[FileSet] = None
  private var shootoutFiles: Option[FileSet] = None
  private var scalapFiles: Option[FileSet] = None
  private var specializedFiles: Option[FileSet] = None
  private var instrumentedFiles: Option[FileSet] = None
  private var presentationFiles: Option[FileSet] = None
  private var antFiles: Option[FileSet] = None
  private var errorOnFailed: Boolean = false
  private var scalacArgs: Option[Seq[Argument]] = None
  private var timeout: Option[String] = None
  private var jUnitReportDir: Option[File] = None
  private var debug = false

  def fileSetToDir(fs: FileSet) = Directory(fs getDir getProject)
  def fileSetToArray(fs: FileSet): Array[SPath] = {
    val root = fileSetToDir(fs)
    (fs getDirectoryScanner getProject).getIncludedFiles map (root / _)
  }

  private def getFiles(fileSet: Option[FileSet]): Array[File] = fileSet match {
    case None     => Array()
    case Some(fs) => fileSetToArray(fs) filterNot (_ hasExtension "log") map (_.jfile)
  }

  private def getFilesAndDirs(fileSet: Option[FileSet]): Array[File] = fileSet match {
    case None     => Array()
    case Some(fs) =>
      def shouldExclude(name: String) = (name endsWith ".obj") || (name startsWith ".")
      // println("----> " + fileSet)

      val fileTests = getFiles(Some(fs)) filterNot (x => shouldExclude(x.getName))
      val dirResult = getDirs(Some(fs))  filterNot (x => shouldExclude(x.getName))
      // println("dirs: " + dirResult.toList)
      // println("files: " + fileTests.toList)

      dirResult ++ fileTests
  }

  private def getDirs(fileSet: Option[FileSet]): Array[File] = fileSet match {
    case None     => Array()
    case Some(fs) =>
      def shouldExclude(name: String) = (name endsWith ".obj") || (name startsWith ".")

      val dirTests: Iterator[SPath] = fileSetToDir(fs).dirs filterNot (x => shouldExclude(x.name))
      val dirResult = dirTests.toList.toArray map (_.jfile)

      dirResult
  }


  private def getPosFiles          = getFilesAndDirs(posFiles)
  private def getNegFiles          = getFilesAndDirs(negFiles)
  private def getRunFiles          = getFilesAndDirs(runFiles)
  private def getJvmFiles          = getFilesAndDirs(jvmFiles)
  private def getResidentFiles     = getFiles(residentFiles)
  private def getBuildManagerFiles = getFilesAndDirs(buildManagerFiles)
  private def getScalacheckFiles   = getFilesAndDirs(scalacheckFiles)
  private def getScriptFiles       = getFiles(scriptFiles)
  private def getShootoutFiles     = getFiles(shootoutFiles)
  private def getScalapFiles       = getFiles(scalapFiles)
  private def getSpecializedFiles  = getFiles(specializedFiles)
  private def getInstrumentedFiles = getFilesAndDirs(instrumentedFiles)
  private def getPresentationFiles = getDirs(presentationFiles)
  private def getAntFiles          = getFiles(antFiles)

  override def execute() {
    val opts = getProject().getProperties() get "env.PARTEST_OPTS"
    if (opts != null && opts.toString != "")
      opts.toString.split(" ") foreach { propDef =>
        log("setting system property " + propDef)
        val kv = propDef split "="
        val key = kv(0) substring 2
        val value = kv(1)
        setProp(key, value)
      }

    if (isPartestDebug || debug) {
      setProp("partest.debug", "true")
      nest.NestUI._verbose = true
    }

    srcDir foreach (x => setProp("partest.srcdir", x))

    val classpath = this.compilationPath getOrElse sys.error("Mandatory attribute 'compilationPath' is not set.")

    val scalaLibrary = {
      (classpath.list map { fs => new File(fs) }) find { f =>
        f.getName match {
          case "scala-library.jar" => true
          case "library" if (f.getParentFile.getName == "classes") => true
          case _ => false
        }
      }
    } getOrElse sys.error("Provided classpath does not contain a Scala library.")

    val scalaReflect = {
      (classpath.list map { fs => new File(fs) }) find { f =>
        f.getName match {
          case "scala-reflect.jar" => true
          case "reflect" if (f.getParentFile.getName == "classes") => true
          case _ => false
        }
      }
    } getOrElse sys.error("Provided classpath does not contain a Scala reflection library.")

    val scalaCompiler = {
      (classpath.list map { fs => new File(fs) }) find { f =>
        f.getName match {
          case "scala-compiler.jar" => true
          case "compiler" if (f.getParentFile.getName == "classes") => true
          case _ => false
        }
      }
    } getOrElse sys.error("Provided classpath does not contain a Scala compiler.")

    val scalaPartest = {
      (classpath.list map { fs => new File(fs) }) find { f =>
        f.getName match {
          case "scala-partest.jar" => true
          case "partest" if (f.getParentFile.getName == "classes") => true
          case _ => false
        }
      }
    } getOrElse sys.error("Provided classpath does not contain a Scala partest.")

    val scalaActors = {
      (classpath.list map { fs => new File(fs) }) find { f =>
        f.getName match {
          case "scala-actors.jar" => true
          case "actors" if (f.getParentFile.getName == "classes") => true
          case _ => false
        }
      }
    } getOrElse sys.error("Provided classpath does not contain a Scala actors.")

    val scalaActorsMigration = {
      (classpath.list map { fs => new File(fs) }) find { f =>
        f.getName match {
          case "scala-actors-migration.jar" => true
          case "actors-migration" if (f.getParentFile.getName == "classes") => true
          case _ => false
        }
      }
    } getOrElse sys.error("Provided classpath does not contain a Scala actors.")

    def scalacArgsFlat: Option[Seq[String]] = scalacArgs map (_ flatMap { a =>
      val parts = a.getParts
      if(parts eq null) Seq[String]() else parts.toSeq
    })

    val antRunner = new scala.tools.partest.nest.AntRunner
    val antFileManager = antRunner.fileManager

    // this is a workaround for https://issues.scala-lang.org/browse/SI-5433
    // when that bug is fixed, this paragraph of code can be safely removed
    // we hack into the classloader that will become parent classloader for scalac
    // this way we ensure that reflective macro lookup will pick correct Code.lift
    val loader = getClass.getClassLoader.asInstanceOf[org.apache.tools.ant.AntClassLoader]
    val path = new org.apache.tools.ant.types.Path(getProject())
    val newClassPath = ClassPath.join(nest.PathSettings.srcCodeLib.toString, loader.getClasspath)
    path.setPath(newClassPath)
    loader.setClassPath(path)

    antFileManager.showDiff = showDiff
    antFileManager.showLog = showLog
    antFileManager.CLASSPATH = ClassPath.join(classpath.list: _*)
    antFileManager.LATEST_LIB = scalaLibrary.getAbsolutePath
    antFileManager.LATEST_REFLECT = scalaReflect.getAbsolutePath
    antFileManager.LATEST_COMP = scalaCompiler.getAbsolutePath
    antFileManager.LATEST_PARTEST = scalaPartest.getAbsolutePath
    antFileManager.LATEST_ACTORS = scalaActors.getAbsolutePath
    antFileManager.LATEST_ACTORS_MIGRATION = scalaActorsMigration.getAbsolutePath

    javacmd foreach (x => antFileManager.JAVACMD = x.getAbsolutePath)
    javaccmd foreach (x => antFileManager.JAVAC_CMD = x.getAbsolutePath)
    scalacArgsFlat foreach (antFileManager.SCALAC_OPTS = _)
    timeout foreach (antFileManager.timeout = _)

    type TFSet = (Array[File], String, String)
    val testFileSets = List(
      (getPosFiles, "pos", "Compiling files that are expected to build"),
      (getNegFiles, "neg", "Compiling files that are expected to fail"),
      (getRunFiles, "run", "Compiling and running files"),
      (getJvmFiles, "jvm", "Compiling and running files"),
      (getResidentFiles, "res", "Running resident compiler scenarii"),
      (getBuildManagerFiles, "buildmanager", "Running Build Manager scenarii"),
      (getScalacheckFiles, "scalacheck", "Running scalacheck tests"),
      (getScriptFiles, "script", "Running script files"),
      (getShootoutFiles, "shootout", "Running shootout tests"),
      (getScalapFiles, "scalap", "Running scalap tests"),
      (getSpecializedFiles, "specialized", "Running specialized files"),
      (getInstrumentedFiles, "instrumented", "Running instrumented files"),
      (getPresentationFiles, "presentation", "Running presentation compiler test files"),
      (getAntFiles, "ant", "Running ant task tests")
    )

    def runSet(set: TFSet): (Int, Int, Iterable[String]) = {
      val (files, name, msg) = set
      if (files.isEmpty) (0, 0, List())
      else {
        log(msg)
        val results: Iterable[(String, TestState)] = antRunner.reflectiveRunTestsForFiles(files, name)
        val (succs, fails) = resultsToStatistics(results)

        val failed: Iterable[String] = results collect {
          case (path, TestState.Fail)    => path + " [FAILED]"
          case (path, TestState.Timeout) => path + " [TIMOUT]"
        }

        // create JUnit Report xml files if directory was specified
        jUnitReportDir foreach { d =>
          d.mkdir

          val report = testReport(name, results, succs, fails)
          scala.xml.XML.save(d.getAbsolutePath+"/"+name+".xml", report)
        }

        (succs, fails, failed)
      }
    }

    val _results = testFileSets map runSet
    val allSuccesses = _results map (_._1) sum
    val allFailures = _results map (_._2) sum
    val allFailedPaths = _results flatMap (_._3)

    def f = if (errorOnFailed && allFailures > 0) (sys error _) else log(_: String)
    def s = if (allFailures > 1) "s" else ""
    val msg =
      if (allFailures > 0)
        "Test suite finished with %d case%s failing:\n".format(allFailures, s)+
        allFailedPaths.mkString("\n")
      else if (allSuccesses == 0) "There were no tests to run."
      else "Test suite finished with no failures."

    f(msg)
  }

  private def oneResult(res: (String, TestState)) =
    <testcase name={res._1}>{
      res._2 match {
        case TestState.Ok      => scala.xml.NodeSeq.Empty
        case TestState.Fail    => <failure message="Test failed"/>
        case TestState.Timeout => <failure message="Test timed out"/>
      }
    }</testcase>

  private def testReport(kind: String, results: Iterable[(String, TestState)], succs: Int, fails: Int) =
    <testsuite name={kind} tests={(succs + fails).toString} failures={fails.toString}>
      <properties/>
      {
        results.map(oneResult(_))
      }
    </testsuite>
}