Skip to content

Commit

Permalink
introduced CoverageBuild / random refactorings (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmck3516 authored Dec 9, 2023
1 parent 957879b commit e244153
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 64 deletions.
43 changes: 21 additions & 22 deletions skippy-gradle/src/main/java/io/skippy/gradle/AnalyzeTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import static io.skippy.gradle.SkippyConstants.SKIPPY_DIRECTORY;
import static java.lang.System.lineSeparator;
import static java.nio.file.Files.writeString;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.joining;

/**
Expand All @@ -43,24 +42,32 @@
*/
class AnalyzeTask extends DefaultTask {

private final ClassFileCollector classCollector;
private final SkippifiedTestCollector skippifiedTestCollector;

/**
* Comment to make the JavaDoc task happy.
* C'tor.
*
* @param classFileCollector
* @param skippifiedTestCollector
*/
@Inject
public AnalyzeTask(ClassFileCollector classCollector, SkippifiedTestCollector skippifiedTestCollector) {
public AnalyzeTask(ClassFileCollector classFileCollector, SkippifiedTestCollector skippifiedTestCollector) {
this.classCollector = classFileCollector;
this.skippifiedTestCollector = skippifiedTestCollector;
setGroup("skippy");
dependsOn("skippyClean");
doLast((task) -> {
createCoverageReportsForSkippifiedTests(skippifiedTestCollector);
createAnalyzedFilesTxt(classCollector);
createCoverageReportsForSkippifiedTests();
createAnalyzedFilesTxt();
});
}

private void createCoverageReportsForSkippifiedTests(SkippifiedTestCollector skippifiedTestCollector) {
private void createCoverageReportsForSkippifiedTests() {
GradleConnector connector = GradleConnector.newConnector();
connector.forProjectDirectory(getProject().getProjectDir());
try (ProjectConnection connection = connector.connect()) {
for (var skippifiedTest : skippifiedTestCollector.collectAllIn(getProject())) {
for (var skippifiedTest : skippifiedTestCollector.collect()) {
runCoverageBuild(connection, skippifiedTest);
}
}
Expand Down Expand Up @@ -94,17 +101,9 @@ private void runCoverageBuild(ProjectConnection connection, SkippifiedTest skipp
}

private void configureCoverageBuild(BuildLauncher build, SkippifiedTest skippifiedTest) {
var tasks = asList(skippifiedTest.getTestTask(), "jacocoTestReport");
var arguments = asList(
"-PskippyCoverageBuild=true",
"-PskippyClassFile=" + skippifiedTest.getAbsolutePath(),
"-PskippyTestTask=" + skippifiedTest.getTestTask()
);
build.forTasks(tasks.toArray(new String[0]));
build.addArguments(arguments.toArray(new String[0]));
if (getLogging().getLevel() != null) {
build.addArguments("--" + getLogging().getLevel().name().toLowerCase());
}
build.forTasks(CoverageBuild.getTasks(skippifiedTest).toArray(new String[0]));
build.addArguments(CoverageBuild.getArguments(skippifiedTest).toArray(new String[0]));

var csvFile = getProject().getProjectDir().toPath().resolve(SKIPPY_DIRECTORY).resolve(skippifiedTest.getFullyQualifiedClassName() + ".csv");

getLogger().lifecycle("\n%s > Capturing coverage data in %s".formatted(
Expand All @@ -113,18 +112,18 @@ private void configureCoverageBuild(BuildLauncher build, SkippifiedTest skippifi
);
getLogger().lifecycle("%s > ./gradlew %s %s".formatted(
skippifiedTest.getFullyQualifiedClassName(),
tasks.stream().collect(joining(" ")),
arguments.stream().collect(joining(" "))
CoverageBuild.getTasks(skippifiedTest).stream().collect(joining(" ")),
CoverageBuild.getArguments(skippifiedTest).stream().collect(joining(" "))
));
getLogger().lifecycle("%s".formatted(skippifiedTest.getFullyQualifiedClassName()));
}

private void createAnalyzedFilesTxt(ClassFileCollector classCollector) {
private void createAnalyzedFilesTxt() {
try {
var skippyAnalysisFile = getProject().getProjectDir().toPath().resolve(SKIPPY_DIRECTORY).resolve(SKIPPY_ANALYSIS_FILES_TXT);
skippyAnalysisFile.toFile().createNewFile();
getLogger().lifecycle("\nCreating the Skippy analysis file %s.".formatted(getProject().getProjectDir().toPath().relativize(skippyAnalysisFile)));
var classFiles = classCollector.collectAllInProject(getProject());
var classFiles = classCollector.collect();
writeString(skippyAnalysisFile, classFiles.stream()
.map(classFile -> "%s:%s".formatted(classFile.getRelativePath(), classFile.getHash()))
.collect(joining(lineSeparator())));
Expand Down
78 changes: 78 additions & 0 deletions skippy-gradle/src/main/java/io/skippy/gradle/CoverageBuild.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2023 the original author or authors.
*
* 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
*
* https://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 io.skippy.gradle;

import io.skippy.gradle.model.ClassFile;
import io.skippy.gradle.model.SkippifiedTest;
import org.gradle.api.Project;

import java.nio.file.Path;
import java.util.List;

import static java.util.Arrays.asList;

/**
* Static utility methods that perform the mapping between a {@link SkippifiedTest} and the corresponding coverage build.
* {@code ./gradlew skippyAnalyze} creates a coverage build per {@link SkippifiedTest} to capture the test coverage for
* each {@link SkippifiedTest}.
*
* @author Florian McKee
*/
final class CoverageBuild {

/**
* The Gradle tasks for the coverage build.
* @return
*/
static List<String> getTasks(SkippifiedTest skippifiedTest) {
return asList(
skippifiedTest.getTestTask(),
"jacocoTestReport"
);
}

static List<String> getArguments(SkippifiedTest skippifiedTest) {
return asList(
"-PskippyCoverageBuild=true",
"-PskippyClassFile=" + skippifiedTest.getRelativePath(),
"-PskippyTestTask=" + skippifiedTest.getTestTask()
);
}

/**
* Returns {@code true} if the {@project} represents a coverage build for a skippified test, {@code false} otherwise.
*
* @param project
* @return {@code true} if the {@project} represents a coverage build for a skippified test, {@code false} otherwise
*/
static boolean isCoverageBuildForSkippifiedTest(Project project) {
return project.hasProperty("skippyCoverageBuild");
}

/**
* Returns the {@link SkippifiedTest} for a {@project} that represents a coverage build.
*
* @param project
* @return the {@link SkippifiedTest} for a {@project} that represents a coverage build
*/
static SkippifiedTest getSkippifiedTest(Project project) {
var classFile = Path.of(String.valueOf(project.property("skippyClassFile")));
var testTaskName = String.valueOf(project.property("skippyTestTask"));
project.getLogger().lifecycle(classFile.toString());
return new SkippifiedTest(new ClassFile(project, classFile), testTaskName);
}
}
36 changes: 17 additions & 19 deletions skippy-gradle/src/main/java/io/skippy/gradle/SkippyPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import io.skippy.gradle.collector.ClassFileCollector;
import io.skippy.gradle.collector.SkippifiedTestCollector;
import io.skippy.gradle.model.ClassFile;
import io.skippy.gradle.model.SkippifiedTest;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPlugin;
Expand All @@ -29,8 +28,6 @@
import org.gradle.testing.jacoco.plugins.JacocoTaskExtension;
import org.gradle.testing.jacoco.tasks.JacocoReport;

import java.nio.file.Path;

import static io.skippy.gradle.SkippyConstants.SKIPPY_DIRECTORY;
import static java.util.Arrays.asList;

Expand All @@ -44,29 +41,30 @@ public final class SkippyPlugin implements org.gradle.api.Plugin<Project> {
@Override
public void apply(Project project) {
project.getPlugins().apply(JavaPlugin.class);
project.getExtensions().create("skippy", SkippyPluginExtension.class);

var isSkippyCoverageBuild = project.hasProperty("skippyCoverageBuild");

if (! isSkippyCoverageBuild) {
var skippyExtension = project.getExtensions().create("skippy", SkippyPluginExtension.class);

// add skippy tasks to the regular build
project.getTasks().register("skippyClean", CleanTask.class);
if (CoverageBuild.isCoverageBuildForSkippifiedTest(project)) {

var classFileCollector = new ClassFileCollector();
var skippifiedTestCollector = new SkippifiedTestCollector(classFileCollector);
// this is a nested coverage build triggered by skippyAnalyze: modify test and jacocoTestReport tasks

var skippifiedTest = CoverageBuild.getSkippifiedTest(project);

project.getPlugins().apply(JacocoPlugin.class);
modifyTestTask(project, skippifiedTest);
modifyJacocoTestReportTask(project, skippifiedTest);

project.getTasks().register("skippyAnalyze", AnalyzeTask.class, classFileCollector, skippifiedTestCollector);
} else {

var classFile = Path.of(String.valueOf(project.property("skippyClassFile")));
var testTaskName = String.valueOf(project.property("skippyTestTask"));
var testClass = new SkippifiedTest(new ClassFile(project, classFile), testTaskName);
// this is a regular build: add skippyClean + skippyAnalyze tasks

// modify test and jacocoTestReport tasks in the skippyCoverage builds
project.getPlugins().apply(JacocoPlugin.class);
modifyTestTask(project, testClass);
modifyJacocoTestReportTask(project, testClass);
project.getTasks().register("skippyClean", CleanTask.class);

var sourceSetContainer = project.getExtensions().getByType(SourceSetContainer.class);
var classFileCollector = new ClassFileCollector(project, sourceSetContainer);
var skippifiedTestCollector = new SkippifiedTestCollector(project, classFileCollector, sourceSetContainer, skippyExtension);

project.getTasks().register("skippyAnalyze", AnalyzeTask.class, classFileCollector, skippifiedTestCollector);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,62 @@
import static java.util.Comparator.comparing;

/**
* Collects all {@link ClassFile}s in {@link Project}s and {@link SourceSet}s.
* Collects {@link ClassFile}s in a project.
*
* @author Florian McKee
*/
public final class ClassFileCollector {

private final Project project;
private final SourceSetContainer sourceSetContainer;

/**
* C'tor
*
* @param project
* @param sourceSetContainer
*/
public ClassFileCollector(Project project, SourceSetContainer sourceSetContainer) {
this.project = project;
this.sourceSetContainer = sourceSetContainer;
}


/**
* Collects all {@link ClassFile}s across all output directories of the {@param project}.
* Collects all {@link ClassFile}s in the project.
*
* @return all {@link ClassFile}s across all output directories of the {@param project}
* @return all {@link ClassFile}s in the project
*/
public List<ClassFile> collectAllInProject(Project project) {
public List<ClassFile> collect() {
var result = new ArrayList<ClassFile>();
var sourceSetContainer = project.getExtensions().getByType(SourceSetContainer.class);
for (var sourceSet : sourceSetContainer) {
result.addAll(collectAllInSourceSet(project, sourceSet));
result.addAll(collect(sourceSet));
}
return sort(result);
}

/**
* Collects all {@link ClassFile}s in the output directories of the {@param sourceSet}.
*
* @param sourceSet
* @return all {@link ClassFile}s in the output directories of the {@param sourceSet}
*/
List<ClassFile> collectAllInSourceSet(Project project, SourceSet sourceSet) {
List<ClassFile> collect(SourceSet sourceSet) {
var classesDirs = sourceSet.getOutput().getClassesDirs().getFiles();
var result = new ArrayList<ClassFile>();
for (var classesDir : classesDirs) {
result.addAll(collectAllInDirectory(project, classesDir));
result.addAll(collect(classesDir));
}
return sort(result);
}

private static List<ClassFile> collectAllInDirectory(Project project, File directory) {
private List<ClassFile> collect(File directory) {
var result = new LinkedList<ClassFile>();
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
result.addAll(collectAllInDirectory(project, file));
result.addAll(collect(file));
} else if (file.getName().endsWith(".class")) {
result.add(new ClassFile(project, file.toPath()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,54 @@

import static java.util.Comparator.comparing;

/**
* Collects {@link SkippifiedTest}s in a project.
*
* @author Florian McKee
*/
public final class SkippifiedTestCollector {

private final Project project;
private final ClassFileCollector classFileCollector;
private final SourceSetContainer sourceSetContainer;
private final SkippyPluginExtension skippyPluginExtension;

public SkippifiedTestCollector(ClassFileCollector classFileCollector) {
/**
* C'tor.
*
* @param project
* @param classFileCollector
* @param sourceSetContainer
* @param skippyPluginExtension
*/
public SkippifiedTestCollector(Project project, ClassFileCollector classFileCollector, SourceSetContainer sourceSetContainer, SkippyPluginExtension skippyPluginExtension) {
this.project = project;
this.classFileCollector = classFileCollector;
this.sourceSetContainer = sourceSetContainer;
this.skippyPluginExtension = skippyPluginExtension;
}

public List<SkippifiedTest> collectAllIn(Project project) {
/**
* Collects all {@link SkippifiedTest}s in the project.
*
* @return all {@link SkippifiedTest}s in the project
*/
public List<SkippifiedTest> collect() {
var result = new LinkedList<SkippifiedTest>();
var skippyPluginExtension = project.getExtensions().getByType(SkippyPluginExtension.class);
for (var sourceSetWithTestTask : skippyPluginExtension.getSourceSetsWithTestTasks()) {
result.addAll(collectAllInSourceSet(project, sourceSetWithTestTask));
result.addAll(collect(sourceSetWithTestTask));
}
return result;
}

private List<SkippifiedTest> collectAllInSourceSet(Project project, SourceSetWithTestTask sourceSetWithTestTask) {
var sourceSetContainer = project.getExtensions().getByType(SourceSetContainer.class);
/**
* Collects all {@link SkippifiedTest}s in the SourceSet identified by {@param sourceSetWithTestTask}.
*
* @return all {@link SkippifiedTest}s in the SourceSet identified by {@param sourceSetWithTestTask}
*/
private List<SkippifiedTest> collect(SourceSetWithTestTask sourceSetWithTestTask) {
var sourceSet = sourceSetContainer.getByName(sourceSetWithTestTask.getSourceSetName());
var classFiles = classFileCollector.collectAllInSourceSet(project, sourceSet);
var classFiles = classFileCollector.collect(sourceSet);
return classFiles.stream()
.filter(classFile -> SkippyJUnit5Detector.usesSkippyJunit5Extension(classFile.getAbsolutePath()))
.map(classFile -> new SkippifiedTest(classFile, sourceSetWithTestTask.getTestTask()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.nio.file.Path;

/**
* Thin wrapper around a class file that adds a couple of convenience methods.
* Thin wrapper around a class file in a {@link Project} that adds a couple of convenience methods.
*
* @author Florian McKee
*/
Expand Down
Loading

0 comments on commit e244153

Please sign in to comment.