[2.x] Add scripted test for #7838 (#8541)

This PR adds a new scripted test `watch-termination-reload` to prevent regression of the fix in PR #7838. The test verifies that the `Reload` action is correctly passed to the `watchOnTermination` callback when a reload is triggered in watch mode, instead of falling back to `CancelWatch` (which was the root cause of issue #7017).
This commit is contained in:
Dairus 2026-01-20 18:13:27 +01:00 committed by GitHub
parent 6493707d2f
commit d2cd284e5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,43 @@
package sbt.watch.termination.reload
import sbt._
import Keys._
import sbt.nio.Keys._
import sbt.nio.Watch
object Build {
val checkTerminationAction = inputKey[Unit]("Check that watchOnTermination received the expected action")
val checkTerminationActionImpl: Def.Initialize[InputTask[Unit]] = Def.inputTask {
val Seq(expectedAction) = Def.spaceDelimited().parsed
val logFile = baseDirectory.value / "action-log.txt"
val loggedAction = IO.read(logFile).trim
assert(loggedAction == expectedAction, s"Expected action '$expectedAction', but got '$loggedAction'")
}
lazy val root = (project in file(".")).settings(
checkTerminationAction := checkTerminationActionImpl.evaluated,
watchOnTermination := { (count: Int, action: Watch.Action) =>
if (action == Watch.Reload) {
val logFile = baseDirectory.value / "action-log.txt"
IO.write(logFile, action.toString)
}
},
watchOnFileInputEvent := { (count: Int, event: Watch.Event) =>
if (event.path.getFileName.toString == "trigger.txt") {
val flagFile = baseDirectory.value / "triggered.txt"
if (!flagFile.exists) {
IO.write(flagFile, "true")
Watch.Reload
} else {
Watch.CancelWatch
}
} else Watch.Ignore
},
watchSources += baseDirectory.value / "trigger.txt",
Compile / compile := {
val trigger = baseDirectory.value / "trigger.txt"
IO.write(trigger, System.currentTimeMillis.toString)
(Compile / compile).value
}
)
}

View File

@ -0,0 +1,3 @@
class Test {
// Minimal source to enable ~compile in watch mode
}

View File

@ -0,0 +1,4 @@
# Start watch mode with ~compile, which triggers file modification and reload
> ~compile
# Check that watchOnTermination received Reload action
> checkTerminationAction Reload