Skip to content

Add detection of scala version via 'implementation' and 'compileOnly' as well #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/functionalTest/java/org.scoverage/DetectScalaLibraryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.scoverage;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

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

private static final String SCALA_VERSION = "0.0";
private static final String SCALA_LIBRARY_PARAMETER = "-PdetectedScalaLibraryVersion=" + SCALA_VERSION + ".0";
private static final String EXPECTED_OUTPUT = "Using scoverage scalac plugin version '" + SCALA_VERSION;

@Parameterized.Parameter(0)
public String projectDir;

@Parameterized.Parameters(name = "{index}: Project {0} ")
public static Collection<Object[]> data() {
Object[][] data = new Object[][]{{"/compile"}, {"/compileOnly"}, {"/implementation"}, {"/dependency-management"}};
return Arrays.asList(data);
}

public DetectScalaLibraryTest() {
super(null);
}

@Test
public void test() {
setProjectName("detect-scala-library" + projectDir);

// build supposed to fail since repositories are not configured
AssertableBuildResult result = runAndFail("clean", SCALA_LIBRARY_PARAMETER, "--info");

// all we want to know is that the plugin detected our configured library version
String output = result.getResult().getOutput();
Assert.assertTrue(output.contains(EXPECTED_OUTPUT));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,6 @@ public void reportScoverageWithoutNormalCompilationAndWithExcludedClasses() thro
Assert.assertFalse(resolve(buildDir(), "classes/scala/scoverage/org/hello/World.class").exists());
}

@Test
public void reportScoverageUnder2_11() throws Exception {
run("clean", ScoveragePlugin.getREPORT_NAME(),
"-PscalaVersionMinor=11",
"-PscalaVersionBuild=8",
"-Pscoverage.scoverageScalaVersion=2_11");
assertReportFilesExist();
}

private void assertReportFilesExist() {

Assert.assertTrue(resolve(reportDir(), "index.html").exists());
Expand Down
22 changes: 13 additions & 9 deletions src/functionalTest/java/org.scoverage/ScoverageFunctionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,12 @@

public abstract class ScoverageFunctionalTest {

private final String projectName;
private final GradleRunner runner;
private String projectName;
private GradleRunner runner;
private final XmlParser parser;

protected ScoverageFunctionalTest(String projectName) {

this.projectName = projectName;
this.runner = GradleRunner.create()
.withProjectDir(projectDir())
.withPluginClasspath()
.forwardOutput();

setProjectName(projectName);
try {
this.parser = new XmlParser();
parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
Expand All @@ -44,6 +38,16 @@ protected ScoverageFunctionalTest(String projectName) {
}
}

protected void setProjectName(String projectName) {
if (projectName != null) {
this.projectName = projectName;
this.runner = GradleRunner.create()
.withProjectDir(projectDir())
.withPluginClasspath()
.forwardOutput();
}
}

protected File projectDir() {

return new File("src/functionalTest/resources/projects/" + projectName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id 'org.scoverage'
}

description = 'defines scala library using the "compile" configuration'

dependencies {
compile group: 'org.scala-lang', name: 'scala-library', version: "${detectedScalaLibraryVersion}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id 'org.scoverage'
}

description = 'defines scala library using the "compileOnly" configuration'

dependencies {
compileOnly group: 'org.scala-lang', name: 'scala-library', version: "${detectedScalaLibraryVersion}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
id 'io.spring.dependency-management' version "1.0.4.RELEASE"
id 'org.scoverage'
}

description = 'defines scala library using the "implementation" configuration and the dependency-management plugin'

dependencyManagement {
dependencies {
dependency group: 'org.scala-lang', name: 'scala-library', version: "${detectedScalaLibraryVersion}"
}
}

dependencies {
implementation group: 'org.scala-lang', name: 'scala-library'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id 'org.scoverage'
}

description = 'defines scala library using the "implementation" configuration'

dependencies {
implementation group: 'org.scala-lang', name: 'scala-library', version: "${detectedScalaLibraryVersion}"
}
54 changes: 34 additions & 20 deletions src/main/groovy/org/scoverage/ScoveragePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,8 @@ class ScoveragePlugin implements Plugin<PluginAware> {
}

project.afterEvaluate {
def scalaVersion = resolveScalaVersion(project)
def scoverageVersion = project.extensions.scoverage.scoverageVersion.get()
def scalaVersion = null

def scalaLibrary = project.configurations.compile.dependencies.find {
it.group == "org.scala-lang" && it.name == "scala-library"
}

if (scalaLibrary != null) {
scalaVersion = scalaLibrary.version
}

if (scalaVersion == null && project.pluginManager.hasPlugin("io.spring.dependency-management")) {
scalaVersion = project.dependencyManagement.compile.managedVersions["org.scala-lang:scala-library"]
}

if (scalaVersion == null) {
scalaVersion = project.extensions.scoverage.scoverageScalaVersion.get()
} else {
scalaVersion = scalaVersion.substring(0, scalaVersion.lastIndexOf("."))
}

def fullScoverageVersion = "$scalaVersion:$scoverageVersion"

project.logger.info("Using scoverage scalac plugin version '$fullScoverageVersion'")
Expand Down Expand Up @@ -319,6 +300,39 @@ class ScoveragePlugin implements Plugin<PluginAware> {
}
}

private String resolveScalaVersion(Project project) {
def scalaVersion = null

def configurations = [
project.configurations.compile,
project.configurations.compileOnly,
project.configurations.implementation
]
def dependencies = configurations.collectMany { it.dependencies }

def scalaLibrary = dependencies.find {
it.group == "org.scala-lang" && it.name == "scala-library"
}

if (scalaLibrary != null) {
scalaVersion = scalaLibrary.version
}

if (scalaVersion == null && project.pluginManager.hasPlugin("io.spring.dependency-management")) {
scalaVersion = project.dependencyManagement.compile.managedVersions["org.scala-lang:scala-library"]
}

if (scalaVersion == null) {
project.logger.info("No scala library detected. Using property 'scoverageScalaVersion'")
scalaVersion = project.extensions.scoverage.scoverageScalaVersion.get()
} else {
project.logger.info("Detected scala library in compilation classpath")
scalaVersion = scalaVersion.substring(0, scalaVersion.lastIndexOf("."))
}

return scalaVersion
}

private Set<? extends Task> recursiveDependenciesOf(Task task) {
if (!taskDependencies.containsKey(task)) {
def directDependencies = task.getTaskDependencies().getDependencies(task)
Expand Down