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

// $Id$

package scala.tools.partest.nest

import java.io.{File, FilenameFilter, IOException, StringWriter}
import java.net.URI

class ConsoleFileManager extends FileManager {

  var testBuild = System.getProperty("scalatest.build")
  var testClasses: Option[String] = None

  val debug: Boolean =
    (System.getProperty("partest.debug", "false") equals "true") ||
    (System.getProperty("scalatest.debug", "false") equals "true")

  def this(buildPath: String, rawClasses: Boolean) = {
    this()
    if (rawClasses)
      testClasses = Some(buildPath)
    else
      testBuild = buildPath
    // re-run because initialization of default
    // constructor must be updated
    findLatest()
  }

  def this(buildPath: String) = {
    this(buildPath, false)
  }

  def this(buildPath: String, rawClasses: Boolean, moreOpts: String) = {
    this(buildPath, rawClasses)
    SCALAC_OPTS = SCALAC_OPTS+" "+moreOpts
  }

  var CLASSPATH = System.getProperty("java.class.path", ".")
  NestUI.verbose("CLASSPATH: "+CLASSPATH)

  var JAVACMD   = System.getProperty("scalatest.javacmd", "java")
  var JAVAC_CMD = System.getProperty("scalatest.javac_cmd", "javac")

  val prefixFile = {
    val cwd = System.getProperty("user.dir")
    if (cwd != null)
      (new File(cwd)).getCanonicalFile
    else
      error("user.dir property not set")
  }
  val PREFIX = prefixFile.getAbsolutePath

/*
if [ -d "$PREFIX/test" ]; then
    TESTROOT="$PREFIX/test";
elif [ -d "$PREFIX/misc/scala-test" ]; then
    TESTROOT="$PREFIX/misc/scala-test";
else
    abort "Test directory not found";
*/

  val testRootFile = {
    val testRootProp = System.getProperty("scalatest.root")
    val testroot =
      if (testRootProp != null)
        new File(testRootProp)
      else {
        // case 1: cwd is `test`
        if (prefixFile.getName == "test" && (new File(prefixFile, "files")).exists)
          prefixFile
        else {
        // case 2: cwd is `test/..`
          val test = new File(prefixFile, "test")
          val scalaTest = new File(new File(prefixFile, "misc"), "scala-test")
          if (test.isDirectory)
            test
          else if (scalaTest.isDirectory)
            scalaTest
          else
            error("Test directory not found")
        }
      }
    testroot.getCanonicalFile
  }
  val TESTROOT = testRootFile.getAbsolutePath

  var srcDirName: String = ""

  val srcDir: File = {
    val srcDirProp = System.getProperty("partest.srcdir")
    val src =
      if (srcDirProp != null) {
        srcDirName = srcDirProp
        new File(testRootFile, srcDirName)
      } else {
        srcDirName = "files"
        new File(testRootFile, srcDirName)
      }
    if (src.isDirectory)
      src.getCanonicalFile
    else {
      val path = TESTROOT + File.separator + "files"
      NestUI.failure("Source directory \"" + path + "\" not found")
      exit(1)
    }
  }

  LIB_DIR = (new File(testRootFile.getParentFile, "lib")).getCanonicalFile.getAbsolutePath

  CLASSPATH = CLASSPATH + File.pathSeparator + {
    val libs = new File(srcDir, "lib")
    // add all jars in libs
    (libs.listFiles(new FilenameFilter {
      def accept(dir: File, name: String) = name endsWith ".jar"
    }) map {file => file.getCanonicalFile.getAbsolutePath}).mkString(""+File.pathSeparator)
  }

