[英]How to deal with closure scoping in groovy code in the pipeline
I've a method named "runTest", which is responsible for running certain tests in the pipeline, and I've another method named "chooseTest" which is responsible for parsing param and appending those tests in a map.我有一个名为“runTest”的方法,它负责在管道中运行某些测试,我有另一个名为“chooseTest”的方法,它负责解析参数并将这些测试附加到映射中。 In "chooseTest" method I also have switch-case which checks for which test needs to be appended in the map.
在“chooseTest”方法中,我还有 switch-case 来检查哪些测试需要附加到地图中。 How would I use closure to call "runTest" method in that switch-case so that I can call it after I break out of the loop (for checking each test).
我将如何使用闭包在该 switch-case 中调用“runTest”方法,以便我可以在退出循环后调用它(用于检查每个测试)。
I've tried making "runTest" itself a closure, but I ran into "Method too long error" which was a headache to debug, so I tried creating a closure variable in each of those if statement cases.我试过让“runTest”本身成为一个闭包,但我遇到了“方法太长错误”,这是一个令人头疼的调试问题,所以我尝试在每个 if 语句案例中创建一个闭包变量。 I ran into scoping issue which doesn't allow me to run those closure calls outside of the for-loop.
我遇到了范围问题,这不允许我在 for 循环之外运行这些闭包调用。 I have attached example of my approaches below
我在下面附上了我的方法的例子
// INSIDE of chooseTest method-->
switch (testName){
case 'aTest':
def runATest = {runTest('aTest')}
break
case 'bTest':
def runBTest = {runTest('bTest')}
break
}
runAtest.call()
//This is where I ran into scoping issue
//Another approach I tried (saw this approach elsewhere, not sure if this is even the correct approach syntactically)
def runTests = []
switch (testName){
case 'aTest':
runTests << {-> runTest('aTest')}
break
case 'bTest':
runTests << {-> runTest('bTest')}
break
}
runTests.each
I expect the closure to not have scoping errors, and when i call it outside of the case statement it invoke the chooseTest method.我希望闭包没有范围错误,当我在 case 语句之外调用它时,它会调用chooseTest 方法。
Well I am such a noobie guys, I could just define like such.好吧,我是个菜鸟,我可以这样定义。
def runAtest
switch(test){
case 'atest':
runAtest = {print('you a noob')}
break
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.