summaryrefslogtreecommitdiff
path: root/src/partest/scala/tools/partest/nest/NestRunner.scala
blob: 37b842d01496bd9d0bd5e068a97bd1187996907d (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
/* NEST (New Scala Test)
 * Copyright 2007-2008 LAMP/EPFL
 * @author Philipp Haller
 */

// $Id$

package scala.tools.partest.nest

import java.io.{File, PrintStream, FileOutputStream, BufferedReader,
                InputStreamReader, StringWriter}

import scala.actors.Actor._

object NestRunner {
  private val version = System.getProperty("java.version", "")
  private val isJava5 = version matches "1.[5|6|7].*"

  private val numActors = Integer.parseInt(System.getProperty("scalatest.actors", "8"))

  private var posCheck = false
  private var negCheck = false
  private var jvmCheck = false
  private var jvm5Check = false
  private var runCheck = false
  private var resCheck = false
  private var shootoutCheck = false

  private var conservative = false

  var showDiff = false
  var showLog = false
  var failed = false

  private var testFiles: List[File] = List()
  private val con = new PrintStream(Console.out)
  private var out = con

  private val errors =
    Integer.parseInt(System.getProperty("scalatest.errors", "0"))

  def main(args: Array[String]) {
    if (args.length == 0)
      NestUI.usage()
    else {
      for (arg <- args) {
        arg match {
          case "--pos"          => posCheck = true
          case "--neg"          => negCheck = true
          case "--jvm"          => jvmCheck = true
          case "--jvm5"         => jvm5Check = true
          case "--run"          => runCheck = true
          case "--res"          => resCheck = true
          case "--shootout"     => shootoutCheck = true
          case "--conservative" => conservative = true
          case "--verbose"      => NestUI._verbose = true
          case "--show-diff"    => showDiff = true
          case "--show-log"     => showLog = true
          case "--failed"       => failed = true
          case "--version"      => //todo: printVersion
          case "--ansi"         => NestUI.initialize(NestUI.MANY)
          case _ =>
            if (arg endsWith ".scala") {
              val file = new File(arg)
              if (file.isFile) {
                NestUI.verbose("adding test file "+file)
                testFiles = file :: testFiles
              } else {
                NestUI.failure("File \"" + arg + "\" not found")
                System.exit(1)
              }
            } else if (out eq con) {
              val file = new File(arg)
              if (file.isFile || file.createNewFile)
                out = new PrintStream(new FileOutputStream(file))
              else {
                NestUI.failure("Result file \"" + arg + "\" not found")
                System.exit(1)
              }
            } else
              NestUI.usage()
        }
      }

      NestUI.outline("Source directory is : "+FileManager.srcDir.getAbsolutePath+"\n")
      NestUI.outline("Scala binaries in   : "+FileManager.BIN_DIR+"\n")

      // obtain scalac version
      //TODO: this does not work under Windows!
      val cmd = FileManager.SCALAC_CMD+" -version"
      NestUI.verbose("running "+cmd)
      val proc = Runtime.getRuntime.exec(cmd)
      val in = proc.getInputStream
      val err = proc.getErrorStream
      val writer = new StringWriter
      val errWriter = new StringWriter
      val appender = new StreamAppender(new InputStreamReader(in), writer)
      val errApp = new StreamAppender(new InputStreamReader(err), errWriter)
      appender.start()
      errApp.start()
      val exitCode = proc.waitFor()
      NestUI.verbose("exit code: "+exitCode)
      appender.join()
      errApp.join()
      val scalaVersion = writer.toString + errWriter.toString

      NestUI.outline("Scala version is    : "+scalaVersion)
      NestUI.outline("Scalac options are  : "+FileManager.SCALAC_OPTS+"\n")

      val vmBin  = System.getProperty("java.home", "")+File.separator+"bin"
      val vmName = System.getProperty("java.vm.name", "")+" (build "+
                   System.getProperty("java.vm.version", "")+", "+
                   System.getProperty("java.vm.info", "")+")"
      val vmOpts = System.getProperty("scalatest.java_options", "?")
      NestUI.outline("Java binaries in    : "+vmBin+"\n")
      NestUI.outline("Java runtime is     : "+vmName+"\n")
      NestUI.outline("Java options are    : "+vmOpts+"\n")

      val start = System.currentTimeMillis
      val (successes, failures) = testCheckAll()
      val end = System.currentTimeMillis
      val total = successes + failures

      val elapsedSecs = (end - start)/1000
      val elapsedMins = elapsedSecs/60
      val elapsedHrs  = elapsedMins/60
      val dispMins = elapsedMins - elapsedHrs  * 60
      val dispSecs = elapsedSecs - elapsedMins * 60
      val dispElapsed = {
        def form(num: Long) = if (num < 10) "0"+num else ""+num
        form(elapsedHrs)+":"+form(dispMins)+":"+form(dispSecs)
      }

      println
      if (failures == 0)
        NestUI.success("All of "+total+" tests were successful (elapsed time: "+dispElapsed+")\n")
      else
        NestUI.failure(failures+" of "+total+" tests failed (elapsed time: "+dispElapsed+")\n")

      if (failures == errors)
        System.exit(0)
      else
        System.exit(1)
    }
  }

  def runTests(kind: String, check: Boolean, msg: String): (Int, Int) = {
    if (check) {
      val fileMgr = new FileManager
      val kindFiles =
        if (!testFiles.isEmpty) {
          NestUI.verbose("testing "+testFiles)
          testFiles
        }
        else if (kind == "res") //TODO: is there a nicer way?
          fileMgr.getFiles(kind, check, ".res")
        else
          fileMgr.getFiles(kind, check)
      if (!kindFiles.isEmpty) {
        NestUI.outline("\n"+msg+"\n")

        val len = kindFiles.length
        val (testsEach, lastFrag) = (len/numActors, len%numActors)
        val last = numActors-1
        val workers = for (i <- List.range(0, numActors)) yield {
          val toTest = kindFiles.slice(i*testsEach, (i+1)*testsEach)
          val worker = new Worker
          worker.start()
          if (i == last)
            worker ! RunTests(kind, (kindFiles splitAt (last*testsEach))._2)
          else
            worker ! RunTests(kind, toTest)
          worker
        }
        var succs = 0; var fails = 0
        workers foreach { w =>
          receive {
            case Results(s, f) => succs += s; fails += f
          }
        }
        (succs, fails)

        //val worker = new Worker
        //worker.runTests(kind, kindFiles)
      } else {
        NestUI.failure("test dir empty")
        (0, 0)
      }
    } else (0, 0)
  }

  /**
   * @return (success count, failure count)
   */
  def testCheckAll(): (Int, Int) = {
    val results = List(runTests("pos", posCheck, "Testing compiler (on files whose compilation should succeed)"),
                       runTests("neg", negCheck, "Testing compiler (on files whose compilation should fail)"),
                       runTests("run", runCheck, "Testing JVM backend"),
                       runTests("jvm", jvmCheck, "Testing JVM backend"),
                       runTests("jvm5", jvm5Check, "Testing JVM backend"),
                       runTests("res", resCheck, "Testing resident compiler"))
    results reduceLeft { (p: (Int, Int), q: (Int, Int)) =>
      (p._1+q._1, p._2+q._2) }
  }
}