From 88b769476219fed81f8a2920d6203ae4736bff0a Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Fri, 2 Jul 2021 14:53:56 +0300 Subject: [PATCH] [Test] Remove useless dependencies on IDEA from :compiler:tests-common --- compiler/tests-common/build.gradle.kts | 15 +- .../test/testFramework/KtUsefulTestCase.java | 232 +++--------------- .../kotlin/test/testFramework/RunAll.java | 66 +++++ .../kotlin/test/util/jetTestUtils.kt | 5 - 4 files changed, 97 insertions(+), 221 deletions(-) create mode 100644 compiler/tests-common/tests/org/jetbrains/kotlin/test/testFramework/RunAll.java diff --git a/compiler/tests-common/build.gradle.kts b/compiler/tests-common/build.gradle.kts index ded3512b1df..fbf59b1ba42 100644 --- a/compiler/tests-common/build.gradle.kts +++ b/compiler/tests-common/build.gradle.kts @@ -59,8 +59,8 @@ dependencies { testCompileOnly(intellijCoreDep()) { includeJars("intellij-core") } testCompile(intellijDep()) { includeJars( - "testFramework", - "testFramework.core", + "testFramework", // needed for parsing tests + "platform-ide-util-io", // needed for CLI process utils in KotlinIntegrationTestBase rootProject = rootProject ) } @@ -72,27 +72,16 @@ dependencies { } testCompile(intellijDep()) { includeJars( - "jps-model", - "extensions", - "util", - "platform-api", - "platform-impl", - "idea", - "idea_rt", "guava", "trove4j", "asm-all", "log4j", "jdom", - "streamex", - "bootstrap", rootProject = rootProject ) isTransitive = false } - testCompile(intellijDep()) { includeJars("platform-util-ui", "platform-concurrency", "platform-objectSerializer") } - testCompile(intellijDep()) { includeJars("platform-ide-util-io") } testApiJUnit5() } diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/testFramework/KtUsefulTestCase.java b/compiler/tests-common/tests/org/jetbrains/kotlin/test/testFramework/KtUsefulTestCase.java index 197fa6919fb..3362f53e07e 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/test/testFramework/KtUsefulTestCase.java +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/test/testFramework/KtUsefulTestCase.java @@ -28,11 +28,9 @@ import com.intellij.openapi.application.impl.ApplicationInfoImpl; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Disposer; -import com.intellij.openapi.util.IconLoader; import com.intellij.openapi.util.JDOMUtil; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileVisitor; @@ -43,8 +41,6 @@ import com.intellij.psi.impl.DocumentCommitThread; import com.intellij.psi.impl.source.PostprocessReformattingAspect; import com.intellij.rt.execution.junit.FileComparisonFailure; import com.intellij.testFramework.*; -import com.intellij.testFramework.exceptionCases.AbstractExceptionCase; -import com.intellij.testFramework.fixtures.IdeaTestExecutionPolicy; import com.intellij.util.*; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.PeekableIterator; @@ -52,7 +48,6 @@ import com.intellij.util.containers.PeekableIteratorWrapper; import com.intellij.util.indexing.FileBasedIndex; import com.intellij.util.indexing.FileBasedIndexImpl; import com.intellij.util.lang.CompoundRuntimeException; -import com.intellij.util.ui.UIUtil; import gnu.trove.Equality; import gnu.trove.THashSet; import junit.framework.AssertionFailedError; @@ -96,7 +91,6 @@ public abstract class KtUsefulTestCase extends TestCase { static { IdeaForkJoinWorkerThreadFactory.setupPoisonFactory(); - Logger.setFactory(TestLoggerFactory.class); } protected static final Logger LOG = Logger.getInstance(KtUsefulTestCase.class); @@ -184,14 +178,7 @@ public abstract class KtUsefulTestCase extends TestCase { super.setUp(); if (shouldContainTempFiles()) { - IdeaTestExecutionPolicy policy = IdeaTestExecutionPolicy.current(); - String testName = null; - if (policy != null) { - testName = policy.getPerTestTempDirName(); - } - if (testName == null) { - testName = FileUtil.sanitizeFileName(getTestName(true)); - } + String testName = FileUtil.sanitizeFileName(getTestName(true)); testName = new File(testName).getName(); // in case the test name contains file separators myTempDir = FileUtil.createTempDirectory(TEMP_DIR_MARKER + testName, "", false).getPath(); FileUtil.resetCanonicalTempPathCache(myTempDir); @@ -199,18 +186,9 @@ public abstract class KtUsefulTestCase extends TestCase { boolean isStressTest = isStressTest(); ApplicationInfoImpl.setInStressTest(isStressTest); - if (isPerformanceTest()) { - Timings.getStatistics(); - } // turn off Disposer debugging for performance tests Disposer.setDebugMode(!isStressTest); - - if (isIconRequired()) { - // ensure that IconLoader will use dummy empty icon - IconLoader.deactivate(); - //IconManager.activate(); - } } protected boolean isIconRequired() { @@ -223,11 +201,6 @@ public abstract class KtUsefulTestCase extends TestCase { // don't use method references here to make stack trace reading easier //noinspection Convert2MethodRef new RunAll( - () -> { - if (isIconRequired()) { - //IconManager.deactivate(); - } - }, () -> disposeRootDisposable(), () -> cleanupSwingDataStructures(), () -> cleanupDeleteOnExitHookList(), @@ -249,8 +222,7 @@ public abstract class KtUsefulTestCase extends TestCase { FileUtil.delete(new File(myTempDir)); } } - }, - () -> waitForAppLeakingThreads(10, TimeUnit.SECONDS) + } ).run(ObjectUtils.notNull(mySuppressedExceptions, Collections.emptyList())); } finally { @@ -369,22 +341,17 @@ public abstract class KtUsefulTestCase extends TestCase { Runnable runnable = () -> { try { - TestLoggerFactory.onTestStarted(); super.runTest(); - TestLoggerFactory.onTestFinished(true); } catch (InvocationTargetException e) { - TestLoggerFactory.onTestFinished(false); e.fillInStackTrace(); throwables[0] = e.getTargetException(); } catch (IllegalAccessException e) { - TestLoggerFactory.onTestFinished(false); e.fillInStackTrace(); throwables[0] = e; } catch (Throwable e) { - TestLoggerFactory.onTestFinished(false); throwables[0] = e; } }; @@ -397,19 +364,11 @@ public abstract class KtUsefulTestCase extends TestCase { } protected boolean shouldRunTest() { - return TestFrameworkUtil.canRunTest(getClass()); + return true; } protected void invokeTestRunnable(@NotNull Runnable runnable) throws Exception { - if (runInDispatchThread()) { - EdtTestUtilKt.runInEdtAndWait(() -> { - runnable.run(); - return null; - }); - } - else { - runnable.run(); - } + runnable.run(); } protected void defaultRunBare() throws Throwable { @@ -481,30 +440,13 @@ public abstract class KtUsefulTestCase extends TestCase { public void runBare() throws Throwable { if (!shouldRunTest()) return; - if (runInDispatchThread()) { - TestRunnerUtil.replaceIdeEventQueueSafely(); - EdtTestUtil.runInEdtAndWait(this::defaultRunBare); - } - else { - defaultRunBare(); - } + defaultRunBare(); } protected boolean runInDispatchThread() { - IdeaTestExecutionPolicy policy = IdeaTestExecutionPolicy.current(); - if (policy != null) { - return policy.runInDispatchThread(); - } return true; } - /** - * If you want a more shorter name than runInEdtAndWait. - */ - protected void edt(@NotNull ThrowableRunnable runnable) { - EdtTestUtil.runInEdtAndWait(runnable); - } - @NotNull public static String toString(@NotNull Iterable collection) { if (!collection.iterator().hasNext()) { @@ -866,7 +808,29 @@ public abstract class KtUsefulTestCase extends TestCase { @NotNull public static String getTestName(@Nullable String name, boolean lowercaseFirstLetter) { - return name == null ? "" : PlatformTestUtil.getTestName(name, lowercaseFirstLetter); + if (name == null) return ""; + name = StringUtil.trimStart(name, "test"); + return StringUtil.isEmpty(name) ? "" : lowercaseFirstLetter(name, lowercaseFirstLetter); + } + + public static @NotNull String lowercaseFirstLetter(@NotNull String name, boolean lowercaseFirstLetter) { + if (lowercaseFirstLetter && !isAllUppercaseName(name)) { + name = Character.toLowerCase(name.charAt(0)) + name.substring(1); + } + return name; + } + + public static boolean isAllUppercaseName(@NotNull String name) { + int uppercaseChars = 0; + for (int i = 0; i < name.length(); i++) { + if (Character.isLowerCase(name.charAt(i))) { + return false; + } + if (Character.isUpperCase(name.charAt(i))) { + uppercaseChars++; + } + } + return uppercaseChars >= 3; } @NotNull @@ -895,15 +859,9 @@ public abstract class KtUsefulTestCase extends TestCase { @Nullable Supplier messageProducer) { String fileText; try { - if (OVERWRITE_TESTDATA) { - VfsTestUtil.overwriteTestData(filePath, actualText); - //noinspection UseOfSystemOutOrSystemErr - System.out.println("File " + filePath + " created."); - } fileText = FileUtil.loadFile(new File(filePath), StandardCharsets.UTF_8); } catch (FileNotFoundException e) { - VfsTestUtil.overwriteTestData(filePath, actualText); throw new AssertionFailedError("No output text found. File " + filePath + " created."); } catch (IOException e) { @@ -958,12 +916,6 @@ public abstract class KtUsefulTestCase extends TestCase { } } - public boolean isPerformanceTest() { - String testName = getName(); - String className = getClass().getSimpleName(); - return TestFrameworkUtil.isPerformanceTest(testName, className); - } - /** * @return true for a test which performs A LOT of computations. * Such test should typically avoid performing expensive checks, e.g. data structure consistency complex validations. @@ -975,8 +927,7 @@ public abstract class KtUsefulTestCase extends TestCase { } private static boolean isStressTest(String testName, String className) { - return TestFrameworkUtil.isPerformanceTest(testName, className) || - containsStressWords(testName) || + return containsStressWords(testName) || containsStressWords(className); } @@ -991,72 +942,6 @@ public abstract class KtUsefulTestCase extends TestCase { }); } - /** - * Checks that code block throw corresponding exception. - * - * @param exceptionCase Block annotated with some exception type - */ - protected void assertException(@NotNull AbstractExceptionCase exceptionCase) { - assertException(exceptionCase, null); - } - - /** - * Checks that code block throw corresponding exception with expected error msg. - * If expected error message is null it will not be checked. - * - * @param exceptionCase Block annotated with some exception type - * @param expectedErrorMsg expected error message - */ - @SuppressWarnings("unchecked") - protected void assertException(@NotNull AbstractExceptionCase exceptionCase, @Nullable String expectedErrorMsg) { - assertExceptionOccurred(true, exceptionCase, expectedErrorMsg); - } - - /** - * Checks that the code block throws an exception of the specified class. - * - * @param exceptionClass Expected exception type - * @param runnable Block annotated with some exception type - */ - public static void assertThrows(@NotNull Class exceptionClass, - @NotNull ThrowableRunnable runnable) { - assertThrows(exceptionClass, null, runnable); - } - - /** - * Checks that the code block throws an exception of the specified class with expected error msg. - * If expected error message is null it will not be checked. - * - * @param exceptionClass Expected exception type - * @param expectedErrorMsgPart expected error message, of any - * @param runnable Block annotated with some exception type - */ - @SuppressWarnings({"unchecked", "SameParameterValue"}) - public static void assertThrows(@NotNull Class exceptionClass, - @Nullable String expectedErrorMsgPart, - @NotNull ThrowableRunnable runnable) { - assertExceptionOccurred(true, new AbstractExceptionCase() { - @Override - public Class getExpectedExceptionClass() { - return (Class)exceptionClass; - } - - @Override - public void tryClosure() throws Throwable { - runnable.run(); - } - }, expectedErrorMsgPart); - } - - /** - * Checks that code block doesn't throw corresponding exception. - * - * @param exceptionCase Block annotated with some exception type - */ - protected void assertNoException(@NotNull AbstractExceptionCase exceptionCase) throws T { - assertExceptionOccurred(false, exceptionCase, null); - } - protected void assertNoThrowable(@NotNull Runnable closure) { String throwableName = null; try { @@ -1068,44 +953,6 @@ public abstract class KtUsefulTestCase extends TestCase { assertNull(throwableName); } - private static void assertExceptionOccurred(boolean shouldOccur, - @NotNull AbstractExceptionCase exceptionCase, - String expectedErrorMsgPart) throws T { - boolean wasThrown = false; - try { - exceptionCase.tryClosure(); - } - catch (Throwable e) { - Throwable cause = e; - - if (shouldOccur) { - wasThrown = true; - assertInstanceOf(cause, exceptionCase.getExpectedExceptionClass()); - if (expectedErrorMsgPart != null) { - assertTrue(cause.getMessage(), cause.getMessage().contains(expectedErrorMsgPart)); - } - } - else if (exceptionCase.getExpectedExceptionClass().equals(cause.getClass())) { - wasThrown = true; - - //noinspection UseOfSystemOutOrSystemErr - System.out.println(); - //noinspection UseOfSystemOutOrSystemErr - e.printStackTrace(System.out); - - fail("Exception isn't expected here. Exception message: " + cause.getMessage()); - } - else { - throw e; - } - } - finally { - if (shouldOccur && !wasThrown) { - fail(exceptionCase.getExpectedExceptionClass().getName() + " must be thrown."); - } - } - } - protected boolean annotatedWith(@NotNull Class annotationClass) { Class aClass = getClass(); String methodName = "test" + getTestName(false); @@ -1140,27 +987,6 @@ public abstract class KtUsefulTestCase extends TestCase { file.refresh(false, true); } - public static VirtualFile refreshAndFindFile(@NotNull final File file) { - return UIUtil.invokeAndWaitIfNeeded(() -> LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file)); - } - - public static void waitForAppLeakingThreads(long timeout, @NotNull TimeUnit timeUnit) { - EdtTestUtil.runInEdtAndWait(() -> { - Application app = ApplicationManager.getApplication(); - if (app != null && !app.isDisposed()) { - FileBasedIndexImpl index = (FileBasedIndexImpl)app.getServiceIfCreated(FileBasedIndex.class); - if (index != null) { - index.getChangedFilesCollector().waitForVfsEventsExecuted(timeout, timeUnit); - } - - DocumentCommitThread commitThread = (DocumentCommitThread)app.getServiceIfCreated(DocumentCommitProcessor.class); - if (commitThread != null) { - commitThread.waitForAllCommits(timeout, timeUnit); - } - } - }); - } - protected class TestDisposable implements Disposable { private volatile boolean myDisposed; diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/testFramework/RunAll.java b/compiler/tests-common/tests/org/jetbrains/kotlin/test/testFramework/RunAll.java new file mode 100644 index 00000000000..6fbafe28382 --- /dev/null +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/test/testFramework/RunAll.java @@ -0,0 +1,66 @@ +/* + * Copyright 2010-2021 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.test.testFramework; + +import com.intellij.util.SmartList; +import com.intellij.util.ThrowableRunnable; +import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.lang.CompoundRuntimeException; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class RunAll { + private final List> myActions; + + public RunAll(ThrowableRunnable ... actions) { + this(Arrays.asList(actions)); + } + + public RunAll(@NotNull List> actions) { + myActions = actions; + } + + @SafeVarargs + @Contract(pure=true) + public final RunAll append(ThrowableRunnable ... actions) { + return new RunAll(ContainerUtil.concat(myActions, actions.length == 1 ? Collections.singletonList(actions[0]) : Arrays.asList(actions))); + } + + public void run() { + run(Collections.emptyList()); + } + + public void run(@NotNull List suppressedExceptions) { + CompoundRuntimeException.throwIfNotEmpty(ContainerUtil.concat(suppressedExceptions, collectExceptions(myActions))); + } + + private static @NotNull List collectExceptions(@NotNull List> actions) { + List result = null; + for (ThrowableRunnable action : actions) { + try { + action.run(); + } + catch (CompoundRuntimeException e) { + if (result == null) { + result = new ArrayList<>(); + } + result.addAll(e.getExceptions()); + } + catch (Throwable e) { + if (result == null) { + result = new SmartList<>(); + } + result.add(e); + } + } + return ContainerUtil.notNullize(result); + } +} diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/util/jetTestUtils.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/test/util/jetTestUtils.kt index c58e214df4e..d55ff693812 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/test/util/jetTestUtils.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/test/util/jetTestUtils.kt @@ -18,7 +18,6 @@ package org.jetbrains.kotlin.test.util import com.intellij.psi.* import com.intellij.psi.util.PsiTreeUtil -import com.intellij.testFramework.fixtures.CodeInsightTestFixture import com.intellij.util.SmartFMap import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.psi.KtPackageDirective @@ -52,7 +51,3 @@ fun PsiFile.findElementsByCommentPrefix(prefix: String): Map return result } -val CodeInsightTestFixture.elementByOffset: PsiElement - get() { - return file.findElementAt(editor.caretModel.offset) ?: error("Can't find element at offset. Probably is missing.") - }