Skip to content

Commit

Permalink
add plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredsburrows committed Apr 11, 2022
1 parent 12dc3a8 commit 9477c25
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/main/kotlin/com/jaredsburrows/license/projectAndroid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@ import com.android.build.gradle.FeatureExtension
import com.android.build.gradle.FeaturePlugin
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.LibraryPlugin
import com.android.build.gradle.TestExtension
import com.android.build.gradle.TestPlugin
import com.android.build.gradle.api.BaseVariant
import org.gradle.api.DomainObjectSet
import org.gradle.api.Project
import java.util.Locale

/** Returns true if Android Gradle project */
internal fun Project.isAndroidProject(): Boolean {
return project.plugins.hasPlugin("android")
// AppPlugin
return project.plugins.hasPlugin("android") ||
project.plugins.hasPlugin("com.android.application") ||
// FeaturePlugin
project.plugins.hasPlugin("com.android.feature") ||
// LibraryPlugin
project.plugins.hasPlugin("android-library") ||
project.plugins.hasPlugin("com.android.library") ||
// TestPlugin
project.plugins.hasPlugin("com.android.test")
}

/**
Expand All @@ -23,6 +34,7 @@ internal fun Project.isAndroidProject(): Boolean {
* AppPlugin - "android", "com.android.application"
* FeaturePlugin - "com.android.feature"
* LibraryPlugin - "android-library", "com.android.library"
* TestPlugin - "com.android.test"
*/
internal fun Project.configureAndroidProject() {
project.plugins.all {
Expand All @@ -43,6 +55,11 @@ internal fun Project.configureAndroidProject() {
configureVariant(libraryVariants)
}
}
is TestPlugin -> {
project.extensions.getByType(TestExtension::class.java).run {
configureVariant(applicationVariants)
}
}
}
}
}
Expand Down
185 changes: 185 additions & 0 deletions src/test/groovy/com/jaredsburrows/license/LicensePluginSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package com.jaredsburrows.license


import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification
import spock.lang.Unroll

import static org.gradle.testkit.runner.TaskOutcome.SUCCESS
import static test.TestUtils.gradleWithCommand

final class LicensePluginSpec extends Specification {
@Rule public final TemporaryFolder testProjectDir = new TemporaryFolder()
private List<File> pluginClasspath
private String classpathString
private File buildFile

def 'setup'() {
def pluginClasspathResource = getClass().classLoader.getResource('plugin-classpath.txt')
if (pluginClasspathResource == null) {
throw new IllegalStateException(
'Did not find plugin classpath resource, run `testClasses` build task.')
}

pluginClasspath = pluginClasspathResource.readLines().collect { new File(it) }
classpathString = pluginClasspath
.collect { it.absolutePath.replace('\\', '\\\\') } // escape backslashes in Windows paths
.collect { "'$it'" }
.join(", ")
buildFile = testProjectDir.newFile('build.gradle')
}

def 'apply with buildscript'() {
given:
buildFile <<
"""
buildscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath files($classpathString)
}
}
apply plugin: 'java'
apply plugin: 'com.jaredsburrows.license'
"""

when:
def result = gradleWithCommand(testProjectDir.root, 'licenseReport', '-s')

then:
result.task(':licenseReport').outcome == SUCCESS
}

def 'apply with plugins'() {
given:
buildFile <<
"""
plugins {
id 'java'
id 'com.jaredsburrows.license'
}
"""

when:
def result = gradleWithCommand(testProjectDir.root, 'licenseReport', '-s')

then:
result.task(':licenseReport').outcome == SUCCESS
}

@Unroll def 'apply with allowed java plugins: #javaPlugin'() {
given:
buildFile <<
"""
plugins {
id '${javaPlugin}'
id 'com.jaredsburrows.license'
}
"""

when:
def result = gradleWithCommand(testProjectDir.root, 'licenseReport', '-s')

then:
result.task(':licenseReport').outcome == SUCCESS

where:
javaPlugin << [
'application', // JavaApplicationPlugin
'groovy', // GroovyPlugin
'java', // JavaPlugin
'java-library', // JavaLibraryPlugin
'scala', // ScalaPlugin
]
}

@Unroll def 'apply with allowed android plugins: #androidPlugin'() {
given:
testProjectDir.newFile('settings.gradle') <<
"""
include 'subproject'
"""

testProjectDir.newFolder('subproject')

testProjectDir.newFile('subproject/build.gradle') <<
"""
buildscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath "com.android.tools.build:gradle:3.6.4"
classpath files($classpathString)
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.jaredsburrows.license'
android {
compileSdkVersion 28
defaultConfig {
if (project.plugins.hasPlugin("com.android.application")) applicationId 'com.example'
if (project.plugins.hasPlugin("com.android.test")) targetProjectPath ':subproject'
}
}
"""

buildFile <<
"""
buildscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath "com.android.tools.build:gradle:3.6.4"
classpath files($classpathString)
}
}
apply plugin: '${androidPlugin}'
apply plugin: 'com.jaredsburrows.license'
android {
compileSdkVersion 28
defaultConfig {
if (project.plugins.hasPlugin("com.android.application")) applicationId 'com.example'
if (project.plugins.hasPlugin("com.android.test")) targetProjectPath ':subproject'
}
}
"""

when:
def result = gradleWithCommand(testProjectDir.root, 'licenseDebugReport', '-s')

then:
result.task(':licenseDebugReport').outcome == SUCCESS

where:
androidPlugin << [
// AppPlugin
'android',
'com.android.application',
// FeaturePlugin
'com.android.feature',
// LibraryPlugin
'android-library',
'com.android.library',
// TestPlugin
'com.android.test',
]
}
}

0 comments on commit 9477c25

Please sign in to comment.