Skip to content

Instantly share code, notes, and snippets.

@hisui
Last active December 14, 2016 10:04
Show Gist options
  • Save hisui/2ecaf072d39f6cf06f1a606987f51bd8 to your computer and use it in GitHub Desktop.
Save hisui/2ecaf072d39f6cf06f1a606987f51bd8 to your computer and use it in GitHub Desktop.
Adds system properties visible from test cases in both case that "fork" is ture or false.
import com.google.cloud.datastore.testing.LocalDatastoreHelper
commands += Command.command("datastoreRun") { state =>
val helper = LocalDatastoreHelper.create(1.0)
helper.start()
atExit {
helper.stop()
}
addSystemPropertiesTo(state)(Seq(
"datastore.host" -> helper.getOptions.getHost
))
}
def addSystemPropertiesTo(state: State)(a: Seq[(String, String)]): State =
Project.extract(state).append(Seq(
javaOptions in GlobalScope ++= a.map(e => s"-D${e._1}=${e._2}"),
testOptions in GlobalScope += Tests.Setup { loader =>
val setProperty = loader.loadClass("java.lang.System")
.getDeclaredMethod("setProperty"
, classOf[String]
, classOf[String])
a.foreach(e =>
setProperty.invoke(null, e._1, e._2)
)
}
), state)
def atExit(f: => Unit): Unit =
java.lang.Runtime.getRuntime.addShutdownHook(new Thread {
override def run(): Unit = try f catch { case _: Throwable => }
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment