Skip to content

Commit 1270643

Browse files
authored
Merge pull request #115 from eyalroth/better-scala-version-detection
Better scala version detection
2 parents 924bf49 + 0c01830 commit 1270643

File tree

12 files changed

+138
-38
lines changed

12 files changed

+138
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.scoverage;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.Parameterized;
7+
8+
import java.util.Arrays;
9+
import java.util.Collection;
10+
11+
@RunWith(Parameterized.class)
12+
public class DetectScalaLibraryTest extends ScoverageFunctionalTest {
13+
14+
private static final String SCALA_VERSION = "2.12";
15+
private static final String SCALA_LIBRARY_PARAMETER = "-PdetectedScalaLibraryVersion=";
16+
17+
private static final String EXPECTED_OUTPUT_A = "Detected scala library in compilation classpath";
18+
private static final String EXPECTED_OUTPUT_B = "Using scoverage scalac plugin version '" + SCALA_VERSION;
19+
20+
@Parameterized.Parameter(0)
21+
public String projectDir;
22+
23+
@Parameterized.Parameters(name = "{index}: Project {0} ")
24+
public static Collection<Object[]> data() {
25+
Object[][] data = new Object[][]{{"/compile"}, {"/compileOnly"}, {"/implementation"}, {"/dependency-management"}};
26+
return Arrays.asList(data);
27+
}
28+
29+
public DetectScalaLibraryTest() {
30+
super(null);
31+
}
32+
33+
@Test
34+
public void test() {
35+
setProjectName("detect-scala-library" + projectDir);
36+
testWithParameter(SCALA_LIBRARY_PARAMETER + SCALA_VERSION + ".0");
37+
testWithParameter(SCALA_LIBRARY_PARAMETER + SCALA_VERSION + ".+");
38+
}
39+
40+
private void testWithParameter(String parameter) {
41+
AssertableBuildResult result = dryRun("clean", parameter, "--info");
42+
String output = result.getResult().getOutput();
43+
Assert.assertTrue(output.contains(EXPECTED_OUTPUT_A));
44+
Assert.assertTrue(output.contains(EXPECTED_OUTPUT_B));
45+
}
46+
47+
}

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

-9
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,6 @@ public void reportScoverageWithoutNormalCompilationAndWithExcludedClasses() thro
133133
Assert.assertFalse(resolve(buildDir(), "classes/scala/scoverage/org/hello/World.class").exists());
134134
}
135135

