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

[#319] Add support for thresholds of aggregated metrics #325

Merged
merged 1 commit into from
Nov 27, 2022
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ buildscript {

sourceCompatibility = 1.8

ext.pitestAggregatorVersion = "1.9.4" //Must be equal to default PIT version in PitestPlugin
ext.pitestAggregatorVersion = "1.9.10" //Must be equal to default PIT version in PitestPlugin

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ class AcceptanceTestsInSeparateSubprojectFunctionalSpec extends AbstractPitestFu
assertHtmlContains("<th>Number of Classes</th>")
assertHtmlContains("<th>Line Coverage</th>")
assertHtmlContains("<th>Mutation Coverage</th>")
assertHtmlContains("<th>Test Strength</th>")
assertHtmlContains("<td>2</td>")
assertHtmlContains("<td>95% ")
assertHtmlContains("<td>40% ")
assertHtmlContains("<td>50% ")
assertHtmlContains("<td><a href=\"./pitest.sample.multimodule.forreport/index.html\">pitest.sample.multimodule.forreport</a></td>")
assertHtmlContains("<td><a href=\"./pitest.sample.multimodule.shared/index.html\">pitest.sample.multimodule.shared</a></td>")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ subprojects {
outputFormats = ["HTML","XML"]
timestampedReports = false
exportLineCoverage = true
reportAggregator {
aggregatedTestStrengthThreshold.set(50)
aggregatedMutationThreshold.set(40)
aggregatedMaxSurviving.set(3)
szpak marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package info.solidsoft.gradle.pitest

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.gradle.api.GradleException
import org.gradle.api.Incubating
import org.gradle.api.provider.Property
import org.gradle.workers.WorkAction
import org.pitest.aggregate.AggregationResult
import org.pitest.aggregate.ReportAggregator
import org.pitest.mutationtest.config.DirectoryResultOutputStrategy
import org.pitest.mutationtest.config.UndatedReportDirCreationStrategy
Expand Down Expand Up @@ -33,9 +35,36 @@ abstract class AggregateReportGenerator implements WorkAction<AggregateReportWor
parameters.reportDir.asFile.get().absolutePath,
new UndatedReportDirCreationStrategy()))
.build()
aggregator.aggregateReport()
AggregationResult aggregationResult = aggregator.aggregateReport()

log.info("Aggregated report ${parameters.reportFile.asFile.get().absolutePath}")

consumeIfPropertyIsSet(parameters.aggregatedTestStrengthThreshold) { threshold ->
if (aggregationResult.testStrength < threshold) {
throw new GradleException(
"Aggregated test strength score of ${aggregationResult.testStrength} " +
"is below threshold of $threshold"
)
}
}

consumeIfPropertyIsSet(parameters.aggregatedMutationThreshold) { threshold ->
if (aggregationResult.mutationCoverage < threshold) {
throw new GradleException(
"Aggregated mutation score of ${aggregationResult.mutationCoverage} " +
"is below threshold of $threshold"
)
}
}

consumeIfPropertyIsSet(parameters.aggregatedMaxSurviving) { threshold ->
if (aggregationResult.mutationsSurvived > threshold) {
throw new GradleException(
"Had ${aggregationResult.mutationsSurvived} " +
"surviving mutants, but only $threshold survivors allowed"
)
}
}
}

private static <T> void consumeIfPropertyIsSet(Property<T> property, Consumer<T> applyPropertyCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ abstract class AggregateReportTask extends DefaultTask {
@Optional
final Property<Charset> outputCharset

@Input
@Optional
final Property<Integer> aggregatedTestStrengthThreshold

@Input
@Optional
final Property<Integer> aggregatedMutationThreshold

@Input
@Optional
final Property<Integer> aggregatedMaxSurviving

@Inject
abstract WorkerExecutor getWorkerExecutor()

Expand All @@ -85,6 +97,9 @@ abstract class AggregateReportTask extends DefaultTask {
lineCoverageFiles = of.fileCollection()
inputCharset = of.property(Charset)
outputCharset = of.property(Charset)
aggregatedTestStrengthThreshold = of.property(Integer)
aggregatedMutationThreshold = of.property(Integer)
aggregatedMaxSurviving = of.property(Integer)
}

@TaskAction
Expand All @@ -103,6 +118,9 @@ abstract class AggregateReportTask extends DefaultTask {
parameters.lineCoverageFiles.from(lineCoverageFiles)
parameters.inputCharset.set(this.inputCharset)
parameters.outputCharset.set(this.outputCharset)
parameters.aggregatedTestStrengthThreshold.set(this.aggregatedTestStrengthThreshold)
parameters.aggregatedMutationThreshold.set(this.aggregatedMutationThreshold)
parameters.aggregatedMaxSurviving.set(this.aggregatedMaxSurviving)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ interface AggregateReportWorkParameters extends WorkParameters {
ConfigurableFileCollection getLineCoverageFiles()
Property<Charset> getInputCharset()
Property<Charset> getOutputCharset()
Property<Integer> getAggregatedTestStrengthThreshold()
Property<Integer> getAggregatedMutationThreshold()
Property<Integer> getAggregatedMaxSurviving()

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class PitestAggregatorPlugin implements Plugin<Project> {
findPluginExtension().ifPresent({ PitestPluginExtension extension ->
inputCharset.set(extension.inputCharset)
outputCharset.set(extension.outputCharset)
aggregatedTestStrengthThreshold.set(extension.reportAggregatorProperties.aggregatedTestStrengthThreshold)
aggregatedMutationThreshold.set(extension.reportAggregatorProperties.aggregatedMutationThreshold)
aggregatedMaxSurviving.set(extension.reportAggregatorProperties.aggregatedMaxSurviving)
} as Consumer<PitestPluginExtension>) //Simplify with Groovy 3+
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PitestPlugin implements Plugin<Project> {
public final static String PITEST_REPORT_DIRECTORY_NAME = 'pitest'
public final static String PITEST_CONFIGURATION_NAME = 'pitest'

public final static String DEFAULT_PITEST_VERSION = '1.9.4'
public final static String DEFAULT_PITEST_VERSION = '1.9.10'
@Internal //6.4 due to main -> mainClass change to avoid deprecation warning in Gradle 7.x - https://github.com/szpak/gradle-pitest-plugin/pull/289
public static final GradleVersion MINIMAL_SUPPORTED_GRADLE_VERSION = GradleVersion.version("6.4") //public as used also in regression tests

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package info.solidsoft.gradle.pitest

import groovy.transform.CompileStatic
import org.gradle.api.Action
import org.gradle.api.Incubating
import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty
Expand Down Expand Up @@ -220,6 +221,8 @@ class PitestPluginExtension {
@Incubating
final ListProperty<String> fileExtensionsToFilter

final ReportAggregatorProperties reportAggregatorProperties

PitestPluginExtension(Project project) {
ObjectFactory of = project.objects
Project p = project
Expand Down Expand Up @@ -270,6 +273,11 @@ class PitestPluginExtension {
outputCharset = of.property(Charset)
features = nullListPropertyOf(p, String)
fileExtensionsToFilter = nullListPropertyOf(p, String)
reportAggregatorProperties = new ReportAggregatorProperties(of)
}

void reportAggregator(Action<? super ReportAggregatorProperties> action) {
action.execute(reportAggregatorProperties)
}

void setReportDir(File reportDir) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package info.solidsoft.gradle.pitest

import groovy.transform.CompileStatic
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property

import javax.inject.Inject

@CompileStatic
class ReportAggregatorProperties {

final Property<Integer> aggregatedTestStrengthThreshold
final Property<Integer> aggregatedMutationThreshold
final Property<Integer> aggregatedMaxSurviving

@Inject
ReportAggregatorProperties(ObjectFactory objects) {
aggregatedTestStrengthThreshold = objects.property(Integer)
aggregatedMutationThreshold = objects.property(Integer)
aggregatedMaxSurviving = objects.property(Integer)
}

}