Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #42 from daspilker/JENKINS-12193
[JENKINS-12193] Hook up injected tests
  • Loading branch information
daspilker committed Feb 9, 2015
2 parents 9f6de01 + fc95674 commit 0031ffa
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
## 0.8.2 (unreleased)

* added task to inject tests for checking the syntax of Jelly and other things
[JENKINS-12193](https://issues.jenkins-ci.org/browse/JENKINS-12193)

## 0.8.1 (2015-01-28)

* create `target` directory (for coreVersions >= 1.545 and < 1.592), clean `target` directory (for coreVersions
Expand Down
Expand Up @@ -248,6 +248,23 @@ class JpiExtension {
*/
String gitHubUrl

/**
* If true, the automatic test injection will be skipped.
*
* Disabled by default because of <a href="https://issues.jenkins-ci.org/browse/JENKINS-21977">JENKINS-21977</a>.
*/
boolean disabledTestInjection = true

/**
* Name of the injected test.
*/
String injectedTestName = 'InjectedTest'

/**
* If true, verify that all the jelly scripts have the Jelly XSS PI in them.
*/
boolean requirePI = true

Developers developers = new Developers()

def developers(Closure closure) {
Expand Down
Expand Up @@ -135,6 +135,9 @@ class JpiPlugin implements Plugin<Project> {

gradleProject.tasks.compileJava.dependsOn(LocalizerTask.TASK_NAME)

Task testInsertionTask = gradleProject.tasks.create(TestInsertionTask.TASK_NAME, TestInsertionTask)
gradleProject.tasks.compileTestJava.dependsOn(testInsertionTask)

def sourcesJar = gradleProject.task(SOURCES_JAR_TASK_NAME, type: Jar, dependsOn: 'classes') {
classifier = 'sources'
from gradleProject.sourceSets.main.allSource
Expand Down
@@ -0,0 +1,51 @@
package org.jenkinsci.gradle.plugins.jpi

import org.gradle.api.DefaultTask
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.TaskAction

import static org.gradle.api.tasks.SourceSet.MAIN_SOURCE_SET_NAME
import static org.gradle.api.tasks.SourceSet.TEST_SOURCE_SET_NAME

class TestInsertionTask extends DefaultTask {
public static final String TASK_NAME = 'insertTest'

@TaskAction
void generateInjectedTest() {
JpiExtension jpiExtension = project.extensions.getByType(JpiExtension)
JavaPluginConvention javaConvention = project.convention.getPlugin(JavaPluginConvention)
SourceSet mainSourceSet = javaConvention.sourceSets.getByName(MAIN_SOURCE_SET_NAME)
SourceSet testSourceSet = javaConvention.sourceSets.getByName(TEST_SOURCE_SET_NAME)

if (jpiExtension.disabledTestInjection) {
return
}

File root = new File(project.buildDir, 'inject-tests')
root.mkdirs()
testSourceSet.java.srcDirs += root

File javaFile = new File(root, "${jpiExtension.injectedTestName}.java")
javaFile.text = """import java.util.*;
/**
* Entry point to auto-generated tests (generated by gradle-jpi-plugin).
*/
public class ${jpiExtension.injectedTestName} extends junit.framework.TestCase {
public static junit.framework.Test suite() throws Exception {
Map parameters = new HashMap();
parameters.put("basedir", ${quote(project.projectDir.absolutePath)});
parameters.put("artifactId", ${quote(jpiExtension.shortName)});
parameters.put("outputDirectory", ${quote(mainSourceSet.output.resourcesDir.absolutePath)});
parameters.put("requirePI", ${quote(String.valueOf(jpiExtension.requirePI))});
return new org.jvnet.hudson.test.PluginAutomaticTestBuilder().build(parameters);
}
}
"""
}

private static String quote(String s) {
"\"${s.replace('\\', '\\\\')}\""
}
}
Expand Up @@ -26,10 +26,11 @@ class JpiPluginSpec extends Specification {
taskClass.isInstance(project.tasks[taskName])

where:
taskName | taskClass
'jpi' | Jpi
'server' | ServerTask
'localizer' | LocalizerTask
'stapler' | StaplerGroovyStubsTask
taskName | taskClass
'jpi' | Jpi
'server' | ServerTask
'localizer' | LocalizerTask
'stapler' | StaplerGroovyStubsTask
'insertTest' | TestInsertionTask
}
}

0 comments on commit 0031ffa

Please sign in to comment.