diff --git a/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt b/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt index 9fc3a223ffa..4ab429b27c7 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt @@ -42,8 +42,6 @@ import org.junit.Assert import java.io.File abstract class AbstractScratchRunActionTest : FileEditorManagerTestCase() { - val module: Module get() = myModule - fun doReplTest(fileName: String) { doTest(fileName, true) } @@ -75,7 +73,7 @@ abstract class AbstractScratchRunActionTest : FileEditorManagerTestCase() { MockLibraryUtil.compileKotlin(baseDir.path, outputDir) - PsiTestUtil.setCompilerOutputPath(module, outputDir.path, false) + PsiTestUtil.setCompilerOutputPath(myFixture.module, outputDir.path, false) val mainFileName = "$dirName/${getTestName(true)}.kts" doCompilingTest(mainFileName) @@ -84,7 +82,7 @@ abstract class AbstractScratchRunActionTest : FileEditorManagerTestCase() { doReplTest(mainFileName) - ModuleRootModificationUtil.updateModel(module) { model -> + ModuleRootModificationUtil.updateModel(myFixture.module) { model -> model.getModuleExtension(CompilerModuleExtension::class.java).inheritCompilerOutputPath(true) } } diff --git a/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt.192 b/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt.192 deleted file mode 100644 index e6230125f00..00000000000 --- a/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt.192 +++ /dev/null @@ -1,221 +0,0 @@ -/* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.idea.scratch - -import com.intellij.ide.scratch.ScratchFileService -import com.intellij.ide.scratch.ScratchRootType -import com.intellij.openapi.actionSystem.AnAction -import com.intellij.openapi.actionSystem.CommonDataKeys -import com.intellij.openapi.roots.CompilerModuleExtension -import com.intellij.openapi.roots.ModuleRootModificationUtil -import com.intellij.openapi.util.io.FileUtil -import com.intellij.openapi.vfs.VirtualFile -import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess -import com.intellij.psi.PsiDocumentManager -import com.intellij.psi.PsiManager -import com.intellij.testFramework.FileEditorManagerTestCase -import com.intellij.testFramework.MapDataContext -import com.intellij.testFramework.PsiTestUtil -import com.intellij.testFramework.TestActionEvent -import com.intellij.util.ui.UIUtil -import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime -import org.jetbrains.kotlin.idea.KotlinLanguage -import org.jetbrains.kotlin.idea.core.script.ScriptDependenciesManager -import org.jetbrains.kotlin.idea.highlighter.KotlinHighlightingUtil -import org.jetbrains.kotlin.idea.scratch.actions.RunScratchAction -import org.jetbrains.kotlin.idea.scratch.actions.ScratchCompilationSupport -import org.jetbrains.kotlin.idea.scratch.output.InlayScratchFileRenderer -import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor -import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor -import org.jetbrains.kotlin.idea.test.PluginTestCaseBase -import org.jetbrains.kotlin.idea.util.application.runWriteAction -import org.jetbrains.kotlin.test.InTextDirectivesUtils -import org.jetbrains.kotlin.test.KotlinTestUtils -import org.jetbrains.kotlin.test.MockLibraryUtil -import org.jetbrains.kotlin.utils.PathUtil -import org.junit.Assert -import java.io.File - -abstract class AbstractScratchRunActionTest : FileEditorManagerTestCase() { - - fun doReplTest(fileName: String) { - doTest(fileName, true) - } - - fun doCompilingTest(fileName: String) { - doTest(fileName, false) - } - - fun doMultiFileTest(dirName: String) { - val javaFiles = arrayListOf() - val kotlinFiles = arrayListOf() - val baseDir = File(testDataPath, dirName) - baseDir.walk().forEach { - if (it.isFile) { - if (it.extension == "java") javaFiles.add(it) - if (it.extension == "kt") kotlinFiles.add(it) - } - } - - javaFiles.forEach { myFixture.copyFileToProject(it.path, FileUtil.getRelativePath(baseDir, it)!!) } - kotlinFiles.forEach { myFixture.copyFileToProject(it.path, FileUtil.getRelativePath(baseDir, it)!!) } - - val outputDir = createTempDir(dirName) - - if (javaFiles.isNotEmpty()) { - val options = listOf("-d", outputDir.path) - KotlinTestUtils.compileJavaFiles(javaFiles, options) - } - - MockLibraryUtil.compileKotlin(baseDir.path, outputDir) - - PsiTestUtil.setCompilerOutputPath(module, outputDir.path, false) - - val mainFileName = "$dirName/${getTestName(true)}.kts" - doCompilingTest(mainFileName) - doReplTest(mainFileName) - - ModuleRootModificationUtil.updateModel(module) { model -> - model.getModuleExtension(CompilerModuleExtension::class.java).inheritCompilerOutputPath(true) - } - } - - fun doTest(fileName: String, isRepl: Boolean) { - val sourceFile = File(testDataPath, fileName) - val fileText = sourceFile.readText() - - val scratchFile = createScratchFile(sourceFile.name, fileText) - - ScriptDependenciesManager.updateScriptDependenciesSynchronously(scratchFile, project) - - val psiFile = PsiManager.getInstance(project).findFile(scratchFile) ?: error("Couldn't find psi file ${sourceFile.path}") - - if (!KotlinHighlightingUtil.shouldHighlight(psiFile)) error("Highlighting for scratch file is switched off") - - val (editor, scratchPanel) = getEditorWithScratchPanel(myManager, scratchFile) ?: error("Couldn't find scratch panel") - scratchPanel.scratchFile.saveOptions { - copy(isRepl = isRepl, isInteractiveMode = false) - } - - if (!InTextDirectivesUtils.isDirectiveDefined(fileText, "// NO_MODULE")) { - scratchPanel.setModule(myFixture.module) - } - - launchScratch(scratchFile) - - val doc = PsiDocumentManager.getInstance(project).getDocument(psiFile) ?: error("Document for ${psiFile.name} is null") - - val actualOutput = StringBuilder(psiFile.text) - for (line in doc.lineCount - 1 downTo 0) { - editor.editor.inlayModel.getInlineElementsInRange( - doc.getLineStartOffset(line), - doc.getLineEndOffset(line) - ).map { it.renderer } - .filterIsInstance() - .forEach { - val str = it.toString() - val offset = doc.getLineEndOffset(line); actualOutput.insert( - offset, - "${str.takeWhile { it.isWhitespace() }}// ${str.trim()}" - ) - } - } - - val expectedFileName = if (isRepl) { - fileName.replace(".kts", ".repl.after") - } else { - fileName.replace(".kts", ".comp.after") - } - val expectedFile = File(testDataPath, expectedFileName) - KotlinTestUtils.assertEqualsToFile(expectedFile, actualOutput.toString()) - } - - protected fun createScratchFile(name: String, text: String): VirtualFile { - val scratchFile = ScratchRootType.getInstance().createScratchFile( - project, - name, - KotlinLanguage.INSTANCE, - text, - ScratchFileService.Option.create_if_missing - ) ?: error("Couldn't create scratch file") - - myFixture.openFileInEditor(scratchFile) - return scratchFile - } - - protected fun launchScratch(scratchFile: VirtualFile) { - val action = RunScratchAction() - val e = getActionEvent(scratchFile, action) - - action.beforeActionPerformedUpdate(e) - Assert.assertTrue(e.presentation.isEnabled && e.presentation.isVisible) - action.actionPerformed(e) - - UIUtil.dispatchAllInvocationEvents() - - val start = System.currentTimeMillis() - // wait until output is displayed in editor or for 1 minute - while (ScratchCompilationSupport.isAnyInProgress() && (System.currentTimeMillis() - start) < 60000) { - Thread.sleep(100) - } - - UIUtil.dispatchAllInvocationEvents() - } - - private fun getActionEvent(virtualFile: VirtualFile, action: AnAction): TestActionEvent { - val context = MapDataContext() - context.put(CommonDataKeys.VIRTUAL_FILE_ARRAY, arrayOf(virtualFile)) - context.put(CommonDataKeys.PROJECT, project) - context.put(CommonDataKeys.EDITOR, myFixture.editor) - return TestActionEvent(context, action) - } - - override fun getTestDataPath() = KotlinTestUtils.getHomeDirectory() - - - override fun getProjectDescriptor(): com.intellij.testFramework.LightProjectDescriptor { - val testName = getTestName(false) - - return when { - testName.endsWith("WithKotlinTest") -> INSTANCE_WITH_KOTLIN_TEST - testName.endsWith("NoRuntime") -> INSTANCE_WITHOUT_RUNTIME - else -> KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE_FULL_JDK - } - } - - override fun setUp() { - super.setUp() - - VfsRootAccess.allowRootAccess(KotlinTestUtils.getHomeDirectory()) - - PluginTestCaseBase.addJdk(myFixture.projectDisposable) { PluginTestCaseBase.fullJdk() } - } - - override fun tearDown() { - super.tearDown() - - VfsRootAccess.disallowRootAccess(KotlinTestUtils.getHomeDirectory()) - - ScratchFileService.getInstance().scratchesMapping.mappings.forEach { file, _ -> - runWriteAction { file.delete(this) } - } - } - - companion object { - private val INSTANCE_WITH_KOTLIN_TEST = object : KotlinWithJdkAndRuntimeLightProjectDescriptor( - arrayListOf( - ForTestCompileRuntime.runtimeJarForTests(), - PathUtil.kotlinPathsForDistDirectory.kotlinTestPath - ) - ) { - override fun getSdk() = PluginTestCaseBase.fullJdk() - } - - private val INSTANCE_WITHOUT_RUNTIME = object : KotlinLightProjectDescriptor() { - override fun getSdk() = PluginTestCaseBase.fullJdk() - } - } -} \ No newline at end of file