Skip to content

Commit

Permalink
scoverage#75 Spawn tasks using Worker API instead of adding scoverage…
Browse files Browse the repository at this point in the history
…'s classpath to the main gradle classloader
  • Loading branch information
eyalroth committed Sep 11, 2021
1 parent eec9a26 commit 08512e6
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 76 deletions.
70 changes: 47 additions & 23 deletions src/main/groovy/org/scoverage/ScoverageAggregate.groovy
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.scoverage

import org.gradle.api.DefaultTask
import org.gradle.api.file.FileCollection
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.logging.Logging
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.TaskAction
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.*
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import scoverage.report.CoverageAggregator

import static org.gradle.api.tasks.PathSensitivity.RELATIVE
Expand All @@ -21,17 +20,14 @@ class ScoverageAggregate extends DefaultTask {

@InputFiles
@PathSensitive(RELATIVE)
final Property<FileCollection> sources = project.objects.property(FileCollection)
final ConfigurableFileCollection sources = project.objects.fileCollection()

@OutputDirectory
final Property<File> reportDir = project.objects.property(File)

@Input
final ListProperty<File> dirsToAggregateFrom = project.objects.listProperty(File)

@Input
final Property<Boolean> deleteReportsOnAggregation = project.objects.property(Boolean)

@Input
final Property<String> sourceEncoding = project.objects.property(String)

Expand All @@ -51,24 +47,52 @@ class ScoverageAggregate extends DefaultTask {

@TaskAction
def aggregate() {
runner.run {
reportDir.get().deleteDir()
reportDir.get().mkdirs()
runner.run(AggregateAction.class) { parameters ->
parameters.sources.from(sources)
parameters.reportDir = reportDir
parameters.dirsToAggregateFrom = dirsToAggregateFrom
parameters.sourceEncoding = sourceEncoding
parameters.coverageOutputCobertura = coverageOutputCobertura
parameters.coverageOutputXML = coverageOutputXML
parameters.coverageOutputHTML = coverageOutputHTML
parameters.coverageDebug = coverageDebug
}
}

static interface Parameters extends WorkParameters {
ConfigurableFileCollection getSources()
Property<File> getReportDir()
ListProperty<File> getDirsToAggregateFrom()
Property<String> getSourceEncoding()
Property<Boolean> getCoverageOutputCobertura()
Property<Boolean> getCoverageOutputXML()
Property<Boolean> getCoverageOutputHTML()
Property<Boolean> getCoverageDebug()
}

static abstract class AggregateAction implements WorkAction<Parameters> {

@Override
void execute() {
def logger = Logging.getLogger(AggregateAction.class)

getParameters().reportDir.get().deleteDir()
getParameters().reportDir.get().mkdirs()

def dirs = []
dirs.addAll(dirsToAggregateFrom.get())
dirs.addAll(getParameters().dirsToAggregateFrom.get())
def coverage = CoverageAggregator.aggregate(dirs.unique() as File[])

if (coverage.nonEmpty()) {
new ScoverageWriter(project.logger).write(
sources.get().getFiles(),
reportDir.get(),
new ScoverageWriter(logger).write(
getParameters().sources.getFiles(),
getParameters().reportDir.get(),
coverage.get(),
sourceEncoding.get(),
coverageOutputCobertura.get(),
coverageOutputXML.get(),
coverageOutputHTML.get(),
coverageDebug.get()
getParameters().sourceEncoding.get(),
getParameters().coverageOutputCobertura.get(),
getParameters().coverageOutputXML.get(),
getParameters().coverageOutputHTML.get(),
getParameters().coverageDebug.get()
)
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/main/groovy/org/scoverage/ScoverageExtension.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class ScoverageExtension {
final Property<Boolean> coverageOutputHTML
final Property<Boolean> coverageDebug

final Property<Boolean> deleteReportsOnAggregation

final List<CheckConfig> checks = new ArrayList<>()

final Property<CoverageType> coverageType
Expand Down Expand Up @@ -86,9 +84,6 @@ class ScoverageExtension {
coverageDebug = project.objects.property(Boolean)
coverageDebug.set(false)

deleteReportsOnAggregation = project.objects.property(Boolean)
deleteReportsOnAggregation.set(false)

coverageType = project.objects.property(CoverageType)
minimumRate = project.objects.property(BigDecimal)
}
Expand Down
21 changes: 14 additions & 7 deletions src/main/groovy/org/scoverage/ScoveragePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.scala.ScalaCompile
import org.gradle.api.tasks.testing.Test
import org.gradle.workers.WorkerExecutor

import javax.inject.Inject
import java.nio.file.Files
import java.util.concurrent.ConcurrentHashMap

Expand All @@ -29,9 +31,15 @@ class ScoveragePlugin implements Plugin<PluginAware> {

static final String DEFAULT_REPORT_DIR = 'reports' + File.separatorChar + 'scoverage'

private final WorkerExecutor workerExecutor
private final ConcurrentHashMap<Task, Set<? extends Task>> crossProjectTaskDependencies = new ConcurrentHashMap<>()
private final ConcurrentHashMap<Task, Set<? extends Task>> sameProjectTaskDependencies = new ConcurrentHashMap<>()

@Inject
ScoveragePlugin(WorkerExecutor workerExecutor) {
this.workerExecutor = workerExecutor
}

@Override
void apply(PluginAware pluginAware) {
if (pluginAware instanceof Project) {
Expand Down Expand Up @@ -80,7 +88,8 @@ class ScoveragePlugin implements Plugin<PluginAware> {

private void createTasks(Project project, ScoverageExtension extension) {

ScoverageRunner scoverageRunner = new ScoverageRunner(project.configurations.scoverage)
ScoverageRunner scoverageRunner = new ScoverageRunner(workerExecutor)
scoverageRunner.runtimeClasspath = project.configurations.scoverage

def originalSourceSet = project.sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME)
def instrumentedSourceSet = project.sourceSets.create('scoverage') {
Expand Down Expand Up @@ -125,7 +134,7 @@ class ScoveragePlugin implements Plugin<PluginAware> {
group = 'verification'
runner = scoverageRunner
reportDir = taskReportDir
sources = originalSourceSet.scala.getSourceDirectories()
sources.from(originalSourceSet.scala.getSourceDirectories())
dataDir = extension.dataDir
sourceEncoding.set(detectedSourceEncoding)
coverageOutputCobertura = extension.coverageOutputCobertura
Expand All @@ -144,10 +153,9 @@ class ScoveragePlugin implements Plugin<PluginAware> {
group = 'verification'
runner = scoverageRunner
reportDir = extension.reportDir
sources = originalSourceSet.scala.getSourceDirectories()
sources.from(originalSourceSet.scala.getSourceDirectories())
dirsToAggregateFrom = dataDirs
sourceEncoding.set(detectedSourceEncoding)
deleteReportsOnAggregation = false
coverageOutputCobertura = extension.coverageOutputCobertura
coverageOutputXML = extension.coverageOutputXML
coverageOutputHTML = extension.coverageOutputHTML
Expand Down Expand Up @@ -308,7 +316,7 @@ class ScoveragePlugin implements Plugin<PluginAware> {
def allReportTasks = childReportTasks + globalReportTask.get()
def allSources = project.objects.fileCollection()
allReportTasks.each {
allSources = allSources.plus(it.sources.get())
allSources = allSources.plus(it.sources)
}
def aggregationTask = project.tasks.create(AGGREGATE_NAME, ScoverageAggregate) {
def dataDirs = allReportTasks.findResults { it.dirsToAggregateFrom.get() }.flatten()
Expand All @@ -319,10 +327,9 @@ class ScoveragePlugin implements Plugin<PluginAware> {
group = 'verification'
runner = scoverageRunner
reportDir = extension.reportDir
sources = allSources
sources.from(allSources)
sourceEncoding.set(detectedSourceEncoding)
dirsToAggregateFrom = dataDirs
deleteReportsOnAggregation = extension.deleteReportsOnAggregation
coverageOutputCobertura = extension.coverageOutputCobertura
coverageOutputXML = extension.coverageOutputXML
coverageOutputHTML = extension.coverageOutputHTML
Expand Down
73 changes: 50 additions & 23 deletions src/main/groovy/org/scoverage/ScoverageReport.groovy
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.scoverage

import org.gradle.api.DefaultTask
import org.gradle.api.file.FileCollection
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.logging.Logging
import org.gradle.api.provider.Property
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.TaskAction
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.*
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import scoverage.report.CoverageAggregator

import static org.gradle.api.tasks.PathSensitivity.RELATIVE
Expand All @@ -27,7 +24,7 @@ class ScoverageReport extends DefaultTask {

@InputFiles
@PathSensitive(RELATIVE)
final Property<FileCollection> sources = project.objects.property(FileCollection)
final ConfigurableFileCollection sources = project.objects.fileCollection()

@OutputDirectory
final Property<File> reportDir = project.objects.property(File)
Expand All @@ -46,25 +43,55 @@ class ScoverageReport extends DefaultTask {

@TaskAction
def report() {
runner.run {
reportDir.get().delete()
reportDir.get().mkdirs()
runner.run(ReportAction.class) { parameters ->
parameters.dataDir = dataDir
parameters.sources.from(sources)
parameters.reportDir = reportDir
parameters.sourceEncoding = sourceEncoding
parameters.coverageOutputCobertura = coverageOutputCobertura
parameters.coverageOutputXML = coverageOutputXML
parameters.coverageOutputHTML = coverageOutputHTML
parameters.coverageDebug = coverageDebug
}
}

static interface Parameters extends WorkParameters {
Property<File> getDataDir()
ConfigurableFileCollection getSources()
Property<File> getReportDir()
Property<String> getSourceEncoding()
Property<Boolean> getCoverageOutputCobertura()
Property<Boolean> getCoverageOutputXML()
Property<Boolean> getCoverageOutputHTML()
Property<Boolean> getCoverageDebug()
}

static abstract class ReportAction implements WorkAction<Parameters> {

def coverage = CoverageAggregator.aggregate([dataDir.get()] as File[])
@Override
void execute() {
getParameters().reportDir.get().delete()
getParameters().reportDir.get().mkdirs()

def coverage = CoverageAggregator.aggregate([getParameters().dataDir.get()] as File[])

def logger = Logging.getLogger(ReportAction.class)

if (coverage.isEmpty()) {
project.logger.info("[scoverage] Could not find coverage file, skipping...")
logger.info("[scoverage] Could not find coverage file, skipping...")
} else {
new ScoverageWriter(project.logger).write(
sources.get().getFiles(),
reportDir.get(),
new ScoverageWriter(logger).write(
getParameters().sources.getFiles(),
getParameters().reportDir.get(),
coverage.get(),
sourceEncoding.get(),
coverageOutputCobertura.get(),
coverageOutputXML.get(),
coverageOutputHTML.get(),
coverageDebug.get())
getParameters().sourceEncoding.get(),
getParameters().coverageOutputCobertura.get(),
getParameters().coverageOutputXML.get(),
getParameters().coverageOutputHTML.get(),
getParameters().coverageDebug.get())
}

}

}
}
41 changes: 23 additions & 18 deletions src/main/groovy/org/scoverage/ScoverageRunner.groovy
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
package org.scoverage

import org.gradle.api.Action
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.Classpath
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import org.gradle.workers.WorkerExecutor

import java.lang.reflect.Method
import javax.inject.Inject

class ScoverageRunner {

private final WorkerExecutor workerExecutor

@Classpath
final FileCollection runtimeClasspath
FileCollection runtimeClasspath

ScoverageRunner(FileCollection runtimeClasspath) {
@Inject
ScoverageRunner(WorkerExecutor workerExecutor) {

this.runtimeClasspath = runtimeClasspath
this.workerExecutor = workerExecutor
}

def run(Closure<?> action) {

URLClassLoader cloader = (URLClassLoader) Thread.currentThread().getContextClassLoader()

Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class)
method.setAccessible(true)

runtimeClasspath.files.each { f ->
def url = f.toURI().toURL()
if (!cloader.getURLs().contains(url)) {
method.invoke(cloader, url)
}
/**
* The runner makes use of Gradle's worker API to run tasks with scoverage's classpath without affecting the main
* gradle classloader/classpath.
*
* @see <a href="https://docs.gradle.org/current/userguide/custom_tasks.html#worker_api">Worker API guide</a>
* @see <a href="https://github.com/gradle/guides/issues/295">Worker API guide issue<a/>
*/
def <T extends WorkParameters> void run(Class<? extends WorkAction<T>> workActionClass, Action<? super T> parameterAction) {

def queue = workerExecutor.classLoaderIsolation() {spec ->
spec.getClasspath().from(runtimeClasspath)
}

action.call()
queue.submit(workActionClass, parameterAction)
}
}

0 comments on commit 08512e6

Please sign in to comment.