Skip to content

Commit

Permalink
Add integration tests for coreVersion
Browse files Browse the repository at this point in the history
This is the first part of a plan to address #148:

1. Cover the behavior with high-level tests as a safety net
2. Move the logic out of the JpiExtension, ensuring the tests pass
3. Migrate all but one of these test cases to unit tests
  • Loading branch information
sghill committed May 3, 2020
1 parent 3f22457 commit 88b5b90
Show file tree
Hide file tree
Showing 2 changed files with 280 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jenkinsci.gradle.plugins.jpi

import groovy.transform.CompileStatic
import groovy.transform.Immutable

@Immutable
@CompileStatic
class ClasspathExpectations {
static final ClasspathExpectations EVERYWHERE_BUT_RUNTIME = new ClasspathExpectations(['annotationProcessor', 'compileClasspath', 'testCompileClasspath', 'testRuntimeClasspath'], ['runtimeClasspath'])
static final ClasspathExpectations COMPILE_ONLY = new ClasspathExpectations(['compileClasspath'], ['runtimeClasspath'])
static final ClasspathExpectations TEST_IMPLEMENTATION_ONLY = new ClasspathExpectations(['testCompileClasspath', 'testRuntimeClasspath'], ['compileClasspath', 'runtimeClasspath'])
static final ClasspathExpectations NOWHERE = new ClasspathExpectations([], ['annotationProcessor', 'compileClasspath', 'runtimeClasspath', 'testCompileClasspath', 'testRuntimeClasspath'])

List<String> on
List<String> off
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
package org.jenkinsci.gradle.plugins.jpi

import org.gradle.testkit.runner.TaskOutcome
import spock.lang.Issue
import spock.lang.Unroll

import static org.jenkinsci.gradle.plugins.jpi.ClasspathExpectations.COMPILE_ONLY
import static org.jenkinsci.gradle.plugins.jpi.ClasspathExpectations.EVERYWHERE_BUT_RUNTIME
import static org.jenkinsci.gradle.plugins.jpi.ClasspathExpectations.NOWHERE
import static org.jenkinsci.gradle.plugins.jpi.ClasspathExpectations.TEST_IMPLEMENTATION_ONLY

class CoreVersionIntegrationSpec extends IntegrationSpec {
private final String projectName = TestDataGenerator.generateName()
private File settings
private File build

def setup() {
settings = projectDir.newFile('settings.gradle')
settings << """rootProject.name = \"$projectName\""""
build = projectDir.newFile('build.gradle')
build << '''\
plugins {
id 'org.jenkins-ci.jpi'
}
class DirectDependencies extends DefaultTask {
@org.gradle.api.tasks.options.Option(option='config', description='configuration to list directs for')
@Input
String config
@TaskAction
void run() {
project.configurations.getByName(config).resolvedConfiguration.firstLevelModuleDependencies.each {
println(it.module.id)
}
}
}
tasks.register('directs', DirectDependencies)
'''.stripIndent()
}

@Unroll
@Issue('JENKINS-26331')
def 'should handle target directory creation for #version'(String version, boolean targetShouldExist) {
given:
build << """
jenkinsPlugin {
coreVersion = '${version}'
}
""".stripIndent()
def exampleTest = new File(projectDir.root, 'src/test/java/ExampleTest.java')
exampleTest.parentFile.mkdirs()
exampleTest << '''\
public class ExampleTest {
@org.junit.Test
public void shouldAdd() {
System.out.println("this is only a test");
}
}
'''.stripIndent()

expect:
def result = gradleRunner()
.withArguments('test', '-s')
.build()
result.task(':test').outcome == TaskOutcome.SUCCESS
new File(projectDir.root, 'target').exists() == targetShouldExist

where:
version | targetShouldExist
'1.544' | false
'1.545' | true
'1.591' | true
'1.592' | false
}

@Unroll
@Issue('JENKINS-26331')
def 'should handle target directory deletion for #version'(String version, boolean targetShouldBeCleaned, TaskOutcome expected) {
given:
build << """
jenkinsPlugin {
coreVersion = '${version}'
}
""".stripIndent()
def target = new File(projectDir.root, 'target')
target.mkdirs()
assert target.exists()

expect:
def result = gradleRunner()
.withArguments('clean', '-s')
.build()
result.task(':clean').outcome == expected
target.exists() != targetShouldBeCleaned

where:
version | targetShouldBeCleaned | expected
'1.597' | true | TaskOutcome.SUCCESS
'1.598' | false | TaskOutcome.UP_TO_DATE
}

@Unroll
def 'should add dependencies for #version'(String version, Map<String, ClasspathExpectations> expectations) {
given:
build << """
jenkinsPlugin {
coreVersion = '${version}'
}
""".stripIndent()

expect:
expectations.each { module, expected ->
expected.on.each { config ->
assert gradleRunner()
.withArguments('directs', "--config=${config}")
.build()
.output
.contains(module)
}
expected.off.each { config ->
assert !gradleRunner()
.withArguments('directs', "--config=${config}")
.build()
.output
.contains(module)
}
}

where:
version | expectations
'2.222.3' | [
'org.jenkins-ci.main:jenkins-core:2.222.3' : EVERYWHERE_BUT_RUNTIME,
'com.google.code.findbugs:annotations:3.0.0' : COMPILE_ONLY,
'findbugs:annotations:1.0.0' : NOWHERE,
'javax.servlet:servlet-api:2.4' : NOWHERE,
'javax.servlet:javax.servlet-api:3.1.0' : COMPILE_ONLY,
'org.jenkins-ci.main:jenkins-test-harness:2.60': TEST_IMPLEMENTATION_ONLY,
'org.jenkins-ci.main:ui-samples-plugin:2.0' : TEST_IMPLEMENTATION_ONLY,
'junit:junit-dep:4.10' : NOWHERE,
]
'2.64' | [
'org.jenkins-ci.main:jenkins-core:2.64' : EVERYWHERE_BUT_RUNTIME,
'com.google.code.findbugs:annotations:3.0.0' : COMPILE_ONLY,
'findbugs:annotations:1.0.0' : NOWHERE,
'javax.servlet:servlet-api:2.4' : NOWHERE,
'javax.servlet:javax.servlet-api:3.1.0' : COMPILE_ONLY,
'org.jenkins-ci.main:jenkins-test-harness:2.60': TEST_IMPLEMENTATION_ONLY,
'org.jenkins-ci.main:ui-samples-plugin:2.0' : TEST_IMPLEMENTATION_ONLY,
'junit:junit-dep:4.10' : NOWHERE,
]
'2.63' | [
'org.jenkins-ci.main:jenkins-core:2.63' : EVERYWHERE_BUT_RUNTIME,
'com.google.code.findbugs:annotations:3.0.0' : COMPILE_ONLY,
'findbugs:annotations:1.0.0' : NOWHERE,
'javax.servlet:servlet-api:2.4' : NOWHERE,
'javax.servlet:javax.servlet-api:3.1.0' : COMPILE_ONLY,
'org.jenkins-ci.main:jenkins-test-harness:2.0': TEST_IMPLEMENTATION_ONLY,
'org.jenkins-ci.main:ui-samples-plugin:2.0' : TEST_IMPLEMENTATION_ONLY,
'junit:junit-dep:4.10' : NOWHERE,
]
'2.0' | [
'org.jenkins-ci.main:jenkins-core:2.0' : EVERYWHERE_BUT_RUNTIME,
'com.google.code.findbugs:annotations:3.0.0' : COMPILE_ONLY,
'findbugs:annotations:1.0.0' : NOWHERE,
'javax.servlet:servlet-api:2.4' : NOWHERE,
'javax.servlet:javax.servlet-api:3.1.0' : COMPILE_ONLY,
'org.jenkins-ci.main:jenkins-test-harness:2.0': TEST_IMPLEMENTATION_ONLY,
'org.jenkins-ci.main:ui-samples-plugin:2.0' : TEST_IMPLEMENTATION_ONLY,
'junit:junit-dep:4.10' : NOWHERE,
]
'1.658' | [
'org.jenkins-ci.main:jenkins-core:1.658' : EVERYWHERE_BUT_RUNTIME,
'com.google.code.findbugs:annotations:3.0.0' : COMPILE_ONLY,
'findbugs:annotations:1.0.0' : NOWHERE,
'javax.servlet:servlet-api:2.4' : COMPILE_ONLY,
'javax.servlet:javax.servlet-api:3.1.0' : NOWHERE,
'org.jenkins-ci.main:jenkins-test-harness:2.0': TEST_IMPLEMENTATION_ONLY,
'org.jenkins-ci.main:ui-samples-plugin:2.0' : TEST_IMPLEMENTATION_ONLY,
'junit:junit-dep:4.10' : NOWHERE,
]
'1.645' | [
'org.jenkins-ci.main:jenkins-core:1.645' : EVERYWHERE_BUT_RUNTIME,
'com.google.code.findbugs:annotations:3.0.0' : COMPILE_ONLY,
'findbugs:annotations:1.0.0' : NOWHERE,
'javax.servlet:servlet-api:2.4' : COMPILE_ONLY,
'javax.servlet:javax.servlet-api:3.1.0' : NOWHERE,
'org.jenkins-ci.main:jenkins-test-harness:2.0': TEST_IMPLEMENTATION_ONLY,
'org.jenkins-ci.main:ui-samples-plugin:2.0' : TEST_IMPLEMENTATION_ONLY,
'junit:junit-dep:4.10' : NOWHERE,
]
'1.644' | [
'org.jenkins-ci.main:jenkins-core:1.644' : EVERYWHERE_BUT_RUNTIME,
'com.google.code.findbugs:annotations:3.0.0' : COMPILE_ONLY,
'findbugs:annotations:1.0.0' : NOWHERE,
'javax.servlet:servlet-api:2.4' : COMPILE_ONLY,
'javax.servlet:javax.servlet-api:3.1.0' : NOWHERE,
'org.jenkins-ci.main:jenkins-test-harness:1.644': TEST_IMPLEMENTATION_ONLY,
'org.jenkins-ci.main:ui-samples-plugin:2.0' : TEST_IMPLEMENTATION_ONLY,
'junit:junit-dep:4.10' : NOWHERE,
]
'1.618' | [
'org.jenkins-ci.main:jenkins-core:1.618' : EVERYWHERE_BUT_RUNTIME,
'com.google.code.findbugs:annotations:3.0.0' : COMPILE_ONLY,
'findbugs:annotations:1.0.0' : NOWHERE,
'javax.servlet:servlet-api:2.4' : COMPILE_ONLY,
'javax.servlet:javax.servlet-api:3.1.0' : NOWHERE,
'org.jenkins-ci.main:jenkins-test-harness:1.618': TEST_IMPLEMENTATION_ONLY,
'org.jenkins-ci.main:ui-samples-plugin:2.0' : TEST_IMPLEMENTATION_ONLY,
'junit:junit-dep:4.10' : NOWHERE,
]
'1.617' | [
'org.jenkins-ci.main:jenkins-core:1.617' : EVERYWHERE_BUT_RUNTIME,
'com.google.code.findbugs:annotations:3.0.0' : NOWHERE,
'findbugs:annotations:1.0.0' : COMPILE_ONLY,
'javax.servlet:servlet-api:2.4' : COMPILE_ONLY,
'javax.servlet:javax.servlet-api:3.1.0' : NOWHERE,
'org.jenkins-ci.main:jenkins-test-harness:1.617': TEST_IMPLEMENTATION_ONLY,
'org.jenkins-ci.main:ui-samples-plugin:2.0' : TEST_IMPLEMENTATION_ONLY,
'junit:junit-dep:4.10' : NOWHERE,
]
'1.533' | [
'org.jenkins-ci.main:jenkins-core:1.533' : EVERYWHERE_BUT_RUNTIME,
'com.google.code.findbugs:annotations:3.0.0' : NOWHERE,
'findbugs:annotations:1.0.0' : COMPILE_ONLY,
'javax.servlet:servlet-api:2.4' : COMPILE_ONLY,
'javax.servlet:javax.servlet-api:3.1.0' : NOWHERE,
'org.jenkins-ci.main:jenkins-test-harness:1.533': TEST_IMPLEMENTATION_ONLY,
'org.jenkins-ci.main:ui-samples-plugin:2.0' : TEST_IMPLEMENTATION_ONLY,
'junit:junit-dep:4.10' : NOWHERE,
]
'1.532' | [
'org.jenkins-ci.main:jenkins-core:1.532' : EVERYWHERE_BUT_RUNTIME,
'com.google.code.findbugs:annotations:3.0.0' : NOWHERE,
'findbugs:annotations:1.0.0' : COMPILE_ONLY,
'javax.servlet:servlet-api:2.4' : COMPILE_ONLY,
'javax.servlet:javax.servlet-api:3.1.0' : NOWHERE,
'org.jenkins-ci.main:jenkins-test-harness:1.532': TEST_IMPLEMENTATION_ONLY,
'org.jenkins-ci.main:ui-samples-plugin:1.532' : TEST_IMPLEMENTATION_ONLY,
'junit:junit-dep:4.10' : NOWHERE,
]
'1.505' | [
'org.jenkins-ci.main:jenkins-core:1.505' : EVERYWHERE_BUT_RUNTIME,
'com.google.code.findbugs:annotations:3.0.0' : NOWHERE,
'findbugs:annotations:1.0.0' : COMPILE_ONLY,
'javax.servlet:servlet-api:2.4' : COMPILE_ONLY,
'javax.servlet:javax.servlet-api:3.1.0' : NOWHERE,
'org.jenkins-ci.main:jenkins-test-harness:1.505': TEST_IMPLEMENTATION_ONLY,
'org.jenkins-ci.main:ui-samples-plugin:1.505' : TEST_IMPLEMENTATION_ONLY,
'junit:junit-dep:4.10' : NOWHERE,
]
'1.504' | [
'org.jenkins-ci.main:jenkins-core:1.504' : EVERYWHERE_BUT_RUNTIME,
'com.google.code.findbugs:annotations:3.0.0' : NOWHERE,
'findbugs:annotations:1.0.0' : COMPILE_ONLY,
'javax.servlet:servlet-api:2.4' : COMPILE_ONLY,
'javax.servlet:javax.servlet-api:3.1.0' : NOWHERE,
'org.jenkins-ci.main:jenkins-test-harness:1.504': TEST_IMPLEMENTATION_ONLY,
'org.jenkins-ci.main:ui-samples-plugin:1.504' : TEST_IMPLEMENTATION_ONLY,
'junit:junit-dep:4.10' : TEST_IMPLEMENTATION_ONLY,
]
}
}

0 comments on commit 88b5b90

Please sign in to comment.