Use test classpath for annotations.jar search

Got unstable behaviour in 202 test because of setting
idea.home.path during the compiler execution.
This commit is contained in:
Nikolay Krasko
2020-07-11 13:16:49 +03:00
parent 07e3dd9ec7
commit 0db7806dec
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.psi
import com.intellij.lang.html.HTMLLanguage
import com.intellij.openapi.application.ApplicationManager
import com.intellij.testFramework.LightProjectDescriptor
import org.intellij.lang.annotations.Language
import org.intellij.lang.regexp.RegExpLanguage
@@ -14,13 +15,10 @@ import org.jetbrains.kotlin.idea.test.SdkAndMockLibraryProjectDescriptor
import org.jetbrains.kotlin.test.JUnit3WithIdeaConfigurationRunner
import org.junit.runner.RunWith
import java.io.File
import java.net.URLClassLoader
@RunWith(JUnit3WithIdeaConfigurationRunner::class)
class KotlinLibInjectionTest : AbstractInjectionTest() {
override fun setUp() {
super.setUp()
}
fun testFunInjection() = assertInjectionPresent(
"""
import injection.html
@@ -45,13 +43,25 @@ class KotlinLibInjectionTest : AbstractInjectionTest() {
doInjectionPresentTest(text, languageId = languageId, unInjectShouldBePresent = false)
}
override fun getProjectDescriptor(): LightProjectDescriptor {
val ideaSdkPath = System.getProperty("idea.home.path")?.takeIf { File(it).isDirectory }
?: throw RuntimeException("Unable to get a valid path from 'idea.home.path' property, please point it to the Idea location")
val urls = (ApplicationManager::class.java.classLoader as? URLClassLoader)?.urLs
?: error("Can't find path urls in classloader")
val libName = "annotations.jar"
val libCandidates = urls
.map { it.path.replace("\\", "/") }
.filter { it.endsWith("/$libName") }
val libPath = libCandidates.singleOrNull()
?: error("Can't find single $libName in classpath among $libCandidates")
val libFile = File(libPath).also { file ->
require(file.exists()) { "Can't find library: ${file.absolutePath}" }
}
return SdkAndMockLibraryProjectDescriptor(
PluginTestCaseBase.getTestDataPathBase() + "/injection/lib/", false, false, false, true,
listOf(File(ideaSdkPath, "lib/annotations.jar").absolutePath)
listOf(libFile.absolutePath)
)
}
}