Skip to content

Commit

Permalink
Add func test for gradle / junit test dry run mode (#201)
Browse files Browse the repository at this point in the history
* Add func test for gradle / junit test dry run mode
* Update gradle distribution to gradle-8.3-20230715071551+0000 in gradle-wrapper.properties

---------

Signed-off-by: Jean Gauthier <[email protected]>
  • Loading branch information
jean-andre-gauthier authored Jul 18, 2023
1 parent 414d60a commit 108e6f0
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 12 deletions.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionSha256Sum=e111cb9948407e26351227dabce49822fb88c37ee72f1d1582a69c68af2e702f
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionSha256Sum=2d50b3ee9d6b59da48e7f0facda054325498b141b1e5b025a7dff2cd96080497
distributionUrl=https\://services.gradle.org/distributions-snapshots/gradle-8.3-20230715071551+0000-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public final class TestTaskConfigurer {

private final static GradleVersion GRADLE_5_1 = GradleVersion.version("5.1");
private final static GradleVersion GRADLE_6_1 = GradleVersion.version("6.1");
private final static GradleVersion GRADLE_8_3 = GradleVersion.version("8.3");
private final static String GRADLE_ENTERPRISE_BASE_PACKAGE = "com.gradle.enterprise";

private TestTaskConfigurer() {
Expand All @@ -54,14 +55,13 @@ public static void configureTestTask(Test test, ObjectFactory objectFactory, Pro

test.getInputs().property("retry.failOnPassedAfterRetry", adapter.getFailOnPassedAfterRetryInput());

Provider<Boolean> isDeactivatedByTestDistributionPlugin =
shouldTestRetryPluginBeDeactivated(test, objectFactory, providerFactory, gradleVersion);
test.getInputs().property("isDeactivatedByTestDistributionPlugin", isDeactivatedByTestDistributionPlugin);
Provider<Boolean> shouldReplaceTestExecutor = shouldReplaceTestExecutor(test, objectFactory, providerFactory, gradleVersion);
test.getInputs().property("isDeactivatedByTestDistributionPlugin", shouldReplaceTestExecutor);

test.getExtensions().add(TestRetryTaskExtension.class, TestRetryTaskExtension.NAME, extension);

test.doFirst(new ConditionalTaskAction(isDeactivatedByTestDistributionPlugin, new InitTaskAction(adapter, objectFactory)));
test.doLast(new ConditionalTaskAction(isDeactivatedByTestDistributionPlugin, new FinalizeTaskAction()));
test.doFirst(new ConditionalTaskAction(shouldReplaceTestExecutor, new InitTaskAction(adapter, objectFactory)));
test.doLast(new ConditionalTaskAction(shouldReplaceTestExecutor, new FinalizeTaskAction()));
}

private static void ensureThatNoRetryExtensionIsPresent(Test testTask) {
Expand All @@ -88,7 +88,7 @@ private static void ensureThatNoRetryExtensionIsPresent(Test testTask) {
}
}

private static Provider<Boolean> shouldTestRetryPluginBeDeactivated(
private static Provider<Boolean> shouldReplaceTestExecutor(
Test test,
ObjectFactory objectFactory,
ProviderFactory providerFactory,
Expand Down Expand Up @@ -143,17 +143,17 @@ private static void setTestExecuter(Test task, RetryTestExecuter retryTestExecut

private static class ConditionalTaskAction implements Action<Task> {

private final Provider<Boolean> isDeactivatedByTestDistributionPlugin;
private final Provider<Boolean> shouldReplaceTestExecutor;
private final Action<Test> delegate;

public ConditionalTaskAction(Provider<Boolean> isDeactivatedByTestDistributionPlugin, Action<Test> delegate) {
this.isDeactivatedByTestDistributionPlugin = isDeactivatedByTestDistributionPlugin;
public ConditionalTaskAction(Provider<Boolean> shouldReplaceTestExecutor, Action<Test> delegate) {
this.shouldReplaceTestExecutor = shouldReplaceTestExecutor;
this.delegate = delegate;
}

@Override
public void execute(@NotNull Task task) {
if (isDeactivatedByTestDistributionPlugin.get()) {
if (shouldReplaceTestExecutor.get()) {
task.getLogger().info("Test execution via the test-retry plugin is deactivated. Retries are handled by the test-distribution plugin.");
} else {
delegate.execute((Test) task);
Expand Down
166 changes: 166 additions & 0 deletions plugin/src/test/groovy/org/gradle/testretry/TestDryRunFuncTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.testretry

import org.gradle.testkit.runner.BuildResult
import org.gradle.util.GradleVersion

class TestDryRunFuncTest extends AbstractGeneralPluginFuncTest {

private static final GradleVersion GRADLE_8_3 = GradleVersion.version("8.3")
private static final String MIN_JUPITER_VERSION_DRY_RUN = "5.10.0-RC1"
private static final String MIN_PLATFORM_VERSION_DRY_RUN = "1.10.0-RC1"

@Override
protected String buildConfiguration() {
return """dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:$MIN_JUPITER_VERSION_DRY_RUN'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher:$MIN_PLATFORM_VERSION_DRY_RUN'
}"""
}

def "emits skipped test method events if dryRun = true and retry plugin is enabled"() {
given:
def gradle83OrAbove = GradleVersion.version(gradleVersion) >= GRADLE_8_3
setupTest(gradle83OrAbove, true)
successfulJUnit5Test()

when:
def result = gradleRunner(gradleVersion).build()

then:
gradle83OrAbove ? methodSkipped(result) : methodPassed(result)

where:
gradleVersion << GRADLE_VERSIONS_UNDER_TEST
}

def "emits skipped test method events when --test-dry-run is used and retry plugin is enabled"() {
given:
def gradle83OrAbove = GradleVersion.version(gradleVersion) >= GRADLE_8_3
setupTest(gradle83OrAbove, false)
successfulJUnit5Test()

when:
def result = gradle83OrAbove ? gradleRunner(gradleVersion, 'test', '-S', "--test-dry-run").build() : gradleRunner(gradleVersion).build()

then:
gradle83OrAbove ? methodSkipped(result) : methodPassed(result)

where:
gradleVersion << GRADLE_VERSIONS_UNDER_TEST
}

def "emits skipped test method events, if dryRun is set to true via system properties and retry plugin is enabled"() {
given:
def gradle83OrAbove = GradleVersion.version(gradleVersion) >= GRADLE_8_3
setupTest(gradle83OrAbove, false, Optional.of(true))
successfulJUnit5Test()

when:
def result = gradleRunner(gradleVersion).build()

then:
gradle83OrAbove ? methodSkipped(result) : methodPassed(result)

where:
gradleVersion << GRADLE_VERSIONS_UNDER_TEST
}

def "does not emit skipped test method events when --no-test-dry-run is used and retry plugin is enabled"() {
given:
def gradle83OrAbove = GradleVersion.version(gradleVersion) >= GRADLE_8_3
setupTest(gradle83OrAbove, false)
successfulJUnit5Test()

when:
def result = gradle83OrAbove ? gradleRunner(gradleVersion, 'test', '-S', "--no-test-dry-run").build() : gradleRunner(gradleVersion).build()

then:
methodPassed(result)

where:
gradleVersion << GRADLE_VERSIONS_UNDER_TEST
}

def "does not emit skipped test method events, if dryRun is set to false via system properties and retry plugin is enabled"() {
given:
def gradle83OrAbove = GradleVersion.version(gradleVersion) >= GRADLE_8_3
setupTest(gradle83OrAbove, false, Optional.of(false))
successfulJUnit5Test()

when:
def result = gradleRunner(gradleVersion).build()

then:
methodPassed(result)

where:
gradleVersion << GRADLE_VERSIONS_UNDER_TEST
}

def "does not emit skipped test method events by default and retry plugin is enabled"() {
given:
def gradle83OrAbove = GradleVersion.version(gradleVersion) >= GRADLE_8_3
setupTest(gradle83OrAbove, false)
successfulJUnit5Test()

when:
def result = gradleRunner(gradleVersion).build()

then:
methodPassed(result)

where:
gradleVersion << GRADLE_VERSIONS_UNDER_TEST
}

private void setupTest(boolean gradle83OrAbove, boolean withTestDryRun) {
setupTest(gradle83OrAbove, withTestDryRun, Optional.empty())
}

private void setupTest(boolean gradle83OrAbove, boolean withTestDryRun, Optional<Boolean> withSysPropDryRun) {
buildFile << """
test {
useJUnitPlatform()
${gradle83OrAbove && withTestDryRun ? "dryRun = true" : ""}
${withSysPropDryRun.map { it -> gradle83OrAbove && it ? "systemProperty('junit.platform.execution.dryRun.enabled', $it)" : "" }.orElse("")}
retry {
maxRetries = 1
}
}
"""
}

private void successfulJUnit5Test() {
writeTestSource """
package acme;
public class SuccessfulTests {
@org.junit.jupiter.api.Test
public void successTest() {}
}
"""
}

private static boolean methodPassed(BuildResult result) {
return result.output.count('PASSED') == 1
}

private static boolean methodSkipped(BuildResult result) {
return result.output.count('SKIPPED') == 1
}
}

0 comments on commit 108e6f0

Please sign in to comment.