Skip to content

Commit

Permalink
Add support for kotlin multiplatform plugin (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquenfaria authored and vanniktech committed Jan 3, 2019
1 parent 37fba63 commit b72e78b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 25 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {
compileOnly 'com.android.tools.build:gradle:3.1.2'

testCompile 'com.android.tools.build:gradle:3.1.2'
testCompile 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.11'
testCompile 'junit:junit:4.12'
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4', { exclude module: "groovy-all" } // Use localGroovy()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class GenerationPlugin implements Plugin<Project> {
"**/intermediates/javac/${sourceName}/*/classes/**" // Android Gradle Plugin 3.2.x support.
]

if (isKotlinAndroid(subProject)) {
if (isKotlinAndroid(subProject) || isKotlinMultiplatform(subProject)) {
classPaths << "**/tmp/kotlin-classes/${sourcePath}/**"
if (productFlavorName) {
classPaths << "**/tmp/kotlin-classes/${productFlavorName}${buildTypeName.capitalize()}/**"
Expand Down Expand Up @@ -356,6 +356,10 @@ class GenerationPlugin implements Plugin<Project> {
return project.plugins.hasPlugin('org.jetbrains.kotlin.android')
}

protected static boolean isKotlinMultiplatform(final Project project) {
return project.plugins.hasPlugin('org.jetbrains.kotlin.multiplatform')
}

protected static boolean isAndroidApplication(final Project project) {
return project.plugins.hasPlugin('com.android.application')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import org.gradle.testing.jacoco.plugins.JacocoPlugin
import org.gradle.testing.jacoco.tasks.JacocoReport
import org.junit.Test

import java.nio.file.Paths

import static com.vanniktech.android.junit.jacoco.ProjectHelper.ProjectType.*

class GenerationTest {
Expand Down Expand Up @@ -269,15 +271,22 @@ class GenerationTest {
}

assert reports.xml.enabled
assert reports.xml.destination.toString() == project.buildDir.absolutePath + "/reports/jacoco/${flavor}${buildType.capitalize()}/jacoco.xml"
assert reports.xml.destination.toPath() == Paths.get(project.buildDir.absolutePath, "/reports/jacoco/${flavor}${buildType.capitalize()}/jacoco.xml")
assert reports.csv.enabled
assert reports.csv.destination.toString() == project.buildDir.absolutePath + "/reports/jacoco/${flavor}${buildType.capitalize()}/jacoco.csv"
assert reports.csv.destination.toPath() == Paths.get(project.buildDir.absolutePath, "/reports/jacoco/${flavor}${buildType.capitalize()}/jacoco.csv")
assert reports.html.enabled
assert reports.html.destination.toString() == project.buildDir.absolutePath + "/reports/jacoco/${flavor}${buildType.capitalize()}"
assert reports.html.destination.toPath() == Paths.get(project.buildDir.absolutePath, "/reports/jacoco/${flavor}${buildType.capitalize()}")

assert classDirectories.dir == project.file("build/")
assert contentEquals(classDirectories.includes, ["**/intermediates/classes/${flavor}/${buildType}/**".toString(), "**/intermediates/javac/${flavor}${buildType.capitalize()}/*/classes/**".toString()])

if (hasKotlin(project)) {
assert contentEquals(classDirectories.includes, ["**/intermediates/classes/${flavor}/${buildType}/**".toString(), "**/intermediates/javac/${flavor}${buildType.capitalize()}/*/classes/**".toString(),
"**/tmp/kotlin-classes/${buildType}/**".toString(), "**/tmp/kotlin-classes/${flavor}${buildType.capitalize()}/**".toString()])
} else {
assert contentEquals(classDirectories.includes, ["**/intermediates/classes/${flavor}/${buildType}/**".toString(), "**/intermediates/javac/${flavor}${buildType.capitalize()}/*/classes/**".toString()])
}

assert taskDependsOn(task, "test${flavor.capitalize()}${buildType.capitalize()}UnitTest")
assert taskDependsOn(project.tasks.findByName('check'), "jacocoTestReport${flavor.capitalize()}${buildType.capitalize()}")
}
Expand Down Expand Up @@ -311,14 +320,18 @@ class GenerationTest {
}

assert reports.xml.enabled
assert reports.xml.destination.toString() == project.buildDir.absolutePath + '/reports/jacoco/debug/jacoco.xml'
assert reports.xml.destination.toPath() == Paths.get(project.buildDir.absolutePath, "/reports/jacoco/debug/jacoco.xml")
assert reports.csv.enabled
assert reports.csv.destination.toString() == project.buildDir.absolutePath + '/reports/jacoco/debug/jacoco.csv'
assert reports.csv.destination.toPath() == Paths.get(project.buildDir.absolutePath, "/reports/jacoco/debug/jacoco.csv")
assert reports.html.enabled
assert reports.html.destination.toString() == project.buildDir.absolutePath + '/reports/jacoco/debug'
assert reports.html.destination.toPath() == Paths.get(project.buildDir.absolutePath, "/reports/jacoco/debug")

assert classDirectories.dir == project.file("build/")
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/debug/**', '**/intermediates/javac/debug/*/classes/**'])
if (hasKotlin(project)) {
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/debug/**', '**/intermediates/javac/debug/*/classes/**', '**/tmp/kotlin-classes/debug/**'])
} else {
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/debug/**', '**/intermediates/javac/debug/*/classes/**'])
}

assert taskDependsOn(debugTask, 'testDebugUnitTest')
assert taskDependsOn(project.tasks.findByName('check'), 'jacocoTestReportDebug')
Expand Down Expand Up @@ -347,14 +360,18 @@ class GenerationTest {
}

assert reports.xml.enabled
assert reports.xml.destination.toString() == project.buildDir.absolutePath + '/reports/jacocoCombined/debug/jacoco.xml'
assert reports.xml.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacocoCombined/debug/jacoco.xml')
assert reports.csv.enabled
assert reports.csv.destination.toString() == project.buildDir.absolutePath + '/reports/jacocoCombined/debug/jacoco.csv'
assert reports.csv.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacocoCombined/debug/jacoco.csv')
assert reports.html.enabled
assert reports.html.destination.toString() == project.buildDir.absolutePath + '/reports/jacocoCombined/debug'
assert reports.html.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacocoCombined/debug')

assert classDirectories.dir == project.file("build/")
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/debug/**', '**/intermediates/javac/debug/*/classes/**'])
if (hasKotlin(project)) {
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/debug/**', '**/intermediates/javac/debug/*/classes/**', '**/tmp/kotlin-classes/debug/**'])
} else {
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/debug/**', '**/intermediates/javac/debug/*/classes/**'])
}

assert taskDependsOn(debugTaskCombined, 'testDebugUnitTest')
assert taskDependsOn(debugTaskCombined, 'createDebugCoverageReport')
Expand Down Expand Up @@ -387,14 +404,18 @@ class GenerationTest {
}

assert reports.xml.enabled
assert reports.xml.destination.toString() == project.buildDir.absolutePath + '/reports/jacoco/release/jacoco.xml'
assert reports.xml.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacoco/release/jacoco.xml')
assert reports.csv.enabled
assert reports.csv.destination.toString() == project.buildDir.absolutePath + '/reports/jacoco/release/jacoco.csv'
assert reports.csv.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacoco/release/jacoco.csv')
assert reports.html.enabled
assert reports.html.destination.toString() == project.buildDir.absolutePath + '/reports/jacoco/release'
assert reports.html.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacoco/release')

assert classDirectories.dir == project.file("build/")
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/release/**', '**/intermediates/javac/release/*/classes/**'])
if (hasKotlin(project)) {
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/release/**', '**/intermediates/javac/release/*/classes/**', '**/tmp/kotlin-classes/release/**'])
} else {
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/release/**', '**/intermediates/javac/release/*/classes/**'])
}

assert taskDependsOn(releaseTask, 'testReleaseUnitTest')
assert taskDependsOn(project.tasks.findByName('check'), 'jacocoTestReportRelease')
Expand Down Expand Up @@ -424,14 +445,18 @@ class GenerationTest {
}

assert reports.xml.enabled
assert reports.xml.destination.toString() == project.buildDir.absolutePath + '/reports/jacocoCombined/release/jacoco.xml'
assert reports.xml.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacocoCombined/release/jacoco.xml')
assert reports.csv.enabled
assert reports.csv.destination.toString() == project.buildDir.absolutePath + '/reports/jacocoCombined/release/jacoco.csv'
assert reports.csv.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacocoCombined/release/jacoco.csv')
assert reports.html.enabled
assert reports.html.destination.toString() == project.buildDir.absolutePath + '/reports/jacocoCombined/release'
assert reports.html.destination.toPath() == Paths.get(project.buildDir.absolutePath, '/reports/jacocoCombined/release')

assert classDirectories.dir == project.file("build/")
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/release/**', '**/intermediates/javac/release/*/classes/**'])
if (hasKotlin(project)) {
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/release/**', '**/intermediates/javac/release/*/classes/**', '**/tmp/kotlin-classes/release/**'])
} else {
assert contentEquals(classDirectories.includes, ['**/intermediates/classes/release/**', '**/intermediates/javac/release/*/classes/**'])
}

assert taskDependsOn(releaseTaskCombined, 'testReleaseUnitTest')
assert taskDependsOn(releaseTaskCombined, 'createReleaseCoverageReport')
Expand Down Expand Up @@ -496,6 +521,10 @@ class GenerationTest {
return false
}

static boolean hasKotlin(Project project) {
return project.plugins.hasPlugin('org.jetbrains.kotlin.android') || project.plugins.hasPlugin('org.jetbrains.kotlin.multiplatform')
}

@Test void getExcludesDefault() {
final def excludes = GenerationPlugin.getExcludes(new JunitJacocoExtension())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ final class ProjectHelper {
project = builder.withName('java').build()
break
case ProjectType.ANDROID_APPLICATION:
case ProjectType.ANDROID_KOTLIN_APPLICATION:
project = builder.withName('android app').build()
def androidMock = new MockFor(AppExtension)
def buildTypesMock = ["debug", "release"].collect { bt ->
Expand All @@ -62,6 +63,7 @@ final class ProjectHelper {
break
case ProjectType.ANDROID_LIBRARY:
case ProjectType.ANDROID_FEATURE:
case ProjectType.ANDROID_KOTLIN_MULTIPLATFORM:
project = builder.withName('android library').build()
def androidMock = new MockFor(LibraryExtension)
def buildTypesMock = ["debug", "release"].collect { bt ->
Expand Down Expand Up @@ -93,8 +95,12 @@ final class ProjectHelper {
break
}

if (projectType.pluginName != null) {
project.plugins.apply(projectType.pluginName)
if (projectType.pluginNames != null) {
for (String pluginName : projectType.pluginNames) {
if (pluginName) {
project.plugins.apply(pluginName)
}
}
}
}

Expand Down Expand Up @@ -153,16 +159,18 @@ final class ProjectHelper {

enum ProjectType {
ANDROID_APPLICATION('com.android.application'),
ANDROID_KOTLIN_APPLICATION('com.android.application', 'org.jetbrains.kotlin.android'),
ANDROID_KOTLIN_MULTIPLATFORM('com.android.library', 'org.jetbrains.kotlin.multiplatform'),
ANDROID_LIBRARY('com.android.library'),
ANDROID_FEATURE('com.android.feature'),
ANDROID_TEST('com.android.test'),
JAVA('java'),
ROOT(null)

private final String pluginName
private final String[] pluginNames

ProjectType(String pluginName) {
this.pluginName = pluginName
ProjectType(String... pluginNames) {
this.pluginNames = pluginNames
}
}
}

0 comments on commit b72e78b

Please sign in to comment.