Skip to content
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

add plugin tests #185

Merged
merged 1 commit into from
Apr 11, 2022
Merged
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
8 changes: 8 additions & 0 deletions src/main/kotlin/com/jaredsburrows/license/project.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.jaredsburrows.license

import org.gradle.api.Project

/** Returns true if plugin exists in project */
internal fun Project.hasPlugin(list: List<String>): Boolean {
return list.find { project.plugins.hasPlugin(it) } != null
}
23 changes: 22 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,29 @@ 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")
return hasPlugin(
listOf(
// AppPlugin
"android",
"com.android.application",
// FeaturePlugin
"com.android.feature",
// LibraryPlugin
"android-library",
"com.android.library",
// TestPlugin
"com.android.test",
)
)
}

/**
Expand All @@ -23,6 +38,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 +59,11 @@ internal fun Project.configureAndroidProject() {
configureVariant(libraryVariants)
}
}
is TestPlugin -> {
project.extensions.getByType(TestExtension::class.java).run {
configureVariant(applicationVariants)
}
}
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/kotlin/com/jaredsburrows/license/projectJava.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import org.gradle.api.Project

/** Returns true if Java Gradle project */
internal fun Project.isJavaProject(): Boolean {
return project.plugins.hasPlugin("java")
return hasPlugin(
listOf(
"application", // JavaApplicationPlugin
"groovy", // GroovyPlugin
"java", // JavaPlugin, also applies JavaBasePlugin
"java-library", // JavaLibraryPlugin, also applies JavaPlugin
"scala", // ScalaPlugin
)
)
}

/**
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',
]
}
}