136-
@Test
137-
public void reportScoverageUnder2_11() throws Exception {
138-
run("clean", ScoveragePlugin.getREPORT_NAME(),
139-
"-PscalaVersionMinor=11",
140-
"-PscalaVersionBuild=8",
141-
"-Pscoverage.scoverageScalaVersion=2_11");
142-
assertReportFilesExist();
143-
}
144-
145136
private void assertReportFilesExist() {
146137

147138
Assert.assertTrue(resolve(reportDir(), "index.html").exists());

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

+13-9
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,12 @@
2323

2424
public abstract class ScoverageFunctionalTest {
2525

26-
private final String projectName;
27-
private final GradleRunner runner;
26+
private String projectName;
27+
private GradleRunner runner;
2828
private final XmlParser parser;
2929

3030
protected ScoverageFunctionalTest(String projectName) {
31-
32-
this.projectName = projectName;
33-
this.runner = GradleRunner.create()
34-
.withProjectDir(projectDir())
35-
.withPluginClasspath()
36-
.forwardOutput();
37-
31+
setProjectName(projectName);
3832
try {
3933
this.parser = new XmlParser();
4034
parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
@@ -44,6 +38,16 @@ protected ScoverageFunctionalTest(String projectName) {
4438
}
4539
}
4640

41+
protected void setProjectName(String projectName) {
42+
if (projectName != null) {
43+
this.projectName = projectName;
44+
this.runner = GradleRunner.create()
45+
.withProjectDir(projectDir())
46+
.withPluginClasspath()
47+
.forwardOutput();
48+
}
49+
}
50+
4751
protected File projectDir() {
4852

4953
return new File("src/functionalTest/resources/projects/" + projectName);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
plugins {
2+
id 'org.scoverage'
3+
}
4+
5+
repositories {
6+
jcenter()
7+
}
8+
9+
description = 'defines scala library using the "compile" configuration'
10+
11+
dependencies {
12+
compile group: 'org.scala-lang', name: 'scala-library', version: "${detectedScalaLibraryVersion}"
13+
}

src/functionalTest/resources/projects/detect-scala-library/compile/settings.gradle

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
plugins {
2+
id 'org.scoverage'
3+
}
4+
5+
repositories {
6+
jcenter()
7+
}
8+
9+
description = 'defines scala library using the "compileOnly" configuration'
10+
11+
dependencies {
12+
compileOnly group: 'org.scala-lang', name: 'scala-library', version: "${detectedScalaLibraryVersion}"
13+
}

src/functionalTest/resources/projects/detect-scala-library/compileOnly/settings.gradle

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
plugins {
2+
id 'io.spring.dependency-management' version "1.0.4.RELEASE"
3+
id 'org.scoverage'
4+
}
5+
6+
repositories {
7+
jcenter()
8+
}
9+
10+
description = 'defines scala library using the "implementation" configuration and the dependency-management plugin'
11+
12+
dependencyManagement {
13+
dependencies {
14+
dependency group: 'org.scala-lang', name: 'scala-library', version: "${detectedScalaLibraryVersion}"
15+
}
16+
}
17+
18+
dependencies {
19+
implementation group: 'org.scala-lang', name: 'scala-library'
20+
}

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

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
plugins {
2+
id 'org.scoverage'
3+
}
4+
5+
repositories {
6+
jcenter()
7+
}
8+
9+
description = 'defines scala library using the "implementation" configuration'
10+
11+
dependencies {
12+
implementation group: 'org.scala-lang', name: 'scala-library', version: "${detectedScalaLibraryVersion}"
13+
}

src/functionalTest/resources/projects/detect-scala-library/implementation/settings.gradle

Whitespace-only changes.

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

+19-20
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,8 @@ class ScoveragePlugin implements Plugin<PluginAware> {
6363
}
6464

6565
project.afterEvaluate {
66+
def scalaVersion = resolveScalaVersion(project)
6667
def scoverageVersion = project.extensions.scoverage.scoverageVersion.get()
67-
def scalaVersion = null
68-
69-
def scalaLibrary = project.configurations.compile.dependencies.find {
70-
it.group == "org.scala-lang" && it.name == "scala-library"
71-
}
72-
73-
if (scalaLibrary != null) {
74-
scalaVersion = scalaLibrary.version
75-
}
76-
77-
if (scalaVersion == null && project.pluginManager.hasPlugin("io.spring.dependency-management")) {
78-
scalaVersion = project.dependencyManagement.compile.managedVersions["org.scala-lang:scala-library"]
79-
}
80-
81-
if (scalaVersion == null) {
82-
scalaVersion = project.extensions.scoverage.scoverageScalaVersion.get()
83-
} else {
84-
scalaVersion = scalaVersion.substring(0, scalaVersion.lastIndexOf("."))
85-
}
86-
8768
def fullScoverageVersion = "$scalaVersion:$scoverageVersion"
8869

8970
project.logger.info("Using scoverage scalac plugin version '$fullScoverageVersion'")
@@ -319,6 +300,24 @@ class ScoveragePlugin implements Plugin<PluginAware> {
319300
}
320301
}
321302

303+
private String resolveScalaVersion(Project project) {
304+
305+
def resolvedDependencies = project.configurations.compileClasspath.resolvedConfiguration.firstLevelModuleDependencies
306+
307+
def scalaLibrary = resolvedDependencies.find {
308+
it.moduleGroup == "org.scala-lang" && it.moduleName == "scala-library"
309+
}
310+
311+
if (scalaLibrary == null) {
312+
project.logger.info("No scala library detected. Using property 'scoverageScalaVersion'")
313+
return project.extensions.scoverage.scoverageScalaVersion.get()
314+
} else {
315+
project.logger.info("Detected scala library in compilation classpath")
316+
def fullScalaVersion = scalaLibrary.moduleVersion
317+
return fullScalaVersion.substring(0, fullScalaVersion.lastIndexOf("."))
318+
}
319+
}
320+
322321
private Set<? extends Task> recursiveDependenciesOf(Task task) {
323322
if (!taskDependencies.containsKey(task)) {
324323
def directDependencies = task.getTaskDependencies().getDependencies(task)

0 commit comments

Comments
 (0)