[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,13 +6,11 @@
package org.jetbrains.kotlin.test
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.util.Disposer
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.test.services.ApplicationDisposableProvider
import org.junit.platform.launcher.TestExecutionListener
import org.junit.platform.launcher.TestPlan
import java.lang.reflect.Field
class ApplicationEnvironmentDisposer : TestExecutionListener {
companion object {
@@ -22,9 +20,7 @@ class ApplicationEnvironmentDisposer : TestExecutionListener {
override fun testPlanExecutionFinished(testPlan: TestPlan) {
KotlinCoreEnvironment.disposeApplicationEnvironment()
Disposer.dispose(ROOT_DISPOSABLE)
val ourApplicationField: Field = ApplicationManager::class.java.getDeclaredField("ourApplication")
ourApplicationField.isAccessible = true
ourApplicationField.set(null, null)
KotlinCoreEnvironment.resetApplicationManager()
}
}