Fix #3564: Filter scripted tests based on project/build.properties

Skip scripted tests where the binary version configured in the build
does not match the binary version of sbt used for cross-building.
This commit is contained in:
Jonas Fonseca 2017-09-20 23:18:37 -04:00
parent 4b8d31a9ea
commit 62a1d42c55
7 changed files with 58 additions and 4 deletions

View File

@ -230,7 +230,7 @@ lazy val scriptedSbtProj = (project in scriptedPath / "sbt")
libraryDependencies ++= Seq(launcherInterface % "provided"),
mimaSettings,
)
.configure(addSbtIO, addSbtUtilLogging, addSbtCompilerInterface, addSbtUtilScripted)
.configure(addSbtIO, addSbtUtilLogging, addSbtCompilerInterface, addSbtUtilScripted, addSbtLmCore)
lazy val scriptedPluginProj = (project in scriptedPath / "plugin")
.dependsOn(sbtProj)

View File

@ -0,0 +1 @@
object Skipped {

View File

@ -0,0 +1 @@
sbt.version=0.12.0

View File

@ -0,0 +1,2 @@
$ copy-file changes/Fail.scala Skipped.scala
> compile

View File

@ -0,0 +1,3 @@
libraryDependencies += {
"org.scala-sbt" %% "scripted-plugin" % sbtVersion.value
}

View File

@ -0,0 +1,17 @@
# Setup failing test that should be skipped when tests are discovered
$ copy-file changes/Fail.scala src/sbt-test/group/skipped/changes/Fail.scala
$ copy-file changes/build.properties src/sbt-test/group/skipped/project/build.properties
$ copy-file changes/test src/sbt-test/group/skipped/test
# Should fail when run explicitly
-> scripted group/skipped
# Should be skipped when discovered
> scripted
# Setup the same test without a project/build.properties file
$ copy-file changes/Fail.scala src/sbt-test/group/not-skipped/changes/Fail.scala
$ copy-file changes/test src/sbt-test/group/not-skipped/test
# Running discovered tests should fail
-> scripted

View File

@ -6,6 +6,7 @@ package sbt
package test
import java.io.File
import java.util.Properties
import scala.util.control.NonFatal
import sbt.internal.scripted._
@ -343,7 +344,9 @@ class ScriptedRunner {
launchOpts: Array[String],
prescripted: File => Unit): Unit = {
val runner = new ScriptedTests(resourceBaseDirectory, bufferLog, bootProperties, launchOpts)
val allTests = get(tests, resourceBaseDirectory, logger) flatMap {
val sbtVersion = bootProperties.getName.dropWhile(!_.isDigit).dropRight(".jar".length)
val accept = isTestCompatible(resourceBaseDirectory, sbtVersion) _
val allTests = get(tests, resourceBaseDirectory, accept, logger) flatMap {
case ScriptedTest(group, name) =>
runner.singleScriptedTest(group, name, prescripted, logger)
}
@ -399,17 +402,44 @@ class ScriptedRunner {
reportErrors(tests.flatMap(test => test.apply().flatten.toSeq).toList)
}
@deprecated("No longer used", "1.0.2")
def get(tests: Seq[String], baseDirectory: File, log: Logger): Seq[ScriptedTest] =
if (tests.isEmpty) listTests(baseDirectory, log) else parseTests(tests)
get(tests, baseDirectory, _ => true, log)
def get(tests: Seq[String],
baseDirectory: File,
accept: ScriptedTest => Boolean,
log: Logger): Seq[ScriptedTest] =
if (tests.isEmpty) listTests(baseDirectory, accept, log) else parseTests(tests)
@deprecated("No longer used", "1.0.2")
def listTests(baseDirectory: File, log: Logger): Seq[ScriptedTest] =
new ListTests(baseDirectory, _ => true, log).listTests
listTests(baseDirectory, _ => true, log)
def listTests(baseDirectory: File,
accept: ScriptedTest => Boolean,
log: Logger): Seq[ScriptedTest] =
(new ListTests(baseDirectory, accept, log)).listTests
def parseTests(in: Seq[String]): Seq[ScriptedTest] =
for (testString <- in) yield {
val Array(group, name) = testString.split("/").map(_.trim)
ScriptedTest(group, name)
}
private def isTestCompatible(resourceBaseDirectory: File, sbtVersion: String)(
test: ScriptedTest): Boolean = {
import sbt.internal.librarymanagement.cross.CrossVersionUtil.binarySbtVersion
val buildProperties = new Properties()
val testDir = new File(new File(resourceBaseDirectory, test.group), test.name)
val buildPropertiesFile = new File(new File(testDir, "project"), "build.properties")
IO.load(buildProperties, buildPropertiesFile)
Option(buildProperties.getProperty("sbt.version")) match {
case Some(version) => binarySbtVersion(version) == binarySbtVersion(sbtVersion)
case None => true
}
}
}
final case class ScriptedTest(group: String, name: String) {