summaryrefslogtreecommitdiff
path: root/src/partest/scala/tools/partest/PartestTask.scala
blob: 8b88021dbfbb5fef36aeca0a10ffc126b6b9c06b (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
/*                     __                                               *\
**     ________ ___   / /  ___     Scala Parallel Testing               **
**    / __/ __// _ | / /  / _ |    (c) 2007-2013, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

package scala.tools
package partest

import scala.util.Properties.setProp
import scala.tools.ant.sabbus.CompilationPathProperty
import java.lang.reflect.Method
import org.apache.tools.ant.Task
import org.apache.tools.ant.types.{ Reference, FileSet}
import org.apache.tools.ant.types.Commandline.Argument
import scala.tools.ant.ScalaTask

/** An Ant task to execute the Scala test suite (NSC).
 *
 *  This task can take the following parameters as attributes:
 *  - `srcdir`,
 *  - `classpath`,
 *  - `classpathref`,
 *  - `erroronfailed`,
 *  - `javacmd`,
 *  - `javaccmd`,
 *  - `scalacopts`,
 *  - `debug`,
 *  - `junitreportdir`.
 *
 *  It also takes the following parameters as nested elements:
 *  - `compilationpath`.
 *
 * @author Philippe Haller
 */
class PartestTask extends Task with CompilationPathProperty with ScalaTask {
  type Path = org.apache.tools.ant.types.Path

  private var kinds: List[String]               = Nil
  private var classpath: Option[Path]           = None
  private var debug                             = false
  private var errorOnFailed: Boolean            = true
  private var jUnitReportDir: Option[File]      = None
  private var javaccmd: Option[File]            = None
  private var javacmd: Option[File]             = Option(sys.props("java.home")) map (x => new File(x, "bin/java"))
  private var scalacArgs: Option[Seq[Argument]] = None
  private var srcDir: Option[String]            = None
  private var colors: Int = 0

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

  def setColors(input: String) {
    try colors = input.toInt catch { case _: NumberFormatException => () }
    if (colors > 0)
      sys.props("partest.colors") = colors.toString
  }

  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 setErrorOnFailed(input: Boolean) {
    errorOnFailed = input
  }

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

  def setKinds(input: String) {
    kinds = words(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 setDebug(input: Boolean) {
    debug = input
  }

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

  override def execute() {
    if (debug || sys.props.contains("partest.debug")) {
      nest.NestUI.setDebug()
    }

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

    val classpath = this.compilationPath getOrElse sys.error("Mandatory attribute 'compilationPath' is not set.")
    val cpfiles = classpath.list map { fs => new File(fs) } toList
    def findCp(name: String) = cpfiles find (f =>
         (f.getName == s"scala-$name.jar")
      || (f.absolutePathSegments endsWith Seq("classes", name))
    ) getOrElse sys.error(s"Provided classpath does not contain a Scala $name element.")

    val scalaLibrary         = findCp("library")
    val scalaReflect         = findCp("reflect")
    val scalaCompiler        = findCp("compiler")
    val scalaPartest         = findCp("partest")
    val scalaActors          = findCp("actors")

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

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

    // antFileManager.failed = runFailed
    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

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

    def runSet(kind: String, files: Array[File]): (Int, Int, List[String]) = {
      if (files.isEmpty) (0, 0, List())
      else {
        log(s"Running ${files.length} tests in '$kind' at $now")
        // log(s"Tests: ${files.toList}")
        val results = antRunner.reflectiveRunTestsForFiles(files, kind)
        val (passed, failed) = results partition (_.isOk)
        val numPassed = passed.size
        val numFailed = failed.size
        def failedMessages = failed map (_.longStatus)

        log(s"Completed '$kind' at $now")

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

          val report = testReport(kind, results, numPassed, numFailed)
          scala.xml.XML.save(d.getAbsolutePath+"/"+kind+".xml", report)
        }

        (numPassed, numFailed, failedMessages)
      }
    }

    val _results       = kinds map (k => runSet(k, TestKinds testsFor k map (_.jfile) toArray))
    val allSuccesses   = _results map (_._1) sum
    val allFailures    = _results map (_._2) sum
    val allFailedPaths = _results flatMap (_._3)

    def f = if (errorOnFailed && allFailures > 0) buildError(_: String) 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: TestState) =
    <testcase name={res.testIdent}>{
      if (res.isOk) scala.xml.NodeSeq.Empty
      else <failure message="Test failed"/>
    }</testcase>

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