Introduce some GradleTestKit-based tests and the supporting infra to run them

Some of this was unapologetically pilfered from the core plugins
This commit is contained in:
R. Tyler Croy 2015-08-14 11:24:32 -07:00
parent 988888d068
commit fe5a55d754
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
13 changed files with 193 additions and 57 deletions

View File

@ -12,7 +12,6 @@ buildscript {
classpath "org.ysb33r.gradle:gradletest:0.5.4" classpath "org.ysb33r.gradle:gradletest:0.5.4"
} }
} }
apply plugin: 'codenarc' apply plugin: 'codenarc'
apply plugin: 'groovy' apply plugin: 'groovy'
apply plugin: 'maven' apply plugin: 'maven'
@ -28,8 +27,7 @@ repositories {
} }
dependencies { dependencies {
compile gradleApi() compile new GradleDist(project, '2.0').asFileTree
compile localGroovy()
['jruby-gradle-plugin', 'jruby-gradle-jar-plugin'].each { String plugin -> ['jruby-gradle-plugin', 'jruby-gradle-jar-plugin'].each { String plugin ->
String pluginDependency = "com.github.jruby-gradle:${plugin}:${jrubyGradleMinVersion}" String pluginDependency = "com.github.jruby-gradle:${plugin}:${jrubyGradleMinVersion}"
@ -37,13 +35,26 @@ dependencies {
gradleTest pluginDependency gradleTest pluginDependency
} }
compile 'com.github.jengelman.gradle.plugins:shadow:1.2.2+'
testCompile ("org.spockframework:spock-core:0.7-groovy-${gradle.gradleVersion.startsWith('1.')?'1.8':'2.0'}") { testCompile ("org.spockframework:spock-core:0.7-groovy-${gradle.gradleVersion.startsWith('1.')?'1.8':'2.0'}") {
exclude module : 'groovy-all' exclude module : 'groovy-all'
} }
} }
plugins.withType(JavaPlugin) {
sourceCompatibility = 1.7
targetCompatibility = 1.7
project.tasks.withType(JavaCompile) { task ->
task.sourceCompatibility = project.sourceCompatibility
task.targetCompatibility = project.targetCompatibility
}
project.tasks.withType(GroovyCompile) { task ->
task.sourceCompatibility = project.sourceCompatibility
task.targetCompatibility = project.targetCompatibility
}
}
codenarc { codenarc {
sourceSets = [sourceSets.main] sourceSets = [sourceSets.main]
configFile = file('gradle/codenarc.xml') configFile = file('gradle/codenarc.xml')

2
buildSrc/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build/
*swp*

View File

@ -0,0 +1,44 @@
import org.gradle.api.Project
import org.gradle.api.file.FileTree
import org.gradle.wrapper.Download
import org.gradle.wrapper.Install
import org.gradle.wrapper.Logger
import org.gradle.wrapper.PathAssembler
import org.gradle.wrapper.WrapperConfiguration
/**
* Include this in your project's buildSrc, then add a dependency to your project:
* compile new GradleDist(project, '2.6').asFileTree
*
* Code courtesy of @ajoberstar
*/
class GradleDist {
private final Project project
final String version
GradleDist(Project project, String version) {
this.project = project
this.version = version
}
String getPath() {
return "https://services.gradle.org/distributions/gradle-${version}-bin.zip"
}
File getAsFile() {
return project.file(getPath())
}
URI getAsURI() {
return project.uri(getPath())
}
FileTree getAsFileTree() {
Logger logger = new Logger(true)
Install install = new Install(logger, new Download(logger, 'gradle', ''), new PathAssembler(project.gradle.gradleUserHomeDir))
WrapperConfiguration config = new WrapperConfiguration()
config.distribution = getAsURI()
File file = install.createDist(config)
return project.fileTree(dir:file, include:'**/*.jar')
}
}

View File

@ -6,13 +6,12 @@ buildscript {
} }
dependencies { dependencies {
//classpath 'com.github.jruby-gradle:jruby-gradle-storm-plugin:0.2.0' classpath 'com.github.jruby-gradle:jruby-gradle-jar-plugin:1.0.2'
/* Replace "%%VERSION%%" with the version of JRuby/Gradle Storm you wish to /* Replace "%%VERSION%%" with the version of JRuby/Gradle Storm you wish to
* use if you want to use this build.gradle outside of gradleTest * use if you want to use this build.gradle outside of gradleTest
*/ */
classpath 'com.github.jruby-gradle:jruby-gradle-storm-plugin:%%VERSION%%' classpath 'com.github.jruby-gradle:jruby-gradle-storm-plugin:%%VERSION%%'
classpath 'com.github.jruby-gradle:jruby-gradle-plugin:1.0.1' //classpath 'com.github.jruby-gradle:jruby-gradle-storm-plugin:0.2.0'
classpath 'com.github.jruby-gradle:jruby-gradle-jar-plugin:1.0.1'
} }
} }

View File

@ -4,4 +4,4 @@ bintrayKey=
sourceCompatibility=1.7 sourceCompatibility=1.7
targetCompatibility=1.7 targetCompatibility=1.7
jrubyGradleMinVersion=1.0.1 jrubyGradleMinVersion=1.0.2

