From f6900ff4bc6304911717367bf89967a617a197ec Mon Sep 17 00:00:00 2001 From: Nikita Skvortsov Date: Tue, 30 Jul 2019 13:52:49 +0300 Subject: [PATCH] [gradle] clear test filters when launching specific test/package IDEA-218613 Gradle build script may define own filters, `includeTestsMatching` invocation appends new pattern to list of filters, and Gradle will invoke all test matching _any_ filter. IDEA should reset list of filters before appending own ones. GitOrigin-RevId: cfa86464f7ab2c1ea33d9646994bae28c82d96f1 --- .../GradleJavaTestEventsIntegrationTest.kt | 65 +++++++++++++++---- .../internal/init/testFilterInit.gradle | 3 +- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/plugins/gradle/java/testSources/execution/test/GradleJavaTestEventsIntegrationTest.kt b/plugins/gradle/java/testSources/execution/test/GradleJavaTestEventsIntegrationTest.kt index e9a5024dfe6f..8f206298f3e2 100644 --- a/plugins/gradle/java/testSources/execution/test/GradleJavaTestEventsIntegrationTest.kt +++ b/plugins/gradle/java/testSources/execution/test/GradleJavaTestEventsIntegrationTest.kt @@ -1,4 +1,3 @@ -import com.intellij.openapi.externalSystem.model.ExternalSystemException import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType @@ -6,17 +5,14 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.util.Pair import com.intellij.testFramework.RunAll import com.intellij.util.ThrowableRunnable -import org.assertj.core.api.Assertions -import org.assertj.core.api.Assertions.* +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy import org.jetbrains.plugins.gradle.GradleManager -import org.jetbrains.plugins.gradle.execution.test.runner.GradleTestsExecutionConsoleManager import org.jetbrains.plugins.gradle.importing.GradleBuildScriptBuilderEx import org.jetbrains.plugins.gradle.importing.GradleImportingTestCase import org.jetbrains.plugins.gradle.importing.withMavenCentral -import org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper import org.jetbrains.plugins.gradle.service.task.GradleTaskManager import org.jetbrains.plugins.gradle.util.GradleConstants -import org.junit.Assert.* import org.junit.Test open class GradleJavaTestEventsIntegrationTest: GradleImportingTestCase() { @@ -31,8 +27,8 @@ open class GradleJavaTestEventsIntegrationTest: GradleImportingTestCase() { createProjectSubFile("src/test/java/my/pack/AClassTest.java", "package my.pack;\n" + - "import org.junit.Test;" + - "import static org.junit.Assert.*;" + + "import org.junit.Test;\n" + + "import static org.junit.Assert.*;\n" + "public class AClassTest {\n" + " @Test\n" + " public void testSuccess() {\n" + @@ -44,19 +40,33 @@ open class GradleJavaTestEventsIntegrationTest: GradleImportingTestCase() { " }\n" + "}"); + createProjectSubFile("src/test/java/my/otherpack/AClassTest.java", + "package my.otherpack;\n" + + "import my.pack.AClass;\n" + + "import org.junit.Test;\n" + + "import static org.junit.Assert.*;\n" + + "public class AClassTest {\n" + + " @Test\n" + + " public void testSuccess() {\n" + + " assertEquals(42, new AClass().method());\n" + + " }\n" + + "}"); + importProject( GradleBuildScriptBuilderEx() .withMavenCentral() .applyPlugin("'java'") .addPostfix("dependencies {", " testCompile 'junit:junit:4.12'", - "}") + "}", + "test { filter { includeTestsMatching 'my.pack.*' } }") .generate() ) RunAll().append( ThrowableRunnable { `call test task produces test events`() }, - ThrowableRunnable { `call build task does not produce test events`() } + ThrowableRunnable { `call build task does not produce test events`() }, + ThrowableRunnable { `call task for specific test overrides existing filters`() } ).run() } @@ -85,8 +95,9 @@ open class GradleJavaTestEventsIntegrationTest: GradleImportingTestCase() { assertThat(eventLog) .contains( "", - "" - ) + "") + .doesNotContain( + "") } private fun `call build task does not produce test events`() { @@ -112,4 +123,34 @@ open class GradleJavaTestEventsIntegrationTest: GradleImportingTestCase() { assertThat(eventLog).noneMatch { it.contains("") } } + private fun `call task for specific test overrides existing filters`() { + val taskId = ExternalSystemTaskId.create(GradleConstants.SYSTEM_ID, ExternalSystemTaskType.EXECUTE_TASK, myProject) + val eventLog = mutableListOf() + val testListener = object : ExternalSystemTaskNotificationListenerAdapter() { + override fun onTaskOutput(id: ExternalSystemTaskId, text: String, stdOut: Boolean) { + eventLog.add(text.trim('\r', '\n', ' ')) + } + }; + + val settings = GradleManager() + .executionSettingsProvider + .`fun`(Pair.create(myProject, projectPath)) + .apply { + putUserData(GradleConstants.RUN_TASK_AS_TEST, true); + withArguments("--tests","my.otherpack.*") + } + + GradleTaskManager().executeTasks(taskId, + listOf(":cleanTest", ":test"), + projectPath, + settings, + null, + testListener); + + assertThat(eventLog) + .contains("") + .doesNotContain("", + "") + } + } diff --git a/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/internal/init/testFilterInit.gradle b/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/internal/init/testFilterInit.gradle index 54a334fea7ba..aafb5a671d4f 100644 --- a/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/internal/init/testFilterInit.gradle +++ b/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/internal/init/testFilterInit.gradle @@ -1,4 +1,4 @@ -import org.gradle.util.* +import org.gradle.util.GradleVersion String[] ijTestIncludes = ${TEST_NAME_INCLUDES} boolean ijTestFilteringSupported = GradleVersion.current().compareTo(GradleVersion.version("1.10"))>=0 @@ -40,6 +40,7 @@ gradle.taskGraph.beforeTask { Task task -> try { if(ijTestIncludes != ['*'] && ijTestFilteringSupported) { task.filter { + setIncludePatterns(new String[0]) ijTestIncludes.each(){ includeTestsMatching "${it}" } } }