2018-01-16 16:42:11 +02:00
|
|
|
package util
|
|
|
|
|
|
|
|
import org.junit.rules.TestRule
|
|
|
|
import org.junit.runner.Description
|
|
|
|
import org.junit.runners.model.Statement
|
|
|
|
import org.yaml.snakeyaml.Yaml
|
|
|
|
|
2018-08-31 09:15:44 +02:00
|
|
|
import com.lesfurets.jenkins.unit.BasePipelineTest
|
|
|
|
|
2018-01-26 14:35:49 +02:00
|
|
|
class JenkinsReadYamlRule implements TestRule {
|
2018-01-16 16:42:11 +02:00
|
|
|
final BasePipelineTest testInstance
|
|
|
|
|
2018-08-31 14:17:39 +02:00
|
|
|
// Empty project configuration file registered by default
|
|
|
|
// since almost every test needs it.
|
|
|
|
def ymls = ['.pipeline/config.yml': {''}]
|
2018-08-31 09:15:44 +02:00
|
|
|
|
|
|
|
JenkinsReadYamlRule(BasePipelineTest testInstance) {
|
2018-01-16 16:42:11 +02:00
|
|
|
this.testInstance = testInstance
|
|
|
|
}
|
|
|
|
|
2018-09-04 09:44:34 +02:00
|
|
|
JenkinsReadYamlRule registerYaml(fileName, yaml) {
|
|
|
|
ymls.put(fileName, yaml)
|
2018-08-31 09:15:44 +02:00
|
|
|
return this
|
|
|
|
}
|
2018-01-16 16:42:11 +02:00
|
|
|
@Override
|
|
|
|
Statement apply(Statement base, Description description) {
|
|
|
|
return statement(base)
|
|
|
|
}
|
|
|
|
|
|
|
|
private Statement statement(final Statement base) {
|
|
|
|
return new Statement() {
|
|
|
|
@Override
|
|
|
|
void evaluate() throws Throwable {
|
2018-01-26 14:35:49 +02:00
|
|
|
testInstance.helper.registerAllowedMethod("readYaml", [Map], { Map m ->
|
2018-09-04 09:44:34 +02:00
|
|
|
def yml
|
2018-01-26 14:35:49 +02:00
|
|
|
if(m.text) {
|
2018-09-04 09:44:34 +02:00
|
|
|
yml = m.text
|
2018-01-26 14:35:49 +02:00
|
|
|
} else if(m.file) {
|
2018-09-04 09:44:34 +02:00
|
|
|
yml = ymls.get(m.file)
|
|
|
|
if(yml == null) throw new NullPointerException("yaml file '${m.file}' not registered.")
|
|
|
|
if(yml instanceof Closure) yml = yml()
|
2018-01-26 14:35:49 +02:00
|
|
|
} else {
|
2018-09-04 09:44:34 +02:00
|
|
|
throw new IllegalArgumentException("Key 'text' and 'file' are both missing in map ${m}.")
|
2018-01-26 14:35:49 +02:00
|
|
|
}
|
2018-09-04 09:44:34 +02:00
|
|
|
return new Yaml().load(yml)
|
2018-01-16 16:42:11 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
base.evaluate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|