From 87e6832f67564e7548294668e4ba3f5dd3904ec7 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Sat, 1 Jun 2019 20:16:58 -0700 Subject: [PATCH] Make scripted watch/on-changes more robust I noticed that sometimes this test hangs in ci. I think it may be because sometimes the event for A got handled while the cache entry for B.scala was invalidated. Because the test runs ~compile, the next run of compile would then reset the cache entry for B.scala. The next run of watchOnIteration invalidtes B.scala again, but this time to the same hash, so it doesn't actually trigger a file input event. By changing the content of B.scala every time, we ensure that it should still cause a file event. I haven't confirmed that this will fix the sporadic hangs because I can't reproduce on my machine, but this change won't hurt. --- sbt/src/sbt-test/watch/on-change/build.sbt | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/sbt/src/sbt-test/watch/on-change/build.sbt b/sbt/src/sbt-test/watch/on-change/build.sbt index b1ccae48d..048ca05aa 100644 --- a/sbt/src/sbt-test/watch/on-change/build.sbt +++ b/sbt/src/sbt-test/watch/on-change/build.sbt @@ -1,8 +1,10 @@ import java.nio.file._ +import java.nio.file.attribute.FileTime + import sbt.nio.Keys._ import sbt.nio._ + import scala.concurrent.duration._ -import StandardCopyOption.{ REPLACE_EXISTING => replace } watchTriggeredMessage := { (i, path: Path, c) => val prev = watchTriggeredMessage.value @@ -16,14 +18,15 @@ watchOnIteration := { i: Int => val src = base.resolve("src").resolve("main").resolve("scala").resolve("sbt").resolve("test") val changes = base.resolve("changes") - Files.copy(changes.resolve("C.scala"), src.resolve("C.scala"), replace) - if (i < 5) { + def copy(fileName: String): Unit = { val content = - new String(Files.readAllBytes(changes.resolve("A.scala"))) + "\n" + ("//" * i) - Files.write(src.resolve("A.scala"), content.getBytes) - } else { - Files.copy(changes.resolve("B.scala"), src.resolve("B.scala"), replace) + new String(Files.readAllBytes(changes.resolve(fileName))) + "\n" + ("//" * i) + Files.write(src.resolve(fileName), content.getBytes) } + val c = src.resolve("C.scala") + Files.setLastModifiedTime(c, FileTime.fromMillis(Files.getLastModifiedTime(c).toMillis + 1111)) + if (i < 5) copy("A.scala") + else copy("B.scala") println(s"Waiting for changes...") Watch.Ignore }