Skip to content

Commit d6f8cc5

Browse files
author
Oleksii Lisikh
committed
Issue #102: Honor java sources when running compileScoverageScala
1 parent 4e29e14 commit d6f8cc5

File tree

8 files changed

+229
-0
lines changed

8 files changed

+229
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package org.scoverage;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class ScalaSingleMultiLangModuleTest extends ScoverageFunctionalTest {
7+
8+
public ScalaSingleMultiLangModuleTest() { super("scala-single-multi-lang-module"); }
9+
10+
@Test
11+
public void test() {
12+
13+
AssertableBuildResult result = dryRun("clean", "test");
14+
15+
result.assertTaskDoesntExist(ScoveragePlugin.getCOMPILE_NAME());
16+
result.assertTaskDoesntExist(ScoveragePlugin.getREPORT_NAME());
17+
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
18+
result.assertTaskDoesntExist(ScoveragePlugin.getCHECK_NAME());
19+
}
20+
21+
@Test
22+
public void build() {
23+
24+
AssertableBuildResult result = dryRun("clean", "build");
25+
26+
result.assertTaskDoesntExist(ScoveragePlugin.getCOMPILE_NAME());
27+
result.assertTaskDoesntExist(ScoveragePlugin.getREPORT_NAME());
28+
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
29+
result.assertTaskDoesntExist(ScoveragePlugin.getCHECK_NAME());
30+
}
31+
32+
@Test
33+
public void reportScoverage() {
34+
35+
AssertableBuildResult result = dryRun("clean", ScoveragePlugin.getREPORT_NAME());
36+
37+
result.assertTaskExists(ScoveragePlugin.getCOMPILE_NAME());
38+
result.assertTaskExists(ScoveragePlugin.getREPORT_NAME());
39+
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
40+
result.assertTaskDoesntExist(ScoveragePlugin.getCHECK_NAME());
41+
}
42+
43+
@Test
44+
public void aggregateScoverage() {
45+
46+
AssertableBuildResult result = runAndFail("clean", ScoveragePlugin.getAGGREGATE_NAME());
47+
48+
result.assertNoTasks();
49+
}
50+
51+
@Test
52+
public void checkScoverage() throws Exception {
53+
54+
AssertableBuildResult result = run("clean", ScoveragePlugin.getCHECK_NAME());
55+
56+
result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
57+
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
58+
result.assertTaskSucceeded(ScoveragePlugin.getCHECK_NAME());
59+
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
60+
61+
assertReportFilesExist();
62+
assertCoverage(66.7);
63+
}
64+
65+
@Test
66+
public void checkScoverageFails() throws Exception {
67+
68+
AssertableBuildResult result = runAndFail("clean", ScoveragePlugin.getCHECK_NAME(),
69+
"test", "--tests", "org.hello.TestNothingSuite");
70+
71+
result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
72+
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
73+
result.assertTaskFailed(ScoveragePlugin.getCHECK_NAME());
74+
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
75+
76+
assertReportFilesExist();
77+
assertCoverage(0.0);
78+
}
79+
80+
@Test
81+
public void reportScoverageWithExcludedClasses() throws Exception {
82+
83+
AssertableBuildResult result = run("clean", ScoveragePlugin.getREPORT_NAME(),
84+
"-PexcludedFile=.*");
85+
86+
result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
87+
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
88+
result.assertTaskDoesntExist(ScoveragePlugin.getCHECK_NAME());
89+
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
90+
91+
Assert.assertTrue(resolve(reportDir(), "index.html").exists());
92+
Assert.assertFalse(resolve(reportDir(), "src/main/scala/org/hello/World.scala.html").exists());
93+
assertCoverage(100.0); // coverage is 100 since no classes are covered
94+
95+
// compiled class should exist in the default classes directory, but not in scoverage
96+
Assert.assertTrue(resolve(buildDir(), "classes/scala/main/org/hello/World.class").exists());
97+
Assert.assertFalse(resolve(buildDir(), "classes/scala/scoverage/org/hello/World.class").exists());
98+
}
99+
100+
@Test
101+
public void reportScoverageWithoutNormalCompilation() throws Exception {
102+
103+
AssertableBuildResult result = run("clean", ScoveragePlugin.getREPORT_NAME(),
104+
"-x", "compileScala");
105+
106+
result.assertTaskSkipped("compileScala");
107+
result.assertTaskSucceeded(ScoveragePlugin.getCOMPILE_NAME());
108+
result.assertTaskSucceeded(ScoveragePlugin.getREPORT_NAME());
109+
result.assertTaskDoesntExist(ScoveragePlugin.getCHECK_NAME());
110+
result.assertTaskDoesntExist(ScoveragePlugin.getAGGREGATE_NAME());
111+
112+
assertReportFilesExist();
113+
assertCoverage(66.7);
114+
115+
Assert.assertTrue(resolve(reportDir(), "index.html").exists());
116+
Assert.assertTrue(resolve(reportDir(), "src/main/scala/org/hello/World.scala.html").exists());
117+
Assert.assertFalse(resolve(reportDir(), "src/main/java/org/hello/JavaWorld.java.html").exists());
118+
119+
Assert.assertTrue(resolve(buildDir(), "classes/java/main/org/hello/JavaWorld.class").exists());
120+
Assert.assertTrue(resolve(buildDir(), "classes/java/scoverage/org/hello/JavaWorld.class").exists());
121+
Assert.assertTrue(resolve(buildDir(), "classes/scala/main/org/hello/World.class").exists());
122+
Assert.assertFalse(resolve(buildDir(), "classes/scala/scoverage/org/hello/World.class").exists());
123+
}
124+
125+
@Test
126+
public void reportScoverageWithoutNormalCompilationAndWithExcludedClasses() throws Exception {
127+
128+
AssertableBuildResult result = run("clean", ScoveragePlugin.getREPORT_NAME(),
129+
"-PexcludedFile=.*", "-x", "compileScala");
130+
131+
Assert.assertTrue(resolve(reportDir(), "index.html").exists());
132+
Assert.assertFalse(resolve(reportDir(), "src/main/scala/org/hello/World.scala.html").exists());
133+
assertCoverage(100.0); // coverage is 100 since no classes are covered
134+
135+
// compiled class should exist in the default classes directory, but not in scoverage
136+
Assert.assertTrue(resolve(buildDir(), "classes/scala/main/org/hello/World.class").exists());
137+
Assert.assertFalse(resolve(buildDir(), "classes/scala/scoverage/org/hello/World.class").exists());
138+
}
139+
140+
141+
private void assertReportFilesExist() {
142+
Assert.assertTrue(resolve(reportDir(), "index.html").exists());
143+
Assert.assertTrue(resolve(reportDir(), "src/main/scala/org/hello/World.scala.html").exists());
144+
}
145+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
plugins {
2+
id 'org.scoverage'
3+
}
4+
5+
repositories {
6+
jcenter()
7+
}
8+
9+
description = 'a single-module Scala project with java and scala sources'
10+
11+
apply plugin: 'java'
12+
apply plugin: 'scala'
13+
14+
dependencies {
15+
compile group: 'org.scala-lang', name: 'scala-library', version: "${scalaVersionMajor}.${scalaVersionMinor}.${scalaVersionBuild}"
16+
17+
testRuntime group: 'org.junit.vintage', name: 'junit-vintage-engine', version: junitVersion
18+
testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: junitPlatformVersion
19+
20+
testCompile group: 'org.scalatest', name: "scalatest_${scalaVersionMajor}.${scalaVersionMinor}", version: scalatestVersion
21+
}
22+
23+
test {
24+
useJUnitPlatform()
25+
}
26+
27+
scoverage {
28+
minimumRate = 0.3
29+
}
30+
31+
if (hasProperty("excludedFile")) {
32+
scoverage.excludedFiles = [excludedFile]
33+
}

Diff for: src/functionalTest/resources/projects/scala-single-multi-lang-module/settings.gradle

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.hello;
2+
3+
public class JavaWorld {
4+
public static void hello() {
5+
System.out.println("Hello, World!");
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.hello
2+
3+
class World {
4+
5+
def foo(): String = {
6+
val s = "a" + "b"
7+
s
8+
}
9+
10+
// not covered by tests
11+
def bar(): String = "y"
12+
13+
def helloFromJava(): Unit = JavaWorld.hello()
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.hello
2+
3+
import org.junit.runner.RunWith
4+
import org.scalatest.FunSuite
5+
import org.scalatest.junit.JUnitRunner
6+
7+
@RunWith(classOf[JUnitRunner])
8+
class TestNothingSuite extends FunSuite {
9+
10+
test("nothing") {
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.hello
2+
3+
import org.junit.runner.RunWith
4+
import org.scalatest.FunSuite
5+
import org.scalatest.junit.JUnitRunner
6+
7+
@RunWith(classOf[JUnitRunner])
8+
class WorldSuite extends FunSuite {
9+
10+
test("foo") {
11+
new World().foo()
12+
}
13+
14+
test("helloFromJava") {
15+
new World().helloFromJava()
16+
}
17+
}

Diff for: src/main/groovy/org/scoverage/ScoveragePlugin.groovy

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class ScoveragePlugin implements Plugin<PluginAware> {
106106
def instrumentedSourceSet = project.sourceSets.create('scoverage') {
107107

108108
resources.source(originalSourceSet.resources)
109+
java.source(originalSourceSet.java)
109110
scala.source(originalSourceSet.scala)
110111

111112
compileClasspath += originalSourceSet.compileClasspath + project.configurations.scoverage

0 commit comments

Comments
 (0)