Skip to content

Commit 0c01830

Browse files
committed
Resolve scala library version according to the resolved compilation classpath
This commit adds support for any plugin which manipulates the configuration via resolution strategy.
1 parent b0c01a7 commit 0c01830

File tree

6 files changed

+35
-30
lines changed

6 files changed

+35
-30
lines changed

src/functionalTest/java/org.scoverage/DetectScalaLibraryTest.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
@RunWith(Parameterized.class)
1212
public class DetectScalaLibraryTest extends ScoverageFunctionalTest {
1313

14-
private static final String SCALA_VERSION = "0.0";
15-
private static final String SCALA_LIBRARY_PARAMETER = "-PdetectedScalaLibraryVersion=" + SCALA_VERSION + ".0";
16-
private static final String EXPECTED_OUTPUT = "Using scoverage scalac plugin version '" + SCALA_VERSION;
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;
1719

1820
@Parameterized.Parameter(0)
1921
public String projectDir;
@@ -31,13 +33,15 @@ public DetectScalaLibraryTest() {
3133
@Test
3234
public void test() {
3335
setProjectName("detect-scala-library" + projectDir);
36+
testWithParameter(SCALA_LIBRARY_PARAMETER + SCALA_VERSION + ".0");
37+
testWithParameter(SCALA_LIBRARY_PARAMETER + SCALA_VERSION + ".+");
38+
}
3439

35-
// build supposed to fail since repositories are not configured
36-
AssertableBuildResult result = runAndFail("clean", SCALA_LIBRARY_PARAMETER, "--info");
37-
38-
// all we want to know is that the plugin detected our configured library version
40+
private void testWithParameter(String parameter) {
41+
AssertableBuildResult result = dryRun("clean", parameter, "--info");
3942
String output = result.getResult().getOutput();
40-
Assert.assertTrue(output.contains(EXPECTED_OUTPUT));
43+
Assert.assertTrue(output.contains(EXPECTED_OUTPUT_A));
44+
Assert.assertTrue(output.contains(EXPECTED_OUTPUT_B));
4145
}
4246

4347
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ plugins {
22
id 'org.scoverage'
33
}
44

5+
repositories {
6+
jcenter()
7+
}
8+
59
description = 'defines scala library using the "compile" configuration'
610

711
dependencies {

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

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ plugins {
22
id 'org.scoverage'
33
}
44

5+
repositories {
6+
jcenter()
7+
}
8+
59
description = 'defines scala library using the "compileOnly" configuration'
610

711
dependencies {

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

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ plugins {
33
id 'org.scoverage'
44
}
55

6+
repositories {
7+
jcenter()
8+
}
9+
610
description = 'defines scala library using the "implementation" configuration and the dependency-management plugin'
711

812
dependencyManagement {

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

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ plugins {
22
id 'org.scoverage'
33
}
44

5+
repositories {
6+
jcenter()
7+
}
8+
59
description = 'defines scala library using the "implementation" configuration'
610

711
dependencies {

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

+7-22
Original file line numberDiff line numberDiff line change
@@ -301,36 +301,21 @@ class ScoveragePlugin implements Plugin<PluginAware> {
301301
}
302302

303303
private String resolveScalaVersion(Project project) {
304-
def scalaVersion = null
305304

306-
def configurations = [
307-
project.configurations.compile,
308-
project.configurations.compileOnly,
309-
project.configurations.implementation
310-
]
311-
def dependencies = configurations.collectMany { it.dependencies }
305+
def resolvedDependencies = project.configurations.compileClasspath.resolvedConfiguration.firstLevelModuleDependencies
312306

313-
def scalaLibrary = dependencies.find {
314-
it.group == "org.scala-lang" && it.name == "scala-library"
307+
def scalaLibrary = resolvedDependencies.find {
308+
it.moduleGroup == "org.scala-lang" && it.moduleName == "scala-library"
315309
}
316310

317-
if (scalaLibrary != null) {
318-
scalaVersion = scalaLibrary.version
319-
}
320-
321-
if (scalaVersion == null && project.pluginManager.hasPlugin("io.spring.dependency-management")) {
322-
scalaVersion = project.dependencyManagement.compile.managedVersions["org.scala-lang:scala-library"]
323-
}
324-
325-
if (scalaVersion == null) {
311+
if (scalaLibrary == null) {
326312
project.logger.info("No scala library detected. Using property 'scoverageScalaVersion'")
327-
scalaVersion = project.extensions.scoverage.scoverageScalaVersion.get()
313+
return project.extensions.scoverage.scoverageScalaVersion.get()
328314
} else {
329315
project.logger.info("Detected scala library in compilation classpath")
330-
scalaVersion = scalaVersion.substring(0, scalaVersion.lastIndexOf("."))
316+
def fullScalaVersion = scalaLibrary.moduleVersion
317+
return fullScalaVersion.substring(0, fullScalaVersion.lastIndexOf("."))
331318
}
332-
333-
return scalaVersion
334319
}
335320

336321
private Set<? extends Task> recursiveDependenciesOf(Task task) {

0 commit comments

Comments
 (0)