View File

@ -3,7 +3,7 @@ import groovy.json.JsonOutput
apply plugin: 'org.ysb33r.gradletest' apply plugin: 'org.ysb33r.gradletest'
configurations { configurations {
integrationTestCompile.extendsFrom testCompile integrationTestCompile.extendsFrom testCompile, compile
integrationTestRuntime.extendsFrom testRuntime, integrationTestCompile integrationTestRuntime.extendsFrom testRuntime, integrationTestCompile
} }
@ -52,7 +52,21 @@ gradleTest {
} }
dependencies { dependencies {
integrationTestCompile gradleTestKit() /* Without pruning the groovy-all dependency from this file list, we end up
* with two groovys running around and this exception at runtime:
* groovy.lang.GroovyRuntimeException: Conflicting module versions. Module [groovy-all is loaded in version 2.3.10 and you are trying to load version 2.3.3
at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl$DefaultModuleListener.onModule(MetaClassRegistryImpl.java:509)
at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanExtensionModuleFromProperties(ExtensionModuleScanner.java:77)
at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanExtensionModuleFromMetaInf(ExtensionModuleScanner.java:71)
at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanClasspathModules(ExtensionModuleScanner.java:53)
at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.<init>(MetaClassRegistryImpl.java:110)
at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.<init>(MetaClassRegistryImpl.java:71)
at groovy.lang.GroovySystem.<clinit>(GroovySystem.java:33)
*/
integrationTestCompile files(gradleTestKit().resolve().findAll { File f ->
!f.name.matches(/groovy-all-(.*).jar/)
})
/* add our TestKit classpath to our test runtime so we can find it again */ /* add our TestKit classpath to our test runtime so we can find it again */
integrationTestRuntime files(prepareGradleTestKitClasspath) integrationTestRuntime files(prepareGradleTestKitClasspath)
} }

View File

@ -11,25 +11,8 @@ import org.junit.rules.TemporaryFolder
import spock.lang.* import spock.lang.*
/** /**
* This integration test will stand up Gradle integration tests using
*/ */
class JRubyStormTaskIntegrationSpec extends Specification { class JRubyStormTaskIntegrationSpec extends Specification {
@Rule
final TemporaryFolder testProjectDir = new TemporaryFolder()
File buildFile
String pluginDependencies
def setup() {
buildFile = testProjectDir.newFile('build.gradle')
def pluginClasspathResource = getClass().classLoader.findResource("plugin-classpath.json")
if (pluginClasspathResource == null) {
throw new IllegalStateException("Did not find plugin classpath resource, run `testClasses` build task.")
}
pluginDependencies = pluginClasspathResource.text //(new JsonSlurper()).parseText(pluginClasspathResource.text)
}
def "evaluation of the project should result in an assemble and run task"() { def "evaluation of the project should result in an assemble and run task"() {
given: given:
Project project = ProjectBuilder.builder().build() Project project = ProjectBuilder.builder().build()
@ -71,6 +54,25 @@ class JRubyStormTaskIntegrationSpec extends Specification {
it.name == 'storm-core' it.name == 'storm-core'
} }
} }
}
/** Integration tests which actually execute Gradle via the GradleTestKit */
class JRubyStormTestKitSpec extends Specification {
@Rule
final TemporaryFolder testProjectDir = new TemporaryFolder()
File buildFile
String pluginDependencies
def setup() {
buildFile = testProjectDir.newFile('build.gradle')
def pluginClasspathResource = getClass().classLoader.findResource("plugin-classpath.json")
if (pluginClasspathResource == null) {
throw new IllegalStateException("Did not find plugin classpath resource, run `testClasses` build task.")
}
pluginDependencies = pluginClasspathResource.text
}
def "executing the assemble task produces a jar artifact"() { def "executing the assemble task produces a jar artifact"() {
given: given:
@ -81,6 +83,7 @@ buildscript {
} }
} }
apply plugin: 'com.github.jruby-gradle.storm' apply plugin: 'com.github.jruby-gradle.storm'
jrubyStorm { jrubyStorm {
} }
""" """
@ -94,6 +97,8 @@ jrubyStorm {
then: then:
File[] artifacts = (new File(testProjectDir.root, ['build', 'libs'].join(File.separator))).listFiles() File[] artifacts = (new File(testProjectDir.root, ['build', 'libs'].join(File.separator))).listFiles()
artifacts && artifacts.size() == 1 artifacts && artifacts.size() == 1
and:
result.task(":assembleJRubyStorm").outcome == TaskOutcome.SUCCESS result.task(":assembleJRubyStorm").outcome == TaskOutcome.SUCCESS
} }
} }

View File