  def findLatest() {
    val testParent = testRootFile.getParentFile
    NestUI.verbose("test parent: "+testParent)

    def prefixFileWith(parent: File, relPath: String): File =
      (new File(parent, relPath)).getCanonicalFile

    def prefixFile(relPath: String): File =
      prefixFileWith(testParent, relPath)

    if (!testClasses.isEmpty) {
      testClassesFile = (new File(testClasses.get)).getCanonicalFile
      NestUI.verbose("Running with classes in "+testClassesFile)
      latestFile        = prefixFileWith(testClassesFile.getParentFile, "bin")
      latestLibFile     = prefixFileWith(testClassesFile, "library")
      latestActFile     = prefixFileWith(testClassesFile, "library")
      latestCompFile    = prefixFileWith(testClassesFile, "compiler")
      latestPartestFile = prefixFileWith(testClassesFile, "partest")
      latestFjbgFile    = prefixFile("lib/fjbg.jar")
    }
    else if (testBuild != null) {
      testBuildFile = prefixFile(testBuild)
      NestUI.verbose("Running on "+testBuild)
      latestFile        = prefixFile(testBuild+"/bin")
      latestLibFile     = prefixFile(testBuild+"/lib/scala-library.jar")
      latestActFile     = prefixFile(testBuild+"/lib/scala-library.jar")
      latestCompFile    = prefixFile(testBuild+"/lib/scala-compiler.jar")
      latestPartestFile = prefixFile(testBuild+"/lib/scala-partest.jar")
    } else {
      def setupQuick() {
        NestUI.verbose("Running build/quick")
        latestFile        = prefixFile("build/quick/bin")
        latestLibFile     = prefixFile("build/quick/classes/library")
        latestActFile     = prefixFile("build/quick/classes/library")
        latestCompFile    = prefixFile("build/quick/classes/compiler")
        latestPartestFile = prefixFile("build/quick/classes/partest")
      }

      def setupInst() {
        NestUI.verbose("Running dist (installed)")
        val p = testParent.getParentFile
        latestFile        = prefixFileWith(p, "bin")
        latestLibFile     = prefixFileWith(p, "lib/scala-library.jar")
        latestActFile     = prefixFileWith(p, "lib/scala-library.jar")
        latestCompFile    = prefixFileWith(p, "lib/scala-compiler.jar")
        latestPartestFile = prefixFileWith(p, "lib/scala-partest.jar")
      }

      def setupDist() {
        NestUI.verbose("Running dists/latest")
        latestFile        = prefixFile("dists/latest/bin")
        latestLibFile     = prefixFile("dists/latest/lib/scala-library.jar")
        latestActFile     = prefixFile("dists/latest/lib/scala-library.jar")
        latestCompFile    = prefixFile("dists/latest/lib/scala-compiler.jar")
        latestPartestFile = prefixFile("dists/latest/lib/scala-partest.jar")
      }

      def setupPack() {
        NestUI.verbose("Running build/pack")
        latestFile        = prefixFile("build/pack/bin")
        latestLibFile     = prefixFile("build/pack/lib/scala-library.jar")
        latestActFile     = prefixFile("build/pack/lib/scala-library.jar")
        latestCompFile    = prefixFile("build/pack/lib/scala-compiler.jar")
        latestPartestFile = prefixFile("build/pack/lib/scala-partest.jar")
      }

      def max(a: Long, b: Long) = if (a > b) a else b

      val dists = new File(testParent, "dists")
      val build = new File(testParent, "build")
      // in case of an installed dist, testRootFile is one level deeper
      val bin = new File(testParent.getParentFile, "bin")

      // detect most recent build
      val quickTime =
        max(prefixFile("build/quick/classes/compiler/compiler.properties").lastModified,
            prefixFile("build/quick/classes/library/library.properties").lastModified)
      val packTime =
        max(prefixFile("build/pack/lib/scala-compiler.jar").lastModified,
            prefixFile("build/pack/lib/scala-library.jar").lastModified)
      val distTime =
        max(prefixFile("dists/latest/lib/scala-compiler.jar").lastModified,
            prefixFile("dists/latest/lib/scala-library.jar").lastModified)
      val instTime = {
        val p = testParent.getParentFile
        max(prefixFileWith(p, "lib/scala-compiler.jar").lastModified,
            prefixFileWith(p, "lib/scala-library.jar").lastModified)
      }

      if (quickTime > packTime) {   // pack ruled out
        if (quickTime > distTime) { // dist ruled out
          if (quickTime > instTime) // inst ruled out
            setupQuick()
          else
            setupInst()
        } else {                    // quick ruled out
          if (distTime > instTime)  // inst ruled out
            setupDist()
          else
            setupInst()
        }
      } else {                      // quick ruled out
        if (packTime > distTime) {  // dist ruled out
          if (packTime > instTime)  // inst ruled out
            setupPack()
          else
            setupInst()
        } else {                    // pack ruled out
          if (distTime > instTime)  // inst ruled out
            setupDist()
          else
            setupInst()
        }
      }
      latestFjbgFile = prefixFile("lib/fjbg.jar")
    }

    BIN_DIR = latestFile.getAbsolutePath
    LATEST_LIB = latestLibFile.getAbsolutePath
    LATEST_COMP = latestCompFile.getAbsolutePath
    LATEST_PARTEST = latestPartestFile.getAbsolutePath

    // detect whether we are running on Windows
    val osName = System.getProperty("os.name")
    NestUI.verbose("OS: "+osName)

    val scalaCommand = if (osName startsWith "Windows")
      "scala.bat" else "scala"
    val scalacCommand = if (osName startsWith "Windows")
      "scalac.bat" else "scalac"

    SCALA = (new File(latestFile, scalaCommand)).getAbsolutePath
    SCALAC_CMD = (new File(latestFile, scalacCommand)).getAbsolutePath
  }

  var BIN_DIR: String = ""
  var LATEST_LIB: String = ""
  var LATEST_COMP: String = ""
  var LATEST_PARTEST: String = ""
  var SCALA: String = ""
  var SCALAC_CMD: String = ""

  var latestFile: File = _
  var latestLibFile: File = _
  var latestActFile: File = _
  var latestCompFile: File = _
  var latestPartestFile: File = _
  var latestFjbgFile: File = _
  var testBuildFile: File = _
  var testClassesFile: File = _
  // initialize above fields
  findLatest()

  var testFiles: List[File] = List()

  def getFiles(kind: String, doCheck: Boolean, filter: Option[(String, Boolean)]): List[File] = {
    val dir = new File(srcDir, kind)
    NestUI.verbose("look in "+dir+" for tests")
    val files = if (dir.isDirectory) {
      if (!testFiles.isEmpty) {
        val dirpath = dir.getAbsolutePath
        testFiles filter { _.getParentFile.getAbsolutePath == dirpath }
      } else if (doCheck) filter match {
        case Some((ending, enableDirs)) =>
          val filter = new FilenameFilter {
            def accept(dir: File, name: String) =
              name.endsWith(ending) ||
              (enableDirs && (name != ".svn") && (!name.endsWith(".obj")) &&
              (new File(dir, name)).isDirectory)
          }
          dir.listFiles(filter).toList
        case None =>
          val filter = new FilenameFilter {
            def accept(dir: File, name: String) = name != ".svn"
          }
          dir.listFiles(filter).toList
      } else // skip
        Nil
    } else {
      NestUI.failure("Directory \"" + dir.getPath + "\" not found")
      Nil
    }
    if (failed)
      files filter { logFileExists(_, kind) }
    else
      files
  }

  def getFiles(kind: String, doCheck: Boolean): List[File] =
    getFiles(kind, doCheck, Some((".scala", true)))

}