Skip to content

Commit 02a2a36

Browse files
authored
Merge pull request #143 from eyalroth/feature/140/gradle-consistent-versions-compat
Make the plugin compatible with Palantir's consistent versions plugin
2 parents bc3da6f + cc9bdaa commit 02a2a36

File tree

8 files changed

+101
-33
lines changed

8 files changed

+101
-33
lines changed

Diff for: README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ build script. These options are as follows:
4646
* `scoverageVersion = <String>` (default `"1.4.1"`): The version of the scoverage scalac plugin. This (gradle) plugin
4747
should be compatible with all 1+ versions.
4848

49-
* `scoverageScalaVersion = <String>` (default `"2.12"`): The scala version of the scoverage scalac plugin. This will
50-
be overridden by the version of the `scala-library` compile dependency (if the dependency is configured).
49+
* `scoverageScalaVersion = <String>` (default `"2.12"`): The scala version of the scoverage scalac plugin. This
50+
overrides the version of the `scala-library` compile dependency (if the dependency is configured).
5151

5252
* `coverageOutputCobertura = <boolean>` (default `true`): Enables/disables cobertura.xml file generation (for both aggregated and non-aggregated reports).
5353

@@ -115,6 +115,13 @@ it is possible to only compile the code with the scoverage scalac plugin, thus r
115115
In order to do so, simply add the arguments `-x compileScala` to the gradle execution.
116116
For example: `gradle reportScoverage -x compileScala`.
117117

