Skip to content

Commit ac4e3ca

Browse files
author
James Iry
committed
Refactor testing logic for only running under certain JDK versions
We had several tests designed to only run if the JDK version was at least some specified version. This commit refactors that common logic into DirectTest.
1 parent fc6da8d commit ac4e3ca

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

src/partest/scala/tools/partest/DirectTest.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package scala.tools.partest
77

88
import scala.tools.nsc._
9+
import settings.ScalaVersion
910
import io.Directory
1011
import util.{ SourceFile, BatchSourceFile, CommandLineParser }
1112
import reporters.{Reporter, ConsoleReporter}
@@ -101,4 +102,30 @@ abstract class DirectTest extends App {
101102
final def log(msg: => Any) {
102103
if (isDebug) Console.err println msg
103104
}
105+
106+
/**
107+
* Run a test only if the current java version is at least the version specified.
108+
*/
109+
def testUnderJavaAtLeast[A](version: String)(yesRun: =>A) = new TestUnderJavaAtLeast(version, { yesRun })
110+
111+
class TestUnderJavaAtLeast[A](version: String, yesRun: => A) {
112+
val javaVersion = System.getProperty("java.specification.version")
113+
114+
// the "ScalaVersion" class parses Java specification versions just fine
115+
val requiredJavaVersion = ScalaVersion(version)
116+
val executingJavaVersion = ScalaVersion(javaVersion)
117+
val shouldRun = executingJavaVersion >= requiredJavaVersion
118+
val preamble = if (shouldRun) "Attempting" else "Doing fallback for"
119+
120+
def logInfo() = log(s"$preamble java $version specific test under java version $javaVersion")
121+
122+
/*
123+
* If the current java version is at least 'version' then 'yesRun' is evaluated
124+
* otherwise 'fallback' is
125+
*/
126+
def otherwise(fallback: =>A): A = {
127+
logInfo()
128+
if (shouldRun) yesRun else fallback
129+
}
130+
}
104131
}

test/files/run/classfile-format-51.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ object Driver {
112112
System.setErr(System.out)
113113
try {
114114
// this test is only valid under JDK 1.7+
115-
// cheat a little by using 'ScalaVersion' because it can parse java versions just as well
116-
val requiredJavaVersion = ScalaVersion("1.7")
117-
val executingJavaVersion = ScalaVersion(System.getProperty("java.specification.version"))
118-
if (executingJavaVersion >= requiredJavaVersion) {
115+
testUnderJavaAtLeast("1.7") {
119116
generateClass()
120117
compile()
118+
()
119+
} otherwise {
120+
()
121121
}
122122
}
123123
finally

test/files/run/classfile-format-52.scala

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,14 @@ class Driver extends HasDefaultMethod {
6161
System.setErr(System.out)
6262
try {
6363
// this test is only valid under JDK 1.8+
64-
// cheat a little by using 'ScalaVersion' because it can parse java versions just as well
65-
val requiredJavaVersion = ScalaVersion("1.8")
66-
val executingJavaVersion = ScalaVersion(System.getProperty("java.specification.version"))
67-
if (executingJavaVersion >= requiredJavaVersion) {
64+
testUnderJavaAtLeast("1.8") {
6865
generateInterface()
6966
compile()
7067
Class.forName("Driver").newInstance()
71-
} else {
72-
// under other versions just dump the expected results
68+
()
69+
} otherwise {
7370
println("hello from publicMethod")
74-
println("hello from staticMethod")
71+
println("hello from staticMethod")
7572
}
7673
}
7774
finally

test/files/run/t7398.scala

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ import scala.tools.partest._
33
object Test extends CompilerTest {
44
import global._
55

6-
def javaVersion = scala.util.Properties.javaVersion
7-
def isJavaEight = javaVersion startsWith "1.8"
86
// This way we auto-pass on non-java8 since there's nothing to check
9-
override lazy val units = {
10-
val res: List[CompilationUnit] = if (isJavaEight) javaCompilationUnits(global)(defaultMethodSource) else Nil
11-
val word = if (isJavaEight) "Attempting" else "Skipping"
12-
log(s"$word java8-specific test under java version $javaVersion")
13-
res
7+
override lazy val units: List[CompilationUnit] = testUnderJavaAtLeast("1.8") {
8+
javaCompilationUnits(global)(defaultMethodSource)
9+
} otherwise {
10+
Nil
1411
}
1512

1613
private def defaultMethodSource = """

0 commit comments

Comments
 (0)