Skip to content

Commit cc9bdaa

Browse files
committed
#140 Only attempt to detect Scala version if it isn't already configured by the scoverageScalaVersion option
1 parent 57a036a commit cc9bdaa

File tree

5 files changed

+53
-26
lines changed

5 files changed

+53
-26
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

+19-11
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ public class DetectScalaLibraryTest extends ScoverageFunctionalTest {
1616
private static final String SCALA_VERSION = "2.12";
1717
private static final String SCALA_LIBRARY_PARAMETER = "-PdetectedScalaLibraryVersion=";
1818

19-
private static final String EXPECTED_OUTPUT_A = "Detected scala library in compilation classpath";
20-
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;
2122

2223
@Parameterized.Parameter(0)
2324
public String projectDir;
@@ -26,16 +27,19 @@ public class DetectScalaLibraryTest extends ScoverageFunctionalTest {
2627
public String[] subVersions;
2728

2829
@Parameterized.Parameter(2)
30+
public boolean detect;
31+
32+
@Parameterized.Parameter(3)
2933
public String[] additionalParameters;
3034

3135
@Parameterized.Parameters(name = "{index}: Project {0} ")
3236
public static Collection<Object[]> data() {
3337
Object[][] data = new Object[][]{
34-
{"/compile", new String[] {".0", ".+"}, new String[0]},
35-
{"/compileOnly", new String[] {".0", ".+"}, new String[0]},
36-
{"/implementation", new String[] {".0", ".+"}, new String[0]},
37-
{"/dependency-management", new String[] {".0", ".+"}, new String[0]},
38-
{"/gradle-consistent-versions", new String[] {"ignored"}, new String[] {"--write-locks"}},
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"}},
3943
};
4044
return Arrays.asList(data);
4145
}
@@ -48,20 +52,24 @@ public DetectScalaLibraryTest() {
4852
public void test() {
4953
setProjectName("detect-scala-library" + projectDir);
5054
for (String subVersion : subVersions) {
51-
testWithParameter(SCALA_LIBRARY_PARAMETER + SCALA_VERSION + subVersion);
55+
testWithParameter(SCALA_LIBRARY_PARAMETER + SCALA_VERSION + subVersion, detect);
5256
}
5357
}
5458

55-
private void testWithParameter(String parameter) {
59+
private void testWithParameter(String parameter, Boolean detect) {
5660

5761
String[] basicParameters = {"clean", parameter, "--info"};
5862
String[] parameters = Stream.concat(Arrays.stream(basicParameters), Arrays.stream(additionalParameters))
5963
.toArray(String[]::new);
6064
AssertableBuildResult result = dryRun(parameters);
6165

6266
String output = result.getResult().getOutput();
63-
Assert.assertTrue(output.contains(EXPECTED_OUTPUT_A));
64-
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));
6573
}
6674

6775
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ description = 'defines scala library using the "implementation" configuration an
1111

1212
dependencies {
1313
implementation group: 'org.scala-lang', name: 'scala-library'
14+
}
15+
16+
scoverage {
17+
// 'detectedScalaLibraryVersion' is set by the test `DetectScalaLibraryTest.java`
18+
scoverageScalaVersion = detectedScalaLibraryVersion
1419
}

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

+20-12
Original file line numberDiff line numberDiff line change
@@ -24,6 +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.12'
2728

2829
static final String DEFAULT_REPORT_DIR = 'reports' + File.separatorChar + 'scoverage'
2930

@@ -347,19 +348,26 @@ class ScoveragePlugin implements Plugin<PluginAware> {
347348

348349
private String resolveScalaVersion(Project project) {
349350

350-
def components = project.configurations.compileClasspath.incoming.resolutionResult.getAllComponents()
351-
352-
def scalaLibrary = components.find {
353-
it.moduleVersion.group == "org.scala-lang" && it.moduleVersion.name == "scala-library"
354-
}
355-
356-
if (scalaLibrary == null) {
357-
project.logger.info("No scala library detected. Using property 'scoverageScalaVersion'")
358-
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
359356
} else {
360-
project.logger.info("Detected scala library in compilation classpath")
361-
def fullScalaVersion = scalaLibrary.moduleVersion.version
362-
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+
}
363371
}
364372
}
365373

0 commit comments

Comments
 (0)