Skip to content
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

Forbid empty testing tasks #36259

Merged
merged 4 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent a few minutes wondering if we should change this to ignore but I figured that removing it like this seemed right. I couldn't think of a good reason why someone might want to change this on the command line rather than rely on TestingConventionsTask.

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