[Tests] Ensure the temporary directories are cleared
This change may prevent OOMs. Context: https://jetbrains.slack.com/archives/C4U955N6B/p1685000899030279
This commit is contained in:
committed by
Space Team
parent
039b5fca7a
commit
46ea908daf
+18
-1
@@ -37,8 +37,10 @@ import org.jetbrains.kotlin.test.model.ResultingArtifact
|
||||
import org.jetbrains.kotlin.test.runners.AbstractKotlinCompilerTest
|
||||
import org.jetbrains.kotlin.test.services.*
|
||||
import org.jetbrains.kotlin.test.services.impl.TemporaryDirectoryManagerImpl
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.TestInfo
|
||||
import java.io.IOException
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import kotlin.io.path.exists
|
||||
@@ -52,7 +54,13 @@ abstract class AbstractAnalysisApiBasedTest : TestWithDisposable() {
|
||||
protected lateinit var testDataPath: Path
|
||||
private set
|
||||
|
||||
private lateinit var testServices: TestServices
|
||||
private var _testServices: TestServices? = null
|
||||
|
||||
private var testServices: TestServices
|
||||
get() = _testServices ?: error("`_testServices` has not been initialized")
|
||||
set(value) {
|
||||
_testServices = value
|
||||
}
|
||||
|
||||
protected open fun configureTest(builder: TestConfigurationBuilder) {
|
||||
configurator.configureTest(builder, disposable)
|
||||
@@ -148,6 +156,15 @@ abstract class AbstractAnalysisApiBasedTest : TestWithDisposable() {
|
||||
doTestByModuleStructure(moduleStructure, testServices)
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun cleanupTemporaryDirectories() {
|
||||
try {
|
||||
_testServices?.temporaryDirectoryManager?.cleanupTemporaryDirectories()
|
||||
} catch (e: IOException) {
|
||||
println("Failed to clean temporary directories: ${e.message}\n${e.stackTrace}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun createTestConfiguration(): TestConfiguration {
|
||||
val testConfiguration = testConfiguration(testDataPath.toString(), configure)
|
||||
Disposer.register(disposable, testConfiguration.rootDisposable)
|
||||
|
||||
@@ -30,8 +30,8 @@ class TestRunner(private val testConfiguration: TestConfiguration) {
|
||||
} finally {
|
||||
try {
|
||||
testConfiguration.testServices.temporaryDirectoryManager.cleanupTemporaryDirectories()
|
||||
} catch (_: IOException) {
|
||||
// ignored
|
||||
} catch (e: IOException) {
|
||||
println("Failed to clean temporary directories: ${e.message}\n${e.stackTrace}")
|
||||
}
|
||||
beforeDispose(testConfiguration)
|
||||
Disposer.dispose(testConfiguration.rootDisposable)
|
||||
|
||||
+17
-6
@@ -5,22 +5,23 @@
|
||||
|
||||
package org.jetbrains.kotlin.test.services.impl
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import com.intellij.openapi.util.io.NioFiles
|
||||
import org.jetbrains.kotlin.test.services.TemporaryDirectoryManager
|
||||
import org.jetbrains.kotlin.test.services.TestServices
|
||||
import org.jetbrains.kotlin.test.services.testInfo
|
||||
import org.jetbrains.kotlin.test.util.KtTestUtil
|
||||
import java.io.File
|
||||
import java.nio.file.Paths
|
||||
import java.util.*
|
||||
|
||||
class TemporaryDirectoryManagerImpl(testServices: TestServices) : TemporaryDirectoryManager(testServices) {
|
||||
private val cache = mutableMapOf<String, File>()
|
||||
private val rootTempDir: File = run {
|
||||
private val rootTempDir = lazy {
|
||||
val testInfo = testServices.testInfo
|
||||
val className = testInfo.className
|
||||
val methodName = testInfo.methodName
|
||||
if (!onWindows && className.length + methodName.length < 255) {
|
||||
return@run KtTestUtil.tmpDirForTest(className, methodName)
|
||||
return@lazy KtTestUtil.tmpDirForTest(className, methodName)
|
||||
}
|
||||
|
||||
// This code will simplify directory name for windows. This is needed because there can occur errors due to long name
|
||||
@@ -32,15 +33,25 @@ class TemporaryDirectoryManagerImpl(testServices: TestServices) : TemporaryDirec
|
||||
}
|
||||
|
||||
override val rootDir: File
|
||||
get() = rootTempDir
|
||||
get() = rootTempDir.value
|
||||
|
||||
override fun getOrCreateTempDirectory(name: String): File {
|
||||
return cache.getOrPut(name) { KtTestUtil.tmpDir(rootTempDir, name) }
|
||||
return cache.getOrPut(name) { KtTestUtil.tmpDir(rootDir, name) }
|
||||
}
|
||||
|
||||
override fun cleanupTemporaryDirectories() {
|
||||
cache.clear()
|
||||
FileUtil.delete(rootTempDir)
|
||||
|
||||
if (rootTempDir.isInitialized()) {
|
||||
NioFiles.deleteRecursively(Paths.get(rootDir.path))
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("removal")
|
||||
fun finalize() {
|
||||
if (rootTempDir.isInitialized() && rootDir.exists()) {
|
||||
error("The temporary directory $rootDir has not been deleted by the time the corresponding ${this::class.simpleName} is finalized.")
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
Reference in New Issue
Block a user