-
Notifications
You must be signed in to change notification settings - Fork 327
/
JobScriptsSpecAlternative.groovy
70 lines (58 loc) · 2.27 KB
/
JobScriptsSpecAlternative.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.dslexample
import com.dslexample.support.TestUtil
import groovy.io.FileType
import javaposse.jobdsl.dsl.DslScriptLoader
import javaposse.jobdsl.dsl.JobManagement
import javaposse.jobdsl.dsl.MemoryJobManagement
import javaposse.jobdsl.dsl.helpers.ScmContext
import javaposse.jobdsl.dsl.helpers.properties.FolderPropertiesContext
import javaposse.jobdsl.dsl.helpers.triggers.TriggerContext
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
/**
* This is an alternative to JobScriptsSpec for testing scripts. Any usage of auto-generated DSL must by stubbed.
* The upside is it's faster to run and easier to set up. The downside is that you won't catch any syntax errors within
* calls to auto-generated DSL methods.
*/
class JobScriptsSpecAlternative extends Specification {
MemoryJobManagement jobManagement = Spy(MemoryJobManagement)
@Shared
private File outputDir = new File('./build/debug-xml')
def setupSpec() {
outputDir.deleteDir()
}
def setup() {
stubGeneratedDslCall TriggerContext, 'githubPullRequest'
stubGeneratedDslCall ScmContext, 'cvsscm'
stubGeneratedDslCall FolderPropertiesContext, 'folderLibraries'
new File('src/scripts').eachFileRecurse(FileType.FILES) {
String path = it.path.replaceAll('\\\\', '/')
jobManagement.availableFiles[path] = it.text
}
}
@Unroll
void 'test script #file.name'(File file) {
when:
new DslScriptLoader(jobManagement).runScript(file.text)
writeItems(outputDir)
then:
noExceptionThrown()
where:
file << TestUtil.getJobFiles()
}
private void stubGeneratedDslCall(Class contextType, String methodName) {
_ * jobManagement.callExtension(methodName, _, contextType, *_) >> JobManagement.NO_VALUE
}
/**
* Write the config.xml for each generated job and view to the build dir.
*/
private void writeItems(File outputDir) {
jobManagement.savedConfigs.each { String name, String xml ->
TestUtil.writeFile(new File(outputDir, 'jobs'), name, xml)
}
jobManagement.savedViews.each { String name, String xml ->
TestUtil.writeFile(new File(outputDir, 'views'), name, xml)
}
}
}