Skip to content

Commit 2364c64

Browse files
authored
Merge pull request #205 from msigmond/master
Add support for excludedFiles and excludedPackages for Scala 3
2 parents f8ea2e0 + 04b360f commit 2364c64

File tree

16 files changed

+150
-33
lines changed

16 files changed

+150
-33
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ You can find instructions on how to apply the plugin at http://plugins.gradle.or
4343
The plugin exposes multiple options that can be configured by setting them in an `scoverage` block within the project's
4444
build script. These options are as follows:
4545

46-
* `scoverageVersion = <String>` (default `"1.4.8`): The version of the scoverage scalac plugin. This (gradle) plugin
46+
* `scoverageVersion = <String>` (default `"2.1.1`): The version of the scoverage scalac plugin. This (gradle) plugin
4747
should be compatible with all 1+ versions.
4848

4949
* `scoverageScalaVersion = <String>` (default `detected`): The scala version of the scoverage scalac plugin. This
@@ -64,6 +64,10 @@ required for the validation to pass (otherwise `checkScoverage` will fail the bu
6464
`checkScoverage` task. For more information on the different types, please refer to the documentation of the scalac
6565
plugin (https://github.com/scoverage/scalac-scoverage-plugin).
6666

67+
* `excludedFiles = <files>` (default `not set`): Comma separated list of regexes for files to exclude from coverage.
68+
69+
* `excludedPackages = <packages, classes and modules>` (default `not set`): Comma separated list of regexes for packages, classes and modules to exclude from coverage.
70+
6771
#### Multiple check tasks
6872

6973
It is possible to configure multiple checks; for instance, one check for a statement rate and another for a branch rate:

build.gradle

+7-5
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ gradlePlugin {
3838
apply plugin: 'maven-publish'
3939
apply plugin: 'groovy'
4040

41-
sourceCompatibility = '1.8'
42-
targetCompatibility = '1.8'
43-
41+
java {
42+
sourceCompatibility = JavaVersion.VERSION_1_8
43+
targetCompatibility = JavaVersion.VERSION_1_8
44+
}
4445

4546
dependencies {
46-
compileOnly "org.scoverage:scalac-scoverage-plugin_2.13.8:2.0.7"
47-
compileOnly "org.scoverage:scalac-scoverage-reporter_2.13:2.0.7"
47+
compileOnly 'org.scoverage:scalac-scoverage-plugin_2.13.14:2.1.1'
48+
compileOnly 'org.scoverage:scalac-scoverage-reporter_2.13:2.1.1'
49+
4850
implementation group: 'commons-io', name: 'commons-io', version: '2.6'
4951

5052
testImplementation 'junit:junit:4.12'
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
4-
distributionSha256Sum=e111cb9948407e26351227dabce49822fb88c37ee72f1d1582a69c68af2e702f
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
4+
distributionSha256Sum=a4b4158601f8636cdeeab09bd76afb640030bb5b144aafe261a5e8af027dc612
55
zipStoreBase=GRADLE_USER_HOME
66
zipStorePath=wrapper/dists
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dependencies {
2-
implementation 'org.scala-lang:scala3-library_3:3.2.0'
3-
testImplementation 'org.scalatest:scalatest_3:3.2.14'
4-
testImplementation "org.scalatestplus:junit-4-13_3:3.2.14.0"
2+
implementation 'org.scala-lang:scala3-library_3:3.4.2'
3+
testImplementation 'org.scalatest:scalatest_3:3.2.16'
4+
testImplementation "org.scalatestplus:junit-4-13_3:3.2.16.0"
55
}

src/functionalTest/java/org/scoverage/ScalaSingleModuleTest.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
import org.junit.Ignore;
55
import org.junit.Test;
66

7+
import java.util.List;
8+
79
public class ScalaSingleModuleTest extends ScoverageFunctionalTest {
810

911
public ScalaSingleModuleTest() {
1012
super("scala-single-module");
1113
}
1214

15+
@Override
16+
protected List<String> getVersionAgruments() {
17+
return ScalaVersionArguments.version2;
18+
}
19+
1320
@Test
1421
public void test() {
1522

@@ -135,7 +142,7 @@ public void reportScoverageWithoutNormalCompilationAndWithExcludedClasses() thro
135142
Assert.assertFalse(resolve(buildDir(), "classes/scala/scoverage/org/hello/World.class").exists());
136143
}
137144

138-
private void assertReportFilesExist() {
145+
protected void assertReportFilesExist() {
139146

140147
Assert.assertTrue(resolve(reportDir(), "index.html").exists());
141148
Assert.assertTrue(resolve(reportDir(), "org/hello/World.scala.html").exists());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.scoverage;
2+
3+
import org.junit.Assert;
4+
5+
import java.util.List;
6+
7+
public class ScalaSingleModuleTestScala3 extends ScalaSingleModuleTest {
8+
9+
@Override
10+
protected List<String> getVersionAgruments() {
11+
return ScalaVersionArguments.version3;
12+
}
13+
14+
@Override
15+
public void checkScoverage() throws Exception {
16+
AssertableBuildResult result = run("clean", ScoveragePlugin.getCHECK_NAME());
17+
18+
result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
19+
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
20+
result.assertTaskSucceeded(ScoveragePlugin.getCHECK_NAME());
21+
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
22+
23+
assertReportFilesExist();
24+
assertCoverage(66.67);
25+
}
26+
27+
@Override
28+
public void reportScoverageWithExcludedClasses() throws Exception {
29+
AssertableBuildResult result = run("clean", ScoveragePlugin.getREPORT_NAME(),
30+
"-PexcludedFile=.*");
31+
32+
result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
33+
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
34+
result.assertTaskDoesntExist(ScoveragePlugin.getCHECK_NAME());
35+
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
36+
37+
Assert.assertTrue(resolve(reportDir(), "index.html").exists());
38+
Assert.assertFalse(resolve(reportDir(), "org/hello/World.scala.html").exists());
39+
assertCoverage(100.0); // coverage is 100 since no classes are covered
40+
41+
// compiled class should exist in the default classes directory, but not in scoverage
42+
Assert.assertTrue(resolve(buildDir(), "classes/scala/main/org/hello/World.class").exists());
43+
}
44+
45+
@Override
46+
public void reportScoverageWithoutNormalCompilationAndWithExcludedClasses() throws Exception {
47+
AssertableBuildResult result = run("clean", ScoveragePlugin.getREPORT_NAME(),
48+
"-PexcludedFile=.*", "-P" + ScoveragePlugin.getSCOVERAGE_COMPILE_ONLY_PROPERTY());
49+
50+
Assert.assertTrue(resolve(reportDir(), "index.html").exists());
51+
Assert.assertFalse(resolve(reportDir(), "org/hello/World.scala.html").exists());
52+
assertCoverage(100.0); // coverage is 100 since no classes are covered
53+
54+
// compiled class should exist in the default classes directory, but not in scoverage
55+
Assert.assertTrue(resolve(buildDir(), "classes/scala/main/org/hello/World.class").exists());
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.scoverage;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public interface ScalaVersionArguments {
7+
List<String> version2WithLegacyScalatest = Arrays.asList(
8+
"-PscalaVersionMajor=2",
9+
"-PscalaVersionMinor=13",
10+
"-PscalaVersionBuild=14",
11+
"-PjunitVersion=5.3.2",
12+
"-PjunitPlatformVersion=1.3.2",
13+
"-PscalatestVersion=3.0.8"
14+
);
15+
16+
List<String> version2 = Arrays.asList(
17+
"-PscalaVersionMajor=2",
18+
"-PscalaVersionMinor=13",
19+
"-PscalaVersionBuild=14",
20+
"-PjunitVersion=5.3.2",
21+
"-PjunitPlatformVersion=1.3.2",
22+
"-PscalatestVersion=3.2.16"
23+
);
24+
25+
List<String> version3 = Arrays.asList(
26+
"-PscalaVersionMajor=3",
27+
"-PscalaVersionMinor=4",
28+
"-PscalaVersionBuild=2",
29+
"-PjunitVersion=5.3.2",
30+
"-PjunitPlatformVersion=1.3.2",
31+
"-PscalatestVersion=3.2.16"
32+
);
33+
}

src/functionalTest/java/org/scoverage/ScoverageFunctionalTest.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,14 @@ private Double coverage(File reportDir, CoverageType coverageType) throws IOExce
115115
return coverageType.normalize(rawValue) * 100.0;
116116
}
117117

118+
protected List<String> getVersionAgruments() {
119+
return ScalaVersionArguments.version2WithLegacyScalatest;
120+
}
121+
118122
private void configureArguments(String... arguments) {
119123

120-
List<String> fullArguments = new ArrayList<>();
124+
List<String> fullArguments = new ArrayList<>(getVersionAgruments());
121125

122-
fullArguments.add("-PscalaVersionMajor=2");
123-
fullArguments.add("-PscalaVersionMinor=13");
124-
fullArguments.add("-PscalaVersionBuild=10");
125-
fullArguments.add("-PjunitVersion=5.3.2");
126-
fullArguments.add("-PjunitPlatformVersion=1.3.2");
127-
fullArguments.add("-PscalatestVersion=3.0.8");
128126
if (Boolean.parseBoolean(System.getProperty("failOnWarning"))) {
129127
fullArguments.add("--warning-mode=fail");
130128
} else {

src/functionalTest/resources/projects/detect-scala-library/dependency-management/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id 'io.spring.dependency-management' version "1.0.4.RELEASE"
2+
id 'io.spring.dependency-management' version "1.1.5"
33
id 'org.scoverage'
44
}
55

src/functionalTest/resources/projects/scala-single-module-dependency-manager/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id 'io.spring.dependency-management' version "1.0.4.RELEASE"
2+
id 'io.spring.dependency-management' version "1.1.5"
33
id 'org.scoverage'
44
}
55

src/functionalTest/resources/projects/scala-single-module-multiple-test-tasks/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id 'io.spring.dependency-management' version "1.0.4.RELEASE"
2+
id 'io.spring.dependency-management' version "1.1.5"
33
id 'org.scoverage'
44
id 'jvm-test-suite'
55
}

src/functionalTest/resources/projects/scala-single-module/build.gradle

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ apply plugin: 'java'
1212
apply plugin: 'scala'
1313

1414
dependencies {
15-
implementation group: 'org.scala-lang', name: 'scala-library', version: "${scalaVersionMajor}.${scalaVersionMinor}.${scalaVersionBuild}"
15+
if (project.getProperties().get("scalaVersionMajor").equals("2")) {
16+
implementation group: 'org.scala-lang', name: 'scala-library', version: "${scalaVersionMajor}.${scalaVersionMinor}.${scalaVersionBuild}"
17+
18+
testImplementation group: 'org.scalatest', name: 'scalatest_2.13', version: scalatestVersion
19+
testImplementation group: 'org.scalatestplus', name: 'junit-4-13_2.13', version: "${scalatestVersion}.0"
20+
} else {
21+
implementation group: 'org.scala-lang', name: 'scala3-library_3', version: "${scalaVersionMajor}.${scalaVersionMinor}.${scalaVersionBuild}"
22+
23+
testImplementation group: 'org.scalatest', name: 'scalatest_3', version: scalatestVersion
24+
testImplementation group: 'org.scalatestplus', name: 'junit-4-13_3', version: "${scalatestVersion}.0"
25+
}
1626

1727
testRuntimeOnly group: 'org.junit.vintage', name: 'junit-vintage-engine', version: junitVersion
1828
testImplementation group: 'org.junit.platform', name: 'junit-platform-runner', version: junitPlatformVersion
19-
20-
testImplementation group: 'org.scalatest', name: "scalatest_${scalaVersionMajor}.${scalaVersionMinor}", version: scalatestVersion
2129
}
2230

2331
test {

src/functionalTest/resources/projects/scala-single-module/src/test/scala/org/hello/TestNothingSuite.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.hello
22

33
import org.junit.runner.RunWith
4-
import org.scalatest.FunSuite
5-
import org.scalatest.junit.JUnitRunner
4+
import org.scalatest.funsuite._
5+
import org.scalatestplus.junit.JUnitRunner
66

77
@RunWith(classOf[JUnitRunner])
8-
class TestNothingSuite extends FunSuite {
8+
class TestNothingSuite extends AnyFunSuite {
99

1010
test("nothing") {
1111
}

src/functionalTest/resources/projects/scala-single-module/src/test/scala/org/hello/WorldSuite.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.hello
22

33
import org.junit.runner.RunWith
4-
import org.scalatest.FunSuite
5-
import org.scalatest.junit.JUnitRunner
4+
import org.scalatest.funsuite._
5+
import org.scalatestplus.junit.JUnitRunner
66

77
@RunWith(classOf[JUnitRunner])
8-
class WorldSuite extends FunSuite {
8+
class WorldSuite extends AnyFunSuite {
99

1010
test("foo") {
1111
new World().foo()

src/main/groovy/org/scoverage/ScoverageExtension.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class ScoverageExtension {
5555
project.plugins.apply(ScalaPlugin.class)
5656

5757
scoverageVersion = project.objects.property(String)
58-
scoverageVersion.set('2.0.8')
58+
scoverageVersion.set('2.1.1')
5959

6060
scoverageScalaVersion = project.objects.property(String)
6161

src/main/groovy/org/scoverage/ScoveragePlugin.groovy

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ScoveragePlugin implements Plugin<PluginAware> {
2424
static final String CHECK_NAME = 'checkScoverage'
2525
static final String COMPILE_NAME = 'compileScoverageScala'
2626
static final String AGGREGATE_NAME = 'aggregateScoverage'
27-
static final String DEFAULT_SCALA_VERSION = '2.13.6'
27+
static final String DEFAULT_SCALA_VERSION = '2.13.14'
2828
static final String SCOVERAGE_COMPILE_ONLY_PROPERTY = 'scoverageCompileOnly';
2929

3030
static final String DEFAULT_REPORT_DIR = 'reports' + File.separatorChar + 'scoverage'
@@ -206,6 +206,14 @@ class ScoveragePlugin implements Plugin<PluginAware> {
206206
} else {
207207
parameters.add("-sourceroot:${project.rootDir.absolutePath}".toString())
208208
parameters.add("-coverage-out:${extension.dataDir.get().absolutePath}".toString())
209+
if (extension.excludedPackages.get()) {
210+
def packages = extension.excludedPackages.get().join(',')
211+
parameters.add("-coverage-exclude-classlikes:$packages".toString())
212+
}
213+
if (extension.excludedFiles.get()) {
214+
def packages = extension.excludedFiles.get().join(';')
215+
parameters.add("-coverage-exclude-files:$packages".toString())
216+
}
209217
scalaCompileOptions.additionalParameters = parameters
210218
}
211219
}

0 commit comments

Comments
 (0)