Skip to content

Commit

Permalink
move debugXml functionality into JobScriptsSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
sheehan committed Oct 22, 2016
1 parent 1be560d commit afcf9c4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 95 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ An example [Job DSL](https://github.com/jenkinsci/job-dsl-plugin) project that u
`./gradlew test` runs the specs.

[JobScriptsSpec](src/test/groovy/com/dslexample/JobScriptsSpec.groovy)
will loop through all DSL files and make sure they don't throw any exceptions when processed.

## Debug XML

`./gradlew debugXml -Dpattern=jobs/**/*Jobs.groovy` runs the DSL and writes the XML output to files to `build/debug-xml`.

will loop through all DSL files and make sure they don't throw any exceptions when processed. All XML output files are written to `build/debug-xml`.
This can be useful if you want to inspect the generated XML before check-in.

## Seed Job
Expand Down
56 changes: 53 additions & 3 deletions src/test/groovy/com/dslexample/JobScriptsSpec.groovy
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
package com.dslexample

import groovy.io.FileType
import hudson.model.Item
import hudson.model.View
import javaposse.jobdsl.dsl.DslScriptLoader
import javaposse.jobdsl.dsl.GeneratedItems
import javaposse.jobdsl.dsl.GeneratedJob
import javaposse.jobdsl.dsl.GeneratedView
import javaposse.jobdsl.dsl.JobManagement
import javaposse.jobdsl.plugin.JenkinsJobManagement
import jenkins.model.Jenkins
import org.junit.ClassRule
import org.jvnet.hudson.test.JenkinsRule
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

/**
* Tests that all dsl scripts in the jobs directory will compile.
* Tests that all dsl scripts in the jobs directory will compile. All config.xml's are written to build/debug-xml.
*/
class JobScriptsSpec extends Specification {

@Shared
@ClassRule
JenkinsRule jenkinsRule = new JenkinsRule()
private JenkinsRule jenkinsRule = new JenkinsRule()

@Shared
private File outputDir = new File('./build/debug-xml')

def setupSpec() {
outputDir.deleteDir()
}

@Unroll
void 'test script #file.name'(File file) {
given:
JobManagement jm = new JenkinsJobManagement(System.out, [:], new File('.'))

when:
new DslScriptLoader(jm).runScript(file.text)
GeneratedItems items = new DslScriptLoader(jm).runScript(file.text)
writeItems items

then:
noExceptionThrown()
Expand All @@ -33,6 +48,41 @@ class JobScriptsSpec extends Specification {
file << jobFiles
}

/**
* Write the config.xml for each generated job and view to the build dir.
*/
void writeItems(GeneratedItems items) {
Jenkins jenkins = jenkinsRule.jenkins

items.jobs.each { GeneratedJob generatedJob ->
String jobName = generatedJob.jobName
Item item = jenkins.getItemByFullName(jobName)
String text = new URL(jenkins.rootUrl + item.url + 'config.xml').text
writeFile new File(outputDir, 'jobs'), jobName, text
}

items.views.each { GeneratedView generatedView ->
String viewName = generatedView.name
View view = jenkins.getView(viewName)
String text = new URL(jenkins.rootUrl + view.url + 'config.xml').text
writeFile new File(outputDir, 'views'), viewName, text
}
}

/**
* Write a single XML file, creating any nested dirs.
*/
void writeFile(File dir, String name, String xml) {
List tokens = name.split('/')
File folderDir = tokens[0..<-1].inject(dir) { File tokenDir, String token ->
new File(tokenDir, token)
}
folderDir.mkdirs()

File xmlFile = new File(folderDir, "${tokens[-1]}.xml")
xmlFile.text = xml
}

static List<File> getJobFiles() {
List<File> files = []
new File('jobs').eachFileRecurse(FileType.FILES) {
Expand Down
86 changes: 0 additions & 86 deletions src/test/groovy/com/dslexample/XmlOutput.groovy

This file was deleted.

0 comments on commit afcf9c4

Please sign in to comment.