Replace JenkinsConfigRule by more speaking readYaml rule

and reset default cache rule.
This commit is contained in:
Marcus Holl
2018-01-29 09:42:23 +01:00
parent 437abcf225
commit 0d87348c21
8 changed files with 63 additions and 22 deletions
+2 -2
View File
@@ -6,8 +6,8 @@ import org.junit.rules.RuleChain
import com.lesfurets.jenkins.unit.BasePipelineTest
import util.JenkinsConfigRule
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
import util.JenkinsSetupRule
import static org.junit.Assert.assertEquals
@@ -22,7 +22,7 @@ class DockerExecuteTest extends BasePipelineTest {
@Rule
public RuleChain ruleChain = RuleChain.outerRule(new JenkinsSetupRule(this))
.around(jlr)
.around(new JenkinsConfigRule(this))
.around(new JenkinsReadYamlRule(this))
int whichDockerReturnValue = 0
+3 -2
View File
@@ -10,8 +10,8 @@ import org.junit.rules.TemporaryFolder
import com.lesfurets.jenkins.unit.BasePipelineTest
import util.JenkinsConfigRule
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
import util.JenkinsSetupRule
import util.JenkinsShellCallRule
@@ -29,7 +29,8 @@ public class MTABuildTest extends BasePipelineTest {
.around(new JenkinsSetupRule(this))
.around(jlr)
.around(jscr)
.around(new JenkinsConfigRule(this))
.around(new JenkinsReadYamlRule(this))
def currentDir
def otherDir
+4 -2
View File
@@ -9,7 +9,8 @@ import com.lesfurets.jenkins.unit.BasePipelineTest
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue
import util.JenkinsConfigRule
import util.JenkinsReadYamlRule
import util.JenkinsResetDefaultCacheRule
import util.JenkinsSetupRule
import util.JenkinsShellCallRule
@@ -22,7 +23,8 @@ class MavenExecuteTest extends BasePipelineTest {
@Rule
public RuleChain ruleChain = RuleChain.outerRule(new JenkinsSetupRule(this))
.around(jscr)
.around(new JenkinsConfigRule(this))
.around(new JenkinsReadYamlRule(this))
.around(new JenkinsResetDefaultCacheRule())
def mavenExecuteScript
def cpe
+4 -2
View File
@@ -10,8 +10,9 @@ import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.JenkinsConfigRule
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
import util.JenkinsSetupRule
import util.JenkinsShellCallRule
@@ -28,7 +29,8 @@ class NeoDeploymentTest extends BasePipelineTest {
.around(new JenkinsSetupRule(this))
.around(jlr)
.around(jscr)
.around(new JenkinsConfigRule(this))
.around(new JenkinsReadYamlRule(this))
def workspacePath
def warArchiveName
+3 -5
View File
@@ -1,9 +1,5 @@
import hudson.AbortException
import util.JenkinsConfigRule
import util.JenkinsSetupRule
import org.junit.rules.TemporaryFolder
import com.lesfurets.jenkins.unit.BasePipelineTest
import org.junit.Before
@@ -12,6 +8,8 @@ import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.JenkinsReadYamlRule
class PipelineExecuteTest extends BasePipelineTest {
private ExpectedException thrown = new ExpectedException().none()
@@ -19,7 +17,7 @@ class PipelineExecuteTest extends BasePipelineTest {
@Rule
public RuleChain ruleChain = RuleChain.outerRule(thrown)
.around(new JenkinsSetupRule(this))
.around(new JenkinsConfigRule(this))
.around(new JenkinsReadYamlRule(this))
def pipelinePath
def checkoutParameters = [:]
+3 -3
View File
@@ -9,8 +9,8 @@ import org.junit.rules.TemporaryFolder
import com.lesfurets.jenkins.unit.BasePipelineTest
import util.JenkinsConfigRule
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
import util.JenkinsSetupRule
class ToolValidateTest extends BasePipelineTest {
@@ -18,7 +18,6 @@ class ToolValidateTest extends BasePipelineTest {
private ExpectedException thrown = new ExpectedException().none()
private TemporaryFolder tmp = new TemporaryFolder()
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
private JenkinsConfigRule jcr = new JenkinsConfigRule(this)
@Rule
public RuleChain ruleChain =
@@ -26,7 +25,8 @@ class ToolValidateTest extends BasePipelineTest {
.around(thrown)
.around(new JenkinsSetupRule(this))
.around(jlr)
.around(jcr)
.around(new JenkinsReadYamlRule(this))
private notEmptyDir
@@ -7,12 +7,12 @@ import org.junit.runner.Description
import org.junit.runners.model.Statement
import org.yaml.snakeyaml.Yaml
class JenkinsConfigRule implements TestRule {
class JenkinsReadYamlRule implements TestRule {
final BasePipelineTest testInstance
JenkinsConfigRule(BasePipelineTest testInstance) {
JenkinsReadYamlRule(BasePipelineTest testInstance) {
this.testInstance = testInstance
}
@@ -25,11 +25,15 @@ class JenkinsConfigRule implements TestRule {
return new Statement() {
@Override
void evaluate() throws Throwable {
testInstance.helper.registerAllowedMethod("readYaml", [Map], { Map parameters ->
Yaml yamlParser = new Yaml()
return yamlParser.load(parameters.text)
testInstance.helper.registerAllowedMethod("readYaml", [Map], { Map m ->
if(m.text) {
return new Yaml().load(m.text)
} else if(m.file) {
throw new UnsupportedOperationException()
} else {
throw new IllegalArgumentException("Key 'text' is missing in map ${m}.")
}
})
DefaultValueCache.reset()
base.evaluate()
}
@@ -0,0 +1,34 @@
package util
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import com.lesfurets.jenkins.unit.BasePipelineTest
import com.sap.piper.DefaultValueCache
class JenkinsResetDefaultCacheRule implements TestRule {
JenkinsResetDefaultCacheRule() {
this(null)
}
//
// Actually not needed. Only provided for the sake of consistency
// with our other rules which comes with an constructor having the
// test case contained in the signature.
JenkinsResetDefaultCacheRule(BasePipelineTest testInstance) {
}
@Override
Statement apply(Statement base, Description description) {
return new Statement() {
@Override
void evaluate() throws Throwable {
DefaultValueCache.reset()
base.evaluate()
}
}
}
}