Skip to content

Commit 2e8d40d

Browse files
committed
Simplify class interception
1 parent 3ec1c0a commit 2e8d40d

18 files changed

+200
-203
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class InterceptingGCL extends GroovyClassLoader {
1212
metaClazz.static.invokeMethod = helper.getMethodInterceptor()
1313
metaClazz.methodMissing = helper.getMethodMissingInterceptor()
1414
metaClazz.propertyMissing = helper.getPropertyMissingInterceptor()
15-
metaClazz.getEnv = {return binding.env}
15+
metaClazz.getEnv = { return binding.env }
1616
// find and replace script method closure with any matching allowed method closure
1717
metaClazz.methods.forEach { scriptMethod ->
1818
def signature = method(scriptMethod.name, scriptMethod.nativeParameterTypes)
@@ -67,10 +67,7 @@ class InterceptingGCL extends GroovyClassLoader {
6767
return super.loadClass(name)
6868
}
6969

70-
// Copy from this.parseClass(GroovyCodeSource, boolean)
71-
cls.metaClass.invokeMethod = helper.getMethodInterceptor()
72-
cls.metaClass.static.invokeMethod = helper.getMethodInterceptor()
73-
cls.metaClass.methodMissing = helper.getMethodMissingInterceptor()
70+
interceptClassMethods(cls.metaClass, helper, binding)
7471

7572
return cls;
7673
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ class PipelineTestHelper {
185185
return callClosure(intercepted.value, args)
186186
}
187187
// if not search for the method declaration
188-
MetaMethod m = delegate.metaClass.getMetaMethod(name, args)
188+
MetaMethod metaMethod = delegate.metaClass.getMetaMethod(name, args)
189189
// ...and call it. If we cannot find it, delegate call to methodMissing
190-
def result = (m ? this.callMethod(m, delegate, args) : delegate.metaClass.invokeMissingMethod(delegate, name, args))
190+
def result = (metaMethod ? this.callMethod(metaMethod, delegate, args) : delegate.metaClass.invokeMissingMethod(delegate, name, args))
191191
return result
192192
}
193193

src/main/groovy/com/lesfurets/jenkins/unit/declarative/AgentDeclaration.groovy

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import com.lesfurets.jenkins.unit.declarative.agent.DockerAgentDeclaration
44
import com.lesfurets.jenkins.unit.declarative.agent.KubernetesAgentDeclaration
55
import groovy.transform.ToString
66

7-
import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.createComponent
87
import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.executeWith
9-
import static groovy.lang.Closure.DELEGATE_FIRST
108

119
@ToString(includePackage = false, includeNames = true, ignoreNulls = true)
1210
class AgentDeclaration {
@@ -23,7 +21,7 @@ class AgentDeclaration {
2321
this.label = label
2422
}
2523

26-
def node(@DelegatesTo(AgentDeclaration) Closure closure) {
24+
def node(Closure closure) {
2725
closure.call()
2826
}
2927

@@ -36,34 +34,37 @@ class AgentDeclaration {
3634
}
3735

3836
def docker(String image) {
39-
this.docker({ -> this.image = image })
37+
this.docker = new DockerAgentDeclaration().with{ da -> da.image = image; da }
4038
}
4139

42-
def docker(@DelegatesTo(strategy = DELEGATE_FIRST, value = DockerAgentDeclaration) Closure closure) {
43-
this.docker = createComponent(DockerAgentDeclaration, closure)
40+
def docker(Closure closure) {
41+
this.docker = new DockerAgentDeclaration();
42+
executeWith(this.docker, closure);
4443
}
4544

4645
def kubernetes(Object kubernetesAgent) {
4746
this.@kubernetes = kubernetesAgent as KubernetesAgentDeclaration
4847
}
4948

50-
def kubernetes(@DelegatesTo(strategy = DELEGATE_FIRST, value = KubernetesAgentDeclaration) Closure closure) {
51-
this.@kubernetes = createComponent(KubernetesAgentDeclaration, closure)
49+
def kubernetes(Closure closure) {
50+
this.@kubernetes = new KubernetesAgentDeclaration();
51+
def kubernetesDecl = this.@kubernetes
52+
executeWith(kubernetesDecl, closure)
5253
}
5354

5455
def dockerfile(boolean dockerfile) {
5556
this.dockerfile = dockerfile
5657
}
5758

58-
def dockerfile(@DelegatesTo(AgentDeclaration) Closure closure) {
59+
def dockerfile(Closure closure) {
5960
closure.call()
6061
}
6162

6263
def dir(String dir) {
6364
this.dockerfileDir = dir
6465
}
6566

66-
def execute(Object delegate) {
67+
def execute(Script script) {
6768
def agentDesc = null
6869

6970
if (label) {
@@ -84,6 +85,6 @@ class AgentDeclaration {
8485
else {
8586
throw new IllegalStateException("No agent description found")
8687
}
87-
executeWith(delegate, { echo "Executing on agent $agentDesc" })
88+
executeWith(script, { echo "Executing on agent $agentDesc" })
8889
}
8990
}

src/main/groovy/com/lesfurets/jenkins/unit/declarative/AllOfDeclaration.groovy

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package com.lesfurets.jenkins.unit.declarative
22

33
import org.springframework.util.AntPathMatcher
44

5-
import static groovy.lang.Closure.DELEGATE_FIRST
5+
import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.executeWith
66

77
class AllOfDeclaration extends WhenDeclaration {
88

99
List<String> branches = []
10-
List<Boolean> expressions = []
10+
List<Closure> expressions = []
1111
List<AnyOfDeclaration> anyOfs = []
1212

1313
def branch(String name) {
@@ -18,35 +18,29 @@ class AllOfDeclaration extends WhenDeclaration {
1818
this.expressions.add(closure)
1919
}
2020

21-
def anyOf(@DelegatesTo(strategy = DELEGATE_FIRST, value = AnyOfDeclaration) Closure closure) {
22-
this.anyOfs.add(createComponent(AnyOfDeclaration, closure))
21+
def anyOf(Closure closure) {
22+
AnyOfDeclaration anyOfDeclaration = new AnyOfDeclaration();
23+
this.anyOfs.add(anyOfDeclaration)
24+
executeWith(anyOfDeclaration, closure)
2325
}
2426

25-
def expressions(Object delegate) {
26-
return this.expressions.collect {executeWith(delegate, it)}.every()
27-
}
28-
29-
def anyOf(Object delegate) {
30-
return this.anyOfs.collect {it.execute(delegate)}
31-
}
32-
33-
Boolean execute(Object delegate) {
34-
def results = []
27+
Boolean execute(Script script) {
28+
List<Boolean> results = []
3529

3630
AntPathMatcher antPathMatcher = new AntPathMatcher()
3731

3832
if (this.branches.size() > 0) {
3933
branches.each { branch ->
40-
results.add(antPathMatcher.match(branch, delegate.env.BRANCH_NAME))
34+
results.add(antPathMatcher.match(branch, script.env.BRANCH_NAME))
4135
}
4236
}
4337

4438
if (this.expressions.size() > 0) {
45-
results.add(expressions(delegate))
39+
results.add(this.expressions.collect { executeWith(delegate, it) }.every())
4640
}
4741

4842
if (this.anyOfs.size() > 0) {
49-
results.addAll(anyOf(delegate))
43+
results.addAll(this.anyOfs.collect {it.execute(script)})
5044
}
5145

5246
return results.every()

src/main/groovy/com/lesfurets/jenkins/unit/declarative/AnyOfDeclaration.groovy

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.lesfurets.jenkins.unit.declarative
22

3-
import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.executeWith
43
import org.springframework.util.AntPathMatcher
54

6-
import static groovy.lang.Closure.DELEGATE_FIRST
7-
5+
import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.executeWith
86

97
class AnyOfDeclaration extends WhenDeclaration {
108

119
List<String> tags = []
1210
List<String> branches = []
13-
List<Boolean> expressions = []
11+
List<Closure> expressions = []
1412
List<AllOfDeclaration> allOfs = []
1513

1614
def tag(String name) {
@@ -25,8 +23,10 @@ class AnyOfDeclaration extends WhenDeclaration {
2523
this.expressions.add(closure)
2624
}
2725

28-
def allOf(@DelegatesTo(strategy = DELEGATE_FIRST, value = AllOfDeclaration) Closure closure) {
29-
this.allOfs.add(createComponent(AllOfDeclaration, closure))
26+
def allOf(Closure closure) {
27+
AllOfDeclaration allOfDeclaration = new AllOfDeclaration();
28+
this.allOfs.add(allOfDeclaration)
29+
executeWith(allOfDeclaration, closure)
3030
}
3131

3232
def allOf(Object delegate) {
@@ -37,29 +37,29 @@ class AnyOfDeclaration extends WhenDeclaration {
3737
return this.expressions.collect {executeWith(delegate, it)}.any()
3838
}
3939

40-
Boolean execute(Object delegate) {
40+
Boolean execute(Script script) {
4141
def results = []
4242

4343
AntPathMatcher antPathMatcher = new AntPathMatcher()
4444

4545
if (this.tags.size() > 0) {
4646
tags.each { tag ->
47-
results.add(antPathMatcher.match(tag, delegate.env.TAG_NAME))
47+
results.add(antPathMatcher.match(tag, script.env.TAG_NAME))
4848
}
4949
}
5050

5151
if (this.branches.size() > 0) {
5252
branches.each { branch ->
53-
results.add(antPathMatcher.match(branch, delegate.env.BRANCH_NAME))
53+
results.add(antPathMatcher.match(branch, script.env.BRANCH_NAME))
5454
}
5555
}
5656

5757
if (this.expressions.size() > 0) {
58-
results.add(expressions(delegate))
58+
results.add(expressions(script))
5959
}
6060

6161
if (this.allOfs.size() > 0) {
62-
results.addAll(allOf(delegate))
62+
results.addAll(allOf(script))
6363
}
6464

6565
return results.any()

src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipeline.groovy

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.lesfurets.jenkins.unit.declarative
22

3-
import static groovy.lang.Closure.*
4-
53
class DeclarativePipeline extends GenericPipelineDeclaration {
64

75
def properties = [:]
@@ -24,33 +22,37 @@ class DeclarativePipeline extends GenericPipelineDeclaration {
2422
}
2523
}
2624

27-
def options(@DelegatesTo(DeclarativePipeline) Closure closure) {
25+
def options(Closure closure) {
2826
options.add(closure)
2927
}
3028

31-
def triggers(@DelegatesTo(DeclarativePipeline) Closure closure) {
29+
def triggers(Closure closure) {
3230
this.triggers = closure
3331
}
3432

3533
def parameters(Object o) {
3634
this.parameters = new ParametersDeclaration().with { it.label = o; it }
3735
}
3836

39-
def parameters(@DelegatesTo(strategy=DELEGATE_FIRST, value=ParametersDeclaration) Closure closure) {
40-
this.parameters = createComponent(ParametersDeclaration, closure)
37+
def parameters(Closure closure) {
38+
this.parameters = new ParametersDeclaration()
39+
this.parameters.binding = closure.binding;
40+
executeWith(this.parameters, closure)
4141
}
4242

43-
def execute(Object delegate) {
44-
super.execute(delegate)
43+
def execute(Script script) {
44+
super.execute(script)
4545
this.options.forEach {
46-
executeWith(delegate, it)
46+
executeWith(script, it)
47+
}
48+
this.agent?.execute(script)
49+
if (this.triggers) {
50+
executeWith(script, this.triggers)
4751
}
48-
this.agent?.execute(delegate)
49-
executeWith(delegate, this.triggers)
5052
this.stages.entrySet().forEach { e ->
51-
e.value.execute(delegate)
53+
e.value.execute(script)
5254
}
53-
this.post?.execute(delegate)
55+
this.post?.execute(script)
5456
}
5557

5658
}

src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ import static com.lesfurets.jenkins.unit.MethodSignature.method
88
abstract class DeclarativePipelineTest extends BasePipelineTest {
99

1010
def pipelineInterceptor = { Closure closure ->
11-
GenericPipelineDeclaration.binding = binding
12-
GenericPipelineDeclaration.createComponent(DeclarativePipeline, closure).execute(delegate)
11+
def declarativePipeline = new DeclarativePipeline()
12+
def rehydratedPipelineCl = closure.rehydrate(declarativePipeline, closure.owner, closure)
13+
rehydratedPipelineCl.resolveStrategy
14+
rehydratedPipelineCl.call();
15+
declarativePipeline.execute(closure.owner)
1316
}
1417

1518
def paramInterceptor = { Map desc ->
1619
addParam(desc.name, desc.defaultValue, false)
1720
}
1821

19-
def stringInterceptor = { Map desc->
22+
def stringInterceptor = { Map desc ->
2023
if (desc) {
2124
// we are in context of parameters { string(...)}
2225
if (desc.name) {

0 commit comments

Comments
 (0)