Permalink
Browse files

Added testsuite project that can use grails-exec against many Grails …

…versions.

To run it, just run ./gradlew grails-exec-testsuite:test
  • Loading branch information...
1 parent b7173cc commit 25ae483e396a46e17d3a2df2352c6f2a470b07a5 @alkemist alkemist committed May 27, 2011
@@ -0,0 +1,86 @@
+description: "A test harness for grails-exec that can run with multiple Grails grailsVersions"
+
+dependsOn ":grails-exec"
+
+buildscript {
+ dependencies {
+ classpath project(":grails-exec")
+ }
+}
+
+dependencies {
+ testCompile project(":grails-exec")
+}
+
+task test (overwrite: true) {}
+
+// no 1.3.5 because of GRAILS-6797 which prevents running tests
+testWith "1.3.0", "1.3.1", "1.3.2", "1.3.3", "1.3.4", "1.3.6", "1.3.7"
+
+def testWith(String... grailsVersions) {
+ grailsVersions.each { grailsVersion ->
+
+ def versionQuirks = new org.grails.exec.GrailsVersion(grailsVersion)
+
+ def grailsBootstrapClasspathName = "grailsBootstrap-$grailsVersion"
+ def grailsCompileClasspathName = "grailsCompile-$grailsVersion"
+ def grailsTestClasspathName = "grailsTest-$grailsVersion"
+ def grailsRuntimeClasspathName = "grailsRuntime-$grailsVersion"
+
+ def classpaths = [
+ bootstrap: grailsBootstrapClasspathName,
+ compile: grailsCompileClasspathName,
+ test: grailsTestClasspathName,
+ runtime: grailsRuntimeClasspathName
+ ]
+
+ classpaths.each { type, config ->
+ configurations.add(config) {
+ description = "${type.capitalize()} Classpath for Grails $grailsVersion"
+ }
+ }
+
+ classpaths.bootstrapRuntime = "grailsBootstrapRuntime-$grailsVersion"
+ configurations.add(classpaths.bootstrapRuntime) {
+ description = "Bootstrap classpath for run-app and test-app"
+ extendsFrom configurations."$classpaths.bootstrap", configurations."$classpaths.runtime"
+ }
+
+ dependencies {
+ ["bootstrap", "scripts"].each {
+ delegate."$grailsBootstrapClasspathName" "org.grails:grails-$it:${grailsVersion}"
+ }
+
+ delegate."$grailsRuntimeClasspathName" "org.grails:grails-core:${grailsVersion}"
+ delegate."$grailsTestClasspathName" "org.grails:grails-test:${grailsVersion}"
+
+ if (versionQuirks.requiresExplicitIvyDependency) {
+ delegate."$grailsBootstrapClasspathName" "org.apache.ivy:ivy:2.1.0"
+ }
+ }
+
+ def testTaskName = "test-against-$grailsVersion"
+ task (testTaskName, type: Test) {
+ description = "Runs the test suite against Grails $grailsVersion"
+ testReportDir = file("${project.testReportDir}-$grailsVersion")
+ testResultsDir = file("${project.testResultsDir}-$grailsVersion")
+
+ ignoreFailures = true
+
+ doFirst {
+ def workspace = file("$buildDir/grails-work/$grailsVersion")
+ assert !workspace.exists() || workspace.deleteDir()
+ assert workspace.mkdirs()
+
+ def prefix = "grails.exec.testsuite"
+ systemProperty "${prefix}.grailsVersion", grailsVersion
+ systemProperty "${prefix}.workspace", workspace.absolutePath
+ classpaths.each { type, config ->
+ systemProperty "${prefix}.classpath.$type", project.configurations."$config".files*.absolutePath.join(":")
+ }
+ }
+ }
+
+ tasks.test.dependsOn tasks.getByName(testTaskName)
+ }
+}
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.grails.exec
+
+import spock.lang.*
+
+class BasicSpec extends GrailsExecSpec {
+
+ def "new project"() {
+ when:
+ newProject "new"
+
+ then:
+ projectFile.exists()
+ projectFile("application.properties").exists()
+ projectFile("grails-app").exists()
+ }
+
+ def "simple test"() {
+ given:
+ newProject "simple-test"
+
+ expect:
+ projectFile("test/unit").exists()
+ !projectFile("target/test-reports").exists()
+
+ when:
+ projectFile("test/unit/PassingTests.groovy") << """
+ class PassingTests extends grails.test.GrailsUnitTestCase {
+ void testIt() {
+ assert true
+ }
+ }
+ """
+
+ and:
+ exec "test-app"
+
+ then:
+ stdout.contains("Running test PassingTests...PASSED")
+ }
+
+}
@@ -0,0 +1,145 @@
+/*
+ * Copyright 2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.grails.exec
+
+import spock.lang.*
+
+abstract class GrailsExecSpec extends Specification {
+
+ def command = null
+ def args = null
+ def props = null
+
+ def project = null
+ def baseDir = ""
+ def grailsHome = null
+
+ def stdout = ""
+ def stderr = ""
+
+ private getSystemProperty(name) {
+ System.getProperty("grails.exec.testsuite.$name")
+ }
+
+ protected getGrailsVersion() {
+ getSystemProperty("grailsVersion")
+ }
+
+ protected commandRequiresRuntimeClasspathForBootstrap(command) {
+ command in ["run-app", "test-app"]
+ }
+
+ protected getBootstrapClasspath() {
+ getClasspath(commandRequiresRuntimeClasspathForBootstrap(command) ? "bootstrapRuntime" : "bootstrap")
+ }
+
+ Collection<File> getClasspath(type) {
+ getSystemProperty("classpath.$type").split(":").collect { new File(it) }
+ }
+
+ def getWorkspacePath() {
+ getSystemProperty("workspace")
+ }
+
+ def getBaseDirPath() {
+ project ? "$workspacePath/$project" : project
+ }
+
+ def getProjectPath() {
+ assert project : "need a current project"
+ baseDirPath
+ }
+
+ def getProjectFile() {
+ new File(projectPath)
+ }
+
+ def projectFile(subpath) {
+ new File(getProjectFile(), subpath)
+ }
+
+ def getRootLoader() {
+ new RootLoader(bootstrapClasspath*.toURL() as URL[])
+ }
+
+ def getExecutor() {
+ def executor = new GrailsExecutor(rootLoader, grailsHome, baseDirPath)
+
+ executor.compileDependencies = getClasspath("compile")
+ executor.testDependencies = getClasspath("test")
+ executor.runtimeDependencies = getClasspath("runtime")
+
+ executor.projectWorkDir = projectFile("target")
+ executor.classesDir = projectFile("target/classes")
+ executor.testClassesDir = projectFile("target/test-classes")
+ executor.resourcesDir = projectFile("target/resources")
+ executor.projectPluginsDir = projectFile("target/plugins")
+ executor.testReportsDir = projectFile("target/test-results")
+
+ executor
+ }
+
+ def exec(command, args = null, props = null) {
+ this.command = command
+ this.args = args
+ this.props = props
+
+ exec()
+ }
+
+ def exec() {
+ if (command == "test-app") {
+ installGrails7296Fix()
+ }
+
+ println ""
+ println "-- Executing $command $props $args (@ $project)"
+ println ""
+
+ def ioSwapper = new SystemOutAndErrSwapper(true, true)
+ ioSwapper.swapIn()
+
+ try {
+ executor.execute(NameUtils.toScriptName(command), args, props)
+ } finally {
+ (stdout, stderr) = ioSwapper.swapOut().collect { new String(it.toByteArray()) }
+ }
+ }
+
+ void newProject(projectName) {
+ project = projectName
+ if (projectFile.exists()) {
+ throw new IllegalStateException("already a test project with name '$projectName', use something else")
+ }
+ exec("init")
+ }
+
+ void cleanBaseDir() {
+ if (baseDir) {
+ assert !baseDir.exists() || baseDir.deleteDir()
+ }
+ }
+
+ private installGrails7296Fix() {
+ projectFile("scripts/_Events.groovy") << """
+ // Override to workaround GRAILS-7296
+ org.codehaus.groovy.grails.test.support.GrailsTestTypeSupport.metaClass.getSourceDir = { ->
+ println "testSourceDir: \$delegate.buildBinding.grailsSettings.testSourceDir"
+ new File(delegate.buildBinding.grailsSettings.testSourceDir, delegate.relativeSourcePath)
+ }
+ """
+ }
+}
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2009 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.grails.exec
+
+class MultiplexingOutputStream extends OutputStream {
+
+ List<OutputStream> streams
+
+ MultiplexingOutputStream(OutputStream[] streams = [] as OutputStream[]) {
+ this.streams = streams.toList()
+ }
+
+ void write(int b) {
+ streams*.write(b)
+ }
+}
Oops, something went wrong.

0 comments on commit 25ae483

Please sign in to comment.