Skip to content

Collect scoverage results from all Test tasks #100

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

Merged
merged 4 commits into from
May 22, 2019
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
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pluginBundle {
apply plugin: 'maven'
apply plugin: 'groovy'

sourceCompatibility = '1.6'
targetCompatibility = '1.6'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'

dependencies {
compileOnly "org.scoverage:scalac-scoverage-plugin_2.12:1.3.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package org.scoverage;

import org.junit.Assert;
import org.junit.Test;

public class ScalaSingleModuleWithMultipleTestTasksTest extends ScoverageFunctionalTest {
public ScalaSingleModuleWithMultipleTestTasksTest() {
super("scala-single-module-multiple-test-tasks");
}

@Test
public void test() {

AssertableBuildResult result = dryRun("clean", "test");

result.assertTaskDoesntExist(ScoveragePlugin.getCOMPILE_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getREPORT_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getCHECK_NAME());
}

@Test
public void build() {

AssertableBuildResult result = dryRun("clean", "build");

result.assertTaskDoesntExist(ScoveragePlugin.getCOMPILE_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getREPORT_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getCHECK_NAME());
}

@Test
public void reportScoverage() {

AssertableBuildResult result = dryRun("clean", ScoveragePlugin.getREPORT_NAME());

result.assertTaskExists(ScoveragePlugin.getCOMPILE_NAME());
result.assertTaskExists(ScoveragePlugin.getREPORT_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getCHECK_NAME());
}

@Test
public void aggregateScoverage() {

AssertableBuildResult result = runAndFail("clean", ScoveragePlugin.getAGGREGATE_NAME());

result.assertNoTasks();
}


@Test
public void checkScoverage() throws Exception {

AssertableBuildResult result = run("clean", ScoveragePlugin.getCHECK_NAME());

result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
result.assertTaskSucceeded("reportTestScoverage");
result.assertTaskSucceeded("reportIntTestScoverage");
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
result.assertTaskSucceeded(ScoveragePlugin.getCHECK_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());

assertReportFilesExist();
assertCoverage(100.0);
}

@Test
public void checkScoverageIntTest() throws Exception {
AssertableBuildResult result = runAndFail("clean", "-x", "reportTestScoverage", ScoveragePlugin.getCHECK_NAME());

result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
result.assertTaskDoesntExist("reportTestScoverage");
result.assertTaskSucceeded("reportIntTestScoverage");
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
result.assertTaskFailed(ScoveragePlugin.getCHECK_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());

assertReportFilesExist();
assertCoverage(50.0);
}


@Test
public void checkScoverageFails() throws Exception {

AssertableBuildResult result = runAndFail("clean", ScoveragePlugin.getCHECK_NAME(),
"intTest", "--tests", "org.hello.TestNothingSuite",
"-x", "test");

result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
result.assertTaskFailed(ScoveragePlugin.getCHECK_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());

assertReportFilesExist();
assertCoverage(0.0);
}

@Test
public void reportScoverageWithExcludedClasses() throws Exception {

AssertableBuildResult result = run("clean", "-PexcludedFile=.*", ScoveragePlugin.getREPORT_NAME());

result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getCHECK_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());

Assert.assertTrue(resolve(reportDir(), "index.html").exists());
Assert.assertFalse(resolve(reportDir(), "src/main/scala/org/hello/World.scala.html").exists());
assertCoverage(100.0); // coverage is 100 since no classes are covered

// compiled class should exist in the default classes directory, but not in scoverage
Assert.assertTrue(resolve(buildDir(), "classes/scala/main/org/hello/World.class").exists());
Assert.assertFalse(resolve(buildDir(), "classes/scala/scoverage/org/hello/World.class").exists());
}

@Test
public void reportScoverageWithoutNormalCompilation() throws Exception {

AssertableBuildResult result = run("clean", ScoveragePlugin.getREPORT_NAME(),
"-x", "compileScala");

result.assertTaskSkipped("compileScala");
result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getCHECK_NAME());
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());

assertReportFilesExist();
assertCoverage(100.0);

Assert.assertTrue(resolve(buildDir(), "classes/scala/main/org/hello/World.class").exists());
Assert.assertFalse(resolve(buildDir(), "classes/scala/scoverage/org/hello/World.class").exists());
}

@Test
public void reportScoverageWithoutNormalCompilationAndWithExcludedClasses() throws Exception {

AssertableBuildResult result = run("clean", ScoveragePlugin.getREPORT_NAME(),
"-PexcludedFile=.*", "-x", "compileScala");

Assert.assertTrue(resolve(reportDir(), "index.html").exists());
Assert.assertFalse(resolve(reportDir(), "src/main/scala/org/hello/World.scala.html").exists());
assertCoverage(100.0); // coverage is 100 since no classes are covered

// compiled class should exist in the default classes directory, but not in scoverage
Assert.assertTrue(resolve(buildDir(), "classes/scala/main/org/hello/World.class").exists());
Assert.assertFalse(resolve(buildDir(), "classes/scala/scoverage/org/hello/World.class").exists());
}


