From fe467bfba2b97f6b52035c9b4fd4b3585476e565 Mon Sep 17 00:00:00 2001 From: Johnathan Gilday Date: Tue, 25 Jun 2024 12:38:57 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Run=20Unit=20Tests=20Concu?= =?UTF-8?q?rrently?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By default, all unit tests run concurrently. We opt-out of this behavior in cases where this is an issue. --- .../testutils/CodemodTestMixin.java | 32 ++++++++++--------- .../main/kotlin/io.codemodder.java.gradle.kts | 17 ++++++++-- .../plugins/maven/MavenProviderTest.java | 3 ++ .../RemoteRepositoriesIntegrationTest.java | 3 ++ 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/framework/codemodder-testutils/src/main/java/io/codemodder/testutils/CodemodTestMixin.java b/framework/codemodder-testutils/src/main/java/io/codemodder/testutils/CodemodTestMixin.java index 228bf86b0..279f00081 100644 --- a/framework/codemodder-testutils/src/main/java/io/codemodder/testutils/CodemodTestMixin.java +++ b/framework/codemodder-testutils/src/main/java/io/codemodder/testutils/CodemodTestMixin.java @@ -68,21 +68,23 @@ default Stream generateTestCases(@TempDir final Path tmpDir) throws .toList(); ThrowingConsumer testExecutor = - path -> - verifyCodemod( - metadata.codemodType(), - metadata.renameTestFile(), - tmpDir, - testResourceDir, - path, - dependencies, - projectProviders, - metadata.doRetransformTest(), - metadata.expectingFixesAtLines(), - metadata.expectingFailedFixesAtLines(), - metadata.sonarIssuesJsonFiles(), - metadata.sonarHotspotsJsonFiles()); - + path -> { + // create a new temporary directory for each test case + final var tmp = Files.createTempDirectory(tmpDir, "test-case-"); + verifyCodemod( + metadata.codemodType(), + metadata.renameTestFile(), + tmp, + testResourceDir, + path, + dependencies, + projectProviders, + metadata.doRetransformTest(), + metadata.expectingFixesAtLines(), + metadata.expectingFailedFixesAtLines(), + metadata.sonarIssuesJsonFiles(), + metadata.sonarHotspotsJsonFiles()); + }; return DynamicTest.stream(inputStream, displayNameGenerator, testExecutor); } diff --git a/gradle/build-plugins/src/main/kotlin/io.codemodder.java.gradle.kts b/gradle/build-plugins/src/main/kotlin/io.codemodder.java.gradle.kts index a1e2d2a28..287e1b381 100644 --- a/gradle/build-plugins/src/main/kotlin/io.codemodder.java.gradle.kts +++ b/gradle/build-plugins/src/main/kotlin/io.codemodder.java.gradle.kts @@ -15,6 +15,19 @@ spotless { } } -tasks.withType(Test::class) { - useJUnitPlatform() +testing { + @Suppress("UnstableApiUsage") + suites { + val test by getting(JvmTestSuite::class) { + useJUnitJupiter() + targets { + all { + testTask.configure { + systemProperty("junit.jupiter.execution.parallel.enabled", "true") + systemProperty("junit.jupiter.execution.parallel.mode.default", "concurrent") + } + } + } + } + } } diff --git a/plugins/codemodder-plugin-maven/src/test/java/io/codemodder/plugins/maven/MavenProviderTest.java b/plugins/codemodder-plugin-maven/src/test/java/io/codemodder/plugins/maven/MavenProviderTest.java index 3164dfb3e..1925c7636 100644 --- a/plugins/codemodder-plugin-maven/src/test/java/io/codemodder/plugins/maven/MavenProviderTest.java +++ b/plugins/codemodder-plugin-maven/src/test/java/io/codemodder/plugins/maven/MavenProviderTest.java @@ -1,6 +1,7 @@ package io.codemodder.plugins.maven; import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; @@ -20,6 +21,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -28,6 +30,7 @@ * Unit tests for {@link MavenProvider}. We bend over backwards here not to test the pom-operator * types. */ +@Execution(SAME_THREAD) final class MavenProviderTest { private static class TestPomModifier implements MavenProvider.PomModifier { diff --git a/plugins/codemodder-plugin-maven/src/test/java/io/codemodder/plugins/maven/operator/RemoteRepositoriesIntegrationTest.java b/plugins/codemodder-plugin-maven/src/test/java/io/codemodder/plugins/maven/operator/RemoteRepositoriesIntegrationTest.java index f5112594e..561c8f07f 100644 --- a/plugins/codemodder-plugin-maven/src/test/java/io/codemodder/plugins/maven/operator/RemoteRepositoriesIntegrationTest.java +++ b/plugins/codemodder-plugin-maven/src/test/java/io/codemodder/plugins/maven/operator/RemoteRepositoriesIntegrationTest.java @@ -1,14 +1,17 @@ package io.codemodder.plugins.maven.operator; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD; import java.io.*; import java.util.*; import org.apache.commons.lang3.SystemUtils; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Execution(SAME_THREAD) final class RemoteRepositoriesIntegrationTest { private static final Logger LOGGER = LoggerFactory.getLogger(RemoteRepositoriesIntegrationTest.class);