forked from abayer/gradle-hpi-plugin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
80 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/groovy/org/jenkinsci/gradle/plugins/jpi/TestInsertionTask.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('\\', '\\\\')}\"" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters