Skip to content

Commit

Permalink
(WIP) require dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin committed Sep 30, 2024
1 parent 12da972 commit b55fd8c
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory
import java.io.File

/**
* DO NOT FORGET TO CALL
*
* ```
* Disposer.dispose(environment.disposable)
* ```
*/
class Environment(
val classpath: List<String>,
kotlinLanguageVersion: LanguageVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonarsource.kotlin.api.checks

import com.intellij.openapi.util.Disposer
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.ObjectAssert
import com.intellij.psi.PsiElement
Expand Down Expand Up @@ -50,6 +51,7 @@ import org.jetbrains.kotlin.psi.psiUtil.allChildren
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import org.sonar.api.SonarEdition
Expand All @@ -65,7 +67,7 @@ import java.nio.charset.StandardCharsets
import java.nio.file.Path
import java.util.TreeMap

internal class ApiExtensionsKtTest {
private class ApiExtensionsKtTest : AbstractApiExtensionsKtTest() {

@Test
fun `test isLocalVariable`() {
Expand Down Expand Up @@ -286,7 +288,7 @@ internal class ApiExtensionsKtTest {
}
}

class ApiExtensionsKtDetermineTypeTest {
private class ApiExtensionsKtDetermineTypeTest : AbstractApiExtensionsKtTest() {
private val bindingContext: BindingContext
private val ktFile: KtFile

Expand Down Expand Up @@ -512,7 +514,7 @@ class ApiExtensionsKtDetermineTypeTest {
}
}

class ApiExtensionsKtDetermineSignatureTest {
private class ApiExtensionsKtDetermineSignatureTest : AbstractApiExtensionsKtTest() {
private val bindingContext: BindingContext
private val ktFile: KtFile

Expand Down Expand Up @@ -547,9 +549,8 @@ class ApiExtensionsKtDetermineSignatureTest {
}
}

class ApiExtensionsScopeFunctionResolutionTest {
companion object {
private fun generateAst(funContent: String) = """
private class ApiExtensionsScopeFunctionResolutionTest : AbstractApiExtensionsKtTest() {
private fun generateAst(funContent: String) = """
package bar
class Foo {
Expand All @@ -560,7 +561,6 @@ class ApiExtensionsScopeFunctionResolutionTest {
}
}
""".let { parse(it) }.let { it.psiFile to it.bindingContext }
}

@Test
fun `resolve this as arg in with`() {
Expand Down Expand Up @@ -701,28 +701,36 @@ class ApiExtensionsScopeFunctionResolutionTest {
}
}

private fun parse(code: String) = kotlinTreeOf(
code,
Environment(
listOf("build/classes/kotlin/main") + System.getProperty("java.class.path").split(File.pathSeparatorChar),
LanguageVersion.LATEST_STABLE
),
TestInputFileBuilder("moduleKey", "src/org/foo/kotlin.kt")
.setCharset(StandardCharsets.UTF_8)
.initMetadata(code)
.build()
)

private fun parseWithoutParsingExceptions(code: String): KtFile {
val environment = Environment(
private abstract class AbstractApiExtensionsKtTest {
/**
* Disposed in [afterEach]
*/
private val environment = Environment(
listOf("build/classes/kotlin/main") + System.getProperty("java.class.path").split(File.pathSeparatorChar),
LanguageVersion.LATEST_STABLE
)
val inputFile = TestInputFileBuilder("moduleKey", "src/org/foo/kotlin.kt")
.setCharset(StandardCharsets.UTF_8)
.initMetadata(code)
.build()
return environment.ktPsiFactory.createFile(inputFile.uri().path, code.replace("""\r\n?""".toRegex(), "\n"))

fun parse(code: String) = kotlinTreeOf(
code,
environment,
TestInputFileBuilder("moduleKey", "src/org/foo/kotlin.kt")
.setCharset(StandardCharsets.UTF_8)
.initMetadata(code)
.build()
)

fun parseWithoutParsingExceptions(code: String): KtFile {
val inputFile = TestInputFileBuilder("moduleKey", "src/org/foo/kotlin.kt")
.setCharset(StandardCharsets.UTF_8)
.initMetadata(code)
.build()
return environment.ktPsiFactory.createFile(inputFile.uri().path, code.replace("""\r\n?""".toRegex(), "\n"))
}

@AfterEach
fun afterEach() {
Disposer.dispose(environment.disposable)
}
}

private class KtExpressionAssert(expression: KtExpression?) : ObjectAssert<KtExpression>(expression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
*/
package org.sonarsource.kotlin.api.checks

import com.intellij.openapi.util.Disposer
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.sonar.api.batch.fs.internal.DefaultInputFile
import org.sonar.api.batch.fs.internal.TestInputFileBuilder
Expand All @@ -35,6 +37,9 @@ import java.nio.file.Paths

class FieldMatcherTest {

/**
* Disposed in [afterEach]
*/
val environment = Environment(listOf("../kotlin-checks-test-sources/build/classes/kotlin/main"), LanguageVersion.LATEST_STABLE)

val path: Path = Paths.get("../kotlin-checks-test-sources/src/main/kotlin/sample/fields.kt")
Expand Down Expand Up @@ -161,6 +166,11 @@ class FieldMatcherTest {
if (matcher.matches(it, tree.bindingContext)) index else null
}).isEqualTo(expectedMatches.toList())
}

@AfterEach
fun afterEach() {
Disposer.dispose(environment.disposable)
}
}

private fun <T> List<T>.rearrange(vararg indices: Int) = List(indices.size) { this[indices[it]] }
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonarsource.kotlin.api.checks

import com.intellij.openapi.util.Disposer
import io.mockk.Called
import io.mockk.spyk
import io.mockk.unmockkAll
Expand All @@ -31,6 +32,7 @@ import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getCall
import org.jetbrains.kotlin.types.typeUtil.TypeNullability
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.sonar.api.batch.fs.internal.TestInputFileBuilder
import org.sonarsource.kotlin.api.frontend.Environment
Expand All @@ -41,7 +43,11 @@ import java.nio.file.Paths

class FunMatcherTest {

/**
* Disposed in [afterEach]
*/
val environment = Environment(listOf("../kotlin-checks-test-sources/build/classes/kotlin/main"), LanguageVersion.LATEST_STABLE)

val path = Paths.get("../kotlin-checks-test-sources/src/main/kotlin/sample/functions.kt")
val content = String(Files.readAllBytes(path))
val inputFile = TestInputFileBuilder("moduleKey", "src/org/foo/kotlin.kt")
Expand Down Expand Up @@ -612,4 +618,10 @@ class FunMatcherTest {
}
}
}

@AfterEach
fun afterEach() {
Disposer.dispose(environment.disposable)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonarsource.kotlin.api.frontend

import com.intellij.openapi.util.Disposer
import io.mockk.every
import io.mockk.mockkStatic
import io.mockk.unmockkAll
Expand Down Expand Up @@ -53,7 +54,7 @@ internal class KotlinSyntaxStructureTest {
every { BindingContextUtils.getRecordedTypeInfo(any(), any()) } throws expectedException

val content = path.readText()
val environment = Environment(System.getProperty("java.class.path").split(File.pathSeparatorChar), LanguageVersion.LATEST_STABLE)
val environment = /* Disposed below */ Environment(System.getProperty("java.class.path").split(File.pathSeparatorChar), LanguageVersion.LATEST_STABLE)
val inputFile = TestInputFileBuilder("moduleKey", path.toString())
.setCharset(StandardCharsets.UTF_8)
.initMetadata(content).build()
Expand All @@ -64,13 +65,15 @@ internal class KotlinSyntaxStructureTest {
.hasMessageStartingWith("Exception while analyzing expression in (4,17) in ")
.hasMessageContaining("/moduleKey/src/test/resources/api/sample/SimpleClass.kt")
}

Disposer.dispose(environment.disposable)
}

@Test
fun `ensure ktfile name is properly set`() {
val path = Path.of("src/test/resources/api/sample/SimpleClass.kt")
val content = path.readText()
val environment = Environment(listOf("../kotlin-checks-test-sources/build/classes/kotlin/main"), LanguageVersion.LATEST_STABLE)
val environment = /* Disposed below */ Environment(listOf("../kotlin-checks-test-sources/build/classes/kotlin/main"), LanguageVersion.LATEST_STABLE)

val inputFile = TestInputFileBuilder("moduleKey", path.toString())
.setCharset(StandardCharsets.UTF_8)
Expand All @@ -79,5 +82,7 @@ internal class KotlinSyntaxStructureTest {

val (ktFile, _, _) = KotlinSyntaxStructure.of(content, environment, inputFile)
assertThat(ktFile.containingFile.name).endsWith("/moduleKey/src/test/resources/api/sample/SimpleClass.kt")

Disposer.dispose(environment.disposable)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonarsource.kotlin.api.frontend

import com.intellij.openapi.util.Disposer
import java.nio.file.Files
import java.nio.file.Path
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -34,7 +35,7 @@ class KotlinTreeTest {

@Test
fun testCreateKotlinTree() {
val environment = Environment(listOf("../kotlin-checks-test-sources/build/classes/kotlin/main"), LanguageVersion.LATEST_STABLE)
val environment = /* Disposed below */ Environment(listOf("../kotlin-checks-test-sources/build/classes/kotlin/main"), LanguageVersion.LATEST_STABLE)
val path = Path.of("../kotlin-checks-test-sources/src/main/kotlin/sample/functions.kt")
val content = String(Files.readAllBytes(path))
val inputFile = TestInputFileBuilder("moduleKey", "src/org/foo/kotlin.kt")
Expand All @@ -51,5 +52,7 @@ class KotlinTreeTest {
val call = tree.bindingContext.get(BindingContext.CALL, ktCallExpression)
val resolvedCall = tree.bindingContext.get(BindingContext.RESOLVED_CALL, call)
assertThat(resolvedCall).isNotNull

Disposer.dispose(environment.disposable)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonarsource.kotlin.api.frontend

import com.intellij.openapi.util.Disposer
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.junit.jupiter.api.Test
Expand All @@ -27,6 +28,7 @@ import org.sonarsource.kotlin.testapi.kotlinTreeOf
import org.assertj.core.api.Assertions
import org.assertj.core.api.ObjectAssert
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.junit.jupiter.api.AfterEach
import java.util.TreeMap

private const val TQ = "\"\"\""
Expand Down Expand Up @@ -133,7 +135,7 @@ internal class TextRangeTrackerTest {
val x =
$regex
""".trimIndent(),
environment = Environment(emptyList(), LanguageVersion.LATEST_STABLE),
environment,
inputFile = DummyInputFile()
)

Expand All @@ -142,4 +144,15 @@ internal class TextRangeTrackerTest {

return TextRangeTracker.of(entries, DummyInputFile(), tree.document)
}
}

/**
* Disposed in [afterEach]
*/
private val environment = Environment(emptyList(), LanguageVersion.LATEST_STABLE)

@AfterEach
fun afterEach() {
Disposer.dispose(environment.disposable)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonarsource.kotlin.metrics

import com.intellij.openapi.util.Disposer
import org.assertj.core.api.Assertions
import com.intellij.psi.PsiElement
import com.intellij.psi.impl.source.tree.LeafPsiElement
Expand All @@ -27,6 +28,7 @@ import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtLoopExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtWhenEntry
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.sonarsource.kotlin.api.frontend.Environment

Expand Down Expand Up @@ -142,10 +144,20 @@ internal class CyclomaticComplexityVisitorTest {
}

private fun getComplexityTrees(content: String): List<PsiElement> {
val env = Environment(emptyList(), LanguageVersion.LATEST_STABLE)
val ktFile = env.ktPsiFactory.createFile(content)
val cyclomaticComplexityVisitor = CyclomaticComplexityVisitor()
ktFile.accept(cyclomaticComplexityVisitor)
return cyclomaticComplexityVisitor.complexityTrees()
}

/**
* Disposed in [afterEach]
*/
private val env = Environment(emptyList(), LanguageVersion.LATEST_STABLE)

@AfterEach
fun afterEach() {
Disposer.dispose(env.disposable)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class IssueSuppressionVisitorTest {
)

private fun scanFile(path: Path, suppress: Boolean, check: AbstractCheck, vararg checks: AbstractCheck): SingleFileVerifier {
// TODO dispose
val env = Environment(System.getProperty("java.class.path").split(File.pathSeparatorChar) + DEFAULT_KOTLIN_CLASSPATH, LanguageVersion.LATEST_STABLE)
val verifier = SingleFileVerifier.create(path, StandardCharsets.UTF_8)
val testFileContent = String(Files.readAllBytes(path), StandardCharsets.UTF_8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
*/
package org.sonarsource.kotlin.metrics

import com.intellij.openapi.util.Disposer
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.config.LanguageVersion
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
Expand All @@ -43,7 +45,11 @@ import kotlin.io.path.name

internal class MetricVisitorTest {

/**
* Disposed in [afterEach]
*/
private val environment = Environment(emptyList(), LanguageVersion.LATEST_STABLE)

private lateinit var mockNoSonarFilter: NoSonarFilter
private lateinit var visitor: MetricVisitor
private lateinit var sensorContext: SensorContextTester
Expand Down Expand Up @@ -343,4 +349,10 @@ internal class MetricVisitorTest {
val ctx = InputFileContextImpl(sensorContext, inputFile, false)
visitor.scan(ctx, kotlinTreeOf(code, environment, inputFile))
}

@AfterEach
fun afterEach() {
Disposer.dispose(environment.disposable)
}

}
Loading

0 comments on commit b55fd8c

Please sign in to comment.