Skip to content

Commit b4b9601

Browse files
committed
Add catch error with map parameter
1 parent 7bd6048 commit b4b9601

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

src/main/groovy/com/lesfurets/jenkins/unit/BasePipelineTest.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ abstract class BasePipelineTest {
149149
updateBuildStatus('FAILURE')
150150
}
151151
}
152+
helper.registerAllowedMethod('catchError', [Map, Closure]) { Map args, Closure c ->
153+
c.delegate = delegate
154+
try {
155+
helper.callClosure(c)
156+
} catch(ignored) {
157+
if (args.message) {
158+
println args.message
159+
}
160+
if (args.buildResult == null || args.buildResult == 'SUCCESS') {
161+
return
162+
}
163+
if (binding.getVariable('currentBuild').currentResult == 'FAILURE') {
164+
// The build result can only get worse, so we must ignore the desired buildResult argument
165+
return
166+
}
167+
updateBuildStatus(args.buildResult)
168+
}
169+
}
152170
helper.registerAllowedMethod("checkout", [Map])
153171
helper.registerAllowedMethod("choice", [Map])
154172
helper.registerAllowedMethod('cifsPublisher', [Map], {true})
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.lesfurets.jenkins
2+
3+
import com.lesfurets.jenkins.unit.BasePipelineTest
4+
import org.junit.Before
5+
import org.junit.Test
6+
7+
class TestCatchError extends BasePipelineTest {
8+
9+
@Override
10+
@Before
11+
void setUp() throws Exception {
12+
super.setUp()
13+
}
14+
15+
@Test()
16+
void should_fail_with_fail_before_and_SuccesCatch() throws Exception {
17+
def script = runInlineScript("""
18+
node() {
19+
stage('test') {
20+
error 'error'
21+
catchError(buildResult: 'SUCCESS') {
22+
throw new Exception()
23+
}
24+
}
25+
}
26+
""")
27+
assertJobStatusFailure()
28+
}
29+
30+
@Test()
31+
void should_unstable_with_unstable_before_and_SuccesCatch() throws Exception {
32+
def script = runInlineScript("""
33+
node() {
34+
stage('test') {
35+
unstable 'unstable'
36+
catchError(buildResult: 'SUCCESS') {
37+
throw new Exception()
38+
}
39+
}
40+
}
41+
""")
42+
assertJobStatusUnstable()
43+
}
44+
45+
@Test()
46+
void should_succes_with_SuccesCatch() throws Exception {
47+
def script = runInlineScript("""
48+
node() {
49+
stage('test') {
50+
catchError(buildResult: 'SUCCESS') {
51+
throw new Exception()
52+
}
53+
}
54+
}
55+
""")
56+
assertJobStatusSuccess()
57+
}
58+
59+
@Test()
60+
void should_unstable_with_UnstableCatch() throws Exception {
61+
def script = runInlineScript("""
62+
node() {
63+
stage('test') {
64+
catchError(buildResult: 'UNSTABLE') {
65+
throw new Exception()
66+
}
67+
}
68+
}
69+
""")
70+
assertJobStatusUnstable()
71+
}
72+
73+
@Test()
74+
void should_fail_with_no_parameter() throws Exception {
75+
def script = runInlineScript("""
76+
node() {
77+
stage('test') {
78+
catchError() {
79+
throw new Exception()
80+
}
81+
}
82+
}
83+
""")
84+
assertJobStatusFailure()
85+
}
86+
87+
}

0 commit comments

Comments
 (0)