118+
119+
### Compatibility with Consistent Versions Plugin
120+
121+
In order for the plugin to work alongside [Palantir's consistent versions plugin](https://github.com/palantir/gradle-consistent-versions),
122+
the Scala version must be manually configured (via `scoverageScalaVersion`); otherwise, the plugin will attempt to
123+
resolve the compilation classpath, which is prohibited by the versions plugin.
124+
118125
Migration to 4.x
119126
----------------
120127

Diff for: src/functionalTest/java/org/scoverage/DetectScalaLibraryTest.java

+37-9
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,40 @@
77

88
import java.util.Arrays;
99
import java.util.Collection;
10+
import java.util.List;
11+
import java.util.stream.Stream;
1012

1113
@RunWith(Parameterized.class)
1214
public class DetectScalaLibraryTest extends ScoverageFunctionalTest {
1315

1416
private static final String SCALA_VERSION = "2.12";
1517
private static final String SCALA_LIBRARY_PARAMETER = "-PdetectedScalaLibraryVersion=";
1618

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+
private static final String EXPECTED_OUTPUT_CONFIGURED_VERSION = "Using configured Scala version";
20+
private static final String EXPECTED_OUTPUT_DETECTED_VERSION = "Detected scala library in compilation classpath";
21+
private static final String EXPECTED_OUTPUT_USING = "Using scoverage scalac plugin version '" + SCALA_VERSION;
1922

2023
@Parameterized.Parameter(0)
2124
public String projectDir;
2225

26+
@Parameterized.Parameter(1)
27+
public String[] subVersions;
28+
29+
@Parameterized.Parameter(2)
30+
public boolean detect;
31+
32+
@Parameterized.Parameter(3)
33+
public String[] additionalParameters;
34+
2335
@Parameterized.Parameters(name = "{index}: Project {0} ")
2436
public static Collection<Object[]> data() {
25-
Object[][] data = new Object[][]{{"/compile"}, {"/compileOnly"}, {"/implementation"}, {"/dependency-management"}};
37+
Object[][] data = new Object[][]{
38+
{"/compile", new String[] {".0", ".+"}, true, new String[0]},
39+
{"/compileOnly", new String[] {".0", ".+"}, true, new String[0]},
40+
{"/implementation", new String[] {".0", ".+"}, true, new String[0]},
41+
{"/dependency-management", new String[] {".0", ".+"}, true, new String[0]},
42+
{"/gradle-consistent-versions", new String[] {"ignored"}, false, new String[] {"--write-locks"}},
43+
};
2644
return Arrays.asList(data);
2745
}
2846

@@ -33,15 +51,25 @@ public DetectScalaLibraryTest() {
3351
@Test
3452
public void test() {
3553
setProjectName("detect-scala-library" + projectDir);
36-
testWithParameter(SCALA_LIBRARY_PARAMETER + SCALA_VERSION + ".0");
37-
testWithParameter(SCALA_LIBRARY_PARAMETER + SCALA_VERSION + ".+");
54+
for (String subVersion : subVersions) {
55+
testWithParameter(SCALA_LIBRARY_PARAMETER + SCALA_VERSION + subVersion, detect);
56+
}
3857
}
3958

40-
private void testWithParameter(String parameter) {
41-
AssertableBuildResult result = dryRun("clean", parameter, "--info");
59+
private void testWithParameter(String parameter, Boolean detect) {
60+
61+
String[] basicParameters = {"clean", parameter, "--info"};
62+
String[] parameters = Stream.concat(Arrays.stream(basicParameters), Arrays.stream(additionalParameters))
63+
.toArray(String[]::new);
64+
AssertableBuildResult result = dryRun(parameters);
65+
4266
String output = result.getResult().getOutput();
43-
Assert.assertTrue(output.contains(EXPECTED_OUTPUT_A));
44-
Assert.assertTrue(output.contains(EXPECTED_OUTPUT_B));
67+
if (detect) {
68+
Assert.assertTrue(output.contains(EXPECTED_OUTPUT_DETECTED_VERSION));
69+
} else {
70+
Assert.assertTrue(output.contains(EXPECTED_OUTPUT_CONFIGURED_VERSION));
71+
}
72+
Assert.assertTrue(output.contains(EXPECTED_OUTPUT_USING));
4573
}
4674

4775
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
plugins {
2+
id "com.palantir.consistent-versions" version "1.16.0"
3+
id 'org.scoverage'
4+
}
5+
6+
repositories {
7+
jcenter()
8+
}
9+
10+
description = 'defines scala library using the "implementation" configuration and the gradle-consistent-versions plugin'
11+
12+
dependencies {
13+
implementation group: 'org.scala-lang', name: 'scala-library'
14+
}
15+
16+
scoverage {
17+
// 'detectedScalaLibraryVersion' is set by the test `DetectScalaLibraryTest.java`
18+
scoverageScalaVersion = detectedScalaLibraryVersion
19+
}

Diff for: src/functionalTest/resources/projects/detect-scala-library/gradle-consistent-versions/settings.gradle

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Run ./gradlew --write-locks to regenerate this file
2+
org.scala-lang:scala-library:2.12.0 (1 constraints: 3705353b)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.scala-lang:scala-library = 2.12.0

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

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ class ScoverageExtension {
6060
scoverageVersion.set('1.4.1')
6161

6262
scoverageScalaVersion = project.objects.property(String)
63-
scoverageScalaVersion.set('2.12')
6463

6564
sources = project.objects.property(File)
6665
sources.set(project.projectDir)

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

+33-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.scoverage
22

33
import org.apache.commons.io.FileUtils
4-
import org.gradle.api.GradleException
54
import org.gradle.api.Plugin
65
import org.gradle.api.Project
76
import org.gradle.api.Task
@@ -25,10 +24,10 @@ class ScoveragePlugin implements Plugin<PluginAware> {
2524
static final String CHECK_NAME = 'checkScoverage'
2625
static final String COMPILE_NAME = 'compileScoverageScala'
2726
static final String AGGREGATE_NAME = 'aggregateScoverage'
27+
static final String DEFAULT_SCALA_VERSION = '2.12'
2828

2929
static final String DEFAULT_REPORT_DIR = 'reports' + File.separatorChar + 'scoverage'
3030

31-
private volatile File pluginFile = null
3231
private final ConcurrentHashMap<Task, Set<? extends Task>> taskDependencies = new ConcurrentHashMap<>();
3332

3433
@Override
@@ -173,13 +172,7 @@ class ScoveragePlugin implements Plugin<PluginAware> {
173172
}
174173

175174
compileTask.configure {
176-
if (pluginFile == null) {
177-
pluginFile = project.configurations[CONFIGURATION_NAME].find {
178-
it.name.startsWith("scalac-scoverage-plugin")
179-
}
180-
}
181-
182-
List<String> parameters = ['-Xplugin:' + pluginFile.absolutePath]
175+
List<String> parameters = []
183176
List<String> existingParameters = scalaCompileOptions.additionalParameters
184177
if (existingParameters) {
185178
parameters.addAll(existingParameters)
@@ -199,6 +192,18 @@ class ScoveragePlugin implements Plugin<PluginAware> {
199192
scalaCompileOptions.additionalParameters = parameters
200193
// the compile task creates a store of measured statements
201194
outputs.file(new File(extension.dataDir.get(), 'scoverage.coverage.xml'))
195+
196+
dependsOn project.configurations[CONFIGURATION_NAME]
197+
doFirst {
198+
/*
199+
It is crucial that this would run in `doFirst`, as this resolves the (dependencies of the)
200+
configuration, which we do not want to do at configuration time (but only at execution time).
201+
*/
202+
def pluginFile = project.configurations[CONFIGURATION_NAME].find {
203+
it.name.startsWith("scalac-scoverage-plugin")
204+
}
205+
scalaCompileOptions.additionalParameters.add('-Xplugin:' + pluginFile.absolutePath)
206+
}
202207
}
203208

204209
project.gradle.taskGraph.whenReady { graph ->
@@ -343,19 +348,26 @@ class ScoveragePlugin implements Plugin<PluginAware> {
343348

344349
private String resolveScalaVersion(Project project) {
345350

346-
def resolvedDependencies = project.configurations.compileClasspath.resolvedConfiguration.firstLevelModuleDependencies
347-
348-
def scalaLibrary = resolvedDependencies.find {
349-
it.moduleGroup == "org.scala-lang" && it.moduleName == "scala-library"
350-
}
351-
352-
if (scalaLibrary == null) {
353-
project.logger.info("No scala library detected. Using property 'scoverageScalaVersion'")
354-
return project.extensions.scoverage.scoverageScalaVersion.get()
351+
def scalaVersionProperty = project.extensions.scoverage.scoverageScalaVersion
352+
if (scalaVersionProperty.isPresent()) {
353+
def configuredScalaVersion = scalaVersionProperty.get()
354+
project.logger.info("Using configured Scala version: $configuredScalaVersion")
355+
return configuredScalaVersion
355356
} else {
356-
project.logger.info("Detected scala library in compilation classpath")
357-
def fullScalaVersion = scalaLibrary.moduleVersion
358-
return fullScalaVersion.substring(0, fullScalaVersion.lastIndexOf("."))
357+
project.logger.info("No Scala version configured. Detecting scala library...")
358+
def components = project.configurations.compileClasspath.incoming.resolutionResult.getAllComponents()
359+
def scalaLibrary = components.find {
360+
it.moduleVersion.group == "org.scala-lang" && it.moduleVersion.name == "scala-library"
361+
}
362+
if (scalaLibrary != null) {
363+
def fullScalaVersion = scalaLibrary.moduleVersion.version
364+
def scalaVersion = fullScalaVersion.substring(0, fullScalaVersion.lastIndexOf("."))
365+
project.logger.info("Detected scala library in compilation classpath. Scala version: $scalaVersion")
366+
return scalaVersion
367+
} else {
368+
project.logger.info("No scala library detected. Using default Scala version: $DEFAULT_SCALA_VERSION")
369+
return DEFAULT_SCALA_VERSION
370+
}
359371
}
360372
}
361373

0 commit comments

Comments
 (0)