Add integration test for bspConfig & embedded launcher jar (fixes #7794) (#8597)

This adds a scripted integration test to verify that the `bspConfig` task correctly generates a valid BSP connection file (`.bsp/sbt.json`) when the embedded launcher JAR is present in the classpath.
This commit is contained in:
Dairus 2026-01-24 21:05:51 +01:00 committed by GitHub
parent f8704752e0
commit 7a9775c87d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 269 additions and 184 deletions

View File

@ -1342,6 +1342,7 @@ lazy val launcherPackageIntegrationTest =
libraryDependencies ++= Seq(
scalaVerify % Test,
hedgehog % Test,
"com.lihaoyi" %% "ujson" % "3.1.0" % Test,
// This needs to be hardcoded here, and not use addSbtIO
"org.scala-sbt" %% "io" % "1.10.5" % Test,
),

View File

@ -0,0 +1,84 @@
package example.test
import scala.sys.process.*
import java.io.File
import java.util.Locale
import sbt.io.IO
import verify.BasicTestSuite
// Test for issues #7792/#7794: BSP config generation and argv execution
object BspConfigTest extends BasicTestSuite:
lazy val isWindows: Boolean =
sys.props("os.name").toLowerCase(Locale.ENGLISH).contains("windows")
lazy val sbtScript =
if (isWindows) new File("launcher-package/target/universal/stage/bin/sbt.bat")
else new File("launcher-package/target/universal/stage/bin/sbt")
def sbtProcessInDir(dir: File)(args: String*) =
Process(
Seq(sbtScript.getAbsolutePath) ++ args,
dir,
"JAVA_OPTS" -> "",
"SBT_OPTS" -> ""
)
test("sbt bspConfig") {
import ujson.*
IO.withTemporaryDirectory { tmp =>
// Create minimal build.sbt for the test project
IO.write(new File(tmp, "build.sbt"), """name := "test-bsp-config"""")
// Run bspConfig to generate .bsp/sbt.json
val configResult = sbtProcessInDir(tmp)("bspConfig", "--batch").!
assert(configResult == 0, s"bspConfig command failed with exit code $configResult")
// Verify .bsp/sbt.json exists
val bspFile = new File(tmp, ".bsp/sbt.json")
assert(bspFile.exists, ".bsp/sbt.json should exist after running bspConfig")
// Parse and verify JSON content
val content = IO.read(bspFile)
val json = ujson.read(content)
// Extract argv array from JSON
val argvValue = json.obj.get("argv")
assert(argvValue.isDefined, "argv field not found in sbt.json")
val argv = argvValue.get.arr.map(_.str).toVector
// Verify argv structure
assert(argv.nonEmpty, "argv should not be empty")
assert(argv.head.contains("java"), s"argv should start with java command, got: ${argv.head}")
assert(argv.contains("-bsp"), s"argv should contain -bsp flag, got: $argv")
// Test execution of the generated argv
// Run the BSP command with a very short timeout to verify it starts correctly
// We just need to verify the command doesn't fail immediately on startup
if (!isWindows) {
// On Unix, we can test the argv execution
// Create a process and check if it starts (will timeout waiting for BSP input)
val process = Process(argv.toSeq, tmp)
val processBuilder = process.run(ProcessLogger(_ => (), _ => ()))
// Give it a moment to fail if it's going to fail immediately
Thread.sleep(500)
// If still running, it means the BSP server started successfully
val isAlive = processBuilder.isAlive()
processBuilder.destroy()
// The process should either still be alive (waiting for BSP messages)
// or have exited with code 0 (graceful)
if (!isAlive) {
val exitCode = processBuilder.exitValue()
assert(
exitCode == 0 || exitCode == 143, // 143 = SIGTERM from destroy()
s"BSP process failed with exit code $exitCode"
)
}
}
}
()
}
end BspConfigTest

View File

@ -1,184 +1,184 @@
package example.test
import scala.sys.process.*
import java.io.File
import java.util.Locale
import sbt.io.IO
import verify.BasicTestSuite
object ExtendedRunnerTest extends BasicTestSuite:
// 1.3.0, 1.3.0-M4
private[test] val versionRegEx = "\\d(\\.\\d+){2}(-\\w+)?"
lazy val isWindows: Boolean = sys.props("os.name").toLowerCase(Locale.ENGLISH).contains("windows")
lazy val isMac: Boolean = sys.props("os.name").toLowerCase(Locale.ENGLISH).contains("mac")
lazy val sbtScript =
if (isWindows) new File("launcher-package/target/universal/stage/bin/sbt.bat")
else new File("launcher-package/target/universal/stage/bin/sbt")
def sbtProcess(args: String*) = sbtProcessWithOpts(args*)("", "")
def sbtProcessWithOpts(args: String*)(javaOpts: String, sbtOpts: String) =
Process(
Seq(sbtScript.getAbsolutePath) ++ args,
new File("launcher-package/citest"),
"JAVA_OPTS" -> javaOpts,
"SBT_OPTS" -> sbtOpts
)
def sbtProcessInDir(dir: File)(args: String*) =
Process(
Seq(sbtScript.getAbsolutePath) ++ args,
dir,
"JAVA_OPTS" -> "",
"SBT_OPTS" -> ""
)
test("sbt runs") {
assert(sbtScript.exists)
val out = sbtProcess("compile", "-v").!
assert(out == 0)
()
}
def testVersion(lines: List[String]): Unit = {
assert(lines.size >= 2)
val expected0 = s"(?m)^sbt version in this project: $versionRegEx(\\r)?"
assert(lines(0).matches(expected0))
val expected1 = s"sbt runner version: $versionRegEx$$"
assert(lines(1).matches(expected1))
}
/* TODO: The lines seems to return List([0Jsbt runner version: 1.11.4) on CI
test("sbt -V|-version|--version should print sbtVersion") {
val out = sbtProcess("-version").!!.trim
testVersion(out.linesIterator.toList)
val out2 = sbtProcess("--version").!!.trim
testVersion(out2.linesIterator.toList)
val out3 = sbtProcess("-V").!!.trim
testVersion(out3.linesIterator.toList)
}
*/
test("sbt -V in empty directory") {
IO.withTemporaryDirectory { tmp =>
val out = sbtProcessInDir(tmp)("-V").!!.trim
val expectedVersion = "^" + versionRegEx + "$"
val targetDir = new File(tmp, "target")
assert(!targetDir.exists, "expected target directory to not exist, but existed")
}
()
}
/* TODO: Not sure why but the output is returning [0J on CI
test("sbt --numeric-version should print sbt script version") {
val out = sbtProcess("--numeric-version").!!.trim
val expectedVersion = "^"+versionRegEx+"$"
assert(out.matches(expectedVersion))
()
}
*/
test("sbt --sbt-jar should run") {
val out = sbtProcess(
"compile",
"-v",
"--sbt-jar",
"../target/universal/stage/bin/sbt-launch.jar"
).!!.linesIterator.toList
assert(
out.contains[String]("../target/universal/stage/bin/sbt-launch.jar") ||
out.contains[String]("\"../target/universal/stage/bin/sbt-launch.jar\"")
)
()
}
test("sbt \"testOnly *\"") {
if (isMac) ()
else {
val out = sbtProcess("testOnly *", "--no-colors", "-v").!!.linesIterator.toList
assert(out.contains[String]("[info] HelloTest"))
()
}
}
test("sbt in empty directory") {
IO.withTemporaryDirectory { tmp =>
val out = sbtProcessInDir(tmp)("about").!
assert(out == 1)
}
IO.withTemporaryDirectory { tmp =>
val out = sbtProcessInDir(tmp)("about", "--allow-empty").!
assert(out == 0)
}
()
}
test("sbt --script-version in empty directory") {
IO.withTemporaryDirectory { tmp =>
val out = sbtProcessInDir(tmp)("--script-version").!!.trim
val expectedVersion = "^" + versionRegEx + "$"
assert(out.matches(expectedVersion))
}
()
}
test("sbt --jvm-client") {
val out = sbtProcess("--jvm-client", "--no-colors", "compile").!!.linesIterator.toList
if (isWindows) {
println(out)
} else {
assert(out.exists { _.contains("server was not detected") })
}
val out2 = sbtProcess("--jvm-client", "--no-colors", "shutdown").!!.linesIterator.toList
if (isWindows) {
println(out2)
} else {
assert(out2.exists { _.contains("disconnected") })
}
()
}
// Test for issue #6485: Test `sbt --client` startup
// https://github.com/sbt/sbt/issues/6485
test("sbt --client startup time") {
if (isWindows || isMac) {
// Skip on Windows (sbtn behavior differs) and macOS CI (slow hostname resolution)
()
} else {
// First call starts the server if not running (warmup)
val warmup = sbtProcess("--client", "version").!
assert(warmup == 0, "Warmup sbt --client version failed")
// Measure startup time for sbt --client when server is already running
// Run multiple times and take the average to reduce variance
val iterations = 5
val times = (1 to iterations).map { _ =>
val start = System.nanoTime()
val exitCode = sbtProcess("--client", "version").!
val elapsed = (System.nanoTime() - start) / 1_000_000 // Convert to milliseconds
assert(exitCode == 0, "sbt --client version failed")
elapsed
}
val avgTime = times.sum / iterations
val maxTime = times.max
println(s"sbt --client startup times (ms): ${times.mkString(", ")}")
println(s"Average: ${avgTime}ms, Max: ${maxTime}ms")
// Cap at 2000ms to catch significant regressions while allowing for CI variance.
// The original issue #5980 mentioned ~200ms on developer machines in 2021,
// but CI runners are typically 2-3x slower than local development machines.
assert(
avgTime < 2000,
s"sbt --client startup time (${avgTime}ms average) exceeded 2000ms threshold"
)
// Cleanup: shutdown the server
val shutdown = sbtProcess("--client", "shutdown").!
assert(shutdown == 0, "Failed to shutdown sbt server")
}
()
}
end ExtendedRunnerTest
package example.test
import scala.sys.process.*
import java.io.File
import java.util.Locale
import sbt.io.IO
import verify.BasicTestSuite
object ExtendedRunnerTest extends BasicTestSuite:
// 1.3.0, 1.3.0-M4
private[test] val versionRegEx = "\\d(\\.\\d+){2}(-\\w+)?"
lazy val isWindows: Boolean = sys.props("os.name").toLowerCase(Locale.ENGLISH).contains("windows")
lazy val isMac: Boolean = sys.props("os.name").toLowerCase(Locale.ENGLISH).contains("mac")
lazy val sbtScript =
if (isWindows) new File("launcher-package/target/universal/stage/bin/sbt.bat")
else new File("launcher-package/target/universal/stage/bin/sbt")
def sbtProcess(args: String*) = sbtProcessWithOpts(args*)("", "")
def sbtProcessWithOpts(args: String*)(javaOpts: String, sbtOpts: String) =
Process(
Seq(sbtScript.getAbsolutePath) ++ args,
new File("launcher-package/citest"),
"JAVA_OPTS" -> javaOpts,
"SBT_OPTS" -> sbtOpts
)
def sbtProcessInDir(dir: File)(args: String*) =
Process(
Seq(sbtScript.getAbsolutePath) ++ args,
dir,
"JAVA_OPTS" -> "",
"SBT_OPTS" -> ""
)
test("sbt runs") {
assert(sbtScript.exists)
val out = sbtProcess("compile", "-v").!
assert(out == 0)
()
}
def testVersion(lines: List[String]): Unit = {
assert(lines.size >= 2)
val expected0 = s"(?m)^sbt version in this project: $versionRegEx(\\r)?"
assert(lines(0).matches(expected0))
val expected1 = s"sbt runner version: $versionRegEx$$"
assert(lines(1).matches(expected1))
}
/* TODO: The lines seems to return List([0Jsbt runner version: 1.11.4) on CI
test("sbt -V|-version|--version should print sbtVersion") {
val out = sbtProcess("-version").!!.trim
testVersion(out.linesIterator.toList)
val out2 = sbtProcess("--version").!!.trim
testVersion(out2.linesIterator.toList)
val out3 = sbtProcess("-V").!!.trim
testVersion(out3.linesIterator.toList)
}
*/
test("sbt -V in empty directory") {
IO.withTemporaryDirectory { tmp =>
val out = sbtProcessInDir(tmp)("-V").!!.trim
val expectedVersion = "^" + versionRegEx + "$"
val targetDir = new File(tmp, "target")
assert(!targetDir.exists, "expected target directory to not exist, but existed")
}
()
}
/* TODO: Not sure why but the output is returning [0J on CI
test("sbt --numeric-version should print sbt script version") {
val out = sbtProcess("--numeric-version").!!.trim
val expectedVersion = "^"+versionRegEx+"$"
assert(out.matches(expectedVersion))
()
}
*/
test("sbt --sbt-jar should run") {
val out = sbtProcess(
"compile",
"-v",
"--sbt-jar",
"../target/universal/stage/bin/sbt-launch.jar"
).!!.linesIterator.toList
assert(
out.contains[String]("../target/universal/stage/bin/sbt-launch.jar") ||
out.contains[String]("\"../target/universal/stage/bin/sbt-launch.jar\"")
)
()
}
test("sbt \"testOnly *\"") {
if (isMac) ()
else {
val out = sbtProcess("testOnly *", "--no-colors", "-v").!!.linesIterator.toList
assert(out.contains[String]("[info] HelloTest"))
()
}
}
test("sbt in empty directory") {
IO.withTemporaryDirectory { tmp =>
val out = sbtProcessInDir(tmp)("about").!
assert(out == 1)
}
IO.withTemporaryDirectory { tmp =>
val out = sbtProcessInDir(tmp)("about", "--allow-empty").!
assert(out == 0)
}
()
}
test("sbt --script-version in empty directory") {
IO.withTemporaryDirectory { tmp =>
val out = sbtProcessInDir(tmp)("--script-version").!!.trim
val expectedVersion = "^" + versionRegEx + "$"
assert(out.matches(expectedVersion))
}
()
}
test("sbt --jvm-client") {
val out = sbtProcess("--jvm-client", "--no-colors", "compile").!!.linesIterator.toList
if (isWindows) {
println(out)
} else {
assert(out.exists { _.contains("server was not detected") })
}
val out2 = sbtProcess("--jvm-client", "--no-colors", "shutdown").!!.linesIterator.toList
if (isWindows) {
println(out2)
} else {
assert(out2.exists { _.contains("disconnected") })
}
()
}
// Test for issue #6485: Test `sbt --client` startup
// https://github.com/sbt/sbt/issues/6485
test("sbt --client startup time") {
if (isWindows || isMac) {
// Skip on Windows (sbtn behavior differs) and macOS CI (slow hostname resolution)
()
} else {
// First call starts the server if not running (warmup)
val warmup = sbtProcess("--client", "version").!
assert(warmup == 0, "Warmup sbt --client version failed")
// Measure startup time for sbt --client when server is already running
// Run multiple times and take the average to reduce variance
val iterations = 5
val times = (1 to iterations).map { _ =>
val start = System.nanoTime()
val exitCode = sbtProcess("--client", "version").!
val elapsed = (System.nanoTime() - start) / 1_000_000 // Convert to milliseconds
assert(exitCode == 0, "sbt --client version failed")
elapsed
}
val avgTime = times.sum / iterations
val maxTime = times.max
println(s"sbt --client startup times (ms): ${times.mkString(", ")}")
println(s"Average: ${avgTime}ms, Max: ${maxTime}ms")
// Cap at 2000ms to catch significant regressions while allowing for CI variance.
// The original issue #5980 mentioned ~200ms on developer machines in 2021,
// but CI runners are typically 2-3x slower than local development machines.
assert(
avgTime < 2000,
s"sbt --client startup time (${avgTime}ms average) exceeded 2000ms threshold"
)
// Cleanup: shutdown the server
val shutdown = sbtProcess("--client", "shutdown").!
assert(shutdown == 0, "Failed to shutdown sbt server")
}
()
}
end ExtendedRunnerTest