Skip to content

Commit

Permalink
Forbid empty testing tasks (#36259)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alpar-t committed Dec 10, 2018
1 parent 3cf31ff commit 4658291
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,6 @@ class BuildPlugin implements Plugin<Project> {
project.tasks.withType(RandomizedTestingTask) {task ->
jvm "${project.runtimeJavaHome}/bin/java"
parallelism System.getProperty('tests.jvms', project.rootProject.ext.defaultParallel)
ifNoTests System.getProperty('tests.ifNoTests', 'fail')
onNonEmptyWorkDirectory 'wipe'
leaveTemporary true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.gradle.api.file.FileTree;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.testing.Test;
import org.gradle.api.tasks.util.PatternFilterable;
Expand Down Expand Up @@ -103,6 +102,22 @@ public void doCheck() throws IOException {
final Map<String, Set<File>> classFilesPerRandomizedTestingTask = classFilesPerRandomizedTestingTask(allTestClassFiles);
final Map<String, Set<File>> classFilesPerGradleTestTask = classFilesPerGradleTestTask();

Map<String, Set<Class<?>>> testClassesPerTask =
Stream.concat(
classFilesPerGradleTestTask.entrySet().stream(),
classFilesPerRandomizedTestingTask.entrySet().stream()
)
.collect(
Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue().stream()
.map(classes::get)
.filter(implementsNamingConvention)
.collect(Collectors.toSet())
)
);


problems = collectProblems(
checkNoneExists(
"Test classes implemented by inner classes will not run",
Expand All @@ -118,6 +133,16 @@ public void doCheck() throws IOException {
.filter(this::seemsLikeATest)
.filter(implementsNamingConvention.negate())
),
collectProblems(
testClassesPerTask.entrySet().stream()
.map( entry ->
checkAtLeastOneExists(
"test class in " + entry.getKey(),
entry.getValue().stream()
)
)
.collect(Collectors.joining())
),
checkNoneExists(
"Test classes are not included in any enabled task (" +
Stream.concat(
Expand Down Expand Up @@ -215,7 +240,6 @@ private Class<? extends Task> getRandomizedTestingTask() {
}

@Input
@SkipWhenEmpty
public Map<String, File> getTestClassNames() {
if (testClassNames == null) {
testClassNames = Boilerplate.getJavaSourceSets(getProject()).getByName("test").getOutput().getClassesDirs()
Expand Down Expand Up @@ -243,6 +267,14 @@ private String checkNoneExists(String message, Stream<? extends Class<?>> stream
}
}

private String checkAtLeastOneExists(String message, Stream<? extends Class<?>> stream) {
if (stream.findAny().isPresent()) {
return "";
} else {
return "Expected at least one " + message + ", but found none.\n";
}
}

private boolean seemsLikeATest(Class<?> clazz) {
try {
ClassLoader classLoader = clazz.getClassLoader();
Expand Down

0 comments on commit 4658291

Please sign in to comment.