Skip to content
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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1427.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: check junit dependencies task can be skipped
links:
- https://github.com/palantir/gradle-baseline/pull/1427
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@

package com.palantir.baseline.tasks;

import static java.util.stream.Collectors.toList;

import com.google.common.base.Preconditions;
import com.palantir.baseline.plugins.BaselineTesting;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.gradle.api.DefaultTask;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.result.ResolvedComponentResult;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.testing.Test;
Expand All @@ -38,29 +44,35 @@ public class CheckJUnitDependencies extends DefaultTask {
public CheckJUnitDependencies() {
setGroup("Verification");
setDescription("Ensures the correct JUnit4/5 dependencies are present, otherwise tests may silently not run");
getOutputs().upToDateWhen(_task -> true);
}

@TaskAction
public final void validateDependencies() {
getProject()
.getConvention()
.getPlugin(JavaPluginConvention.class)
.getSourceSets()
.forEach(ss -> {
if (ss.getName().equals("main")) {
return;
}

Optional<Test> maybeTestTask = BaselineTesting.getTestTaskForSourceSet(getProject(), ss);
if (!maybeTestTask.isPresent()) {
// source set doesn't have a test task, e.g. 'schema'
return;
}
Test task = maybeTestTask.get();

getProject().getLogger().info("Analyzing source set {} with task {}", ss.getName(), task.getName());
validateSourceSet(ss, task);
});
getProbablyTestSourceSets().forEach(ss -> {
Optional<Test> maybeTestTask = BaselineTesting.getTestTaskForSourceSet(getProject(), ss);
if (!maybeTestTask.isPresent()) {
// source set doesn't have a test task, e.g. 'schema'
return;
}
Test task = maybeTestTask.get();

getProject().getLogger().info("Analyzing source set {} with task {}", ss.getName(), task.getName());
validateSourceSet(ss, task);
});
}

@Classpath
public final Provider<List<Configuration>> getConfigurations() {
Copy link
Contributor

@robert3005 robert3005 Jun 23, 2020

Choose a reason for hiding this comment

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

I think gradle will get angry at you for using Provider<List<>> instead of ListProperty. If you want to have dynamic set of inputs you have to do it via getInputs() similarly to how you handle outputs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is using the same pattern as check unused dependencies. Propeties are differnet to providers, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be fine, right? There was a bug in Gradle 6.something that tried to cast Providers to CollectionPropertyInternal at some point, forcing you to use Propertys, but this was fixed?

return getProject().provider(() -> getProbablyTestSourceSets()
.map(SourceSet::getRuntimeClasspathConfigurationName)
.map(getProject().getConfigurations()::getByName)
.collect(toList()));
}

private Stream<SourceSet> getProbablyTestSourceSets() {
return getProject().getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().stream()
.filter(ss -> !ss.getName().equals(SourceSet.MAIN_SOURCE_SET_NAME));
}

private void validateSourceSet(SourceSet ss, Test task) {
Expand Down