Skip to content

Commit 4658291

Browse files
committed
Forbid empty testing tasks (#36259)
Closes #34820 With this change we allow for no tests being ran in randomized testing task, and forbid empty testing tasks from the testing conventions task. We will no longer have to disable the task if all tests are muted.
1 parent 3cf31ff commit 4658291

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,6 @@ class BuildPlugin implements Plugin<Project> {
864864
project.tasks.withType(RandomizedTestingTask) {task ->
865865
jvm "${project.runtimeJavaHome}/bin/java"
866866
parallelism System.getProperty('tests.jvms', project.rootProject.ext.defaultParallel)
867-
ifNoTests System.getProperty('tests.ifNoTests', 'fail')
868867
onNonEmptyWorkDirectory 'wipe'
869868
leaveTemporary true
870869

buildSrc/src/main/java/org/elasticsearch/gradle/precommit/TestingConventionsTasks.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.gradle.api.file.FileTree;
2626
import org.gradle.api.tasks.Input;
2727
import org.gradle.api.tasks.OutputFile;
28-
import org.gradle.api.tasks.SkipWhenEmpty;
2928
import org.gradle.api.tasks.TaskAction;
3029
import org.gradle.api.tasks.testing.Test;
3130
import org.gradle.api.tasks.util.PatternFilterable;
@@ -103,6 +102,22 @@ public void doCheck() throws IOException {
103102
final Map<String, Set<File>> classFilesPerRandomizedTestingTask = classFilesPerRandomizedTestingTask(allTestClassFiles);
104103
final Map<String, Set<File>> classFilesPerGradleTestTask = classFilesPerGradleTestTask();
105104

105+
Map<String, Set<Class<?>>> testClassesPerTask =
106+
Stream.concat(
107+
classFilesPerGradleTestTask.entrySet().stream(),
108+
classFilesPerRandomizedTestingTask.entrySet().stream()
109+
)
110+
.collect(
111+
Collectors.toMap(
112+
Map.Entry::getKey,
113+
entry -> entry.getValue().stream()
114+
.map(classes::get)
115+
.filter(implementsNamingConvention)
116+
.collect(Collectors.toSet())
117+
)
118+
);
119+
120+
106121
problems = collectProblems(
107122
checkNoneExists(
108123
"Test classes implemented by inner classes will not run",
@@ -118,6 +133,16 @@ public void doCheck() throws IOException {
118133
.filter(this::seemsLikeATest)
119134
.filter(implementsNamingConvention.negate())
120135
),
136+
collectProblems(
137+
testClassesPerTask.entrySet().stream()
138+
.map( entry ->
139+
checkAtLeastOneExists(
140+
"test class in " + entry.getKey(),
141+
entry.getValue().stream()
142+
)
143+
)
144+
.collect(Collectors.joining())
145+
),
121146
checkNoneExists(
122147
"Test classes are not included in any enabled task (" +
123148
Stream.concat(
@@ -215,7 +240,6 @@ private Class<? extends Task> getRandomizedTestingTask() {
215240
}
216241

217242
@Input
218-
@SkipWhenEmpty
219243
public Map<String, File> getTestClassNames() {
220244
if (testClassNames == null) {
221245
testClassNames = Boilerplate.getJavaSourceSets(getProject()).getByName("test").getOutput().getClassesDirs()
@@ -243,6 +267,14 @@ private String checkNoneExists(String message, Stream<? extends Class<?>> stream
243267
}
244268
}
245269

270+
private String checkAtLeastOneExists(String message, Stream<? extends Class<?>> stream) {
271+
if (stream.findAny().isPresent()) {
272+
return "";
273+
} else {
274+
return "Expected at least one " + message + ", but found none.\n";
275+
}
276+
}
277+
246278
private boolean seemsLikeATest(Class<?> clazz) {
247279
try {
248280
ClassLoader classLoader = clazz.getClassLoader();

0 commit comments

Comments
 (0)