summaryrefslogtreecommitdiff
path: root/src/partest/scala/tools/partest/nest/Worker.scala
blob: 2e2049ffbeac962fe38cd60e0a0855738dc951cc (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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
/* NEST (New Scala Test)
 * Copyright 2007-2009 LAMP/EPFL
 * @author Philipp Haller
 */

// $Id$

package scala.tools.partest
package nest

import java.io._
import java.net.{URLClassLoader, URL}
import java.util.{Timer, TimerTask}

import scala.tools.nsc.{ObjectRunner, GenericRunnerCommand}
import scala.tools.nsc.io

import scala.actors.{Actor, Exit, TIMEOUT}
import scala.actors.Actor._
import scala.tools.scalap.scalax.rules.scalasig.{ByteCode, ClassFileParser, ScalaSigAttributeParsers}

import scala.collection.mutable.HashMap

case class RunTests(kind: String, files: List[File])
case class Results(succ: Int, fail: Int, logs: List[LogFile], outdirs: List[File])
case class LogContext(file: LogFile, writers: Option[(StringWriter, PrintWriter)])

abstract class TestResult {
  def file: File
}
case class Result(override val file: File, context: LogContext) extends TestResult
case class Timeout(override val file: File) extends TestResult

class LogFile(parent: File, child: String) extends File(parent, child) {
  var toDelete = false
}

class Worker(val fileManager: FileManager) extends Actor {
  import fileManager._
  import scala.tools.nsc.{Settings, CompilerCommand, Global}
  import scala.tools.nsc.reporters.ConsoleReporter
  import scala.tools.nsc.util.FakePos

  var reporter: ConsoleReporter = _
  val timer = new Timer

  def error(msg: String) {
    reporter.error(FakePos("scalac"),
                   msg + "\n  scalac -help  gives more information")
  }

  def act() {
    react {
      case RunTests(kind, files) =>
        NestUI.verbose("received "+files.length+" to test")
        val master = sender
        runTests(kind, files, (succ: Int, fail: Int) => {
          master ! Results(succ, fail, createdLogFiles, createdOutputDirs)
        })
    }
  }

  private def basename(name: String): String = {
    val inx = name.lastIndexOf(".")
    if (inx < 0) name else name.substring(0, inx)
  }

  def printInfoStart(file: File, printer: PrintWriter) {
    NestUI.outline("testing: ", printer)
    val filesdir = file.getAbsoluteFile.getParentFile.getParentFile
    val testdir = filesdir.getParentFile
    val totalWidth = 56
    val name = {
      // 1. try with [...]/files/run/test.scala
      val testPathLen = testdir.getAbsolutePath.length
      val name = file.getAbsolutePath.substring(testPathLen)
      if (name.length <= totalWidth)
        name
      // 2. try with [...]/run/test.scala
      else {
        val filesPathLen = filesdir.getAbsolutePath.length
        file.getAbsolutePath.substring(filesPathLen)
      }
    }
    NestUI.normal("[...]"+name+List.toString(List.fill(totalWidth-name.length)(' ')), printer)
  }

  def printInfoEnd(success: Boolean, printer: PrintWriter) {
    NestUI.normal("[", printer)
    if (success) NestUI.success("  OK  ", printer)
    else NestUI.failure("FAILED", printer)
    NestUI.normal("]\n", printer)
  }

  def printInfoTimeout(printer: PrintWriter) {
    NestUI.normal("[", printer)
    NestUI.failure("TIMOUT", printer)
    NestUI.normal("]\n", printer)
  }

  var log = ""
  var createdLogFiles: List[LogFile] = List()
  var createdOutputDirs: List[File] = List()

  def createLogFile(file: File, kind: String): LogFile = {
    val logFile = fileManager.getLogFile(file, kind)
    createdLogFiles = logFile :: createdLogFiles
    logFile
  }

  def createOutputDir(dir: File, fileBase: String, kind: String): File = {
    val outDir = new File(dir, fileBase + "-" + kind + ".obj")
    if (!outDir.exists)
      outDir.mkdir()
    createdOutputDirs = outDir :: createdOutputDirs
    outDir
  }

  /* Note: not yet used/tested. */
  def execTestObjectRunner(file: File, outDir: File, logFile: File) {
    val consFM = new ConsoleFileManager
    import consFM.{latestCompFile, latestLibFile, latestActFile,
                   latestPartestFile}

    val classpath: List[URL] =
      outDir.toURL ::
      //List(file.getParentFile.toURL) :::
      List(latestCompFile.toURL, latestLibFile.toURL,
           latestActFile.toURL, latestPartestFile.toURL) :::
      (List.fromString(CLASSPATH, File.pathSeparatorChar) map { x =>
        (new File(x)).toURL })
    NestUI.verbose("ObjectRunner classpath: "+classpath)

    try {
      // configure input/output files
      val logOut    = new FileOutputStream(logFile)
      val logWriter = new PrintStream(logOut)

      // grab global lock
      fileManager.synchronized {

        val oldStdOut = System.out
        val oldStdErr = System.err
        System.setOut(logWriter)
        System.setErr(logWriter)

        /*
         " -Djava.library.path="+logFile.getParentFile.getAbsolutePath+
         " -Dscalatest.output="+outDir.getAbsolutePath+
         " -Dscalatest.lib="+LATEST_LIB+
         " -Dscalatest.cwd="+outDir.getParent+
         " -Djavacmd="+JAVACMD+
         */

        System.setProperty("java.library.path", logFile.getParentFile.getCanonicalFile.getAbsolutePath)
        System.setProperty("scalatest.output", outDir.getCanonicalFile.getAbsolutePath)
        System.setProperty("scalatest.lib", LATEST_LIB)
        System.setProperty("scalatest.cwd", outDir.getParent)

        ObjectRunner.run(classpath, "Test", List("jvm"))

        logWriter.flush()
        logWriter.close()

        System.setOut(oldStdOut)
        System.setErr(oldStdErr)
      }

      /*val out = new FileOutputStream(logFile, true)
      Console.withOut(new PrintStream(out)) {
        ObjectRunner.run(classpath, "Test", List("jvm"))
      }
      out.flush
      out.close*/
    } catch {
      case e: Exception =>
        NestUI.verbose(e+" ("+file.getPath+")")
        e.printStackTrace()
    }
  }

  def javac(outDir: File, files: List[File], output: File): Boolean = {
    // compile using command-line javac compiler
    val javacCmd = if ((fileManager.JAVAC_CMD.indexOf("${env.JAVA_HOME}") != -1) ||
                       fileManager.JAVAC_CMD.equals("/bin/javac") ||
                       fileManager.JAVAC_CMD.equals("\\bin\\javac"))
      "javac"
    else
      fileManager.JAVAC_CMD

    val cmd = javacCmd+
      " -d "+outDir.getAbsolutePath+
      " -classpath "+outDir+File.pathSeparator+CLASSPATH+
      " "+files.mkString(" ")

    val (success, msg) = try {
      val exitCode = runCommand(cmd, output)
      NestUI.verbose("javac returned exit code: "+exitCode)
      if (exitCode != 0)
        (false, "Running \"javac\" failed with exit code: "+exitCode+"\n"+cmd+"\n")
      else
        (true, "")
    } catch {
      case e: Exception =>
        val swriter = new StringWriter
        e.printStackTrace(new PrintWriter(swriter))
        (false, "Running \"javac\" failed:\n"+cmd+"\n"+swriter.toString+"\n")
    }
    if (!success) {
      val writer = new PrintWriter(new FileWriter(output, true), true)
      writer.print(msg)
      writer.close()
    }
    success
  }

  /** Runs <code>command</code> redirecting standard out and
   *  error out to <code>output</code> file.
   */
  def runCommand(command: String, output: File): Int = {
    NestUI.verbose("running command:\n"+command)
    val proc = Runtime.getRuntime.exec(command)
    val in = proc.getInputStream
    val err = proc.getErrorStream
    val writer = new PrintWriter(new FileWriter(output), true)
    val inApp = new StreamAppender(new BufferedReader(new InputStreamReader(in)),
                                   writer)
    val errApp = new StreamAppender(new BufferedReader(new InputStreamReader(err)),
                                    writer)
    val async = new Thread(errApp)
    async.start()
    inApp.run()
    async.join()
    writer.close()
    try {
      proc.exitValue()
    } catch {
      case e: IllegalThreadStateException => 0
    }
  }

  def execTest(outDir: File, logFile: File, fileBase: String) {
    // check whether there is a ".javaopts" file
    val argsFile = new File(logFile.getParentFile, fileBase+".javaopts")
    val argString = if (argsFile.exists) {
      NestUI.verbose("Found javaopts file: "+argsFile)
      val fileReader = new FileReader(argsFile)
      val reader = new BufferedReader(fileReader)
      val options = reader.readLine()
      reader.close()
      NestUI.verbose("Found javaopts file '%s', using options: '%s'".format(argsFile, options))
      options
    } else ""

    val cp = System.getProperty("java.class.path", ".")
    NestUI.verbose("java.class.path: "+cp)

    def quote(path: String) = "\""+path+"\""

    // Note! As this currently functions, JAVA_OPTS must precede argString
    // because when an option is repeated to java only the last one wins.
    // That means until now all the .javaopts files were being ignored because
    // they all attempt to change options which are also defined in
    // scalatest.java_opts, leading to debug output like:
    //
    // debug: Found javaopts file 'files/shootout/message.scala-2.javaopts', using options: '-Xss32k'
    // debug: java -Xss32k -Xss2m -Xms256M -Xmx1024M -classpath [...]
    val cmd =
      JAVACMD+
      " "+JAVA_OPTS+
      " "+argString+
      " -classpath "+outDir+File.pathSeparator+CLASSPATH+
      " -Djava.library.path="+logFile.getParentFile.getAbsolutePath+
      " -Dscalatest.output="+outDir.getAbsolutePath+
      " -Dscalatest.lib="+LATEST_LIB+
      " -Dscalatest.cwd="+outDir.getParent+
      " -Djavacmd="+JAVACMD+
      " -Duser.language=en -Duser.country=US"+
      " scala.tools.nsc.MainGenericRunner"+
      " Test jvm"
    NestUI.verbose(cmd)

    runCommand(cmd, logFile)

    if (fileManager.showLog) {
      // produce log as string in `log`
      val reader = new BufferedReader(new FileReader(logFile))
      val swriter = new StringWriter
      val pwriter = new PrintWriter(swriter, true)
      val appender = new StreamAppender(reader, pwriter)
      appender.run()
      log = swriter.toString
    }
  }

  def existsCheckFile(dir: File, fileBase: String, kind: String) = {
    val checkFile = {
      val chkFile = new File(dir, fileBase + ".check")
      if (chkFile.isFile)
        chkFile
      else
        new File(dir, fileBase + "-" + kind + ".check")
    }
    checkFile.exists && checkFile.canRead
  }

  def compareOutput(dir: File, fileBase: String, kind: String, logFile: File): String = {
    // if check file exists, compare with log file
    val checkFile = {
      val chkFile = new File(dir, fileBase + ".check")
      if (chkFile.isFile)
        chkFile
      else
        new File(dir, fileBase + "-" + kind + ".check")
    }
    if (!checkFile.exists || !checkFile.canRead) {
      val reader = new BufferedReader(new FileReader(logFile))
      val swriter = new StringWriter
      val pwriter = new PrintWriter(swriter, true)
      val appender = new StreamAppender(reader, pwriter)
      appender.run()
      swriter.toString
    }
    else fileManager.compareFiles(logFile, checkFile)
  }

  def file2String(logFile: File) = {
    val logReader = new BufferedReader(new FileReader(logFile))
    val strWriter = new StringWriter
    val logWriter = new PrintWriter(strWriter, true)
    val logAppender = new StreamAppender(logReader, logWriter)
    logAppender.run()
    logReader.close()
    strWriter.toString
  }

  /** Runs a list of tests.
   *
   * @param kind  The test kind (pos, neg, run, etc.)
   * @param files The list of test files
   */
  def runTests(kind: String, files: List[File], topcont: (Int, Int) => Unit) {
    val compileMgr = new CompileManager(fileManager)
    var errors = 0
    var succeeded = true
    var diff = ""
    var log = ""

    /** 1. Creates log file and output directory.
     *  2. Runs <code>script</code> function, providing log file and
     *     output directory as arguments.
     */
    def runInContext(file: File, kind: String, script: (File, File) => Unit): LogContext = {
      // when option "--failed" is provided
      // execute test only if log file is present
      // (which means it failed before)
      val logFile = createLogFile(file, kind)
      if (!fileManager.failed || (logFile.exists && logFile.canRead)) {
        val swr = new StringWriter
        val wr = new PrintWriter(swr)
        succeeded = true
        diff = ""
        log = ""
        printInfoStart(file, wr)

        val fileBase: String = basename(file.getName)
        NestUI.verbose(this+" running test "+fileBase)
        val dir = file.getParentFile
        val outDir = createOutputDir(dir, fileBase, kind)
        NestUI.verbose("output directory: "+outDir)

        // run test-specific code
        try {
          script(logFile, outDir)
        } catch {
          case e: Exception =>
            val writer = new PrintWriter(new FileWriter(logFile), true)
            e.printStackTrace(writer)
            writer.close()
            succeeded = false
        }

        LogContext(logFile, Some((swr, wr)))
      } else
        LogContext(logFile, None)
    }

    def compileFilesIn(dir: File, kind: String, logFile: File, outDir: File) {
      val testFiles = dir.listFiles.toList

      val groups = for (i <- 0 to 9) yield testFiles filter { f =>
        f.getName.endsWith("_"+i+".java") ||
        f.getName.endsWith("_"+i+".scala") }

      val noSuffix = testFiles filter { f =>
        !groups.exists(_ contains f) && (
        f.getName.endsWith(".java") ||
        f.getName.endsWith(".scala")) }

      def compileGroup(g: List[File]) {
        val scalaFiles = g.filter(_.getName.endsWith(".scala"))
        val javaFiles = g.filter(_.getName.endsWith(".java"))

        if (!scalaFiles.isEmpty &&
            !compileMgr.shouldCompile(outDir,
                                      javaFiles ::: scalaFiles,
                                      kind, logFile)) {
          NestUI.verbose("scalac: compilation of "+g+" failed\n")
          succeeded = false
        }

        if (succeeded && !javaFiles.isEmpty) {
          succeeded = javac(outDir, javaFiles, logFile)
          if (succeeded && !scalaFiles.isEmpty
              && !compileMgr.shouldCompile(outDir,
                                           scalaFiles,
                                           kind, logFile)) {
            NestUI.verbose("scalac: compilation of "+scalaFiles+" failed\n")
            succeeded = false
          }
        }
      }

      if (!noSuffix.isEmpty)
        compileGroup(noSuffix)
      for (grp <- groups) {
        if (succeeded)
          compileGroup(grp)
      }
    }

    def failCompileFilesIn(dir: File, kind: String, logFile: File, outDir: File) {
      val testFiles = dir.listFiles.toList
      val javaFiles = testFiles.filter(_.getName.endsWith(".java"))
      val scalaFiles = testFiles.filter(_.getName.endsWith(".scala"))
      if (!(scalaFiles.isEmpty && javaFiles.isEmpty) &&
          !compileMgr.shouldFailCompile(outDir, javaFiles ::: scalaFiles, kind, logFile)) {
        NestUI.verbose("compilation of "+scalaFiles+" failed\n")
        succeeded = false
      }
    }

    def runJvmTest(file: File, kind: String): LogContext =
      runInContext(file, kind, (logFile: File, outDir: File) => {
        if (file.isDirectory) {
          compileFilesIn(file, kind, logFile, outDir)
        } else if (!compileMgr.shouldCompile(List(file), kind, logFile)) {
          NestUI.verbose("compilation of "+file+" failed\n")
          succeeded = false
        }
        if (succeeded) { // run test
          val fileBase = basename(file.getName)
          val dir      = file.getParentFile

          //TODO: detect whether we have to use Runtime.exec
          val useRuntime = true

          if (useRuntime)
            execTest(outDir, logFile, fileBase)
          else
            execTestObjectRunner(file, outDir, logFile)
          NestUI.verbose(this+" finished running "+fileBase)

          diff = compareOutput(dir, fileBase, kind, logFile)
          if (!diff.equals("")) {
            NestUI.verbose("output differs from log file\n")
            succeeded = false
          }
        }
      })

    def processSingleFile(file: File): LogContext = kind match {
      case "scalacheck" =>
        runInContext(file, kind, (logFile: File, outDir: File) => {
          if (file.isDirectory) {
            compileFilesIn(file, kind, logFile, outDir)
          } else if (!compileMgr.shouldCompile(List(file), kind, logFile)) {
            NestUI.verbose("compilation of "+file+" failed\n")
            succeeded = false
          }
          if (succeeded) {
            val consFM = new ConsoleFileManager
            import consFM.{latestCompFile, latestLibFile, latestActFile,
                           latestPartestFile}

            NestUI.verbose("compilation of "+file+" succeeded\n")

            val libs = new File(fileManager.LIB_DIR)
            val scalacheckURL = new File(libs, "ScalaCheck.jar") toURL
            val outURL = outDir.getCanonicalFile.toURL
            val classpath: List[URL] =
              List(outURL, scalacheckURL, latestCompFile.toURL, latestLibFile.toURL,
                   latestActFile.toURL, latestPartestFile.toURL).removeDuplicates

            // XXX this is a big cut-and-paste mess, but the revamp is coming
            val logOut    = new FileOutputStream(logFile)
            val logWriter = new PrintStream(logOut)
            val oldStdOut = System.out
            val oldStdErr = System.err
            System.setOut(logWriter)
            System.setErr(logWriter)

            ObjectRunner.run(classpath, "Test", Nil)

            logWriter.flush()
            logWriter.close()
            System.setOut(oldStdOut)
            System.setErr(oldStdErr)

            NestUI.verbose(io.File(logFile).slurp())
            // obviously this must be improved upon
            succeeded = io.File(logFile).lines() forall (_ contains " OK")
          }
        })

      case "pos" =>
        runInContext(file, kind, (logFile: File, outDir: File) => {
          if (file.isDirectory) {
            compileFilesIn(file, kind, logFile, outDir)
          } else if (!compileMgr.shouldCompile(List(file), kind, logFile)) {
            NestUI.verbose("compilation of "+file+" failed\n")
            succeeded = false
          }
        })

      case "neg" =>
        runInContext(file, kind, (logFile: File, outDir: File) => {
          if (file.isDirectory) {
            failCompileFilesIn(file, kind, logFile, outDir)
          } else if (!compileMgr.shouldFailCompile(List(file), kind, logFile)) {
            succeeded = false
          }
          if (succeeded) { // compare log file to check file
            val fileBase = basename(file.getName)
            val dir      = file.getParentFile
            if (!existsCheckFile(dir, fileBase, kind)) {
              // diff is contents of logFile
              diff = file2String(logFile)
            } else
              diff = compareOutput(dir, fileBase, kind, logFile)

            if (!diff.equals("")) {
              NestUI.verbose("output differs from log file\n")
              succeeded = false
            }
          }
        })

      case "run" =>
        runJvmTest(file, kind)

      case "jvm" =>
        runJvmTest(file, kind)

      case "res" => {
          // when option "--failed" is provided
          // execute test only if log file is present
          // (which means it failed before)

          //val (logFileOut, logFileErr) = createLogFiles(file, kind)
          val logFile = createLogFile(file, kind)
          if (!fileManager.failed || (logFile.exists && logFile.canRead)) {
            val swr = new StringWriter
            val wr = new PrintWriter(swr)
            succeeded = true; diff = ""; log = ""
            printInfoStart(file, wr)

            val fileBase: String = basename(file.getName)
            NestUI.verbose(this+" running test "+fileBase)
            val dir = file.getParentFile
            val outDir = createOutputDir(dir, fileBase, kind)
            if (!outDir.exists) outDir.mkdir()
            val resFile = new File(dir, fileBase + ".res")
            NestUI.verbose("outDir:  "+outDir)
            NestUI.verbose("logFile: "+logFile)
            //NestUI.verbose("logFileErr: "+logFileErr)
            NestUI.verbose("resFile: "+resFile)

            // run compiler in resident mode
            // $SCALAC -d "$os_dstbase".obj -Xresident -sourcepath . "$@"

            try {

            val sourcedir  = logFile.getParentFile.getCanonicalFile
            val sourcepath = sourcedir.getAbsolutePath+File.separator
            NestUI.verbose("sourcepath: "+sourcepath)

            val argString =
              "-d "+outDir.getCanonicalFile.getAbsolutePath+
              " -Xresident"+
              " -sourcepath "+sourcepath
            val argList = List.fromString(argString, ' ')

            // configure input/output files
            val logOut    = new FileOutputStream(logFile)
            val logWriter = new PrintStream(logOut)
            val resReader = new BufferedReader(new FileReader(resFile))
            val logConsoleWriter = new PrintWriter(new OutputStreamWriter(logOut))

            // create compiler
            val settings = new Settings(error)
            settings.sourcepath.value = sourcepath
            settings.classpath.value = fileManager.CLASSPATH
            reporter = new ConsoleReporter(settings, scala.Console.in, logConsoleWriter)
            val command = new CompilerCommand(argList, settings, error, false)
            object compiler extends Global(command.settings, reporter)

            // simulate resident compiler loop
            val prompt = "\nnsc> "

            val resCompile = (line: String) => {
              NestUI.verbose("compiling "+line)
              val cmdArgs = List.fromString(line, ' ') map { fs => new File(dir, fs).getAbsolutePath }
              NestUI.verbose("cmdArgs: "+cmdArgs)
              val sett = new Settings(error)
              sett.sourcepath.value = sourcepath
              val command = new CompilerCommand(cmdArgs, sett, error, true)
              (new compiler.Run) compile command.files
            }

            def loop(action: (String) => Unit) {
              logWriter.print(prompt)
              val line = resReader.readLine()
              if ((line ne null) && line.length() > 0) {
/*
                val parent = self
                self.trapExit = true
                val child = link {
                  action(line)
                }

                receiveWithin(fileManager.timeout.toLong) {
                  case TIMEOUT =>
                    NestUI.verbose("action timed out")
                    false
                  case Exit(from, reason) if from == child => reason match {
                    case 'normal => // do nothing
                    case t: Throwable =>
                      NestUI.verbose("while invoking compiler:")
                      NestUI.verbose("caught "+t)
                      t.printStackTrace
                      if (t.getCause != null)
                        t.getCause.printStackTrace
                      false
                  }
                }
*/
                action(line)
                loop(action)
              }
            }
            val oldStdOut = System.out
            val oldStdErr = System.err
            System.setOut(logWriter)
            System.setErr(logWriter)
            loop(resCompile)
            resReader.close()
            logWriter.flush()
            logWriter.close()

            System.setOut(oldStdOut)
            System.setErr(oldStdErr)

            val tempLogFile = new File(dir, fileBase+".temp.log")
            val logFileReader = new BufferedReader(new FileReader(logFile))
            val tempLogFilePrinter = new PrintWriter(new FileWriter(tempLogFile))
            val appender =
              new StreamAppender(logFileReader, tempLogFilePrinter)

	    // function that removes a given string from another string
	    def removeFrom(line: String, path: String): String = {
              // find `path` in `line`
              val index = line.indexOf(path)
              if (index != -1) {
                line.substring(0, index) + line.substring(index + path.length, line.length)
              } else line
            }

            appender.runAndMap({ s =>
              val woPath = removeFrom(s, dir.getAbsolutePath/*.replace(File.separatorChar,'/')*/+File.separator)
              // now replace single '\' with '/'
              woPath.replace('\\', '/')
            })
            logFileReader.close()
            tempLogFilePrinter.close()

            val tempLogFileReader = new BufferedReader(new FileReader(tempLogFile))
            val logFilePrinter= new PrintWriter(new FileWriter(logFile), true)
            (new StreamAppender(tempLogFileReader, logFilePrinter)).run
            tempLogFileReader.close()
            logFilePrinter.close()

            tempLogFile.delete()

            diff = compareOutput(dir, fileBase, kind, logFile)
            if (!diff.equals("")) {
              NestUI.verbose("output differs from log file\n")
              succeeded = false
            }

            } catch {
              case e: Exception =>
	        e.printStackTrace()
                succeeded = false
            }

            LogContext(logFile, Some((swr, wr)))
          } else
            LogContext(logFile, None)
        }

      case "shootout" => {
          // when option "--failed" is provided
          // execute test only if log file is present
          // (which means it failed before)
          val logFile = createLogFile(file, kind)
          if (!fileManager.failed || (logFile.exists && logFile.canRead)) {
            val swr = new StringWriter
            val wr = new PrintWriter(swr)
            succeeded = true; diff = ""; log = ""
            printInfoStart(file, wr)

            val fileBase: String = basename(file.getName)
            NestUI.verbose(this+" running test "+fileBase)
            val dir = file.getParentFile
            val outDir = createOutputDir(dir, fileBase, kind)
            if (!outDir.exists) outDir.mkdir()

            // 2. define file {outDir}/test.scala that contains code to compile/run
            val testFile = new File(outDir, "test.scala")
            NestUI.verbose("outDir:   "+outDir)
            NestUI.verbose("logFile:  "+logFile)
            NestUI.verbose("testFile: "+testFile)

            // 3. cat {test}.scala.runner {test}.scala > testFile
            val runnerFile = new File(dir, fileBase+".scala.runner")
            val bodyFile   = new File(dir, fileBase+".scala")
            val appender = StreamAppender.concat(new FileInputStream(runnerFile),
                                                 new FileInputStream(bodyFile),
                                                 new FileOutputStream(testFile))
            appender.run()

            try { // *catch-all*
              // 4. compile testFile
              if (!compileMgr.shouldCompile(List(testFile), kind, logFile)) {
                NestUI.verbose("compilation of "+file+" failed\n")
                succeeded = false
              } else {
                NestUI.verbose("compilation of "+testFile+"succeeded")
                // -------- run test --------

                //TODO: detect whether we have to use Runtime.exec
                val useRuntime = true

                if (useRuntime)
                  execTest(outDir, logFile, fileBase)
                else
                  execTestObjectRunner(file, outDir, logFile)
                NestUI.verbose(this+" finished running "+fileBase)
              } // successful compile
            } catch { // *catch-all*
              case e: Exception =>
                NestUI.verbose("caught "+e)
                succeeded = false
            }

            diff = compareOutput(dir, fileBase, kind, logFile)
            if (!diff.equals("")) {
              NestUI.verbose("output differs from log file\n")
              succeeded = false
            }

            LogContext(logFile, Some((swr, wr)))
          } else
            LogContext(logFile, None)
        }

      case "scalap" => {

        def decompileFile(clazz: Class[_], packObj: Boolean) = {
          val byteCode = ByteCode.forClass(clazz)
          val classFile = ClassFileParser.parse(byteCode)
          val Some(sig) = classFile.attribute("ScalaSig").map(_.byteCode).map(ScalaSigAttributeParsers.parse)
          import scala.tools.scalap.Main._
          parseScalaSignature(sig, packObj)
        }

        runInContext(file, kind, (logFile: File, outDir: File) => {
          val sourceDir = file.getParentFile
          val sourceDirName = sourceDir.getName

          // 1. Find file with result text
          val results = sourceDir.listFiles(new FilenameFilter {
            def accept(dir: File, name: String) = name == "result.test"
          })

          if (results.length != 1) {
            NestUI.verbose("Result file not found in directory " + sourceDirName + " \n")
          } else {
            val resFile = results(0)
            // 2. Compile source file
            if (!compileMgr.shouldCompile(outDir, List(file), kind, logFile)) {
              NestUI.verbose("compilerMgr failed to compile %s to %s".format(file, outDir))
              succeeded = false
            } else {

              // 3. Decompile file and compare results
              val isPackageObject = sourceDir.getName.startsWith("package")
              val className = sourceDirName.capitalize + (if (!isPackageObject) "" else ".package")
              val url = outDir.toURI.toURL
              val loader = new URLClassLoader(Array(url), getClass.getClassLoader)
              val clazz = loader.loadClass(className)

              val result = decompileFile(clazz, isPackageObject)

              try {
                val fstream = new FileWriter(logFile);
                val out = new BufferedWriter(fstream);
                out.write(result)
                out.close();
              } catch {
                case e: IOException => NestUI.verbose(e.getMessage()); succeeded = false
              }

              val diff = fileManager.compareFiles(logFile, resFile)
              if (!diff.equals("")) {
                NestUI.verbose("output differs from log file\n")
                succeeded = false
              }
            }
          }
        })
      }

      case "script" => {
        val osName = System.getProperty("os.name", "")
          // when option "--failed" is provided
          // execute test only if log file is present
          // (which means it failed before)
          val logFile = createLogFile(file, kind)
          if (!fileManager.failed || (logFile.exists && logFile.canRead)) {
            val swr = new StringWriter
            val wr = new PrintWriter(swr)
            succeeded = true; diff = ""; log = ""
            printInfoStart(file, wr)

            val fileBase: String = basename(file.getName)
            NestUI.verbose(this+" running test "+fileBase)

            // check whether there is an args file
            val argsFile = new File(file.getParentFile, fileBase+".args")
            NestUI.verbose("argsFile: "+argsFile)
            val argString = if (argsFile.exists) {
              val swriter = new StringWriter
              val app = StreamAppender(new BufferedReader(new FileReader(argsFile)),
                                       swriter)
              app.run()
              " "+swriter.toString
            } else ""

            try {
              val cmdString =
                if (osName startsWith "Windows") {
                  val batchFile = new File(file.getParentFile, fileBase+".bat")
                  NestUI.verbose("batchFile: "+batchFile)
                  batchFile.getAbsolutePath
                }
                else file.getAbsolutePath
              val proc = Runtime.getRuntime.exec(cmdString+argString)
              val in = proc.getInputStream
              val err = proc.getErrorStream
              val writer = new PrintWriter(new FileWriter(logFile), true)
              val inApp = new StreamAppender(new BufferedReader(new InputStreamReader(in)),
                                             writer)
              val errApp = new StreamAppender(new BufferedReader(new InputStreamReader(err)),
                                              writer)
              val async = new Thread(errApp)
              async.start()
              inApp.run()
              async.join()

              writer.close()

              diff = compareOutput(file.getParentFile, fileBase, kind, logFile)
              if (!diff.equals("")) {
                NestUI.verbose("output differs from log file\n")
                succeeded = false
              }
            } catch { // *catch-all*
              case e: Exception =>
                NestUI.verbose("caught "+e)
                succeeded = false
            }

            LogContext(logFile, Some((swr, wr)))
          } else
            LogContext(logFile, None)
      }
    }

    def reportAll(cont: (Int, Int) => Unit) {
      NestUI.verbose("finished testing "+kind+" with "+errors+" errors")
      NestUI.verbose("created "+compileMgr.numSeparateCompilers+" separate compilers")
      timer.cancel()
      cont(files.length-errors, errors)
    }

    def reportResult(logs: Option[LogContext]) {
      if (!succeeded) {
        errors += 1
        NestUI.verbose("incremented errors: "+errors)
      }

      try {
        // delete log file only if test was successful
        if (succeeded && !logs.isEmpty)
          logs.get.file.toDelete = true

        if (!logs.isEmpty)
          logs.get.writers match {
            case Some((swr, wr)) =>
              printInfoEnd(succeeded, wr)
              wr.flush()
              swr.flush()
              NestUI.normal(swr.toString)
              if (!succeeded && fileManager.showDiff && diff != "")
                NestUI.normal(diff)
              if (!succeeded && fileManager.showLog)
                showLog(logs.get.file)
            case None =>
          }
      } catch {
        case npe: NullPointerException =>
      }
    }

    val numFiles = files.size
    if (numFiles == 0)
      reportAll(topcont)

    // maps canonical file names to the test result (0: OK, 1: FAILED, 2: TIMOUT)
    val status = new HashMap[String, Int]

    var fileCnt = 1
    Actor.loopWhile(fileCnt <= numFiles) {
      val parent = self

      actor {
        val testFile = files(fileCnt-1)

        val ontimeout = new TimerTask {
          def run() = parent ! Timeout(testFile)
        }
        timer.schedule(ontimeout, fileManager.timeout.toLong)

        val context = try {
          processSingleFile(testFile)
        } catch {
          case t: Throwable =>
            NestUI.verbose("while invoking compiler ("+files+"):")
            NestUI.verbose("caught "+t)
            t.printStackTrace
            if (t.getCause != null)
              t.getCause.printStackTrace
            LogContext(null, None)
        }
        parent ! Result(testFile, context)
      }

      react {
        case res: TestResult =>
          val path = res.file.getCanonicalPath
          status.get(path) match {
            case Some(stat) => // ignore message
            case None => res match {
              case Timeout(_) =>
                status += (path -> 2)
                val swr = new StringWriter
                val wr = new PrintWriter(swr)
                printInfoStart(files(fileCnt-1), wr)
                printInfoTimeout(wr)
                wr.flush()
                swr.flush()
                NestUI.normal(swr.toString)
                succeeded = false
                reportResult(None)
                if (fileCnt == numFiles)
                  reportAll(topcont)
                fileCnt += 1
              case Result(_, logs) =>
                status += (path -> (if (succeeded) 0 else 1))
                reportResult(if (logs != null) Some(logs) else None)
                if (fileCnt == numFiles)
                  reportAll(topcont)
                fileCnt += 1
            }
          }
      }
    }
  }

  def showLog(logFile: File) {
    try {
      val logReader = new BufferedReader(new FileReader(logFile))
      val strWriter = new StringWriter
      val logWriter = new PrintWriter(strWriter, true)
      val logAppender = new StreamAppender(logReader, logWriter)
      logAppender.run()
      logReader.close()
      val log = strWriter.toString
      NestUI.normal(log)
    } catch {
      case fnfe: java.io.FileNotFoundException =>
        NestUI.failure("Couldn't open log file \""+logFile+"\".")
    }
  }
}