Skip to content

Commit a877250

Browse files
author
R. Tyler Croy
committed
Merge pull request #22 from ysb33r/master
Fixed #20 by adding integration tests
2 parents 9ab8182 + 7df7c32 commit a877250

File tree

13 files changed

+335
-108
lines changed

13 files changed

+335
-108
lines changed

HACKING.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
== Running Tests
2+
3+
This project has both unit tests and integration tests. In our context the distinguishing point is that integration tests
4+
need network connectivity, and unit tests need no more than local file system access. We also expect unit tests to be
5+
very quick, whereas integration tests are allowed to take a _little_ longer.
6+
7+
Unit tests are in 'src/test' and integration tests are in 'src/integTest'. To run integration tests you would need to
8+
do `./gradlew check` or `./gradlew build`. For unittests just doing `./gradlew test` is enough.
9+
10+
Test logging is controlled via `logback-test.xml`. Be aware that integration tests generate a lot of debug information.
11+
Please do not commit the config file back with DEBUG turned on
12+
13+
== Release HOWTO
14+
15+
*TBC*

build.gradle

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
apply plugin: 'groovy'
22
apply plugin: 'maven'
33
apply plugin: 'com.jfrog.bintray'
4+
apply from: 'gradle/integration-tests.gradle'
45

56
// Pull in the bintray gradle plugin for uploading releases
67
buildscript {
@@ -30,6 +31,10 @@ dependencies {
3031
testCompile ("org.spockframework:spock-core:0.7-groovy-${gradle.gradleVersion.startsWith('1.')?'1.8':'2.0'}") {
3132
exclude module : 'groovy-all'
3233
}
34+
integrationTestCompile ("org.spockframework:spock-core:0.7-groovy-${gradle.gradleVersion.startsWith('1.')?'1.8':'2.0'}") {
35+
exclude module : 'groovy-all'
36+
}
37+
3338
}
3439

3540
test {
@@ -42,12 +47,18 @@ test {
4247
systemProperties TEST_SCRIPT_DIR : new File(projectDir,'src/test/resources/scripts').absolutePath
4348
systemProperties 'logback.configurationFile' : new File(projectDir,'src/test/resources/logback-test.xml').absolutePath
4449

50+
}
51+
52+
integrationTest {
53+
systemProperties TESTROOT : new File(buildDir,'tmp/test/integration-tests').absolutePath
54+
systemProperties TEST_SCRIPT_DIR : new File(projectDir,'src/integTest/resources/scripts').absolutePath
55+
systemProperties 'logback.configurationFile' : new File(projectDir,'src/integTest/resources/logback-test.xml').absolutePath
56+
4557
if(gradle.startParameter.isOffline()) {
4658
systemProperties 'TESTS_ARE_OFFLINE' : '1'
4759
}
4860
}
4961

50-
5162
task sourcesJar(type: Jar, dependsOn: classes) {
5263
classifier = 'sources'
5364
from sourceSets.main.allSource

gradle/integration-tests.gradle

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// This is based upon what Rob Fletcher has done at
2+
// https://raw.githubusercontent.com/robfletcher/gradle-compass/master/gradle/integration-tests.gradle
3+
4+
configurations {
5+
integrationTestCompile {
6+
extendsFrom testCompile
7+
}
8+
integrationTestRuntime {
9+
extendsFrom integrationTestCompile, testRuntime
10+
}
11+
}
12+
13+
sourceSets {
14+
integrationTest {
15+
java.srcDir file("src/integTest/java")
16+
groovy.srcDir file("src/integTest/groovy")
17+
resources.srcDir file("src/integTest/resources")
18+
compileClasspath = sourceSets.main.output + sourceSets.test.output + configurations.integrationTestCompile
19+
runtimeClasspath = output + compileClasspath + configurations.integrationTestRuntime
20+
}
21+
}
22+
23+
task integrationTest(type: Test, dependsOn: jar) {
24+
testClassesDir = sourceSets.integrationTest.output.classesDir
25+
classpath = sourceSets.integrationTest.runtimeClasspath
26+
systemProperties["integTest.projects"] = new File(sourceSets.integrationTest.output.resourcesDir, "projects").absolutePath
27+
}
28+
29+
check.dependsOn integrationTest
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.lookout.jruby
2+
3+
import org.gradle.testfixtures.ProjectBuilder
4+
import spock.lang.*
5+
import static org.gradle.api.logging.LogLevel.LIFECYCLE
6+
7+
8+
/**
9+
* Created by schalkc on 20/08/2014.
10+
*/
11+
class JRubyExecIntegrationSpec extends Specification {
12+
static final boolean TESTS_ARE_OFFLINE = System.getProperty('TESTS_ARE_OFFLINE') != null
13+
static final File TEST_SCRIPT_DIR = new File( System.getProperty('TEST_SCRIPT_DIR') ?: 'src/integTest/resources/scripts')
14+
static final File TESTROOT = new File(System.getProperty('TESTROOT') ?: 'build/tmp/test/integration-tests')
15+
static final String TASK_NAME = 'RubyWax'
16+
17+
def project
18+
def execTask
19+
20+
void setup() {
21+
project = ProjectBuilder.builder().build()
22+
project.buildDir = TESTROOT
23+
project.logging.level = LIFECYCLE
24+
project.apply plugin: 'com.lookout.jruby'
25+
execTask = project.task(TASK_NAME,type: JRubyExec)
26+
}
27+
28+
@IgnoreIf({TESTS_ARE_OFFLINE})
29+
def "Changing the jruby version will load the correct jruby"() {
30+
when: "Version is set on the task"
31+
final String newVersion = '1.7.11'
32+
assert project.jruby.execVersion != newVersion
33+
execTask.jrubyVersion = newVersion
34+
project.evaluate()
35+
36+
def jarName = project.configurations.getByName('jrubyExec$$'+TASK_NAME).files.find { it.toString().find('jruby-complete') }
37+
def matches = jarName ? (jarName =~ /.*(jruby-complete-.+.jar)/ ) : null
38+
39+
then: "jruby-complete-${newVersion}.jar must be selected"
40+
jarName != null
41+
matches != null
42+
"jruby-complete-${newVersion}.jar".toString() == matches[0][1]
43+
}
44+
45+
@IgnoreIf({TESTS_ARE_OFFLINE})
46+
def "Running a Hello World script"() {
47+
given:
48+
def output = new ByteArrayOutputStream()
49+
project.configure(execTask) {
50+
script "${TEST_SCRIPT_DIR}/helloWorld.rb"
51+
standardOutput output
52+
}
53+
54+
when:
55+
project.evaluate()
56+
execTask.exec()
57+
58+
then:
59+
output.toString() == "Hello, World\n"
60+
}
61+
62+
@IgnoreIf({TESTS_ARE_OFFLINE})
63+
def "Running a script that requires a gem"() {
64+
given:
65+
def output = new ByteArrayOutputStream()
66+
project.configure(execTask) {
67+
setEnvironment [:]
68+
script "${TEST_SCRIPT_DIR}/requiresGem.rb"
69+
standardOutput output
70+
}
71+
72+
when:
73+
project.dependencies.add(JRubyExec.JRUBYEXEC_CONFIG,'rubygems:credit_card_validator:1.2.0' )
74+
project.evaluate()
75+
execTask.exec()
76+
77+
then:
78+
output.toString() == "Not valid\n"
79+
}
80+
81+
@IgnoreIf({TESTS_ARE_OFFLINE})
82+
def "Running a script that requires a gem, a separate jRuby and a separate configuration"() {
83+
given:
84+
def output = new ByteArrayOutputStream()
85+
project.with {
86+
configurations.create('RubyWax')
87+
dependencies.add('RubyWax','rubygems:credit_card_validator:1.1.0')
88+
configure(execTask) {
89+
script "${TEST_SCRIPT_DIR}/requiresGem.rb"
90+
standardOutput output
91+
jrubyVersion '1.7.11'
92+
configuration 'RubyWax'
93+
}
94+
}
95+
96+
97+
when:
98+
project.evaluate()
99+
execTask.exec()
100+
101+
then:
102+
output.toString() == "Not valid\n"
103+
}
104+
105+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.lookout.jruby
2+
3+
import org.gradle.testfixtures.ProjectBuilder
4+
import spock.lang.*
5+
import static org.gradle.api.logging.LogLevel.LIFECYCLE
6+
7+
8+
/**
9+
* Created by schalkc on 20/08/2014.
10+
*/
11+
class JRubyPluginIntegrationSpec extends Specification {
12+
13+
static final boolean TESTS_ARE_OFFLINE = System.getProperty('TESTS_ARE_OFFLINE') != null
14+
static final File TESTROOT = new File(System.getProperty('TESTROOT') ?: 'build/tmp/test/unittests')
15+
16+
def project
17+
18+
void setup() {
19+
project = ProjectBuilder.builder().build()
20+
project.buildDir = TESTROOT
21+
project.logging.level = LIFECYCLE
22+
project.apply plugin: 'com.lookout.jruby'
23+
}
24+
25+
@IgnoreIf({TESTS_ARE_OFFLINE})
26+
def "jrubyWar task needs to add jruby-complete jar"() {
27+
given: "That we have a test version that is different that the compiled-in defaultVersion"
28+
final String useVersion = '1.7.3'
29+
assert useVersion != project.jruby.defaultVersion
30+
new File(TESTROOT,'libs').mkdirs()
31+
new File(TESTROOT,'classes/main').mkdirs()
32+
33+
when: "We change the default version and the rubyWar task is executed (via copy)"
34+
project.jruby {
35+
defaultVersion useVersion
36+
}
37+
def jrw = project.tasks.jrubyWar
38+
project.evaluate()
39+
jrw.copy()
40+
def jar = project.configurations.jrubyWar.files.find { it.toString().find('jruby-complete') }
41+
def jarMatch = (jar !=null) ? (jar =~ /.*(jruby-complete-.+.jar)/) : jar
42+
43+
then: "We expect the task to have completed succesfully"
44+
jrw.outputs.files.singleFile.exists()
45+
46+
and: "We expect to have a jruby-complete-XXX.jar"
47+
jar != null
48+
49+
and: "jruby-complete-XXX.jar must match ${useVersion}"
50+
jarMatch != null
51+
"jruby-complete-${useVersion}.jar".toString() == jarMatch[0][1]
52+
}
53+
}
54+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
5+
</encoder>
6+
</appender>
7+
8+
<root level="info">
9+
<appender-ref ref="STDOUT" />
10+
</root>
11+
</configuration>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
puts "Hello, World"

src/main/groovy/com/lookout/jruby/JRubyExec.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.gradle.api.tasks.JavaExec
1111
import org.gradle.api.tasks.Optional
1212
import org.gradle.api.tasks.TaskInstantiationException
1313
import org.gradle.internal.FileUtils
14+
import org.gradle.process.ExecResult
1415
import org.gradle.process.JavaExecSpec
1516
import org.gradle.util.CollectionUtils
1617

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.lookout.jruby.internal
2+
3+
import org.gradle.api.Project
4+
5+
/**
6+
* @author Schalk W. Cronjé
7+
*/
8+
class JRubyExecDelegate {
9+
def passthrough = []
10+
String script
11+
12+
def methodMissing(String name, args) {
13+
if( name == 'args' || name == 'setArgs' ) {
14+
throw new UnsupportedOperationException("Use jrubyArgs/scriptArgs instead")
15+
}
16+
if( name == 'main' ) {
17+
throw new UnsupportedOperationException("Setting main class for jruby is not a valid operation")
18+
}
19+
20+
if(args.size() == 1) {
21+
passthrough.add( [ "${name}" : args[0] ] )
22+
} else {
23+
passthrough.add( [ "${name}" : args ] )
24+
}
25+
}
26+
//
27+
// static def jrubyexecDelegatingClosure = { Project project, Closure cl ->
28+
// def proxy = new JRubyExecProxy()
29+
// Closure cl2 = cl.clone()
30+
// cl2.delegate = proxy
31+
// cl2.call()
32+
//
33+
// if( proxy.script == null ) {
34+
// throw new NullPointerException("'script' is not set")
35+
// }
36+
//
37+
// project.javaexec {
38+
// proxy.passthrough.each { item ->
39+
// def k = item.keySet()[0]
40+
// def v = item.values()[0]
41+
// "${k}" v
42+
// }
43+
// main 'org.jruby.Main'
44+
// }
45+
// }
46+
//
47+
// static void addToProject(Project project) {
48+
// project.ext {
49+
// jrubyexec = JRubyExecProxy.jrubyexecDelegatingClosure.curry(project)
50+
// }
51+
// }
52+
}
53+
54+
55+
56+
/*
57+
class Proxy {
58+
def data = []
59+
def methodMissing(String name, args) {
60+
if(args.size() == 1) {
61+
data.add( [ "${name}" : args[0] ] )
62+
} else {
63+
data.add( [ "${name}" : args ] )
64+
}
65+
}
66+
}
67+
68+
class Mine {
69+
Proxy p = new Proxy()
70+
def nm = new NotMine()
71+
def runThis(Closure cl) {
72+
Closure cl2 = cl.clone()
73+
cl2.delegate = p
74+
cl2.call()
75+
nm.configure {
76+
p.data.each { item ->
77+
def k = item.keySet()[0]
78+
def v = item.values()[0]
79+
"${k}" v
80+
}
81+
}
82+
}
83+
84+
*/

0 commit comments

Comments
 (0)