private void assertReportFilesExist() {

Assert.assertTrue(resolve(reportDir(), "index.html").exists());
Assert.assertTrue(resolve(reportDir(), "src/main/scala/org/hello/World.scala.html").exists());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
plugins {
id 'io.spring.dependency-management' version "1.0.4.RELEASE"
id 'org.scoverage'
}

repositories {
jcenter()
}

description = 'a single-module Scala project with dependency manager that builds successfully with 100% coverage'

apply plugin: 'scala'


dependencyManagement {
dependencies {
dependency group: 'org.scala-lang', name: 'scala-library', version: "${scalaVersionMajor}.${scalaVersionMinor}.${scalaVersionBuild}"
}
}

dependencies {
compile group: 'org.scala-lang', name: 'scala-library'

// scala compilation with the dependency management plugin needs this (otherwise compilation will fail)
zinc group: 'com.typesafe.zinc', name: 'zinc', version: '0.3.15'
zinc group: 'org.scala-lang', name: 'scala-library', version: '2.10.5'

testRuntime group: 'org.junit.vintage', name: 'junit-vintage-engine', version: junitVersion
testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: junitPlatformVersion

testCompile group: 'org.scalatest', name: "scalatest_${scalaVersionMajor}.${scalaVersionMinor}", version: scalatestVersion
}

test {
useJUnitPlatform()
}


configurations {
intTestCompile.extendsFrom testCompile
intTestRuntime.extendsFrom testRuntime
}
sourceSets {
intTest {
resources.srcDir file('src/intTest/resources')
scala {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file("${projectDir}/src/intTest/scala")
}
}
}

test {
maxParallelForks = 1
}

task intTest(type: Test) {
testClassesDirs = sourceSets.intTest.output.classesDirs
classpath = sourceSets.intTest.runtimeClasspath
outputs.upToDateWhen { false }

maxParallelForks = 1
}
check.dependsOn(intTest)
intTest.mustRunAfter(test)

scoverage {
minimumRate = 0.6
}

if (hasProperty("excludedFile")) {
scoverage.excludedFiles = [excludedFile]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.hello

import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class TestNothingSuite extends FunSuite {

test("nothing") {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hello

import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class WorldIntSuite extends FunSuite {

test("bar") {
new World().bar()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.hello

class World {

def foo(): String = {
val s = "a" + "b"
s
}

def bar(): String = "bar"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hello

import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class WorldSuite extends FunSuite {

test("foo") {
new World().foo()
}
}
7 changes: 3 additions & 4 deletions src/main/groovy/org/scoverage/OverallCheckTask.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.scoverage

import org.apache.tools.ant.taskdefs.Local
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.provider.Property
Expand Down Expand Up @@ -69,12 +68,12 @@ class OverallCheckTask extends DefaultTask {

@VisibleForTesting
protected static String errorMsg(String actual, String expected, CoverageType type) {
return "Only $actual% of project is covered by tests instead of $expected% (coverageType: $type)"
"Only $actual% of project is covered by tests instead of $expected% (coverageType: $type)"
}

@VisibleForTesting
protected static String fileNotFoundErrorMsg(CoverageType coverageType) {
return "Coverage file (type: $coverageType) not found, check your configuration."
"Coverage file (type: $coverageType) not found, check your configuration."
}

@VisibleForTesting
Expand All @@ -100,6 +99,6 @@ class OverallCheckTask extends DefaultTask {
} catch (FileNotFoundException fnfe) {
return new GradleException(fileNotFoundErrorMsg(coverageType), fnfe)
}
return null
null
}
}
20 changes: 19 additions & 1 deletion src/main/groovy/org/scoverage/ScoverageAggregate.groovy
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.scoverage

import org.gradle.api.DefaultTask
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import scala.collection.JavaConverters
import scoverage.IOUtils
import scoverage.report.CoverageAggregator

class ScoverageAggregate extends DefaultTask {
Expand All @@ -14,6 +17,9 @@ class ScoverageAggregate extends DefaultTask {
@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)

Expand All @@ -27,12 +33,24 @@ class ScoverageAggregate extends DefaultTask {
@Input
final Property<Boolean> coverageDebug = project.objects.property(Boolean)

ScoverageAggregate() {
dirsToAggregateFrom.set([])
}

@TaskAction
def aggregate() {
runner.run {
def rootDir = project.projectDir

def coverage = CoverageAggregator.aggregate(rootDir, deleteReportsOnAggregation.get())
def coverage
if (dirsToAggregateFrom.get().isEmpty()) {
coverage = CoverageAggregator.aggregate(rootDir, deleteReportsOnAggregation.get())
} else {
def reportFiles = dirsToAggregateFrom.get().collectMany {
JavaConverters.seqAsJavaList(IOUtils.reportFileSearch(it, IOUtils.isReportFile()))
}
coverage = CoverageAggregator.aggregate(JavaConverters.asScalaBuffer(reportFiles), deleteReportsOnAggregation.get())
}

reportDir.get().deleteDir()

Expand Down
Loading