Skip to content
This repository was archived by the owner on Sep 8, 2022. It is now read-only.

Commit 95bf49f

Browse files
committed
Reliable initialization of test source path
`testSourcePath` determines where we look for tests, relative to the `testRoot` As `testSourcePath` and `testRoot` are mutable, all jar paths that depend on it may change as well. We don't use system properties to configure partest internally, as their behavior depends on the security policy in place. ant, for example, does not allow overwriting properties. TODO: make all of this immutable, but that's pretty involved. A lot of stuff depends on it (all the more reason to refactor)
1 parent 2698d6f commit 95bf49f

File tree

5 files changed

+52
-36
lines changed

5 files changed

+52
-36
lines changed

src/main/scala/scala/tools/partest/PartestDefaults.scala

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ import scala.tools.nsc.Properties.{ propOrElse, propOrNone, propOrEmpty }
66
import java.lang.Runtime.{ getRuntime => runtime }
77

88
object PartestDefaults {
9-
10-
def testRootName = propOrNone("partest.root")
11-
def srcDirName = propOrElse("partest.srcdir", "files")
12-
def testRootDir = testRootName map (x => Directory(x))
13-
14-
// def classPath = propOrElse("partest.classpath", "")
15-
def classPath = ClassPath split PathResolver.Environment.javaUserClassPath // XXX
16-
179
def javaCmd = propOrElse("partest.javacmd", "java")
1810
def javacCmd = propOrElse("partest.javac_cmd", "javac")
1911
def javaOpts = propOrElse("partest.java_opts", "")

src/main/scala/scala/tools/partest/PartestTask.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class PartestTask extends Task with CompilationPathProperty with ScalaTask {
115115
NestUI.setDebug()
116116
}
117117

118-
srcDir foreach (x => setProp("partest.srcdir", x))
118+
srcDir foreach (nest.PathSettings.testSourcePath = _)
119119

120120
if (compilationPath.isEmpty) sys.error("Mandatory attribute 'compilationPath' is not set.")
121121

src/main/scala/scala/tools/partest/nest/ConsoleFileManager.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import scala.tools.nsc.{ io, util }
1515
import PathResolver.{ Environment, Defaults }
1616

1717
object ConsoleFileManager {
18-
val classPath = {
18+
def classPath = {
1919
val srcDir = {
2020
val src = PathSettings.srcDir
2121
if (!src.isDirectory) {
@@ -26,14 +26,17 @@ object ConsoleFileManager {
2626
}
2727
val libs = (srcDir / Directory("lib")).files filter (_ hasExtension "jar") map (_.toCanonical.path)
2828

29+
// def classPath = propOrElse("partest.classpath", "")
30+
val userCp = ClassPath split PathResolver.Environment.javaUserClassPath // XXX
31+
2932
// add all jars in libs
30-
val cp = (PartestDefaults.classPath ++ libs.toList)
33+
val cp = (userCp ++ libs.toList)
3134
vlog("testClassPath: " + cp)
3235
cp map (Path(_))
3336
}
3437

3538
def mostRecentTrifecta(testBuild: Option[String], testClasses: Option[String]) = {
36-
val testParent = PathSettings.testRoot.parent
39+
import PathSettings.testParent
3740
val testClassesDir = testClasses map (tc => Path(tc).toCanonical.toDirectory)
3841
val testBuildDir = testBuild map (b => (testParent / b).toCanonical.toDirectory)
3942

src/main/scala/scala/tools/partest/nest/ConsoleRunner.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package nest
1010
import utils.Properties._
1111
import scala.tools.nsc.Properties.{ versionMsg, setProp }
1212
import scala.collection.{ mutable, immutable }
13-
import PathSettings.srcDir
1413
import TestKinds._
1514
import scala.reflect.internal.util.Collections.distinctBy
1615
import scala.tools.cmd.{ CommandLine, CommandLineParser, Instance }
@@ -110,7 +109,7 @@ class ConsoleRunner(argstr: String) extends {
110109
echoWarning(s"Discarding ${invalid.size} invalid test paths")
111110
}
112111

113-
optSourcePath foreach (x => setProp("partest.srcdir", x))
112+
optSourcePath foreach (PathSettings.testSourcePath = _)
114113
optTimeout foreach (x => setProp("partest.timeout", x))
115114

116115
NestUI echo banner

src/main/scala/scala/tools/partest/nest/PathSettings.scala

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,66 @@ package nest
77

88
import scala.tools.nsc.util.ClassPath
99
import scala.tools.nsc.io.{ Path, File, Directory }
10+
import scala.tools.nsc.Properties.{ propOrElse, propOrNone }
1011
import Path._
1112

13+
/** Get current value for path settings -- these depend on the mutable `testSourcePath`.
14+
* Default values are read from system properties `partest.srcdir` and `partest.root`.
15+
*
16+
* TODO: make `testSourcePath` immutable, but that's pretty involved as a lot of stuff depends on it (all the more reason to refactor)
17+
* (we don't use system properties to configure partest internally,
18+
* as their behavior depends on the security policy in place -- e.g., ant does not allow overwriting properties)
19+
*
20+
* NOTE: the members are methods because `testSourcePath` changes.
21+
*/
1222
object PathSettings {
13-
import PartestDefaults.{ testRootDir, srcDirName }
23+
private[this] var myTestSourcePath: String = null
24+
private def defaultSrcDirName = propOrElse("partest.srcdir", "files")
25+
26+
/** testSourcePath determines where we look for tests, relative to the `testRoot`
27+
* after it's been read, it cannot be changed
28+
*/
29+
def testSourcePath: String = {
30+
if (myTestSourcePath eq null) myTestSourcePath = defaultSrcDirName
31+
myTestSourcePath
32+
}
33+
34+
/** testSourcePath determines where we look for tests, relative to the `testRoot`
35+
* it can be set only if it hasn't been read yet
36+
*/
37+
def testSourcePath_= (path: String): Unit = {
38+
// assert(myTestSourcePath eq null, "Test Source Path set after use.")
39+
myTestSourcePath = path
40+
}
41+
42+
// defaults can be set using the environment, but note PathSettings is mutable
43+
private def defaultTestRootName = propOrNone("partest.root")
1444

1545
private def cwd = Directory.Current getOrElse sys.error("user.dir property not set")
16-
private def isPartestDir(d: Directory) = (d.name == "test") && (d / srcDirName isDirectory)
46+
private def isPartestDir(d: Directory) = (d.name == "test") && (d / testSourcePath isDirectory)
1747
private def findJar(name: String, ds: Directory*): Either[String, File] =
1848
ds.toStream flatMap (_.files) filter (_ hasExtension "jar") find ( _.name startsWith name ) map (Right(_)) getOrElse
1949
Left(s"'${name}.jar' not found in '${ds map (_.path) mkString ", "}'.")
2050

2151
// Directory <root>/test
22-
lazy val testRoot: Directory = testRootDir getOrElse {
52+
def testRoot: Directory = (defaultTestRootName map (Directory(_))) getOrElse {
2353
val candidates: List[Directory] = (cwd :: cwd.parents) flatMap (d => List(d, Directory(d / "test")))
2454

2555
candidates find isPartestDir getOrElse sys.error("Directory 'test' not found.")
2656
}
57+
def testParent = testRoot.parent
2758

2859
// Directory <root>/test/files or .../scaladoc
29-
lazy val srcDir = Directory(testRoot / srcDirName toCanonical)
60+
def srcDir = Directory(testRoot / testSourcePath toCanonical)
3061

3162
// Directory <root>/build/pack/lib
32-
lazy val buildPackLibDir = Directory(buildDir / "pack" / "lib")
63+
private def buildPackLibDir = Directory(buildDir / "pack" / "lib")
3364

3465
// Directory <root>/test/files/lib
35-
lazy val srcLibDir = Directory(srcDir / "lib")
66+
private def srcLibDir = Directory(srcDir / "lib")
3667

3768
// Directory <root>/build
38-
lazy val buildDir: Directory = {
69+
private def buildDir: Directory = {
3970
val bases = testRoot :: testRoot.parents
4071
// In the classic "ant" build, the relevant subdirectory is called build,
4172
// but in the postmodern "sbt" build, it is called target. Look for both.
@@ -44,19 +75,10 @@ object PathSettings {
4475
dirs.headOption getOrElse sys.error("Neither 'build' nor 'target' dir found under test root " + testRoot + ".")
4576
}
4677

47-
48-
lazy val srcSpecLib = findJar("instrumented", Directory(srcDir / "speclib"))
49-
lazy val srcCodeLib = findJar("code", Directory(srcDir / "codelib"), Directory(testRoot / "files" / "codelib") /* work with --srcpath pending */)
50-
lazy val agentLib = findJar("scala-partest-javaagent", buildPackLibDir)
51-
lazy val scalaCheck = findJar("scalacheck", buildPackLibDir, srcLibDir)
52-
lazy val testInterface = findJar("test-interface", buildPackLibDir, srcLibDir)
53-
lazy val diffUtils = findJar("diffutils", buildPackLibDir)
54-
55-
/** The platform-specific support jar, `tools.jar`.
56-
*/
57-
lazy val platformTools = PathResolver.SupplementalLocations.platformTools
58-
}
59-
60-
class PathSettings() {
61-
// def classpathAsURLs: List[URL]
78+
def srcSpecLib = findJar("instrumented", Directory(srcDir / "speclib"))
79+
def srcCodeLib = findJar("code", Directory(srcDir / "codelib"), Directory(testRoot / "files" / "codelib") /* work with --srcpath pending */)
80+
def agentLib = findJar("scala-partest-javaagent", buildPackLibDir)
81+
def scalaCheck = findJar("scalacheck", buildPackLibDir, srcLibDir)
82+
def testInterface = findJar("test-interface", buildPackLibDir, srcLibDir)
83+
def diffUtils = findJar("diffutils", buildPackLibDir)
6284
}

0 commit comments

Comments
 (0)