From 2f5d542965641048c901a0437edd72cc615a33aa Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Sun, 27 Sep 2020 09:46:32 -0700 Subject: [PATCH] Don't fork server tests I was seeing some failures in travis ci where the build crashed because it wasn't able to open a socket. This failure was happening in the scalatest fork runner so I decided to avoid forking entirely. An alternative approach would possibly be just to remove the scalatest framework from the server test project but not forking is nicer anyway. --- build.sbt | 31 +++++++++++++------ .../test/scala/sbt/RunFromSourceMain.scala | 4 +-- .../src/test/scala/testpkg/TestServer.scala | 24 ++++++-------- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/build.sbt b/build.sbt index 5648deea3..5755459f7 100644 --- a/build.sbt +++ b/build.sbt @@ -1075,7 +1075,7 @@ lazy val sbtProj = (project in file("sbt")) .configure(addSbtIO, addSbtCompilerBridge) lazy val serverTestProj = (project in file("server-test")) - .dependsOn(sbtProj % "test->test", scriptedSbtReduxProj % "test->test") + .dependsOn(sbtProj % "compile->test", scriptedSbtReduxProj % "compile->test") .settings( testedBaseSettings, crossScalaVersions := Seq(baseScalaVersion), @@ -1086,15 +1086,26 @@ lazy val serverTestProj = (project in file("server-test")) Test / run / connectInput := true, Test / run / outputStrategy := Some(StdoutOutput), Test / run / fork := true, - Test / fork := true, - Test / javaOptions ++= { - val cp = (Test / fullClasspathAsJars).value.map(_.data).mkString(java.io.File.pathSeparator) - List( - s"-Dsbt.server.classpath=$cp", - s"-Dsbt.server.version=${version.value}", - s"-Dsbt.server.scala.version=${scalaVersion.value}", - s"-Dsbt.supershell=false", - ) + Test / sourceGenerators += Def.task { + val rawClasspath = + (Compile / fullClasspathAsJars).value.map(_.data).mkString(java.io.File.pathSeparator) + val cp = + if (scala.util.Properties.isWin) rawClasspath.replaceAllLiterally("\\", "\\\\") + else rawClasspath + val content = { + s"""| + |package testpkg + | + |object TestProperties { + | val classpath = "$cp" + | val version = "${version.value}" + | val scalaVersion = "${scalaVersion.value}" + |} + """.stripMargin + } + val file = (Test / target).value / "generated" / "src" / "test" / "scala" / "testpkg" / "TestProperties.scala" + IO.write(file, content) + file :: Nil }, ) diff --git a/sbt/src/test/scala/sbt/RunFromSourceMain.scala b/sbt/src/test/scala/sbt/RunFromSourceMain.scala index 24d3991d5..85b1decfb 100644 --- a/sbt/src/test/scala/sbt/RunFromSourceMain.scala +++ b/sbt/src/test/scala/sbt/RunFromSourceMain.scala @@ -37,10 +37,10 @@ object RunFromSourceMain { ): Process = { val fo = fo0 .withWorkingDirectory(workingDirectory) - .withRunJVMOptions(sys.props.get("sbt.ivy.home") match { + .withRunJVMOptions((sys.props.get("sbt.ivy.home") match { case Some(home) => Vector(s"-Dsbt.ivy.home=$home") case _ => Vector() - }) + }) ++ fo0.runJVMOptions) implicit val runner = new ForkRun(fo) val options = Vector(workingDirectory.toString, scalaVersion, sbtVersion, cp.mkString(pathSeparator)) diff --git a/server-test/src/test/scala/testpkg/TestServer.scala b/server-test/src/test/scala/testpkg/TestServer.scala index b5a45abdb..a5595c09e 100644 --- a/server-test/src/test/scala/testpkg/TestServer.scala +++ b/server-test/src/test/scala/testpkg/TestServer.scala @@ -14,7 +14,7 @@ import java.util.concurrent.{ LinkedBlockingQueue, TimeUnit } import java.util.concurrent.atomic.AtomicBoolean import verify._ -import sbt.RunFromSourceMain +import sbt.{ ForkOptions, OutputStrategy, RunFromSourceMain } import sbt.io.IO import sbt.io.syntax._ import sbt.protocol.ClientSocket @@ -43,18 +43,9 @@ trait AbstractServerTest extends TestSuite[Unit] { "server-test" ) temp = base.toFile - val classpath = sys.props.get("sbt.server.classpath") match { - case Some(s: String) => s.split(java.io.File.pathSeparator).map(file) - case _ => throw new IllegalStateException("No server classpath was specified.") - } - val sbtVersion = sys.props.get("sbt.server.version") match { - case Some(v: String) => v - case _ => throw new IllegalStateException("No server version was specified.") - } - val scalaVersion = sys.props.get("sbt.server.scala.version") match { - case Some(v: String) => v - case _ => throw new IllegalStateException("No server scala version was specified.") - } + val classpath = TestProperties.classpath.split(File.pathSeparator).map(new File(_)) + val sbtVersion = TestProperties.version + val scalaVersion = TestProperties.scalaVersion svr = TestServer.get(testDirectory, scalaVersion, sbtVersion, classpath, temp) } override def tearDownSuite(): Unit = { @@ -169,7 +160,12 @@ case class TestServer( import TestServer.hostLog hostLog("fork to a new sbt instance") - val process = RunFromSourceMain.fork(baseDirectory, scalaVersion, sbtVersion, classpath) + val forkOptions = + ForkOptions() + .withOutputStrategy(OutputStrategy.StdoutOutput) + .withRunJVMOptions(Vector("-Dsbt.ci=true", "-Dsbt.io.virtual=false")) + val process = + RunFromSourceMain.fork(forkOptions, baseDirectory, scalaVersion, sbtVersion, classpath) lazy val portfile = baseDirectory / "project" / "target" / "active.json"