[2.x] test: Migrate ClassLoaderCacheTest to verify.BasicTestSuite (#8534)

This commit is contained in:
Dream 2026-01-19 17:13:21 -05:00 committed by GitHub
parent 2f348a09b9
commit a5f915bf87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 36 additions and 38 deletions

View File

@ -11,47 +11,45 @@ package sbt.internal
import java.io.File import java.io.File
import java.nio.file.Files import java.nio.file.Files
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import sbt.internal.classpath.ClassLoaderCache import sbt.internal.classpath.ClassLoaderCache
import sbt.io.IO import sbt.io.IO
import verify.BasicTestSuite
object ClassLoaderCacheTest { object ClassLoaderCacheTest extends BasicTestSuite:
extension (c: ClassLoaderCache) {
def get(classpath: Seq[File]): ClassLoader = c(classpath.toList) extension (c: ClassLoaderCache) def get(classpath: Seq[File]): ClassLoader = c(classpath.toList)
}
} private def withCache[R](f: ClassLoaderCache => R): R =
class ClassLoaderCacheTest extends AnyFlatSpec with Matchers {
import ClassLoaderCacheTest.*
private def withCache[R](f: ClassLoaderCache => R): R = {
val cache = new ClassLoaderCache(ClassLoader.getSystemClassLoader) val cache = new ClassLoaderCache(ClassLoader.getSystemClassLoader)
try f(cache) try f(cache)
finally cache.close() finally cache.close()
}
"ClassLoaderCache" should "make a new loader when full" in withCache { cache => test("ClassLoaderCache should make a new loader when cleared"):
val classPath = Seq.empty[File] withCache: cache =>
val firstLoader = cache.get(classPath) val classPath = Seq.empty[File]
cache.clear() val firstLoader = cache.get(classPath)
val secondLoader = cache.get(classPath) cache.clear()
assert(firstLoader != secondLoader) val secondLoader = cache.get(classPath)
} assert(firstLoader != secondLoader)
it should "not make a new loader when it already exists" in withCache { cache =>
val classPath = Seq.empty[File] test("ClassLoaderCache should reuse loader for same classpath"):
val firstLoader = cache.get(classPath) withCache: cache =>
val secondLoader = cache.get(classPath) val classPath = Seq.empty[File]
assert(firstLoader == secondLoader) val firstLoader = cache.get(classPath)
} val secondLoader = cache.get(classPath)
"Snapshots" should "be invalidated" in IO.withTemporaryDirectory { dir => assert(firstLoader == secondLoader)
val snapshotJar = Files.createFile(dir.toPath.resolve("foo-SNAPSHOT.jar")).toFile
val regularJar = Files.createFile(dir.toPath.resolve("regular.jar")).toFile test("Snapshots should be invalidated when modified"):
withCache { cache => IO.withTemporaryDirectory: dir =>
val jarClassPath = snapshotJar :: regularJar :: Nil val snapshotJar = Files.createFile(dir.toPath.resolve("foo-SNAPSHOT.jar")).toFile
val initLoader = cache.get(jarClassPath) val regularJar = Files.createFile(dir.toPath.resolve("regular.jar")).toFile
IO.setModifiedTimeOrFalse(snapshotJar, System.currentTimeMillis + 5000L) withCache: cache =>
val secondLoader = cache.get(jarClassPath) val jarClassPath = snapshotJar :: regularJar :: Nil
assert(initLoader != secondLoader) val initLoader = cache.get(jarClassPath)
assert(cache.get(jarClassPath) == secondLoader) IO.setModifiedTimeOrFalse(snapshotJar, System.currentTimeMillis + 5000L)
assert(cache.get(jarClassPath) != initLoader) val secondLoader = cache.get(jarClassPath)
} Predef.assert(initLoader != secondLoader)
} Predef.assert(cache.get(jarClassPath) == secondLoader)
} Predef.assert(cache.get(jarClassPath) != initLoader)
end ClassLoaderCacheTest