From 1fa42099a0e718879b90114d0ce7d3dead86d029 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Mon, 30 Sep 2024 14:10:05 +0200 Subject: [PATCH] (WIP) require dispose --- .../frontend/KotlinCoreEnvironmentTools.kt | 8 ++- .../kotlin/api/checks/ApiExtensionsKtTest.kt | 60 +++++++++++-------- .../kotlin/api/checks/FieldMatcherTest.kt | 10 ++++ .../kotlin/api/checks/FunMatcherTest.kt | 12 ++++ .../api/frontend/KotlinSyntaxStructureTest.kt | 9 ++- .../kotlin/api/frontend/KotlinTreeTest.kt | 5 +- .../api/frontend/TextRangeTrackerTest.kt | 17 +++++- .../CyclomaticComplexityVisitorTest.kt | 14 ++++- .../metrics/IssueSuppressionVisitorTest.kt | 1 + .../kotlin/metrics/MetricVisitorTest.kt | 12 ++++ .../kotlin/metrics/StatementsVisitorTest.kt | 8 ++- .../kotlin/plugin/KotlinSensorTest.kt | 1 + .../plugin/cpd/CopyPasteDetectorTest.kt | 18 +++++- 13 files changed, 137 insertions(+), 38 deletions(-) diff --git a/sonar-kotlin-api/src/main/java/org/sonarsource/kotlin/api/frontend/KotlinCoreEnvironmentTools.kt b/sonar-kotlin-api/src/main/java/org/sonarsource/kotlin/api/frontend/KotlinCoreEnvironmentTools.kt index 6d1c116ee..613812c9d 100644 --- a/sonar-kotlin-api/src/main/java/org/sonarsource/kotlin/api/frontend/KotlinCoreEnvironmentTools.kt +++ b/sonar-kotlin-api/src/main/java/org/sonarsource/kotlin/api/frontend/KotlinCoreEnvironmentTools.kt @@ -44,7 +44,13 @@ import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory import java.io.File -// FIXME(Godin): trace calls when classpath is empty? +/** + * DO NOT FORGET TO CALL + * + * ``` + * Disposer.dispose(environment.disposable) + * ``` + */ class Environment( val classpath: List, kotlinLanguageVersion: LanguageVersion, diff --git a/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/checks/ApiExtensionsKtTest.kt b/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/checks/ApiExtensionsKtTest.kt index 20a17cddc..065e56664 100644 --- a/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/checks/ApiExtensionsKtTest.kt +++ b/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/checks/ApiExtensionsKtTest.kt @@ -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 @@ -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 @@ -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`() { @@ -286,7 +288,7 @@ internal class ApiExtensionsKtTest { } } -class ApiExtensionsKtDetermineTypeTest { +private class ApiExtensionsKtDetermineTypeTest : AbstractApiExtensionsKtTest() { private val bindingContext: BindingContext private val ktFile: KtFile @@ -512,7 +514,7 @@ class ApiExtensionsKtDetermineTypeTest { } } -class ApiExtensionsKtDetermineSignatureTest { +private class ApiExtensionsKtDetermineSignatureTest : AbstractApiExtensionsKtTest() { private val bindingContext: BindingContext private val ktFile: KtFile @@ -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 { @@ -560,7 +561,6 @@ class ApiExtensionsScopeFunctionResolutionTest { } } """.let { parse(it) }.let { it.psiFile to it.bindingContext } - } @Test fun `resolve this as arg in with`() { @@ -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(expression) { diff --git a/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/checks/FieldMatcherTest.kt b/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/checks/FieldMatcherTest.kt index 3d5e1a2b6..0e42ee67e 100644 --- a/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/checks/FieldMatcherTest.kt +++ b/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/checks/FieldMatcherTest.kt @@ -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 @@ -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") @@ -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 List.rearrange(vararg indices: Int) = List(indices.size) { this[indices[it]] } diff --git a/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/checks/FunMatcherTest.kt b/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/checks/FunMatcherTest.kt index dbd5d5044..c120c84a7 100644 --- a/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/checks/FunMatcherTest.kt +++ b/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/checks/FunMatcherTest.kt @@ -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 @@ -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 @@ -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") @@ -612,4 +618,10 @@ class FunMatcherTest { } } } + + @AfterEach + fun afterEach() { + Disposer.dispose(environment.disposable) + } + } diff --git a/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/frontend/KotlinSyntaxStructureTest.kt b/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/frontend/KotlinSyntaxStructureTest.kt index 009872a85..0244e70d4 100644 --- a/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/frontend/KotlinSyntaxStructureTest.kt +++ b/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/frontend/KotlinSyntaxStructureTest.kt @@ -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 @@ -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() @@ -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) @@ -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) } } diff --git a/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/frontend/KotlinTreeTest.kt b/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/frontend/KotlinTreeTest.kt index fb40b3d76..c56b0afe8 100644 --- a/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/frontend/KotlinTreeTest.kt +++ b/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/frontend/KotlinTreeTest.kt @@ -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 @@ -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") @@ -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) } } diff --git a/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/frontend/TextRangeTrackerTest.kt b/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/frontend/TextRangeTrackerTest.kt index d8cc23152..a06f10667 100644 --- a/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/frontend/TextRangeTrackerTest.kt +++ b/sonar-kotlin-api/src/test/java/org/sonarsource/kotlin/api/frontend/TextRangeTrackerTest.kt @@ -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 @@ -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 = "\"\"\"" @@ -133,7 +135,7 @@ internal class TextRangeTrackerTest { val x = $regex """.trimIndent(), - environment = Environment(emptyList(), LanguageVersion.LATEST_STABLE), + environment, inputFile = DummyInputFile() ) @@ -142,4 +144,15 @@ internal class TextRangeTrackerTest { return TextRangeTracker.of(entries, DummyInputFile(), tree.document) } -} \ No newline at end of file + + /** + * Disposed in [afterEach] + */ + private val environment = Environment(emptyList(), LanguageVersion.LATEST_STABLE) + + @AfterEach + fun afterEach() { + Disposer.dispose(environment.disposable) + } + +} diff --git a/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/CyclomaticComplexityVisitorTest.kt b/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/CyclomaticComplexityVisitorTest.kt index a8f170a14..461956410 100644 --- a/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/CyclomaticComplexityVisitorTest.kt +++ b/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/CyclomaticComplexityVisitorTest.kt @@ -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 @@ -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 @@ -142,10 +144,20 @@ internal class CyclomaticComplexityVisitorTest { } private fun getComplexityTrees(content: String): List { - 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) + } + } diff --git a/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/IssueSuppressionVisitorTest.kt b/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/IssueSuppressionVisitorTest.kt index 898053276..39d17816f 100644 --- a/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/IssueSuppressionVisitorTest.kt +++ b/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/IssueSuppressionVisitorTest.kt @@ -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) diff --git a/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/MetricVisitorTest.kt b/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/MetricVisitorTest.kt index 663595e6b..3cc542616 100644 --- a/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/MetricVisitorTest.kt +++ b/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/MetricVisitorTest.kt @@ -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 @@ -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 @@ -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) + } + } diff --git a/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/StatementsVisitorTest.kt b/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/StatementsVisitorTest.kt index 4ba5680d2..14fee0288 100644 --- a/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/StatementsVisitorTest.kt +++ b/sonar-kotlin-metrics/src/test/java/org/sonarsource/kotlin/metrics/StatementsVisitorTest.kt @@ -19,6 +19,7 @@ */ package org.sonarsource.kotlin.metrics +import com.intellij.openapi.util.Disposer import org.assertj.core.api.Assertions import org.jetbrains.kotlin.config.LanguageVersion import org.junit.jupiter.api.Test @@ -60,8 +61,11 @@ internal class StatementsVisitorTest { } private fun statements(content: String): Int { - val ktFile = Environment(emptyList(), LanguageVersion.LATEST_STABLE).ktPsiFactory.createFile(content) + val environment = /* Disposed below */ Environment(emptyList(), LanguageVersion.LATEST_STABLE) + val ktFile = environment.ktPsiFactory.createFile(content) val statementsVisitor = StatementsVisitor() ktFile.accept(statementsVisitor) - return statementsVisitor.statements + val result = statementsVisitor.statements + Disposer.dispose(environment.disposable) + return result } diff --git a/sonar-kotlin-plugin/src/test/java/org/sonarsource/kotlin/plugin/KotlinSensorTest.kt b/sonar-kotlin-plugin/src/test/java/org/sonarsource/kotlin/plugin/KotlinSensorTest.kt index 44c4b0dfd..f3488c64a 100644 --- a/sonar-kotlin-plugin/src/test/java/org/sonarsource/kotlin/plugin/KotlinSensorTest.kt +++ b/sonar-kotlin-plugin/src/test/java/org/sonarsource/kotlin/plugin/KotlinSensorTest.kt @@ -247,6 +247,7 @@ internal class KotlinSensorTest : AbstractSensorTest() { context.fileSystem().add(inputFile) populateCacheWithExpectedEntries(listOf(inputFile), context) mockkStatic("org.sonarsource.kotlin.api.sensors.AbstractKotlinSensorExecuteContextKt") + // TODO dispose every { environment(any(), any()) } returns Environment(listOf("file1.kt"), LanguageVersion.LATEST_STABLE) mockkStatic("org.sonarsource.kotlin.api.frontend.KotlinCoreEnvironmentToolsKt") every { analyzeAndGetBindingContext(any(), any()) } throws IOException("Boom!") diff --git a/sonar-kotlin-plugin/src/test/java/org/sonarsource/kotlin/plugin/cpd/CopyPasteDetectorTest.kt b/sonar-kotlin-plugin/src/test/java/org/sonarsource/kotlin/plugin/cpd/CopyPasteDetectorTest.kt index 9d32b1371..fd10ac5cf 100644 --- a/sonar-kotlin-plugin/src/test/java/org/sonarsource/kotlin/plugin/cpd/CopyPasteDetectorTest.kt +++ b/sonar-kotlin-plugin/src/test/java/org/sonarsource/kotlin/plugin/cpd/CopyPasteDetectorTest.kt @@ -19,9 +19,11 @@ */ package org.sonarsource.kotlin.plugin.cpd +import com.intellij.openapi.util.Disposer import org.assertj.core.api.Assertions import org.assertj.core.api.ObjectAssert import org.jetbrains.kotlin.config.LanguageVersion +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.RegisterExtension import org.junit.jupiter.api.io.TempDir @@ -56,6 +58,11 @@ private val content = """ class CopyPasteDetectorTest { + /** + * Disposed in [afterEach] + */ + private val environment = Environment(emptyList(), LanguageVersion.LATEST_STABLE) + @JvmField @TempDir var tmpFolder: Path? = null @@ -73,7 +80,7 @@ class CopyPasteDetectorTest { .setContents(content) .build() - val root = kotlinTreeOf(content, Environment(emptyList(), LanguageVersion.LATEST_STABLE), inputFile) + val root = kotlinTreeOf(content, environment, inputFile) val ctx = InputFileContextImpl(sensorContext, inputFile, false) CopyPasteDetector().scan(ctx, root) @@ -124,7 +131,7 @@ class CopyPasteDetectorTest { .setContents(content) .build() - val root = kotlinTreeOf(content, Environment(emptyList(), LanguageVersion.LATEST_STABLE), inputFile) + val root = kotlinTreeOf(content, environment, inputFile) val ctx = InputFileContextImpl(sensorContext, inputFile, false) val readCache = DummyReadCache(emptyMap()) @@ -156,7 +163,7 @@ class CopyPasteDetectorTest { .setContents(content) .build() - val root = kotlinTreeOf(content, Environment(emptyList(), LanguageVersion.LATEST_STABLE), inputFile) + val root = kotlinTreeOf(content, environment, inputFile) val ctx = InputFileContextImpl(sensorContext, inputFile, false) val readCache = DummyReadCache(emptyMap()) @@ -175,6 +182,11 @@ class CopyPasteDetectorTest { .hasSize(1) .containsExactly("No CPD tokens cached for next analysis of input file moduleKey:dummy.kt.") } + + @AfterEach + fun afterEach() { + Disposer.dispose(environment.disposable) + } }