@ -3,12 +3,13 @@ package com.github.jrubygradle.storm
import com.github.jrubygradle.JRubyPlugin import com.github.jrubygradle.JRubyPlugin
import org.gradle.api.DefaultTask import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.Task import org.gradle.api.Task
import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.Configuration
import org.gradle.api.tasks.Input import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional import org.gradle.api.tasks.Optional
import com.github.jrubygradle.storm.internal.JRubyStorm as JRubyStormInternal
/** /**
* Implement the custom behaviors needed to build a JRubyStorm topology * Implement the custom behaviors needed to build a JRubyStorm topology
*/ */
@ -72,8 +73,8 @@ class JRubyStorm extends DefaultTask {
super() super()
configuration = project.configurations.maybeCreate(DEFAULT_CONFIGURATION_NAME) configuration = project.configurations.maybeCreate(DEFAULT_CONFIGURATION_NAME)
this.group JRubyPlugin.TASK_GROUP_NAME this.group JRubyPlugin.TASK_GROUP_NAME
this.runTask = this.createRunTask(this.project, this.name) this.runTask = JRubyStormInternal.createRunTask(this.project, this.name, this)
this.assembleTask = this.createAssembleTask(this.project, this.name) this.assembleTask = JRubyStormInternal.createAssembleTask(this.project, this.name)
project.afterEvaluate { this.updateDependencies() } project.afterEvaluate { this.updateDependencies() }
} }
@ -109,27 +110,4 @@ class JRubyStorm extends DefaultTask {
this.runTask?.group newGroup this.runTask?.group newGroup
this.assembleTask?.group newGroup this.assembleTask?.group newGroup
} }
private Task createRunTask(Project project, String baseName) {
JRubyStormLocal runTask = project.task("run${prepareNameForSuffix(baseName)}",
type: JRubyStormLocal)
runTask.parentTask = this
return runTask
}
private Task createAssembleTask(Project project, String baseName) {
return project.task("assemble${prepareNameForSuffix(baseName)}")
}
/**
* Prepare a name for suffixing to a task name, i.e. with a baseName of
* "foo" if I need a task to prepare foo, this will return 'Foo' so I can
* make a "prepareFoo" task and it cases properly
*
* This method has a special handling for the string 'jruby' where it will
* case it properly like "JRuby" instead of "Jruby"
*/
private String prepareNameForSuffix(String baseName) {
return baseName.replaceAll("(?i)jruby", 'JRuby').capitalize()
}
} }

View File

@ -15,9 +15,7 @@ class JRubyStormPlugin implements Plugin<Project> {
project.extensions.create('storm', JRubyStormExtension) project.extensions.create('storm', JRubyStormExtension)
project.task('jrubyStorm', type: JRubyStorm) project.task('jrubyStorm', type: JRubyStorm)
project.afterEvaluate { updateRepositories(project)
updateRepositories(project)
}
} }
@PackageScope @PackageScope

View File

@ -0,0 +1,31 @@
package com.github.jrubygradle.storm.internal
import com.github.jrubygradle.storm.JRubyStormLocal
import org.gradle.api.Project
import org.gradle.api.Task
class JRubyStorm {
static Task createAssembleTask(Project project, String baseName) {
return project.task("assemble${prepareNameForSuffix(baseName)}", type: JRubyStormJar)
}
/**
* Prepare a name for suffixing to a task name, i.e. with a baseName of
* "foo" if I need a task to prepare foo, this will return 'Foo' so I can
* make a "prepareFoo" task and it cases properly
*
* This method has a special handling for the string 'jruby' where it will
* case it properly like "JRuby" instead of "Jruby"
*/
static String prepareNameForSuffix(String baseName) {
return baseName.replaceAll("(?i)jruby", 'JRuby').capitalize()
}
static Task createRunTask(Project project, String baseName, Task parent) {
JRubyStormLocal runTask = project.task("run${prepareNameForSuffix(baseName)}",
type: JRubyStormLocal)
runTask.parentTask = parent
return runTask
}
}

View File

@ -0,0 +1,8 @@
package com.github.jrubygradle.storm.internal
import com.github.jrubygradle.jar.JRubyJar
import groovy.transform.InheritConstructors
@InheritConstructors
class JRubyStormJar extends JRubyJar {
}

View File

@ -0,0 +1,17 @@
package com.github.jrubygradle.storm.internal
import com.github.jrubygradle.jar.JRubyJar
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.*
class JRubyStormJarSpec extends Specification {
def "when constructing the task"() {
given:
Project project = ProjectBuilder.builder().build()
project.apply plugin: 'com.github.jruby-gradle.storm'
expect: "the task to be a JRubyJar"
project.task('spock', type: JRubyStormJar) instanceof JRubyJar
}
}

View File

@ -0,0 +1,29 @@
package com.github.jrubygradle.storm.internal
import com.github.jrubygradle.storm.JRubyStormLocal
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.bundling.Jar
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.*
class JRubyStormSpec extends Specification {
Project project
def setup() {
project = ProjectBuilder.builder().build()
project.apply plugin: 'com.github.jruby-gradle.storm'
}
def "createAssembleTask() should return a Jar type task"() {
expect:
JRubyStorm.createAssembleTask(project, 'spock') instanceof Jar
}
def "createRunTask() should return a JRubyStormLocal type task"() {
given:
Task task = project.task('spockParent', type: com.github.jrubygradle.storm.JRubyStorm)
expect:
JRubyStorm.createRunTask(project, 'spock', task) instanceof JRubyStormLocal
}
}