-
Notifications
You must be signed in to change notification settings - Fork 3
Add @RestrictToGradleVersionsEqualTo annotation for filtering test matrix
#337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FinlayRJW
wants to merge
31
commits into
develop
Choose a base branch
from
finlayw/with-only
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 22 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
eab5317
addtional gradle versions
FinlayRJW 92c670b
tidy
FinlayRJW 2f996ed
move to helper
FinlayRJW 5fcaf9d
fix
FinlayRJW cff2347
allow method level
FinlayRJW dec4d01
update docs
FinlayRJW 8651c12
cleanup
FinlayRJW cf57cb8
spotless apply
FinlayRJW 92d8ce4
better test exceptions
FinlayRJW 2774507
update docs
FinlayRJW 3ae9011
helper methods
FinlayRJW 013ce05
reason
FinlayRJW 32333a4
with only
FinlayRJW e5d0346
good
FinlayRJW 08838e4
tidy up
FinlayRJW dffa4f5
tidy tests
FinlayRJW 184107d
tidy
FinlayRJW 371490d
merge
FinlayRJW 1963aab
private not public
FinlayRJW e66a41e
spotless
FinlayRJW 70d49b2
Merge branch 'finlayw/additional-gradle-versions' into finlayw/with-only
FinlayRJW 19087dd
tidy
FinlayRJW b353b81
WithSpecialCaseGradleVersions
FinlayRJW b1f0e21
merge
FinlayRJW 96b012d
RestrictToGradleVersionsEqualTo
FinlayRJW 0e9e915
change name
FinlayRJW 463b098
Merge finlayw/additional-gradle-versions into finlayw/with-only
FinlayRJW 01b87bd
change name
FinlayRJW 7e9985a
format
FinlayRJW d3eb98e
merge
FinlayRJW 08adc6d
undo incorrect change
FinlayRJW File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...-plugin-testing-junit/src/main/java/com/palantir/gradle/testing/junit/GradleVersions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * (c) Copyright 2025 Palantir Technologies Inc. All rights reserved. | ||
| * | ||
| * 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 com.palantir.gradle.testing.junit; | ||
|
|
||
| import com.google.common.base.Splitter; | ||
| import com.palantir.gradle.testing.execution.GradleVersion; | ||
| import java.lang.reflect.AnnotatedElement; | ||
| import java.lang.reflect.Method; | ||
| import java.util.Arrays; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtensionContext; | ||
| import org.junit.platform.commons.support.AnnotationSupport; | ||
|
|
||
| /** | ||
| * Utility class for Gradle versions based on annotations. | ||
| */ | ||
| final class GradleVersions { | ||
|
|
||
| private static final String GRADLE_VERSIONS_CONFIG_PARAM = "com.palantir.gradle.testing.gradle_versions_to_test"; | ||
|
|
||
| /** | ||
| * Returns all Gradle versions for the test class, including versions from all methods, | ||
| * with the class-level {@link WithOnlyGradleVersions} filter applied. | ||
| * | ||
| * <p>This is used to build the complete test matrix for the class. | ||
| */ | ||
| static Set<GradleVersion> allFilteredVersions(ExtensionContext context) { | ||
| Set<GradleVersion> versions = configuredVersions(context); | ||
|
|
||
| context.getTestClass().ifPresent(clazz -> { | ||
| versions.addAll(versionsFromAnnotation(clazz)); | ||
|
|
||
| Arrays.stream(clazz.getDeclaredMethods()) | ||
| .filter(GradleVersions::isTestMethod) | ||
| .forEach(method -> versions.addAll(versionsFromAnnotation(method))); | ||
|
|
||
| applyFilter(versions, clazz); | ||
| }); | ||
|
|
||
| return versions; | ||
| } | ||
|
|
||
| /** | ||
| * Returns all Gradle versions for a specific method, including class-level versions, | ||
| * with the method-level {@link WithOnlyGradleVersions} filter applied. | ||
| * | ||
| * <p>This is used to determine if a method should run for a given Gradle version. | ||
| */ | ||
| static Set<GradleVersion> filteredVersionsForMethod(ExtensionContext context) { | ||
| Set<GradleVersion> versions = configuredVersions(context); | ||
|
|
||
| context.getTestClass().ifPresent(clazz -> versions.addAll(versionsFromAnnotation(clazz))); | ||
|
|
||
| context.getTestMethod().ifPresent(method -> { | ||
| versions.addAll(versionsFromAnnotation(method)); | ||
| applyFilter(versions, method); | ||
| }); | ||
|
|
||
| return versions; | ||
| } | ||
|
|
||
| private static Set<GradleVersion> configuredVersions(ExtensionContext context) { | ||
| return new LinkedHashSet<>(context.getConfigurationParameter(GRADLE_VERSIONS_CONFIG_PARAM) | ||
| .map(param -> Splitter.on(',').splitToList(param).stream() | ||
| .map(GradleVersion::new) | ||
| .toList()) | ||
| .orElseThrow(() -> new RuntimeException("Not configured with the gradle versions to test against. " | ||
| + "Have you applied the `com.palantir.gradle-plugin-testing` plugin to this project?"))); | ||
| } | ||
|
|
||
| private static Set<GradleVersion> versionsFromAnnotation(AnnotatedElement element) { | ||
| return AnnotationSupport.findAnnotation(element, WithGradleVersions.class).stream() | ||
| .flatMap(annotation -> Arrays.stream(annotation.value())) | ||
| .map(GradleVersion::new) | ||
| .collect(Collectors.toCollection(LinkedHashSet::new)); | ||
| } | ||
|
|
||
| private static void applyFilter(Set<GradleVersion> versions, AnnotatedElement element) { | ||
| AnnotationSupport.findAnnotation(element, WithOnlyGradleVersions.class).ifPresent(annotation -> { | ||
| Set<GradleVersion> allowed = | ||
| Arrays.stream(annotation.value()).map(GradleVersion::new).collect(Collectors.toSet()); | ||
| versions.retainAll(allowed); | ||
| }); | ||
| } | ||
|
|
||
| private static boolean isTestMethod(Method method) { | ||
| return AnnotationSupport.isAnnotated(method, Test.class); | ||
| } | ||
|
|
||
| private GradleVersions() {} | ||
| } |
49 changes: 49 additions & 0 deletions
49
...gin-testing-junit/src/main/java/com/palantir/gradle/testing/junit/WithGradleVersions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * (c) Copyright 2025 Palantir Technologies Inc. All rights reserved. | ||
| * | ||
| * 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 com.palantir.gradle.testing.junit; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Annotation for adding additional Gradle versions to individual test classes or methods. | ||
| * | ||
| * <p><b>Important:</b> This annotation is intended for exceptional cases where a specific test or test class | ||
| * needs to run against additional Gradle versions beyond the globally configured versions. For configuring | ||
| * Gradle versions across your entire test suite, prefer setting versions in the | ||
| * {@code gradle/gradle-test-versions.yml} file | ||
| * | ||
| * <p>The versions specified in this annotation will be merged with the versions configured in the yml file. | ||
| */ | ||
| @Target({ElementType.TYPE, ElementType.METHOD}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface WithGradleVersions { | ||
|
|
||
| /** | ||
| * The additional Gradle versions to test against. | ||
| * @return an array of Gradle version strings (e.g., "7.6.5", "8.0") | ||
| */ | ||
| String[] value(); | ||
|
|
||
| /** | ||
| * Optional reason explaining why these additional Gradle versions are needed. | ||
| * @return the reason for requiring these specific versions | ||
| */ | ||
| String reason() default ""; | ||
| } |
55 changes: 55 additions & 0 deletions
55
...ng-junit/src/main/java/com/palantir/gradle/testing/junit/WithGradleVersionsCondition.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * (c) Copyright 2025 Palantir Technologies Inc. All rights reserved. | ||
| * | ||
| * 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 com.palantir.gradle.testing.junit; | ||
|
|
||
| import com.palantir.gradle.testing.execution.GradleVersion; | ||
| import java.util.Set; | ||
| import org.junit.jupiter.api.extension.ConditionEvaluationResult; | ||
| import org.junit.jupiter.api.extension.ExecutionCondition; | ||
| import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
|
||
| /** | ||
| * Execution condition that filters tests based on Gradle version. | ||
| * | ||
| * <p>When a method has its own {@link WithGradleVersions} annotation, this condition ensures | ||
| * that only the method-specific versions (plus base and class-level versions) run for that method. | ||
| * For methods without the annotation, only base and class-level versions run. | ||
| * | ||
| * <p>When a method has filter annotations like {@link WithOnlyGradleVersions}, the allowed versions | ||
| * are filtered accordingly. Class-level filter annotations are handled in {@link GradleVersioningClassTemplate} | ||
| * to filter the test matrix upfront. | ||
| */ | ||
| final class WithGradleVersionsCondition implements ExecutionCondition { | ||
|
|
||
| @Override | ||
| public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { | ||
| if (context.getTestMethod().isEmpty()) { | ||
| return ConditionEvaluationResult.enabled("No test method"); | ||
| } | ||
|
|
||
| GradleVersion currentVersion = GradleVersionStore.gradleVersion(context); | ||
| Set<GradleVersion> versionsForThisMethod = GradleVersions.filteredVersionsForMethod(context); | ||
|
|
||
| if (versionsForThisMethod.contains(currentVersion)) { | ||
| return ConditionEvaluationResult.enabled( | ||
| "Gradle version " + currentVersion + " is in the allowed set for this method"); | ||
| } | ||
|
|
||
| return ConditionEvaluationResult.disabled("Gradle version " + currentVersion | ||
| + " is not in the allowed set for this method: " + versionsForThisMethod); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
...testing-junit/src/main/java/com/palantir/gradle/testing/junit/WithOnlyGradleVersions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * (c) Copyright 2025 Palantir Technologies Inc. All rights reserved. | ||
| * | ||
| * 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 com.palantir.gradle.testing.junit; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Annotation for filtering Gradle versions to only run the specified versions. | ||
| * | ||
| * <p>Unlike {@link WithGradleVersions} which adds versions to the test matrix, this annotation filters the | ||
| * available versions to only include the specified ones. If a specified version is not in the test matrix | ||
| * (from configuration or {@code @WithGradleVersions}), it will simply not run. | ||
| * | ||
| * <p>To run a specific version that isn't in the matrix, use both annotations: | ||
| * {@code @WithGradleVersions("8.5")} and {@code @WithOnlyGradleVersions("8.5")}. | ||
| */ | ||
| @Target({ElementType.TYPE, ElementType.METHOD}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface WithOnlyGradleVersions { | ||
|
|
||
| /** | ||
| * The Gradle versions to filter to. | ||
| * @return an array of Gradle version strings (e.g., "7.6.5", "8.0") | ||
| */ | ||
| String[] value(); | ||
|
|
||
| /** | ||
| * Optional reason explaining why only these specific Gradle versions should run. | ||
| * @return the reason for filtering to these specific versions | ||
| */ | ||
| String reason() default ""; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name
@WithOnlyGradleVersionscould imply an authoritative list of versions you run the test with. This can be confusing.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about
RestrictToGradleVersionsEqualToI think that implies it only filters?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then we can add
RestrictToGradleVersionsGreaterThanetc later