Skip to content
This repository
Browse code

Added some basic tests.

  • Loading branch information...
commit a0c4d652fd320ad78c661cfd6c6218cc0e724f45 1 parent 5aa4bbe
Luke Daley alkemist authored
2  build.gradle
@@ -11,11 +11,13 @@ isSnapshot = version.endsWith("SNAPSHOT")
11 11 repositories {
12 12 mavenCentral()
13 13 maven { url "http://repo.grails.org/grails/repo" }
  14 + maven { url "http://m2repo.spockframework.org/snapshots" }
14 15 }
15 16
16 17 dependencies {
17 18 compile gradleApi(), "org.grails:grails-launcher:1.0.0"
18 19 groovy localGroovy()
  20 + testCompile "org.spockframework:spock-core:0.6-groovy-1.8-SNAPSHOT"
19 21 }
20 22
21 23 uploadArchives {
12 src/main/groovy/org/grails/gradle/plugin/GrailsPlugin.groovy
@@ -11,6 +11,8 @@ class GrailsPlugin implements Plugin<Project> {
11 11 throw new RuntimeException("[GrailsPlugin] the 'grailsVersion' project property is not set - you need to set this before applying the plugin")
12 12 }
13 13
  14 + String grailsVersion = project.grailsVersion
  15 +
14 16 project.configurations {
15 17 compile
16 18 runtime.extendsFrom compile
@@ -21,13 +23,19 @@ class GrailsPlugin implements Plugin<Project> {
21 23 }
22 24
23 25 project.dependencies {
24   - ["bootstrap", "scripts"].each {
  26 + ["bootstrap", "scripts", "resources"].each {
25 27 bootstrap("org.grails:grails-$it:${project.grailsVersion}") {
26 28 exclude group: "org.slf4j"
27 29 }
28 30 }
29 31
30   - bootstrap "org.apache.ivy:ivy:2.1.0"
  32 + if (grailsVersion.startsWith("1.3")) {
  33 + bootstrap "org.apache.ant:ant:1.8.2"
  34 + }
  35 +
  36 + if (!grailsVersion.startsWith("2")) {
  37 + bootstrap "org.apache.ivy:ivy:2.1.0"
  38 + }
31 39 }
32 40
33 41 project.task("init", type: GrailsTask) {
19 src/test/groovy/org/grails/gradle/plugin/PluginSpec.groovy
... ... @@ -0,0 +1,19 @@
  1 +package org.grails.gradle.plugin
  2 +
  3 +import spock.lang.Specification
  4 +import org.gradle.testfixtures.ProjectBuilder
  5 +import org.gradle.api.Project
  6 +
  7 +class PluginSpec extends Specification {
  8 +
  9 + Project project = ProjectBuilder.builder().build()
  10 +
  11 + def setup() {
  12 + project.grailsVersion = "2.0.0"
  13 + project.apply plugin: "grails"
  14 + }
  15 +
  16 + GrailsTask grailsTask(String name, Closure config = {}) {
  17 + GrailsTask task = project.task(name, type: GrailsTask, config) as GrailsTask
  18 + }
  19 +}
21 src/test/groovy/org/grails/gradle/plugin/TaskConfigurationSpec.groovy
... ... @@ -0,0 +1,21 @@
  1 +package org.grails.gradle.plugin
  2 +
  3 +import spock.lang.Specification
  4 +
  5 +class TaskConfigurationSpec extends PluginSpec {
  6 +
  7 + def "command defaults to task name"() {
  8 + given:
  9 + def task = grailsTask("compile")
  10 +
  11 + expect:
  12 + task.command == "compile"
  13 +
  14 + when:
  15 + task.command = "test"
  16 +
  17 + then:
  18 + task.command == "test"
  19 + }
  20 +
  21 +}
25 src/test/groovy/org/grails/gradle/plugin/integ/InitSpec.groovy
... ... @@ -0,0 +1,25 @@
  1 +package org.grails.gradle.plugin.integ
  2 +
  3 +import spock.lang.Unroll
  4 +
  5 +class InitSpec extends IntegSpec {
  6 +
  7 + @Unroll({"can create grails $grailsVersion project"})
  8 + def "can init project"() {
  9 + given:
  10 + applyPlugin(grailsVersion)
  11 +
  12 + when:
  13 + launcher("init", "-s").run().rethrowFailure()
  14 +
  15 + then:
  16 + task("init").state.didWork
  17 +
  18 + and:
  19 + file("grails-app").exists()
  20 +
  21 + where:
  22 + grailsVersion << ["1.3.7", "2.0.0"]
  23 + }
  24 +
  25 +}
75 src/test/groovy/org/grails/gradle/plugin/integ/IntegSpec.groovy
... ... @@ -0,0 +1,75 @@
  1 +package org.grails.gradle.plugin.integ
  2 +
  3 +import org.gradle.GradleLauncher
  4 +import org.gradle.StartParameter
  5 +import org.gradle.api.Task
  6 +import org.gradle.api.execution.TaskExecutionListener
  7 +import org.gradle.api.tasks.TaskState
  8 +import org.junit.Rule
  9 +import org.junit.rules.TemporaryFolder
  10 +import spock.lang.Specification
  11 +
  12 +abstract class IntegSpec extends Specification {
  13 + @Rule final TemporaryFolder dir = new TemporaryFolder()
  14 +
  15 + static class ExecutedTask {
  16 + Task task
  17 + TaskState state
  18 + }
  19 +
  20 + List<ExecutedTask> executedTasks = []
  21 +
  22 + GradleLauncher launcher(String... args) {
  23 + StartParameter startParameter = GradleLauncher.createStartParameter(args)
  24 + startParameter.setProjectDir(dir.root)
  25 + GradleLauncher launcher = GradleLauncher.newInstance(startParameter)
  26 + executedTasks.clear()
  27 + launcher.addListener(new TaskExecutionListener() {
  28 + void beforeExecute(Task task) {
  29 + IntegSpec.this.executedTasks << new ExecutedTask(task: task)
  30 + }
  31 +
  32 + void afterExecute(Task task, TaskState taskState) {
  33 + IntegSpec.this.executedTasks.last().state = taskState
  34 + taskState.metaClass.upToDate = taskState.skipMessage == "UP-TO-DATE"
  35 + }
  36 + })
  37 + launcher
  38 + }
  39 +
  40 + File getBuildFile() {
  41 + file("build.gradle")
  42 + }
  43 +
  44 + File file(String path) {
  45 + def parts = path.split("/")
  46 + if (parts.size() > 1) {
  47 + dir.newFolder(*parts[0..-2])
  48 + }
  49 + dir.newFile(path)
  50 + }
  51 +
  52 + ExecutedTask task(String name) {
  53 + executedTasks.find { it.task.name == name }
  54 + }
  55 +
  56 + def setup() {
  57 + buildFile << """
  58 + GrailsPlugin = project.class.classLoader.loadClass("org.grails.gradle.plugin.GrailsPlugin")
  59 + GrailsTask = project.class.classLoader.loadClass("org.grails.gradle.plugin.GrailsTask")
  60 + version = "1.0"
  61 +
  62 + repositories {
  63 + maven { url "http://repo.grails.org/grails/core" }
  64 + }
  65 + """
  66 + }
  67 +
  68 + def applyPlugin(String grailsVersion = "2.0.0") {
  69 + buildFile << """
  70 + grailsVersion = "$grailsVersion"
  71 + apply plugin: GrailsPlugin
  72 + """
  73 + }
  74 +
  75 +}

0 comments on commit a0c4d65

Please sign in to comment.
Something went wrong with that request. Please try again.