[Test] Reset ApplicationManager.application when shared application environments are disposed

- Shared application environments can be disposed in the middle of test
  plan execution when the project count reaches zero. So resetting
  `ApplicationManager` only on "test plan execution finished" is
  incorrect, because it does not account for that scenario. Instead, we
  should reset `ApplicationManager` every time the application
  environment is disposed.
- We should also keep the manual reset in `testPlanExecutionFinished`
  because `disposeApplicationEnvironment` may not need to dispose
  anything if there isn't a shared application environment, but we
  should still reset `ApplicationManager` if an unshared application
  environment was created.
- This fixes "Some test disposed, but forgot to clear MockApplication"
  exceptions which appeared after KT-64099 because it opened up proper
  disposal of the shared application environment (instead of it being in
  a state of leakage because not all project disposables were properly
  disposed).

^KT-63650
^KT-64099
This commit is contained in:
Marco Pennekamp
2023-12-05 21:16:06 +01:00
committed by Space Team
parent 85c9c57da4
commit dcf7b84082
3 changed files with 31 additions and 10 deletions
@@ -6,9 +6,8 @@
package org.jetbrains.kotlin.test.testFramework
import com.intellij.openapi.application.Application
import com.intellij.openapi.application.ApplicationManager
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.utils.rethrow
import java.lang.reflect.Field
fun resetApplicationToNull(old: Application?) {
if (old != null) return
@@ -17,9 +16,7 @@ fun resetApplicationToNull(old: Application?) {
fun resetApplicationToNull() {
try {
val ourApplicationField: Field = ApplicationManager::class.java.getDeclaredField("ourApplication")
ourApplicationField.setAccessible(true)
ourApplicationField.set(null, null)
KotlinCoreEnvironment.resetApplicationManager()
} catch (e: Exception) {
throw rethrow(e)
}