From 77ca8ea29a3d5321b71d60fb576a3a9ba8d4f562 Mon Sep 17 00:00:00 2001 From: Matt Dziuban Date: Tue, 7 Oct 2025 14:01:38 -0400 Subject: [PATCH] Add test for recovering from invalid JSON in disk cache. --- .../test/scala/sbt/util/ActionCacheTest.scala | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/util-cache/src/test/scala/sbt/util/ActionCacheTest.scala b/util-cache/src/test/scala/sbt/util/ActionCacheTest.scala index ef25ed777..ee6d878e5 100644 --- a/util-cache/src/test/scala/sbt/util/ActionCacheTest.scala +++ b/util-cache/src/test/scala/sbt/util/ActionCacheTest.scala @@ -80,6 +80,31 @@ object ActionCacheTest extends BasicTestSuite: // check that the action has been invoked only once assert(called == 1) + test("Disk cache can recover gracefully from invalid JSON"): + withDiskCache(testActionCacheInvalidJson) + + def testActionCacheInvalidJson(cache: DiskActionCacheStore): Unit = + import sjsonnew.BasicJsonProtocol.* + var called = 0 + val action: ((Int, Int)) => InternalActionResult[Int] = { (a, b) => + called += 1 + InternalActionResult(a + b, Nil) + } + IO.withTemporaryDirectory: tempDir => + val config = getCacheConfig(cache, tempDir) + + val v1 = ActionCache.cache((1, 1), Digest.zero, Digest.zero, tags, config)(action) + assert(v1 == 2) + + val acFiles = cache.acBase.toFile.listFiles + assert(acFiles.length == 1) + IO.write(acFiles.head, "{") + + val v2 = ActionCache.cache((1, 1), Digest.zero, Digest.zero, tags, config)(action) + assert(v2 == 2) + // check that the action has been invoked twice + assert(called == 2) + def withInMemoryCache(f: InMemoryActionCacheStore => Unit): Unit = val cache = InMemoryActionCacheStore() f(cache)